<?php

namespace common\models;

use common\helpers\snowflake\SnowFake;
use common\helpers\Tool;
use Exception;
use Yii;

/**
 * This is the model class for table "{{%SERVE_LOG}}".
 *
 * @property string $ID
 * @property string $PERIOD_NUM 业绩期
 * @property string $LOG_DESCRIBE 日志信息
 * @property string $CREATE_AT 创建时间
 * @property string $CALC_MONTH 结算年月
 */
class ServeLog extends \common\components\ActiveRecord
{
    /**
     * {@inheritdoc}
     */
    public static function tableName()
    {
        return '{{%SERVE_LOG}}';
    }

    /**
     * {@inheritdoc}
     */
    public function rules()
    {
        return [
            [['PERIOD_NUM', 'CALC_MONTH', 'CREATE_AT'], 'required'],
            [['ID'], 'unique'],
        ];
    }

    /**
     * {@inheritdoc}
     */
    public function attributeLabels()
    {
        return [
            'ID' => 'ID',
            'PERIOD_NUM' => '业绩期',
            'LOG_DESCRIBE' => '描述信息',
            'CALC_MONTH' => '总人数',
            'CREATE_AT' => '总业绩',
        ];
    }

    // =============================业绩相关日志==========================================================================
    /**
     * 无逻辑日志,直接传文案
     * @param int $periodNum
     * @param int $calcMonth 业绩期
     * @param string $desc 文案
     * @param int $sort 展示顺序,太快用时间戳判断不出来
     */
    public static function noParamsLog($periodNum, $calcMonth, $desc, $sort) {
        $logData[] = [
            'PERIOD_NUM' => $periodNum,
            'CALC_MONTH' => $calcMonth,
            'LOG_DESCRIBE' => $desc,
            'CREATE_AT' => time(),
            'RECORD_SORT' => $sort
        ];

        ServeLog::batchInsert($logData);
        return true;
    }
    
    // 第一步,记录期数结算年月
    /**
     * 记录业绩期,结算年月
     * @param int $periodNum
     * @param int $calcMonth 业绩期
     */
    public static function periodMonth($periodNum, $calcMonth) {
        $logData[] = [
            'PERIOD_NUM' => $periodNum,
            'CALC_MONTH' => $calcMonth,
            'LOG_DESCRIBE' => "期数:$periodNum,结算年月:$calcMonth,开始计算业绩",
            'CREATE_AT' => time(),
            'RECORD_SORT' => 100
        ];

        ServeLog::batchInsert($logData);
        return true;
    }

    /**
     * 记录推送过来的基础数据
     * @param int $periodNum
     * @param int $calcMonth 业绩期
     */
    public static function basicData($periodNum, $calcMonth) {
        $userCount = User::find()->where('1=1')->count('ID');
        $infoCount = UserInfo::find()->where('1=1')->count('ID');
        $networkConut = UserNetwork::find()->where('1=1')->count('ID');
        $userPerfConut = UserPerf::find()->where('1=1')->count('ID');
        $calcBonusQyConut = CalcBonusQY::find()
        ->where('PERIOD_NUM!=:PERIOD_NUM', [':PERIOD_NUM'=>$periodNum])
        ->count('ID');
        $perfPeriodCount = PerfPeriod::find()
        ->where('PERIOD_NUM!=:PERIOD_NUM', [':PERIOD_NUM'=>$periodNum])
        ->count('ID');
        $perfOrderCount = PerfOrder::find()->where('1=1')->count('ID');
        $relationCount = UserRelation::find()->where('1=1')->count('ID');
        $periodCount = Period::find()->where('1=1')->count('ID');
        $perfMonthCount = PerfMonth::find()
        ->where('CALC_MONTH!=:CALC_MONTH', [':CALC_MONTH'=>$calcMonth])
        ->count('ID');
        $desc = "记录推送基础数据信息:AR_USER-推送用户总条数:$userCount;AR_USER_INFO-推送用户信息总条数:$infoCount;AR_USER_NETWORK_NEW-推送安置网络总条数:$networkConut;AR_USER_PERF-推送用户结余业绩总条数:$userPerfConut;AR_CALC_BONUS_QY-推送团队奖总条数:$calcBonusQyConut;AR_PERF_PERIOD-推送期业绩总条数:$perfPeriodCount;AR_PERIOD-推送业绩期总条数:$periodCount;AR_USER_RELATION_NEW-推送开拓网络总条数:$relationCount;AR_PERF_MONTH-推送月业绩表总条数:$perfMonthCount;AR_PERF_ORDER-推送业绩单总条数:$perfOrderCount";
        $logData[] = [
            'PERIOD_NUM' => $periodNum,
            'CALC_MONTH' => $calcMonth,
            'LOG_DESCRIBE' => $desc,
            'CREATE_AT' => time(),
            'RECORD_SORT' => 200
        ];

        ServeLog::batchInsert($logData);
        return true;
    }

