<?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_PREPARE}}".
 *
 * @property string $ID
 * @property string $USER_ID 会员ID
 * @property string $LAST_DEC_LV 结算时会员级别
 * @property string $LAST_EMP_LV 结算时会员聘级
 * @property int $LAST_STATUS 结算时会有状态
 * @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 $VIP_PV_1L_ZC VIP一市场报单业绩
 * @property string $VIP_PV_2L_ZC VIP二市场报单业绩
 * @property string $VIP_PV_3L_ZC VIP三市场报单业绩
 * @property string $VIP_PV_4L_ZC VIP四市场报单业绩
 * @property string $VIP_PV_5L_ZC VIP五市场报单业绩
 * @property string $VIP_SURPLUS_1L_ZC VIP一市场剩余报单业绩
 * @property string $VIP_SURPLUS_2L_ZC VIP二市场剩余报单业绩
 * @property string $VIP_SURPLUS_3L_ZC VIP三市场剩余报单业绩
 * @property string $VIP_SURPLUS_4L_ZC VIP四市场剩余报单业绩
 * @property string $VIP_SURPLUS_5L_ZC VIP五市场剩余报单业绩
 * @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 $CF_PERCENT 车房养老奖分工系数
 * @property int $LX_PERCENT 领袖分红分工系数
 * @property int $FX_STATUS 本月复销状态作为下月发奖资格
 * @property int $CALC_MONTH 结算月
 * @property string $P_CALC_MONTH 表分区日期索引
 * @property int $CREATED_AT 创建时间
 */
class PerfMonthPrepare extends \common\components\ActiveRecord
{
    const NEXT_MONTH_FX_TRUE = 1;
    const NEXT_MONTH_FX_FALSE = 0;

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

    /**
     * {@inheritdoc}
     */
    public function rules()
    {
        return [
            [['USER_ID', 'CALC_MONTH', 'P_CALC_MONTH', 'CREATED_AT'], 'required'],
            [['LAST_STATUS', 'CF_PERCENT', 'LX_PERCENT', 'FX_STATUS', 'CALC_MONTH', 'CREATED_AT'], 'integer'],
            [['FX_AMOUNT_CASH', 'PV_PCS', 'PV_PCS_FX', 'PV_1L', 'PV_2L', 'PV_3L', 'PV_4L', 'PV_5L', 'VIP_PV_1L_ZC', 'VIP_PV_2L_ZC',  'VIP_PV_3L_ZC', 'VIP_PV_4L_ZC', 'VIP_PV_5L_ZC', 'VIP_SURPLUS_1L_ZC', 'VIP_SURPLUS_2L_ZC', 'VIP_SURPLUS_3L_ZC', 'VIP_SURPLUS_4L_ZC', 'VIP_SURPLUS_5L_ZC', 'PV_1L_TOTAL', 'PV_2L_TOTAL', 'PV_3L_TOTAL', 'PV_4L_TOTAL', 'PV_5L_TOTAL'], 'number'],
            [['ID', 'USER_ID', 'LAST_DEC_LV', 'LAST_EMP_LV'], 'string', 'max' => 32],
            [['P_CALC_MONTH'], 'safe'],
            [['ID'], 'unique'],
        ];
    }

    /**
     * {@inheritdoc}
     */
    public function attributeLabels()
    {
        return [
            'ID' => 'ID',
            'USER_ID' => '会员ID',
            'LAST_DEC_LV' => '结算时会员级别',
            'LAST_EMP_LV' => '结算时会员聘级',
            'LAST_STATUS' => '结算时会有状态',
            'FX_AMOUNT_CASH' => '复消现金金额',
            'PV_PCS' => '个人消费',
            'PV_PCS_FX' => '个人复消业绩',
            'PV_1L' => '一市场业绩',
            'PV_2L' => '二市场业绩',
            'PV_3L' => '三市场业绩',
            'PV_4L' => '四市场业绩',
            'PV_5L' => '五市场业绩',
            'VIP_PV_1L_ZC' => 'VIP一市场报单业绩',
            'VIP_PV_2L_ZC' => 'VIP二市场报单业绩',
            'VIP_PV_3L_ZC' => 'VIP三市场报单业绩',
            'VIP_PV_4L_ZC' => 'VIP四市场报单业绩',
            'VIP_PV_5L_ZC' => 'VIP五市场报单业绩',
            'VIP_SURPLUS_1L_ZC' => 'VIP一市场剩余报单业绩',
            'VIP_SURPLUS_2L_ZC' => 'VIP二市场剩余报单业绩',
            'VIP_SURPLUS_3L_ZC' => 'VIP三市场剩余报单业绩',
            'VIP_SURPLUS_4L_ZC' => 'VIP四市场剩余报单业绩',
            'VIP_SURPLUS_5L_ZC' => 'VIP五市场剩余报单业绩',
            'PV_1L_TOTAL' => '一市场历史累计加该月业绩',
            'PV_2L_TOTAL' => '二市场历史累计加该月业绩',
            'PV_3L_TOTAL' => '三市场历史累计加该月业绩',
            'PV_4L_TOTAL' => '四市场历史累计加该月业绩',
            'PV_5L_TOTAL' => '五市场历史累计加该月业绩',
            'CF_PERCENT' => '车房养老奖分工系数',
            'LX_PERCENT' => '领袖分红分工系数',
            'FX_STATUS' => '本月复销状态作为下月发奖资格',
            'CALC_MONTH' => '结算月',
            'P_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;
    }

    // 获取会员的月业绩信息
    public static function getMonthPerfPrepare($userId,$calcMonth) {
        $result = PerfMonthPrepare::findUseSlaves()
        ->where(
            'USER_ID=:USER_ID AND CALC_MONTH=:CALC_MONTH', [':USER_ID' => $userId,':CALC_MONTH'=>$calcMonth]
        )
        ->asArray()
        ->one();

        return $result;
    }

    // 校验业绩状态
    public static function checkPrepareStatus($pv) {
        if ($pv >= 10000) {
            return true;
        }

        return false;
    }

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