<?php
/**
 * Created by PhpStorm.
 * User: leo
 * Date: 2018/6/12
 * Time: 下午2:11
 */

namespace common\helpers;


use common\models\CalcBonus;
use common\models\PerfCompany;
use common\models\Period;
use common\models\UserInfo;
use common\models\Withdraw;
use yii\base\BaseObject;
use yii\base\StaticInstanceTrait;

class ChartData extends BaseObject
{
    use StaticInstanceTrait;

    /**
     * 年度累计奖金
     * @return int|mixed
     */
    public function totalBonus(){
        return 0;
        $nowYear = Date::nowYear();
        $bonus = CalcBonus::findUseSlaves()->select('SUM(BONUS_QY) AS BONUS_QY_SUM, SUM(BONUS_YC) AS BONUS_YC_SUM, SUM(BONUS_FX) AS BONUS_FX_SUM, SUM(BONUS_LS) AS BONUS_LS_SUM, SUM(BONUS_CF) AS BONUS_CF_SUM, SUM(BONUS_LX) AS BONUS_LX_SUM, SUM(BONUS_FL) AS BONUS_FL_SUM, SUM(BONUS_BT) AS BONUS_BT_SUM, SUM(BONUS_FW) AS BONUS_FW_SUM')->where('CALC_YEAR=:CALC_YEAR AND IS_SENT=:IS_SENT', [':CALC_YEAR'=>$nowYear, ':IS_SENT'=>1])->asArray()->one();
        if($bonus){
            return $bonus['BONUS_QY_SUM'] + $bonus['BONUS_YC_SUM'] + $bonus['BONUS_FX_SUM'] + $bonus['BONUS_LS_SUM'] + $bonus['BONUS_CF_SUM'] + $bonus['BONUS_LX_SUM'] + $bonus['BONUS_FL_SUM'] + $bonus['BONUS_BT_SUM'] + $bonus['BONUS_FW_SUM'];
        } else {
            return 0;
        }
    }

    /**
     * 年度总业绩
     * @return int|mixed
     */
    public function totalPerf(){
        return 0;
        $nowYear = Date::nowYear();
        $perf = PerfCompany::findUseSlaves()->select('SUM(ZC_PV) AS ZC_PV_SUM, SUM(YH_PV) AS YH_PV_SUM, SUM(ZG_PV) AS ZG_PV_SUM, SUM(LS_PV) AS LS_PV_SUM, SUM(FX_PV) AS FX_PV_SUM')->where('CALC_YEAR=:CALC_YEAR', [':CALC_YEAR'=>$nowYear])->asArray()->one();
        if($perf){
            return $perf['ZC_PV_SUM'] + $perf['YH_PV_SUM'] + $perf['ZG_PV_SUM'] + $perf['LS_PV_SUM'] + $perf['FX_PV_SUM'];
        } else {
            return 0;
        }
    }

    /**
     * 总提现金额
     * @return int|mixed
     */
    public function totalWithdraw(){
        $withdraw = Withdraw::findUseSlaves()->select('SUM(AMOUNT) AS AMOUNT_SUM')->where('AUDIT_STATUS=:AUDIT_STATUS AND CREATED_AT > :START_TIME AND CREATED_AT < :END_TIME', [':AUDIT_STATUS' => Withdraw::STATUS_PAID, ':START_TIME' => Date::yearStart(), ':END_TIME' => Date::yearEnd()])->asArray()->one();
        if($withdraw){
            return $withdraw['AMOUNT_SUM'];
        } else {
            return 0;
        }
    }

    /**
     * 会员总数量
     * @return int|string
     */
    public function totalUser(){
        return UserInfo::findUseSlaves()->where('1=1')->count();
    }

