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

namespace common\helpers;

use common\models\CalcBonus;
use common\models\Period;
use yii\base\BaseObject;
use yii\base\StaticInstanceTrait;

class ChartData extends BaseObject
{
    use StaticInstanceTrait;

    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);
    }

    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,
            ];
        }
    }
}