    /**
     * 记录清除的期业绩和月业绩
     * @param int $periodNum
     * @param int $calcMonth 业绩期
     */
    public static function clearPerf($periodNum, $calcMonth) {
        $perfPeriodCount = PerfPeriod::find()
        ->where('PERIOD_NUM=:PERIOD_NUM', [':PERIOD_NUM'=>$periodNum])
        ->count('ID');
        $perfMonthCount = PerfMonth::find()
        ->where('CALC_MONTH=:CALC_MONTH', [':CALC_MONTH'=>$calcMonth])
        ->count('ID');
        $desc = "清除业绩数据:AR_PERF_PERIOD-清除期业绩表总条数:$perfPeriodCount;AR_PERF_MONTH-清除月业绩表总条数:$perfMonthCount";
        $logData[] = [
            'PERIOD_NUM' => $periodNum,
            'CALC_MONTH' => $calcMonth,
            'LOG_DESCRIBE' => $desc,
            'CREATE_AT' => time(),
            'RECORD_SORT' => 300
        ];

        ServeLog::batchInsert($logData);
        return true;
    }

    /**
     * 记录缓存用户数据
     * @param int $periodNum
     * @param int $calcMonth 业绩期
     */
    public static function cacheUserTotal($periodNum, $calcMonth) {
        $allUserCount = UserInfo::findUseDbCalc()
        ->from(UserInfo::tableName(). 'AS UI')
        ->select('UI.USER_ID,U.STATUS')
        ->leftJoin(User::tableName() . 'AS U', 'U.ID=UI.USER_ID')
        ->where('1=1')
        ->count('UI.USER_ID');
        $activeUserCount = UserInfo::findUseDbCalc()
        ->from(UserInfo::tableName(). 'AS UI')
        ->select('UI.USER_ID,U.STATUS')
        ->leftJoin(User::tableName() . 'AS U', 'U.ID=UI.USER_ID')
        ->where('U.STATUS=1')
        ->count('UI.USER_ID');
        $total = $allUserCount+$activeUserCount;
        $desc = "缓存用户信息-结束,缓存数量:$total";
        $logData[] = [
            'PERIOD_NUM' => $periodNum,
            'CALC_MONTH' => $calcMonth,
            'LOG_DESCRIBE' => $desc,
            'CREATE_AT' => time(),
            'RECORD_SORT' => 500
        ];

        ServeLog::batchInsert($logData);
        return true;
    }

    /**
     * 记录期业绩数据
     * @param int $periodNum
     * @param int $calcMonth 业绩期
     */
    public static function perfPeriodInfo($periodNum, $calcMonth) {
        $data = PerfPeriod::findUseSlaves()
        ->select('SUM(PV_PCS) AS TOTAL_PV_PCS, COUNT(ID) AS TOTAL_NUM')
        ->where('PERIOD_NUM=:PERIOD_NUM', ['PERIOD_NUM'=>$periodNum])
        ->asArray()
        ->one();
        $num = $data['TOTAL_NUM'];
        $total_pvpcs = Tool::formatPrice($data['TOTAL_PV_PCS']);
        $desc = "期业绩计算完成:AR_PERF_PERIOD-期业绩总条数:$num,期业绩个人消费PV总数:$total_pvpcs";
        $logData[] = [
            'PERIOD_NUM' => $periodNum,
            'CALC_MONTH' => $calcMonth,
            'LOG_DESCRIBE' => $desc,
            'CREATE_AT' => time(),
            'RECORD_SORT' => 600
        ];

        ServeLog::batchInsert($logData);
        return true;
    }

