<?php

namespace common\models;

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

/**
 * This is the model class for table "{{%USER_BONUS}}".
 *
 * @property string $ID
 * @property string $USER_ID 会员ID
 * @property string $BONUS 奖金总额
 * @property string $BONUS_FREEZE 奖金冻结部分
 * @property string $CF 车房养老奖
 * @property string $LX 领袖分红奖
 * @property string $STORE_TOTAL 店服务奖累计
 * @property string $ORI_STORE_TOTAL 店服务奖原奖金累计
 * @property string $QY_TOTAL 区域津贴累计
 * @property string $YC_TOTAL 育成津贴累计
 * @property string $VIP_TOTAL VIP奖金累计
 * @property string $FX_TOTAL 复销奖累计
 * @property string $LS_TOTAL 零售奖累计
 * @property string $FW_TOTAL 区级店业绩提成累计
 * @property string $BT_TOTAL 店补累计
 * @property string $BT_PROD_TOTAL 产品补贴累计
 * @property string $BT_TOOL_TOTAL 工具流补贴累计
 * @property string $CF_TOTAL 车房养老奖累计
 * @property string $LX_TOTAL 领袖分红累计
 * @property string $DEDUCT_ZR_TOTAL 责任业绩扣除累计
 * @property string $FL_TOTAL 形象店货补累计
 * @property int $VER 乐观锁标识
 * @property int $CREATED_AT 创建时间
 * @property int $BD_TOTAL 服务奖累计
 * @property int $TG_TOTAL 推广奖累计
 * @property int $YJ_TOTAL 业绩奖累计
 * @property int $GX_TOTAL 共享奖累计
 * @property int $GL_TOTAL 管理奖累计
 * @property int $ORI_QY_TOTAL 团队奖原奖金累计
 * @property int $ORI_YC_TOTAL 荣衔奖原奖金累计
 * @property int $ORI_VIP_TOTAL VIP奖原奖金累计
 * @property int $ORI_BD_TOTAL 服务奖原奖金累计
 * @property int $ORI_TG_TOTAL 推广奖原奖金累计
 * @property int $ORI_YJ_TOTAL 业绩奖原奖金累计
 * @property int $ORI_GX_TOTAL 共享奖原奖金累计
 * @property int $ORI_GL_TOTAL 管理奖原奖金累计
 * @property int $RECONSUME_POINTS 复消积分
 * @property int $RECONSUME_POINTS_EXPIRED 复消积分过期
 * @property int $RECONSUME_POINTS_TOTAL 复消积分累计
 * @property int $MANAGE_TAX 管理费累计
 * @property int $BONUS_TOTAL 总奖金累计
 */
class UserBonus extends \common\components\ActiveRecord
{
    const TYPE = BalanceAudit::TYPE;

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

    /**
     * {@inheritdoc}
     */
    public function rules()
    {
        return [
            [['USER_ID', 'CREATED_AT'], 'required'],
            [['BS_TOTAL','ORI_BS_TOTAL','BS','BONUS', 'BONUS_FREEZE', 'CF', 'LX', 'QY_TOTAL', 'YC_TOTAL', 'VIP_TOTAL', 'FX_TOTAL', 'LS_TOTAL', 'FW_TOTAL', 'BT_TOTAL', 'BT_PROD_TOTAL', 'BT_TOOL_TOTAL', 'CF_TOTAL', 'LX_TOTAL', 'DEDUCT_ZR_TOTAL', 'FL_TOTAL', 'BD_TOTAL','TG_TOTAL','YJ_TOTAL','GX_TOTAL','GL_TOTAL', 'ORI_QY_TOTAL','ORI_YC_TOTAL','ORI_YC_TOTAL','ORI_BD_TOTAL','ORI_TG_TOTAL','ORI_YJ_TOTAL','ORI_GX_TOTAL','ORI_GL_TOTAL','RECONSUME_POINTS','MANAGE_TAX', 'RECONSUME_POINTS_EXPIRED', 'RECONSUME_POINTS_TOTAL', 'BONUS_TOTAL'], 'number'],
            [['VER', 'CREATED_AT'], 'integer'],
            [['ID','USER_ID'], 'string', 'max' => 32],
            [['USER_ID'], 'unique'],
        ];
    }