    /**
     * 获取会员注册量
     * @return array
     */
    public function getUserData(){
        $monthAgo = [
            [
                'start' => Date::monthStart(Date::nowTime()),
                'end' => Date::monthEnd(Date::nowTime()),
            ],
            [
                'start' => Date::monthStart(Date::lastNumMonth(1)),
                'end' => Date::monthEnd(Date::lastNumMonth(1)),
            ],
            [
                'start' => Date::monthStart(Date::lastNumMonth(2)),
                'end' => Date::monthEnd(Date::lastNumMonth(2)),
            ],
            [
                'start' => Date::monthStart(Date::lastNumMonth(3)),
                'end' => Date::monthEnd(Date::lastNumMonth(3)),
            ],
            [
                'start' => Date::monthStart(Date::lastNumMonth(4)),
                'end' => Date::monthEnd(Date::lastNumMonth(4)),
            ],
        ];
        $result = [];
        foreach($monthAgo as $key => $value){
            $result[$key]['month'] = date('n月', Date::lastNumMonth($key));
            $result[$key]['num'] = $this->_monthUserRegNum($monthAgo[$key]['start'], $monthAgo[$key]['end']);
        }
        return array_reverse($result);
    }

    /**
     * 获取奖金统计数据
     * @return array
     */
    public function getBonusData(){
        $period = Period::instance();
        $calcMonth = $period->getNowYearMonth();
        $formatCalcMonth = substr($calcMonth, 0, 4).'-'.substr($calcMonth, 4, 2).'-01';
        $calcMonths = [
            [
                'month' => $calcMonth,
            ],
            [
                'month' => Date::lastNumMonth(1, strtotime($formatCalcMonth), 'Ym'),
            ],
            [
                'month' => Date::lastNumMonth(2, strtotime($formatCalcMonth), 'Ym'),
            ],
            [
                'month' => Date::lastNumMonth(3, strtotime($formatCalcMonth), 'Ym'),
            ],
            [
                'month' => Date::lastNumMonth(4, strtotime($formatCalcMonth), 'Ym'),
            ],
        ];
        foreach($calcMonths as $key => $value){
            $calcMonths[$key]['month'] = date('n月', Date::lastNumMonth($key));
            $calcMonths[$key]['num'] = $this->_monthBonus($value['month']);
        }
        return array_reverse($calcMonths);
    }

    /**
     * 获取提现量
     * @return array
     */
    public function getWithdrawData(){
        $monthAgo = [
            [
                'start' => Date::monthStart(Date::nowTime()),
                'end' => Date::monthEnd(Date::nowTime()),
            ],
            [
                'start' => Date::monthStart(Date::lastNumMonth(1)),
                'end' => Date::monthEnd(Date::lastNumMonth(1)),
            ],
            [
                'start' => Date::monthStart(Date::lastNumMonth(2)),
                'end' => Date::monthEnd(Date::lastNumMonth(2)),
            ],
            [
                'start' => Date::monthStart(Date::lastNumMonth(3)),
                'end' => Date::monthEnd(Date::lastNumMonth(3)),
            ],
            [
                'start' => Date::monthStart(Date::lastNumMonth(4)),
                'end' => Date::monthEnd(Date::lastNumMonth(4)),
            ],
        ];
        $result = [];
        foreach($monthAgo as $key => $value){
            $result[$key]['month'] = date('n月', Date::lastNumMonth($key));
            $result[$key]['num'] = $this->_monthWithdrawNum($monthAgo[$key]['start'], $monthAgo[$key]['end']);
        }
        return array_reverse($result);
    }

    public function userBonusData($userId){
        $period = Period::instance();
        $calcMonth = $period->getNowYearMonth();
        $formatCalcMonth = substr($calcMonth, 0, 4).'-'.substr($calcMonth, 4, 2).'-01';
        $calcMonths = [
            [
                'month' => $calcMonth,
            ],
            [
                'month' => Date::lastNumMonth(1, strtotime($formatCalcMonth), 'Ym'),
            ],
            [
                'month' => Date::lastNumMonth(2, strtotime($formatCalcMonth), 'Ym'),
            ],
            [
                'month' => Date::lastNumMonth(3, strtotime($formatCalcMonth), 'Ym'),
            ],
            [
                'month' => Date::lastNumMonth(4, strtotime($formatCalcMonth), 'Ym'),
            ],
        ];
        foreach($calcMonths as $key => $value){
            $calcMonths[$key] = array_merge($calcMonths[$key], $this->_userMonthBonus($value['month'], $userId));
        }
        return array_reverse($calcMonths);
    }

