<?php

namespace common\models;

use common\helpers\user\Info;
use Yii;

/**
 * This is the model class for table "{{%PERF_PERIOD}}".
 *
 * @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_ZC 个人消费注册单
 * @property string $PV_PCS_YH 个人消费优惠单
 * @property string $PV_PCS_ZG 个人消费增购单
 * @property string $PV_PCS_LS 个人消费零售单
 * @property string $PV_PCS_FX 个人消费复销单
 * @property string $PV_PCS_FX_CASH 现金复消
 * @property string $PV_PCS_FX_POINT 积分复消
 * @property string $PV_1L 一市场业绩
 * @property string $PV_1L_TOUCH 一市场对碰业绩
 * @property string $PV_1L_ZC
 * @property string $PV_1L_YH
 * @property string $PV_1L_ZG
 * @property string $PV_1L_LS
 * @property string $PV_1L_FX
 * @property string $PV_2L 二市场业绩
 * @property string $PV_2L_TOUCH 二市场对碰业绩
 * @property string $PV_2L_ZC
 * @property string $PV_2L_YH
 * @property string $PV_2L_ZG
 * @property string $PV_2L_LS
 * @property string $PV_2L_FX
 * @property string $PV_3L 三市场业绩
 * @property string $PV_3L_TOUCH 三市场对碰业绩
 * @property string $PV_3L_ZC
 * @property string $PV_3L_YH
 * @property string $PV_3L_ZG
 * @property string $PV_3L_LS
 * @property string $PV_3L_FX
 * @property string $PV_4L 四市场业绩
 * @property string $PV_4L_TOUCH 四市场对碰业绩
 * @property string $PV_4L_ZC
 * @property string $PV_4L_YH
 * @property string $PV_4L_ZG
 * @property string $PV_4L_LS
 * @property string $PV_4L_FX
 * @property string $PV_5L 五市场业绩
 * @property string $PV_5L_TOUCH 五市场对碰业绩
 * @property string $PV_5L_ZC
 * @property string $PV_5L_YH
 * @property string $PV_5L_ZG
 * @property string $PV_5L_LS
 * @property string $PV_5L_FX
 * @property string $PV_LS_TOUCH 隐藏零售区业绩30%后的
 * @property string $SURPLUS_1L 一市场结余业绩
 * @property string $SURPLUS_1L_ZC 一市场首购结余业绩
 * @property string $SURPLUS_1L_FX 一市场复消结余业绩
 * @property string $SURPLUS_2L 二市场结余业绩
 * @property string $SURPLUS_2L_ZC 二市场首购结余业绩
 * @property string $SURPLUS_2L_FX 二市场复消结余业绩
 * @property string $SURPLUS_3L 三市场结余业绩
 * @property string $SURPLUS_3L_ZC 三市场首购结余业绩
 * @property string $SURPLUS_3L_FX 三市场复消结余业绩
 * @property string $SURPLUS_4L 四市场结余业绩
 * @property string $SURPLUS_4L_ZC 四市场首购结余业绩
 * @property string $SURPLUS_4L_FX 四市场复消结余业绩
 * @property string $SURPLUS_5L 五市场结余业绩
 * @property string $SURPLUS_5L_ZC 五市场首购结余业绩
 * @property string $SURPLUS_5L_FX 五市场复消结余业绩
 * @property string $SURPLUS_LS 零售隐藏区结余业绩
 * @property int $PERIOD_NUM 期数
 * @property int $CALC_MONTH 结算月YYYYMM
 * @property string $P_CALC_MONTH 表分区日期索引
 * @property int $CREATED_AT 创建时间
 */
class PerfPeriod extends \common\components\ActiveRecord
{
    /**
     * {@inheritdoc}
     */
    public static function tableName()
    {
        return '{{%PERF_PERIOD}}';
    }