    /**
     * 记录月业绩数据
     * @param int $periodNum
     * @param int $calcMonth 业绩期
     */
    public static function perfMonthInfo($periodNum, $calcMonth) {
        $data = PerfMonth::findUseSlaves()
        ->select('SUM(PV_PCS) AS TOTAL_PV_PCS, COUNT(ID) AS TOTAL_NUM')
        ->where('CALC_MONTH=:CALC_MONTH', ['CALC_MONTH'=>$calcMonth])
        ->asArray()
        ->one();
        $num = $data['TOTAL_NUM'];
        $total_pvpcs = Tool::formatPrice($data['TOTAL_PV_PCS']);
        $desc = "月业绩计算完成:AR_PERF_MONTH-月业绩表总条数:$num,月业绩个人消费总数:$total_pvpcs";
        $logData[] = [
            'PERIOD_NUM' => $periodNum,
            'CALC_MONTH' => $calcMonth,
            'LOG_DESCRIBE' => $desc,
            'CREATE_AT' => time(),
            'RECORD_SORT' => 700
        ];

        ServeLog::batchInsert($logData);
        return true;
    }

    /**
     * 记录期业绩活跃数据
     * @param int $periodNum
     * @param int $calcMonth 业绩期
     */
    public static function perfPeriodActive($periodNum, $calcMonth) {
        $data = PerfPeriod::findUseSlaves()
        ->select('COUNT(ID) AS TOTAL_NUM')
        ->where('PERIOD_NUM=:PERIOD_NUM AND IS_ACTIVE=1', ['PERIOD_NUM'=>$periodNum])
        ->asArray()
        ->one();
        $num = $data['TOTAL_NUM'];
        $desc = "期业绩更新活跃状态，活跃人数:$num";
        $logData[] = [
            'PERIOD_NUM' => $periodNum,
            'CALC_MONTH' => $calcMonth,
            'LOG_DESCRIBE' => $desc,
            'CREATE_AT' => time(),
            'RECORD_SORT' => 800
        ];

        ServeLog::batchInsert($logData);
        return true;
    }

    // =============================奖金相关日志=====================================================================================
    /**
     * 记录清除的奖金流水表数据
     * @param int $periodNum
     * @param int $calcMonth 业绩期
     * @param int $isCalcMonth 是否月节点
     */
    public static function clearTableBonus($periodNum, $calcMonth, $isCalcMonth) {
        $bonusCount = CalcBonus::find()->where('1=1')->count('ID');
        $bonusTgCount = CalcBonusTG::find()->where('1=1')->count('ID');
        $bonusBdCount = CalcBonusBD::find()->where('1=1')->count('ID');
        $bonusQyCount = CalcBonusQY::find()->where('PERIOD_NUM=:PERIOD_NUM', [':PERIOD_NUM'=>$periodNum])->count('ID');
        $bonusBsCount = $bonusQuarterCount = $bonusVillaCount = $bonusGarageCount = 0;

        if ($isCalcMonth) {
            $bonusBsCount = CalcBonusBS::find()->where('1=1')->count('ID');
            $bonusQuarterCount = CalcBonusQuarter::find()->where('1=1')->count('ID');
            $bonusVillaCount = CalcBonusVilla::find()->where('1=1')->count('ID');
            $bonusGarageCount = CalcBonusGarage::find()->where('1=1')->count('ID');
        }

        $desc = "清除奖金流水表数据:AR_CALC_BONUS-清除奖金汇总表总数:$bonusCount;AR_CALC_BONUS_TG-清除推广将流水表总条数:$bonusTgCount;AR_CALC_BONUS_BD-清除服务奖流水表总条数:$bonusBdCount;AR_CALC_BONUS_QY-清除团队奖流水表总条数:$bonusQyCount;AR_CALC_BONUS_BS-清除管理奖流水表总条数:$bonusBsCount;AR_CALC_BONUS_QUARTER-清除季度奖流水表总条数:$bonusQuarterCount;AR_CALC_BONUS_VILLA-清除房奖流水表总条数:$bonusVillaCount;AR_CALC_BONUS_GARAGE-清除车房奖流水表总条数:$bonusGarageCount";
        $logData[] = [
            'PERIOD_NUM' => $periodNum,
            'CALC_MONTH' => $calcMonth,
            'LOG_DESCRIBE' => $desc,
            'CREATE_AT' => time(),
            'RECORD_SORT' => 3100
        ];

        ServeLog::batchInsert($logData);
        return true;
    }

