<?php

namespace common\models;

use backendApi\modules\v1\models\Admin;
use common\helpers\user\Info;
use common\libs\logging\operate\valueType\Config as ValueTypeConfig;
use Yii;

/**
 * This is the model class for table "{{%PERF_AUDIT}}".
 *
 * @property string $ID
 * @property string $USER_ID 会员ID
 * @property string $PV_1L 一市场累计
 * @property string $PV_2L 二市场累计
 * @property string $PV_3L 三市场累计
 * @property string $PV_4L 四市场累计
 * @property string $PV_5L 五市场累计
 * @property string $SURPLUS_1L 一市场结余
 * @property string $SURPLUS_2L 二市场结余
 * @property string $SURPLUS_3L 三市场结余
 * @property string $SURPLUS_4L 四市场结余
 * @property string $SURPLUS_5L 五市场结余
 * @property string $SURPLUS_LS 零售结余
 * @property int $PERF_TYPE 业绩类型
 * @property int $PERF_LOCATION 业绩区域
 * @property string $AMOUNT 调整金额
 * @property string $PERF_BEFORE 业绩调整前
 * @property string $PERF_AFTER 业绩调整后
 * @property int $PERIOD_NUM 调整期数
 * @property int $AUDIT_STATUS 审核状态
 * @property string $CREATE_ADMIN 创建管理员
 * @property string $REMARK 备注
 * @property string $AUDIT_ADMIN 审核管理员
 * @property int $CREATED_AT 创建时间
 * @property int $AUDITED_AT 审核时间
 */
class PerfAudit extends \common\components\ActiveRecord
{
    const PERF_TYPE_PV=1;
    const PERF_TYPE_SURPLUS=2;
    const CHANGE_PERF_TYPE = [
        self::PERF_TYPE_PV => '总网业绩',
        self::PERF_TYPE_SURPLUS => '大区剩余'
    ];
    const PERF_LOCATION_NAME = [
        1=>'一市场',
        2=>'二市场',
        3=>'三市场',
        4=>'四市场',
        5=>'五市场',
        6=>'虚拟零售区',
    ];

    /**
     * {@inheritdoc}
     */
    public static function tableName()
    {
        return '{{%PERF_AUDIT}}';
    }

    /**
     * {@inheritdoc}
     */
    public function rules()
    {
        return [
            [['USER_ID', 'CREATED_AT'], 'required'],
            [['PV_1L', 'PV_2L', 'PV_3L', 'PV_4L', 'PV_5L', 'SURPLUS_1L', 'SURPLUS_2L', 'SURPLUS_3L', 'SURPLUS_4L', 'SURPLUS_5L', 'SURPLUS_LS', 'PERF_BEFORE', 'PERF_AFTER', 'AMOUNT'], 'number'],
            [['PERF_TYPE', 'PERF_LOCATION', 'PERIOD_NUM', 'AUDIT_STATUS', 'CREATED_AT', 'AUDITED_AT'], 'integer'],
            [['ID', 'USER_ID', 'CREATE_ADMIN', 'AUDIT_ADMIN'], 'string', 'max' => 32],
            [['REMARK'], 'string', 'max' => 4000],
            [['ID'], 'unique'],
        ];
    }

    /**
     * {@inheritdoc}
     */
    public function attributeLabels()
    {
        return [
            'ID' => 'ID',
            'USER_ID' => '会员ID',
            'PV_1L' => '一市场累计',
            'PV_2L' => '二市场累计',
            'PV_3L' => '三市场累计',
            'PV_4L' => '四市场累计',
            'PV_5L' => '五市场累计',
            'SURPLUS_1L' => '一市场结余',
            'SURPLUS_2L' => '二市场结余',
            'SURPLUS_3L' => '三市场结余',
            'SURPLUS_4L' => '四市场结余',
            'SURPLUS_5L' => '五市场结余',
            'SURPLUS_LS' => '零售结余',
            'PERF_TYPE' => '业绩类型',
            'PERF_LOCATION' => '业绩区域',
            'AMOUNT' => '调整金额',
            'PERF_BEFORE' => '业绩调整前',
            'PERF_AFTER' => '业绩调整后',
            'PERIOD_NUM' => '调整期数',
            'AUDIT_STATUS' => '审核状态',
            'CREATE_ADMIN' => '创建管理员',
            'REMARK' => '备注',
            'AUDIT_ADMIN' => '审核管理员',
            'CREATED_AT' => '创建时间',
            'AUDITED_AT' => '审核时间',
        ];
    }