    /**
     * 某月会员注册量
     * @param $monthStart
     * @param $monthEnd
     * @return int|string
     */
    private function _monthUserRegNum($monthStart, $monthEnd){
        return UserInfo::findUseSlaves()->where('CREATED_AT>:START_TIME AND CREATED_AT<:END_TIME', [':START_TIME'=>$monthStart, ':END_TIME'=>$monthEnd])->count();
    }

    /**
     * 月奖金和
     * @param $calcMonth
     * @return int|mixed
     */
    private function _monthBonus($calcMonth){
        if($calcMonth){
            $bonus = CalcBonus::findUseSlaves()->yearMonth($calcMonth)->select('SUM(BONUS_QY) AS BONUS_QY_SUM, SUM(BONUS_YC) AS BONUS_YC_SUM, SUM(BONUS_FX) AS BONUS_FX_SUM, SUM(BONUS_LS) AS BONUS_LS_SUM, SUM(BONUS_CF) AS BONUS_CF_SUM, SUM(BONUS_LX) AS BONUS_LX_SUM, SUM(BONUS_FL) AS BONUS_FL_SUM, SUM(BONUS_BT) AS BONUS_BT_SUM, SUM(BONUS_FW) AS BONUS_FW_SUM')->where('CALC_MONTH=:CALC_MONTH AND IS_SENT=1', [':CALC_MONTH'=>$calcMonth])->asArray()->one();
            if($bonus){
                return $bonus['BONUS_QY_SUM'] + $bonus['BONUS_YC_SUM'] + $bonus['BONUS_FX_SUM'] + $bonus['BONUS_LS_SUM'] + $bonus['BONUS_CF_SUM'] + $bonus['BONUS_LX_SUM'] + $bonus['BONUS_FL_SUM'] + $bonus['BONUS_BT_SUM'] + $bonus['BONUS_FW_SUM'];
            }
        }
        return 0;
    }

    /**
     * 月提现
     * @param $monthStart
     * @param $monthEnd
     * @return string
     */
    private function _monthWithdrawNum($monthStart, $monthEnd){
        return Tool::formatPrice(Withdraw::findUseSlaves()->where('CREATED_AT>:START_TIME AND CREATED_AT<:END_TIME AND AUDIT_STATUS=:AUDIT_STATUS', [':START_TIME'=>$monthStart, ':END_TIME'=>$monthEnd, ':AUDIT_STATUS'=>Withdraw::STATUS_PAID])->sum('AMOUNT'));
    }

    private function _userMonthBonus($calcMonth, $userId){
        $bonus = CalcBonus::findUseSlaves()->yearMonth($calcMonth)->select('BONUS_QY, BONUS_YC, BONUS_FX, BONUS_LS, BONUS_CF, BONUS_LX, BONUS_FL, BONUS_BT, BONUS_FW')->where('CALC_MONTH=:CALC_MONTH AND IS_SENT=1 AND USER_ID=:USER_ID', [':CALC_MONTH'=>$calcMonth, ':USER_ID'=>$userId])->asArray()->one();
        if($bonus){
            return [
                'BONUS_QY' => $bonus['BONUS_QY'],
                'BONUS_YC' => $bonus['BONUS_YC'],
                'BONUS_FX' => $bonus['BONUS_FX'],
                'BONUS_LS' => $bonus['BONUS_LS'],
                'BONUS_CF' => $bonus['BONUS_CF'],
                'BONUS_LX' => $bonus['BONUS_LX'],
                'BONUS_FL' => $bonus['BONUS_FL'],
                'BONUS_BT' => $bonus['BONUS_BT'],
                'BONUS_FW' => $bonus['BONUS_FW'],
            ];
        } else {
            return [
                'BONUS_QY' => 0,
                'BONUS_YC' => 0,
                'BONUS_FX' => 0,
                'BONUS_LS' => 0,
                'BONUS_CF' => 0,
                'BONUS_LX' => 0,
                'BONUS_FL' => 0,
                'BONUS_BT' => 0,
                'BONUS_FW' => 0,
            ];
        }
    }


}