    /**
     * 记录管理奖数据
     * @param int $periodNum
     * @param int $calcMonth 业绩期
     */
    public static function bonusBSData($periodNum, $calcMonth) {
        $data = CalcBonusBS::findUseSlaves()
        ->select('SUM(AMOUNT) AS ORI_AMOUNT,SUM(ORI_BONUS) AS REAL_BONUS,COUNT(ID) AS USER_NUM')
        ->where('PERIOD_NUM=:PERIOD_NUM', ['PERIOD_NUM'=>$periodNum])
        ->asArray()
        ->one();
        $num = $data['USER_NUM'];
        $oriBonus = Tool::formatPrice($data['ORI_AMOUNT']);
        $realBonus = Tool::formatPrice($data['REAL_BONUS']);
        // 查询得将人数
        $hasBonus = CalcBonusBS::findUseSlaves()
        ->select('COUNT(ID) AS BONUS_USER_NUM')
        ->where('PERIOD_NUM=:PERIOD_NUM AND ORI_BONUS > 0', ['PERIOD_NUM'=>$periodNum])
        ->asArray()
        ->one();
        $hasBonusNum = $hasBonus['BONUS_USER_NUM'];
        $desc = "计算管理奖完成AR_CALC_BONUS_BS:人数:$num,得奖人数:$hasBonusNum,原金额:$oriBonus,实发金额:$realBonus";
        $logData[] = [
            'PERIOD_NUM' => $periodNum,
            'CALC_MONTH' => $calcMonth,
            'LOG_DESCRIBE' => $desc,
            'CREATE_AT' => time(),
            'RECORD_SORT' => 3200
        ];

        ServeLog::batchInsert($logData);
        return true;
    }

    /**
     * 记录月业绩,小组业绩数据
     * @param int $periodNum
     * @param int $calcMonth 业绩期
     */
    public static function perfMonthPgs($periodNum, $calcMonth) {
        $data = PerfMonth::findUseSlaves()
        ->select('SUM(DIRECTOR_BONUS_PGS) AS SUM_PGS,COUNT(ID) AS USER_NUM')
        ->where('CALC_MONTH=:CALC_MONTH', ['CALC_MONTH'=>$calcMonth])
        ->asArray()
        ->one();
        $userNum = $data['USER_NUM'];
        $totalPgs = Tool::formatPrice($data['SUM_PGS']);
        $desc = "月业绩表增加小组业绩完成AR_PERF_MONTH:小组业绩:$totalPgs,条数:$userNum";
        $logData[] = [
            'PERIOD_NUM' => $periodNum,
            'CALC_MONTH' => $calcMonth,
            'LOG_DESCRIBE' => $desc,
            'CREATE_AT' => time(),
            'RECORD_SORT' => 3300
        ];

        ServeLog::batchInsert($logData);
        return true;
    }

    /**
     * 记录服务奖数据
     * @param int $periodNum
     * @param int $calcMonth 业绩期
     */
    public static function bonusBDData($periodNum, $calcMonth) {
        $data = CalcBonusBD::findUseSlaves()
        ->select('SUM(AMOUNT) AS SUM_AMOUNT,COUNT(ID) AS USER_NUM')
        ->where('PERIOD_NUM=:PERIOD_NUM AND AMOUNT>0', ['PERIOD_NUM'=>$periodNum])
        ->asArray()
        ->one();
        $userNum = $data['USER_NUM'];
        $totalAmount = Tool::formatPrice($data['SUM_AMOUNT']);
        $desc = "计算服务奖完成AR_CALC_BONUS_BD:得奖人数:$userNum,金额:$totalAmount";
        $logData[] = [
            'PERIOD_NUM' => $periodNum,
            'CALC_MONTH' => $calcMonth,
            'LOG_DESCRIBE' => $desc,
            'CREATE_AT' => time(),
            'RECORD_SORT' => 3400
        ];

        ServeLog::batchInsert($logData);
        return true;
    }