    /**
     * {@inheritdoc}
     */
    public function rules()
    {
        return [
            [['USER_ID', 'PERIOD_NUM', 'CALC_MONTH', 'P_CALC_MONTH', 'CREATED_AT'], 'required'],
            [['LAST_STATUS', 'PERIOD_NUM', 'CALC_MONTH', 'CREATED_AT'], 'integer'],
            [['FX_AMOUNT_CASH', 'PV_PCS', 'PV_PCS_ZC', 'PV_PCS_YH', 'PV_PCS_ZG', 'PV_PCS_LS', 'PV_PCS_FX', 'PV_PCS_FX_CASH', 'PV_PCS_FX_POINT', 'PV_1L', 'PV_1L_TOUCH', 'PV_1L_ZC', 'PV_1L_YH', 'PV_1L_ZG', 'PV_1L_LS', 'PV_1L_FX', 'PV_2L', 'PV_2L_TOUCH', 'PV_2L_ZC', 'PV_2L_YH', 'PV_2L_ZG', 'PV_2L_LS', 'PV_2L_FX', 'PV_3L', 'PV_3L_TOUCH', 'PV_3L_ZC', 'PV_3L_YH', 'PV_3L_ZG', 'PV_3L_LS', 'PV_3L_FX', 'PV_4L', 'PV_4L_TOUCH', 'PV_4L_ZC', 'PV_4L_YH', 'PV_4L_ZG', 'PV_4L_LS', 'PV_4L_FX', 'PV_5L', 'PV_5L_TOUCH', 'PV_5L_ZC', 'PV_5L_YH', 'PV_5L_ZG', 'PV_5L_LS', 'PV_5L_FX', 'PV_LS_TOUCH', 'SURPLUS_1L', 'SURPLUS_1L_ZC', 'SURPLUS_1L_FX', 'SURPLUS_2L', 'SURPLUS_2L_ZC', 'SURPLUS_2L_FX', 'SURPLUS_3L', 'SURPLUS_3L_ZC', 'SURPLUS_3L_FX', 'SURPLUS_4L', 'SURPLUS_4L_ZC', 'SURPLUS_4L_FX', 'SURPLUS_5L', 'SURPLUS_5L_ZC', 'SURPLUS_5L_FX', 'SURPLUS_LS'], '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_ZC' => '个人消费注册单',
            'PV_PCS_YH' => '个人消费优惠单',
            'PV_PCS_ZG' => '个人消费增购单',
            'PV_PCS_LS' => '个人消费零售单',
            'PV_PCS_FX' => '个人消费复销单',
            'PV_PCS_FX_CASH' => '现金复消',
            'PV_PCS_FX_POINT' => '积分复消',
            'PV_1L' => '一市场业绩',
            'PV_1L_TOUCH' => '一市场对碰业绩',
            'PV_1L_ZC' => 'Pv 1 L  Zc',
            'PV_1L_YH' => 'Pv 1 L  Yh',
            'PV_1L_ZG' => 'Pv 1 L  Zg',
            'PV_1L_LS' => 'Pv 1 L  Ls',
            'PV_1L_FX' => 'Pv 1 L  Fx',
            'PV_2L' => '二市场业绩',
            'PV_2L_TOUCH' => '二市场对碰业绩',
            'PV_2L_ZC' => 'Pv 2 L  Zc',
            'PV_2L_YH' => 'Pv 2 L  Yh',
            'PV_2L_ZG' => 'Pv 2 L  Zg',
            'PV_2L_LS' => 'Pv 2 L  Ls',
            'PV_2L_FX' => 'Pv 2 L  Fx',
            'PV_3L' => '三市场业绩',
            'PV_3L_TOUCH' => '三市场对碰业绩',
            'PV_3L_ZC' => 'Pv 3 L  Zc',
            'PV_3L_YH' => 'Pv 3 L  Yh',
            'PV_3L_ZG' => 'Pv 3 L  Zg',
            'PV_3L_LS' => 'Pv 3 L  Ls',
            'PV_3L_FX' => 'Pv 3 L  Fx',
            'PV_4L' => '四市场业绩',
            'PV_4L_TOUCH' => '四市场对碰业绩',
            'PV_4L_ZC' => 'Pv 4 L  Zc',
            'PV_4L_YH' => 'Pv 4 L  Yh',
            'PV_4L_ZG' => 'Pv 4 L  Zg',
            'PV_4L_LS' => 'Pv 4 L  Ls',
            'PV_4L_FX' => 'Pv 4 L  Fx',
            'PV_5L' => '五市场业绩',
            'PV_5L_TOUCH' => '五市场对碰业绩',
            'PV_5L_ZC' => 'Pv 5 L  Zc',
            'PV_5L_YH' => 'Pv 5 L  Yh',
            'PV_5L_ZG' => 'Pv 5 L  Zg',
            'PV_5L_LS' => 'Pv 5 L  Ls',
            'PV_5L_FX' => 'Pv 5 L  Fx',
            'PV_LS_TOUCH' => '隐藏零售区业绩30%后的',
            'SURPLUS_1L' => '一市场结余业绩',
            'SURPLUS_1L_ZC' => '一市场首购结余业绩',
            'SURPLUS_1L_FX' => '一市场复消结余业绩',
            'SURPLUS_2L' => '二市场结余业绩',
            'SURPLUS_2L_ZC' => '二市场首购结余业绩',
            'SURPLUS_2L_FX' => '二市场复消结余业绩',
            'SURPLUS_3L' => '三市场结余业绩',
            'SURPLUS_3L_ZC' => '三市场首购结余业绩',
            'SURPLUS_3L_FX' => '三市场复消结余业绩',
            'SURPLUS_4L' => '四市场结余业绩',
            'SURPLUS_4L_ZC' => '四市场首购结余业绩',
            'SURPLUS_4L_FX' => '四市场复消结余业绩',
            'SURPLUS_5L' => '五市场结余业绩',
            'SURPLUS_5L_ZC' => '五市场首购结余业绩',
            'SURPLUS_5L_FX' => '五市场复消结余业绩',
            'SURPLUS_LS' => '零售隐藏区结余业绩',
            'PERIOD_NUM' => '期数',
            'CALC_MONTH' => '结算月YYYYMM',
            'P_CALC_MONTH' => '表分区日期索引',
            'CREATED_AT' => '创建时间',
        ];
    }

    /**
     * 操作日志记录条件
     * @return array
     */
    public function attrLabelsWithLogType(){
        return [
            'USER_ID' => '会员ID',
            'SURPLUS_1L' => '一市场结余业绩',
            'SURPLUS_2L' => '二市场结余业绩',
            'SURPLUS_3L' => '三市场结余业绩',
            'SURPLUS_4L' => '四市场结余业绩',
            'SURPLUS_5L' => '五市场结余业绩',
            'SURPLUS_LS' => '虚拟零售区结余业绩',
        ];
    }

    /**
     * 获取期结余业绩
     * @param $periodNum
     * @param $userId
     * @return array|\yii\db\ActiveRecord|null
     * @throws \yii\db\Exception
     */
    public static function getPeriodSurplusPerf($periodNum, $userId){
        $period = Period::instance();
        $yearMonth = $period->getYearMonth($periodNum);
        $result = static::findUseDbCalc()->yearMonth($yearMonth)->select('SURPLUS_1L,SURPLUS_2L,SURPLUS_3L,SURPLUS_4L,SURPLUS_5L,SURPLUS_LS')->where('USER_ID=:USER_ID AND PERIOD_NUM=:PERIOD_NUM', [':USER_ID'=>$userId, ':PERIOD_NUM'=>$periodNum])->asArray()->one();
        if(!$result){
            $result = [
                'USER_ID' => $userId,
                'SURPLUS_1L' => 0,
                'SURPLUS_2L' => 0,
                'SURPLUS_3L' => 0,
                'SURPLUS_4L' => 0,
                'SURPLUS_5L' => 0,
                'SURPLUS_LS' => 0,
            ];
        }

        return $result;
    }
}
