<?php

namespace common\models;

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

/**
 * This is the model class for table "{{%USER_PERF}}".
 *
 * @property string $ID ID
 * @property string $USER_ID 会员ID
 * @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_1L 一市场累计业绩
 * @property string $PV_2L 二市场累计业绩
 * @property string $PV_3L 三市场累计业绩
 * @property string $PV_4L 四市场累计业绩
 * @property string $PV_5L 五市场累计业绩
 * @property int $CREATED_AT 创建时间
 * @property string $PV_PSS 推荐团队累计业绩
 * @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 $PV_PSS_TOTAL 推荐团队累计业绩
 */
class UserPerf extends \common\components\ActiveRecord
{
    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return '{{%USER_PERF}}';
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['USER_ID', 'CREATED_AT'], 'required'],
            [['PV_PCS_ZC', 'PV_PCS_FX', 'PV_1L', 'PV_2L', 'PV_3L', 'PV_4L', 'PV_5L', 'PV_PSS', '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', 'PV_PSS_TOTAL'], 'number'],
            [['CREATED_AT'], 'integer'],
            [['USER_ID', 'ID'], 'string', 'max' => 32],
            [['USER_ID'], 'unique'],
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'ID' => 'ID',
            'USER_ID' => '会员ID',
            'PV_PCS_ZC' => '个人注册单累计',
            'PV_PCS_FX' => '个人复销单累计',
            'PV_1L' => '一市场累计业绩',
            'PV_2L' => '二市场累计业绩',
            'PV_3L' => '三市场累计业绩',
            'PV_4L' => '四市场累计业绩',
            'PV_5L' => '五市场累计业绩',
            'CREATED_AT' => '创建时间',
            'PV_PSS' => '推荐团队累计业绩',
            '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' => '五市场复消结余业绩',
            'PV_PSS_TOTAL' => '推荐团队累计业绩',
        ];
    }

    /**
     * 用户累计业绩(默认主库)
     * @param $userId
     * @param string $db
     * @param bool $isSlaves
     * @return array|null|\yii\db\ActiveRecord
     */
    public static function getOneUserPerf($userId, $db = 'db', $isSlaves = false){
        $data = static::find($isSlaves, $db)
        ->select('USER_ID,PV_PCS_ZC,PV_PCS_FX,PV_PSS,PV_1L,PV_2L,PV_3L,PV_4L,PV_5L,SURPLUS_1L,SURPLUS_2L,SURPLUS_3L,SURPLUS_4L,SURPLUS_5L,SURPLUS_1L_ZC,SURPLUS_2L_ZC,SURPLUS_3L_ZC,SURPLUS_4L_ZC,SURPLUS_5L_ZC,SURPLUS_1L_FX,SURPLUS_2L_FX,SURPLUS_3L_FX,SURPLUS_4L_FX,SURPLUS_5L_FX,PV_PSS_TOTAL')
        ->where('USER_ID=:USER_ID', [':USER_ID'=>$userId])
        ->asArray()
        ->one();
        if(!$data){
            $data = [
                'USER_ID' => $userId,
                'PV_PCS_ZC' => 0,
                'PV_PCS_FX' => 0,
                'PV_PSS' => 0,
                'PV_1L' => 0,
                'PV_2L' => 0,
                'PV_3L' => 0,
                'PV_4L' => 0,
                'PV_5L' => 0,
                'SURPLUS_1L' => 0,
                'SURPLUS_2L' => 0,
                'SURPLUS_3L' => 0,
                'SURPLUS_4L' => 0,
                'SURPLUS_5L' => 0,
                'SURPLUS_1L_ZC' => 0,
                'SURPLUS_2L_ZC' => 0,
                'SURPLUS_3L_ZC' => 0,
                'SURPLUS_4L_ZC' => 0,
                'SURPLUS_5L_ZC' => 0,
                'SURPLUS_1L_FX' => 0,
                'SURPLUS_2L_FX' => 0,
                'SURPLUS_3L_FX' => 0,
                'SURPLUS_4L_FX' => 0,
                'SURPLUS_5L_FX' => 0,
                'PV_PSS_TOTAL' => 0,
            ];
        }
        return $data;
    }

    /**
     * 操作日志记录条件
     * @return array
     */
    public function attrLabelsWithLogType(){
        return [
            'USER_ID' => '会员ID',
            'PV_1L' => '一市场累计业绩',
            'PV_2L' => '二市场累计业绩',
            'PV_3L' => '三市场累计业绩',
            'PV_4L' => '四市场累计业绩',
            'PV_5L' => '五市场累计业绩',
            'PV_PSS' => '推荐团队累计业绩',
        ];
    }

    /**
     * 获取一名会员余额
     * @param $userId
     * @return array|null
     */
    public static function getPvByUserId($userId){
        $data = self::findOneAsArray('USER_ID=:USER_ID', [':USER_ID'=>$userId], 'USER_ID,PV_1L,PV_2L,PV_3L,PV_4L,PV_5L,PV_PSS');
        if(!$data){
            $data = [
                'USER_ID' => $userId,
                'PV_1L' => 0,
                'PV_2L' => 0,
                'PV_3L' => 0,
                'PV_4L' => 0,
                'PV_5L' => 0,
                'PV_PSS' => 0,
            ];
        }
        return $data;
    }

    /**
     * 获取当前结余业绩
     * @param $userId
     * @return array|\yii\db\ActiveRecord|null
     * @throws \yii\db\Exception
     */
    public static function getPeriodSurplusPerf($userId){
        $result = static::findUseDbCalc()->select('SURPLUS_1L,SURPLUS_2L,SURPLUS_3L,SURPLUS_4L,SURPLUS_5L')->where('USER_ID=:USER_ID', [':USER_ID'=>$userId])->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;
    }
}