    /**
     * 记录推广奖数据
     * @param int $periodNum
     * @param int $calcMonth 业绩期
     */
    public static function bonusTGData($periodNum, $calcMonth) {
        $data = CalcBonusTG::findUseSlaves()
        ->select('SUM(AMOUNT) AS SUM_AMOUNT,COUNT(ID) AS USER_NUM')
        ->where('PERIOD_NUM=:PERIOD_NUM AND AMOUNT>0', ['PERIOD_NUM'=>$periodNum])
        ->asArray()
        ->one();
        $userNum = $data['USER_NUM'];
        $totalAmount = Tool::formatPrice($data['SUM_AMOUNT']);
        $desc = "计算推广奖完成AR_CALC_BONUS_TG:得奖人数:$userNum,金额:$totalAmount";
        $logData[] = [
            'PERIOD_NUM' => $periodNum,
            'CALC_MONTH' => $calcMonth,
            'LOG_DESCRIBE' => $desc,
            'CREATE_AT' => time(),
            'RECORD_SORT' => 3500
        ];

        ServeLog::batchInsert($logData);
        return true;
    }

    /**
     * 记录团队奖数据
     * @param int $periodNum
     * @param int $calcMonth 业绩期
     */
    public static function bonusQYData($periodNum, $calcMonth) {
        // 获取封顶人数
        $cappedNumData = CalcBonusQY::findUseSlaves()
        ->select('ID,USER_ID')
        ->where('PERIOD_NUM=:PERIOD_NUM AND ORI_CAPPED_BONUS_QY > HOPE_BONUS', ['PERIOD_NUM'=>$periodNum])
        ->groupBy('USER_ID')
        ->asArray()
        ->all();
        $cappedNumData = array_column($cappedNumData, 'USER_ID');
        $cappedNum = count($cappedNumData);
        // 封顶金额
        $cappedBonusData = CalcBonusQY::findUseSlaves()
        ->select('(SUM(ORI_CAPPED_BONUS_QY)-SUM(HOPE_BONUS)) AS CAPPED_AMOUNT')
        ->where('PERIOD_NUM=:PERIOD_NUM AND ORI_CAPPED_BONUS_QY > HOPE_BONUS', ['PERIOD_NUM'=>$periodNum])
        ->asArray()
        ->one();
        $cappendBonus = Tool::formatPrice($cappedBonusData['CAPPED_AMOUNT']);

        // 得奖人数
        $bonusNumData = CalcBonusQY::findUseSlaves()
        ->select('ID,USER_ID')
        ->where('PERIOD_NUM=:PERIOD_NUM AND AMOUNT > 0', ['PERIOD_NUM'=>$periodNum])
        ->groupBy('USER_ID')
        ->asArray()
        ->all();
        $bonusNumData = array_column($bonusNumData, 'USER_ID');
        $bonusNum = count($bonusNumData);
        // 实发金额
        $bonusData = CalcBonusQY::findUseSlaves()
        ->select('SUM(AMOUNT) AS BONUS_AMOUNT')
        ->where('PERIOD_NUM=:PERIOD_NUM', ['PERIOD_NUM'=>$periodNum])
        ->asArray()
        ->one();
        $realBonus = Tool::formatPrice($bonusData['BONUS_AMOUNT']);

        // 人数
        $allNumData = CalcBonusQY::findUseSlaves()
        ->select('ID,USER_ID')
        ->where('PERIOD_NUM=:PERIOD_NUM', ['PERIOD_NUM'=>$periodNum])
        ->groupBy('USER_ID')
        ->asArray()
        ->all();
        $allNumData = array_column($allNumData, 'USER_ID');
        $allNum = count($allNumData);
        // 金额
        $allBonusData = CalcBonusQY::findUseSlaves()
        ->select('SUM(HOPE_BONUS) AS ALL_BONUS_AMOUNT')
        ->where('PERIOD_NUM=:PERIOD_NUM', ['PERIOD_NUM'=>$periodNum])
        ->asArray()
        ->one();
        $oriBonus = Tool::formatPrice($allBonusData['ALL_BONUS_AMOUNT']);

        $desc = "计算团队奖完成AR_CALC_BONUS_QY:人数:$allNum,得奖人数:$bonusNum,原金额:$oriBonus,实发金额:$realBonus,封顶人数:$cappedNum,封顶金额:$cappendBonus";
        $logData[] = [
            'PERIOD_NUM' => $periodNum,
            'CALC_MONTH' => $calcMonth,
            'LOG_DESCRIBE' => $desc,
            'CREATE_AT' => time(),
            'RECORD_SORT' => 3600
        ];

        ServeLog::batchInsert($logData);
        return true;
    }

