<?php

namespace common\models;

use common\helpers\Tool;
use Yii;
use common\libs\logging\operate\valueType\Config as ValueTypeConfig;

/**
 * This is the model class for table "{{%PERF_MONTH}}".
 *
 * @property string $ID
 * @property string $USER_ID 会员ID
 * @property string $FX_AMOUNT_CASH 复消现金金额
 * @property string $PV_PCS 个人消费
 * @property string $PV_PCS_FX 个人复消业绩
 * @property string $PV_1L 一市场业绩
 * @property string $PV_2L 二市场业绩
 * @property string $PV_3L 三市场业绩
 * @property string $PV_4L 四市场业绩
 * @property string $PV_5L 五市场业绩
 * @property string $PV_1L_TOTAL 一市场历史累计加该月业绩
 * @property string $PV_2L_TOTAL 二市场历史累计加该月业绩
 * @property string $PV_3L_TOTAL 三市场历史累计加该月业绩
 * @property string $PV_4L_TOTAL 四市场历史累计加该月业绩
 * @property string $PV_5L_TOTAL 五市场历史累计加该月业绩
 * @property int $CALC_MONTH 结算月
 * @property int $CREATED_AT 创建时间
 */
class PerfMonth extends \common\components\ActiveRecord
{
    const NEXT_MONTH_FX_TRUE = 1;
    const NEXT_MONTH_FX_FALSE = 0;

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

    /**
     * {@inheritdoc}
     */
    public function rules()
    {
        return [
            [['USER_ID', 'CALC_MONTH', 'CREATED_AT'], 'required'],
            [['CALC_MONTH', 'CREATED_AT'], 'integer'],
            [['FX_AMOUNT_CASH', 'PV_PCS', 'PV_PCS_FX', 'PV_1L', 'PV_2L', 'PV_3L', 'PV_4L', 'PV_5L','PV_1L_TOTAL', 'PV_2L_TOTAL', 'PV_3L_TOTAL', 'PV_4L_TOTAL', 'PV_5L_TOTAL'], 'number'],
            [['ID', 'USER_ID'], 'string', 'max' => 32],
            [['ID'], 'unique'],
        ];
    }

    /**
     * {@inheritdoc}
     */
    public function attributeLabels()
    {
        return [
            'ID' => 'ID',
            'USER_ID' => '会员ID',
            'FX_AMOUNT_CASH' => '复消现金金额',
            'PV_PCS' => '个人消费',
            'PV_PCS_FX' => '个人复消业绩',
            'PV_1L' => '一市场业绩',
            'PV_2L' => '二市场业绩',
            'PV_3L' => '三市场业绩',
            'PV_4L' => '四市场业绩',
            'PV_5L' => '五市场业绩',
            'PV_1L_TOTAL' => '一市场历史累计加该月业绩',
            'PV_2L_TOTAL' => '二市场历史累计加该月业绩',
            'PV_3L_TOTAL' => '三市场历史累计加该月业绩',
            'PV_4L_TOTAL' => '四市场历史累计加该月业绩',
            'PV_5L_TOTAL' => '五市场历史累计加该月业绩',
            'CALC_MONTH' => '结算月',
            'CREATED_AT' => '创建时间',
        ];
    }

    /**
     * 获取会员的月业绩总和
     * @param $yearMonth
     * @param $userId
     * @return array|null|\yii\db\ActiveRecord
     */
    public static function getMonthPerf($yearMonth, $userId){
        $result = static::find()->yearMonth($yearMonth)->where('USER_ID=:USER_ID', [':USER_ID'=>$userId])->asArray()->one();
        if(!$result){
            $result = [
                'USER_ID' => $userId,
                'FX_AMOUNT_CASH' => 0,
                'PV_PCS' => 0,
                'PV_PSS' => 0,
                'PV_1L' => 0,
                'PV_2L' => 0,
                'PV_3L' => 0,
                'PV_4L' => 0,
                'PV_5L' => 0,
                'PV_1L_TOTAL' => 0,
                'PV_2L_TOTAL' => 0,
                'PV_3L_TOTAL' => 0,
                'PV_4L_TOTAL' => 0,
                'PV_5L_TOTAL' => 0,
                'PV_PSS_TOTAL' => 0,
                'DEC_LEVEL' => null,
                'EMP_LEVEL' => null,
                'CF_PERCENT' => 0,
                'LX_PERCENT' => 0,
                'FX_STATUS' => self::NEXT_MONTH_FX_FALSE,
            ];
        }
        $result['PV_TOTAL'] = Tool::formatPrice($result['PV_1L_TOTAL']+$result['PV_2L_TOTAL']+$result['PV_3L_TOTAL']+$result['PV_4L_TOTAL']+$result['PV_5L_TOTAL']);
        return $result;
    }

    /**
     * 操作日志记录条件
     * @return array
     */
    public function attrLabelsWithLogType(){
        return [
            'USER_ID' => '会员ID',
            'FX_STATUS' => [
                'label' => '复销资格',
                'type' => function($data){
                    $value = $data['value'];
                    return $value==1?'是':'否';
                },
            ],
            'CALC_MONTH' => '结算月',
        ];
    }
}