    /**
     * 按期数获取会员总网业绩和大区剩余
     * @param $userId
     * @param $periodNum
     * @return array
     */
    public static function getUserPerfAndPerfPeriod($userId,$periodNum){
        $perfPeriod = PerfPeriod::findOneAsArray('USER_ID=:USER_ID AND PERIOD_NUM=:PERIOD_NUM', [':USER_ID' => $userId, ':PERIOD_NUM' => $periodNum]);
        $userPerf = UserPerf::findOneAsArray('USER_ID=:USER_ID', [':USER_ID' => $userId]);
        return ['SURPLUS_1L' => $perfPeriod['SURPLUS_1L'] ?? 0, 'SURPLUS_2L' => $perfPeriod['SURPLUS_2L'] ?? 0, 'SURPLUS_3L' => $perfPeriod['SURPLUS_3L'] ?? 0, 'SURPLUS_4L' => $perfPeriod['SURPLUS_4L'] ?? 0, 'SURPLUS_5L' => $perfPeriod['SURPLUS_5L'] ?? 0, 'SURPLUS_LS' => $perfPeriod['SURPLUS_LS'] ?? 0, 'PV_1L' => $userPerf['PV_1L'] ?? 0, 'PV_2L' => $userPerf['PV_2L'] ?? 0, 'PV_3L' => $userPerf['PV_3L'] ?? 0, 'PV_4L' => $userPerf['PV_4L'] ?? 0, 'PV_5L' => $userPerf['PV_5L'] ?? 0];
    }

    /**
     * 操作日志记录条件
     * @return array
     */
    public function attrLabelsWithLogType(){
        return [
            'USER_ID' => '会员ID',
            'PV_1L' => '一市场累计',
            'PV_2L' => '二市场累计',
            'PV_3L' => '三市场累计',
            'PV_4L' => '四市场累计',
            'PV_5L' => '五市场累计',
            'SURPLUS_1L' => '一市场结余',
            'SURPLUS_2L' => '二市场结余',
            'SURPLUS_3L' => '三市场结余',
            'SURPLUS_4L' => '四市场结余',
            'SURPLUS_5L' => '五市场结余',
            'SURPLUS_LS' => '零售结余',
            'PERF_TYPE' => [
                'label' => '业绩类型',
                'type' => function($data){
                    $value = $data['value'];
                    return self::CHANGE_PERF_TYPE[$value];
                },
            ],
            'PERF_LOCATION' => [
                'label' => '业绩区域',
                'type' => function($data){
                    $value = $data['value'];
                    return self::PERF_LOCATION_NAME[$value];
                },
            ],
            'AMOUNT' => '调整金额',
            'PERF_BEFORE' => '业绩调整前',
            'PERF_AFTER' => '业绩调整后',
            'PERIOD_NUM' => '调整期数',
            'AUDIT_STATUS' => [
                'label' => '审核状态',
                'type' => function($data){
                    $value = $data['value'];
                    $result = '';
                    if($value == 'un' || $value == \Yii::$app->params['auditStatus']['un']['value']){
                        $result = \Yii::$app->params['auditStatus']['un']['label'];
                    } elseif($value == 'true' || $value == \Yii::$app->params['auditStatus']['true']['value']) {
                        $result = \Yii::$app->params['auditStatus']['true']['label'];
                    } elseif($value == 'false' || $value == \Yii::$app->params['auditStatus']['false']['value']) {
                        $result = \Yii::$app->params['auditStatus']['false']['label'];
                    } elseif($value == 'reject' || $value == \Yii::$app->params['auditStatus']['reject']['value']) {
                        $result = \Yii::$app->params['auditStatus']['reject']['label'];
                    }
                    return $result;
                },
            ],
            'CREATE_ADMIN' => [
                'label' => '创建管理员',
                'type' => function($data){
                    $value = $data['value'];
                    $result = Admin::findOneAsArray('ID=:ID', [':ID'=>$value], 'ADMIN_NAME');
                    return $result['ADMIN_NAME'];
                },
            ],
            'REMARK' => '备注',
            'AUDIT_ADMIN' => [
                'label' => '审核管理员',
                'type' => function($data){
                    $value = $data['value'];
                    $result = Admin::findOneAsArray('ID=:ID', [':ID'=>$value], 'ADMIN_NAME');
                    return $result['ADMIN_NAME'];
                },
            ],
            'CREATED_AT' => [
                'label' => '创建时间',
                'type' => ValueTypeConfig::DATE_TIME_TYPE,
            ],
            'AUDITED_AT' => [
                'label' => '审核时间',
                'type' => ValueTypeConfig::DATE_TIME_TYPE,
            ],
        ];
    }
}