    /**
     * 记录房奖数据
     * @param int $periodNum
     * @param int $calcMonth 业绩期
     */
    public static function bonusVillaData($periodNum, $calcMonth) {
        // 得奖人数和实发金额
        $hasNumData = CalcBonusVilla::findUseSlaves()
        ->select('COUNT(ID) AS USER_NUM, SUM(AMOUNT) AS BONUS_AMOUNT')
        ->where('PERIOD_NUM=:PERIOD_NUM AND AMOUNT > 0', ['PERIOD_NUM'=>$periodNum])
        ->asArray()
        ->one();
        $hasNum = $hasNumData['USER_NUM'];
        $realBonus =  Tool::formatPrice($hasNumData['BONUS_AMOUNT']);
        
        // 封顶金额
        $cappedBonusData = CalcBonusVilla::findUseSlaves()
        ->select('(SUM(CAP_AMOUNT) - SUM(AMOUNT)) AS CAPPED_AMOUNT')
        ->where('PERIOD_NUM=:PERIOD_NUM AND CAP_AMOUNT > 0 AND CAP_AMOUNT > AMOUNT', ['PERIOD_NUM'=>$periodNum])
        ->asArray()
        ->one();
        $cappendBonus = Tool::formatPrice($cappedBonusData['CAPPED_AMOUNT']);

        $desc = "计算房奖完成AR_CALC_BONUS_VILLA:得奖人数:$hasNum,金额:$realBonus,封顶金额:$cappendBonus";
        $logData[] = [
            'PERIOD_NUM' => $periodNum,
            'CALC_MONTH' => $calcMonth,
            'LOG_DESCRIBE' => $desc,
            'CREATE_AT' => time(),
            'RECORD_SORT' => 3800
        ];

        ServeLog::batchInsert($logData);
        return true;
    }

    /**
     * 记录车奖数据
     * @param int $periodNum
     * @param int $calcMonth 业绩期
     */
    public static function bonusGarageData($periodNum, $calcMonth) {
        // 得奖人数和实发金额
        $hasNumData = CalcBonusGarage::findUseSlaves()
        ->select('COUNT(ID) AS USER_NUM, SUM(AMOUNT) AS BONUS_AMOUNT')
        ->where('PERIOD_NUM=:PERIOD_NUM AND AMOUNT > 0', ['PERIOD_NUM'=>$periodNum])
        ->asArray()
        ->one();
        $hasNum = $hasNumData['USER_NUM'];
        $realBonus = Tool::formatPrice($hasNumData['BONUS_AMOUNT']);
        
        // 封顶金额
        $cappedBonusData = CalcBonusGarage::findUseSlaves()
        ->select('(SUM(CAP_AMOUNT) - SUM(AMOUNT)) AS CAPPED_AMOUNT')
        ->where('PERIOD_NUM=:PERIOD_NUM AND CAP_AMOUNT > 0 AND CAP_AMOUNT>AMOUNT', ['PERIOD_NUM'=>$periodNum])
        ->asArray()
        ->one();
        $cappendBonus = Tool::formatPrice($cappedBonusData['CAPPED_AMOUNT']);

        $desc = "计算车奖完成AR_CALC_BONUS_GARAGE:得奖人数:$hasNum,金额:$realBonus,封顶金额:$cappendBonus";
        $logData[] = [
            'PERIOD_NUM' => $periodNum,
            'CALC_MONTH' => $calcMonth,
            'LOG_DESCRIBE' => $desc,
            'CREATE_AT' => time(),
            'RECORD_SORT' => 3900
        ];

        ServeLog::batchInsert($logData);
        return true;
    }

    /**
     * 记录季度奖数据
     * @param int $periodNum
     * @param int $calcMonth 业绩期
     */
    public static function bonusQuarterData($periodNum, $calcMonth) {
        // 得奖人数和实发金额
        $hasNumData = CalcBonusQuarter::findUseSlaves()
        ->select('COUNT(ID) AS USER_NUM, SUM(AMOUNT) AS BONUS_AMOUNT')
        ->where('PERIOD_NUM=:PERIOD_NUM AND AMOUNT > 0', ['PERIOD_NUM'=>$periodNum])
        ->asArray()
        ->one();
        $hasNum = $hasNumData['USER_NUM'];
        $realBonus = Tool::formatPrice($hasNumData['BONUS_AMOUNT']);
        
        // 原奖金
        $cappedBonusData = CalcBonusQuarter::findUseSlaves()
        ->select('SUM(ORI_BONUS) AS ORI_AMOUNT')
        ->where('PERIOD_NUM=:PERIOD_NUM', ['PERIOD_NUM'=>$periodNum])
        ->asArray()
        ->one();
        $oriAmount = Tool::formatPrice($cappedBonusData['ORI_AMOUNT']);

        $desc = "计算季度奖完成AR_CALC_BONUS_QUARTER:得奖人数:$hasNum,原金额:$oriAmount,实发金额:$realBonus";
        $logData[] = [
            'PERIOD_NUM' => $periodNum,
            'CALC_MONTH' => $calcMonth,
            'LOG_DESCRIBE' => $desc,
            'CREATE_AT' => time(),
            'RECORD_SORT' => 4000
        ];

        ServeLog::batchInsert($logData);
        return true;
    }