    /**
     * {@inheritdoc}
     */
    public function attributeLabels()
    {
        return [
            'ID' => 'ID',
            'USER_ID' => '会员ID',
            'BONUS' => '奖金总额',
            'BONUS_FREEZE' => '奖金冻结部分',
            'CF' => '车房养老奖',
            'LX' => '领袖分红奖',
            'STORE_TOTAL' => '店服务奖累计',
            'QY_TOTAL' => '区域津贴累计',
            'YC_TOTAL' => '育成津贴累计',
            'VIP_TOTAL' => 'VIP奖金累计',
            'FX_TOTAL' => '复销奖累计',
            'LS_TOTAL' => '零售奖累计',
            'FW_TOTAL' => '服务奖累计',
            'BT_TOTAL' => '店补累计',
            'BT_PROD_TOTAL' => '产品补贴累计',
            'BT_TOOL_TOTAL' => '工具流补贴累计',
            'CF_TOTAL' => '车房养老奖累计',
            'LX_TOTAL' => '领袖分红累计',
            'DEDUCT_ZR_TOTAL' => '责任业绩扣除累计',
            'FL_TOTAL' => '形象店货补累计',
            'VER' => '乐观锁标识',
            'CREATED_AT' => '创建时间',
            'BD_TOTAL'=> '服务奖累计',
            'TG_TOTAL'=> '推广奖累计',
            'YJ_TOTAL'=> '业绩奖累计',
            'GX_TOTAL'=> '共享奖累计',
            'GL_TOTAL'=> '管理奖累计',
            'ORI_STORE_TOTAL' => '店服务奖原奖金累计',
            'ORI_QY_TOTAL' => '团队奖原奖金累计',
            'ORI_YC_TOTAL' => '荣衔奖原奖金累计',
            'ORI_VIP_TOTAL' => 'VIP奖原奖金累计',
            'ORI_BD_TOTAL' => '服务奖原奖金累计',
            'ORI_TG_TOTAL' => '推广奖原奖金累计',
            'ORI_YJ_TOTAL' => '业绩奖原奖金累计',
            'ORI_GX_TOTAL' => '共享奖原奖金累计',
            'ORI_GL_TOTAL' => '管理奖原奖金累计',
            'RECONSUME_POINTS' => '复销点数',
            'RECONSUME_POINTS_TOTAL' => '复销点数累计',
            'RECONSUME_POINTS_EXPIRED' => '复销点数过期',
            'MANAGE_TAX' => '管理费累计',
            'BONUS_TOTAL' => '总奖金累计',
            'BS' => '蓝星奖(即新的管理奖)',
            'BS_TOTAL' => '蓝星（管理）累计',
            'ORI_BS_TOTAL' => '蓝星原奖金累计'
        ];
    }

    /**
     * 操作日志记录条件
     * @return array
     */
    public function attrLabelsWithLogType(){
        return [
            'USER_ID' => '会员ID',
            'BONUS' => '会员账户',
            'CF' => '车房养老奖',
            'LX' => '领袖分红奖',
        ];
    }

    /**
     * 乐观锁
     * @return string
     */
    public function optimisticLock()
    {
        return 'VER';
    }

    /**
     * 获取一名会员的余额
     * @param $userId
     * @return array|null
     */
    public static function getBonusByUserId($userId){
        $data = UserBonus::findOneAsArray('USER_ID=:USER_ID', [':USER_ID'=>$userId], 'USER_ID,BONUS,CF,LX');
        if(!$data){
            $data = [
                'USER_ID' => $userId,
                'BONUS' => 0,
                'CF' => 0,
                'LX' => 0,
            ];
        }
        return $data;
    }
}