    /**
     * 记录奖金汇总表数据
     * @param int $periodNum
     * @param int $calcMonth 业绩期
     */
    public static function bonusData($periodNum, $calcMonth) {
        $bonusData = CalcBonus::findUseSlaves()
        ->select('SUM(BONUS_REAL) AS LOG_BONUS_REAL,SUM(BONUS_TOTAL) AS LOG_BONUS_TOTAL,SUM(ORI_BONUS_BS) AS LOG_BONUS_BS,
        SUM(ORI_BONUS_QY) AS LOG_BONUS_QY,SUM(ORI_BONUS_BD) AS LOG_BONUS_BD,SUM(ORI_BONUS_TG) AS LOG_BONUS_TG,
        SUM(ORI_BONUS_QUARTER) AS LOG_BONUS_QUARTER,SUM(ORI_BONUS_GARAGE) AS LOG_BONUS_GARAGE,SUM(ORI_BONUS_VILLA) AS LOG_BONUS_VILLA,
        SUM(MANAGE_TAX) AS LOG_MANAGE_TAX,SUM(RECONSUME_POINTS) AS LOG_RECONSUME_POINTS')
        ->where('PERIOD_NUM=:PERIOD_NUM', ['PERIOD_NUM'=>$periodNum])
        ->asArray()
        ->one();
        $totalBonus = Tool::formatPrice($bonusData['LOG_BONUS_TOTAL']);// 总金额
        $realBonus = Tool::formatPrice($bonusData['LOG_BONUS_REAL']);// 实发总额
        $bsBonus = Tool::formatPrice($bonusData['LOG_BONUS_BS']);// 管理奖总额
        $qyBonus = Tool::formatPrice($bonusData['LOG_BONUS_QY']);// 团队奖
        $bdBonus = Tool::formatPrice($bonusData['LOG_BONUS_BD']);// 服务奖
        $tgBonus = Tool::formatPrice($bonusData['LOG_BONUS_TG']);// 推广奖
        $quarterBonus = Tool::formatPrice($bonusData['LOG_BONUS_QUARTER']);// 季度奖
        $garageBonus = Tool::formatPrice($bonusData['LOG_BONUS_GARAGE']);// 车奖
        $villaBonus = Tool::formatPrice($bonusData['LOG_BONUS_VILLA']);// 房奖
        $manageTax = Tool::formatPrice($bonusData['LOG_MANAGE_TAX']);// 管理费
        $reconsumePoints = Tool::formatPrice($bonusData['LOG_RECONSUME_POINTS']);// 复消积分

        // 得奖人数
        $hasNum = CalcBonus::find()
        ->where('PERIOD_NUM=:PERIOD_NUM AND BONUS_REAL>0', [':PERIOD_NUM'=>$periodNum])
        ->count('ID');
        
        $desc = "奖金写入奖金汇总表完成AR_CALC_BONUS:得奖人数:$hasNum,实发金额:$realBonus,原金额:$totalBonus,管理奖金额:$bsBonus,
团队奖金额:$qyBonus,服务奖金额:$bdBonus,推广奖金额:$tgBonus,季度奖金额:$quarterBonus,车奖金额:$garageBonus,房奖金额:$villaBonus,
管理费:$manageTax,复消积分:$reconsumePoints";
        $logData[] = [
            'PERIOD_NUM' => $periodNum,
            'CALC_MONTH' => $calcMonth,
            'LOG_DESCRIBE' => $desc,
            'CREATE_AT' => time(),
            'RECORD_SORT' => 4200
        ];

        ServeLog::batchInsert($logData);
        return true;
    }
}