<?php
namespace backendApi\modules\v1\models\lists\bonus;

use backendApi\modules\v1\models\Admin;
use common\helpers\Cache;
use common\helpers\Tool;
use common\helpers\user\Info;
use common\libs\dataList\column\Price;
use common\libs\dataList\column\YesNo;
use common\libs\dataList\DataListInterface;
use common\models\CalcBonus;
use common\models\PerfOrder;
use common\models\Period;
use common\models\Region;
use common\models\User as modelUser;
use common\models\UserBind;
use common\models\UserBonus;
use common\models\UserInfo;
use common\libs\dataList\column\DateTime;
use common\models\UserPerf;
use common\models\UserSystem;
use common\models\Withdraw;
use Yii;

class OtherPeriodBonusList extends \common\libs\dataList\DataList implements DataListInterface
{
    /**
     * 列表名称
     * @return string
     */
    public function getListName(){
        return '往期奖金列表';
    }

    /**
     * 列表筛选到的数据
     * @throws \yii\base\Exception
     */
    public function dataHandle()
    {
        $yearMonth = $this->others['yearMonth'];
        $this->listData = CalcBonus::lists($this->condition, $this->params, [
            //'select'=>'CB.*, UP.SURPLUS_1L AS UP_SURPLUS_1L, UP.SURPLUS_2L AS UP_SURPLUS_2L, UP.SURPLUS_3L AS UP_SURPLUS_3L,UI.CON_UID,UI.REC_UID',
            'select'=>'CB.*,UI.CON_UID,UI.REC_UID',
            'from' => CalcBonus::tableName().' AS CB',
            'join' => [
                //['LEFT JOIN', UserPerf::tableName() . ' AS UP', 'CB.USER_ID=UP.USER_ID'],
                ['LEFT JOIN', UserInfo::tableName() . ' AS UI', 'CB.USER_ID=UI.USER_ID'],
            ],
            'yearMonth' => $yearMonth,
            'orderBy' => 'CB.CREATED_AT DESC, CB.ID DESC',
            'page' => $this->page,
            'pageSize' => $this->pageSize,
        ]);
        unset($yearMonth);
        if ($this->listData['list']) {
            foreach ($this->listData['list'] as $key => $value) {
                $this->listData['list'][$key]['LAST_DEC_LV_NAME'] = Cache::getDecLevelConfig()[$value['LAST_DEC_LV']]['LEVEL_NAME'];
                $this->listData['list'][$key]['LAST_EMP_LV_NAME'] = Cache::getEmpLevelConfig()[$value['LAST_EMP_LV']]['LEVEL_NAME'];
                $this->listData['list'][$key]['LAST_STATUS_NAME'] = Tool::paramConvert(Yii::$app->params['userStatus'])[$value['LAST_STATUS']]['label'];

                $this->listData['list'][$key]['LAST_REC_USER_NAME'] = Info::getUserNameByUserId($value['REC_UID']);
                $this->listData['list'][$key]['LAST_REC_REAL_NAME'] = Info::getUserRealNameByUserId($value['REC_UID']);
                $this->listData['list'][$key]['LAST_CON_USER_NAME'] = Info::getUserNameByUserId($value['CON_UID']);
                $this->listData['list'][$key]['LAST_CON_REAL_NAME'] = Info::getUserRealNameByUserId($value['CON_UID']);
            }
        }
    }

    /**
     * 要展示和导出的所有字段
     * @return array
     */
    public function getColumn(){
        if(!$this->columns){
            $this->columns = [
                'ID' => null,
                'PERIOD_NUM' => [
                    'header' => '结算期数',
                    'headerOther' => [
                        'width' => '150',
                    ],
                    'valueOther' => [
                        'tag'=>['type'=>'', 'size' => 'small', 'class'=>'no-border'],
                    ],
                ],
                'CALCULATED_AT' => [
                    'header' => '结算时间',
                    'value' => function($row) {
                        return (new DateTime([
                            'value' => $row['CALCULATED_AT'],
                        ]))->result();
                    },
                    'headerOther' => ['width' => '170'],
                ],
                'LAST_USER_NAME' => [
                    'header' => '会员编号',
                    'headerOther' => [
                        'width' => '150',
                    ],
                    'valueOther' => [
                        'tag'=>['type'=>'info', 'size' => 'small', 'class'=>'no-border']
                    ],
                ],
                'LAST_REAL_NAME' => [
                    'header' => '会员姓名',
                    'headerOther' => [
                        'width' => '120',
                    ],
                    'valueOther' => [
                        'tag'=>['type'=>'success', 'size' => 'small', 'class'=>'no-border']
                    ],
                ],
                'LAST_DEC_LV_NAME' => [
                    'header' => '结算时会员级别',
                    'headerOther' => [
                        'width' => '120',
                    ],
                ],
                'LAST_EMP_LV_NAME' => [
                    'header' => '结算时会员聘级',
                    'headerOther' => [
                        'width' => '140',
                    ],
                ],
                'LAST_MOBILE' => [
                    'header' => '手机号码',
                    'headerOther' => [
                        'width' => '120',
                    ],
                ],
                'LAST_PERIOD_AT' => [
                    'header' => '加入期数',
                    'headerOther' => [
                        'width' => '100',
                    ],
                ],
                'LAST_CREATED_AT' => [
                    'header' => '加入时间',
                    'value' => function($row) {
                        return (new DateTime([
                            'value' => $row['LAST_CREATED_AT'],
                        ]))->result();
                    },
                    'headerOther' => ['width' => '170'],
                ],
                'LAST_REC_USER_NAME' => [
                    'header' => '开拓者编号',
                    'headerOther' => [
                        'width' => '150',
                    ],
                ],
                'LAST_REC_REAL_NAME' => [
                    'header' => '开拓者姓名',
                    'headerOther' => [
                        'width' => '100',
                    ],
                ],
                'LAST_CON_USER_NAME' => [
                    'header' => '上级编号',
                    'headerOther' => [
                        'width' => '150',
                    ],
                ],
                'LAST_CON_REAL_NAME' => [
                    'header' => '上级姓名',
                    'headerOther' => [
                        'width' => '100',
                    ],
                ],
                'PV_1L' => [
                    'header' => '一市场新增业绩',
                    'value' => function($row) {
                        return (new Price([
                            'value' => $row['PV_1L'],
                        ]))->result();
                    },
                    'headerOther' => [
                        'width' => '120',
                        'prop'=>'PV_1L',
                    ],
                ],
                'SURPLUS_1L' => [
                    'header' => '一市场结余业绩',
                    'value' => function($row) {
                        return (new Price([
                            'value' => $row['SURPLUS_1L'],
                        ]))->result();
                    },
                    'headerOther' => [
                        'width' => '120',
                        'prop'=>'SURPLUS_1L',
                    ],
                ],
                'PV_2L' => [
                    'header' => '二市场新增业绩',
                    'value' => function($row) {
                        return (new Price([
                            'value' => $row['PV_2L'],
                        ]))->result();
                    },
                    'headerOther' => [
                        'width' => '120',
                        'prop'=>'PV_2L',
                    ],
                ],
                'SURPLUS_2L' => [
                    'header' => '二市场结余业绩',
                    'value' => function($row) {
                        return (new Price([
                            'value' => $row['SURPLUS_2L'],
                        ]))->result();
                    },
                    'headerOther' => [
                        'width' => '120',
                        'prop'=>'SURPLUS_2L',
                    ],
                ],
//                'UP_SURPLUS_2L' => [
//                    'header' => '当前二市场结余业绩',
//                    'value' => function($row) {
//                        return (new Price([
//                            'value' => $row['UP_SURPLUS_2L'],
//                        ]))->result();
//                    },
//                    'headerOther' => [
//                        'width' => '120',
//                        'prop'=>'UP_SURPLUS_2L',
//                    ],
//                ],
                'PV_3L' => [
                    'header' => '三市场新增业绩',
                    'value' => function($row) {
                        return (new Price([
                            'value' => $row['PV_3L'],
                        ]))->result();
                    },
                    'headerOther' => [
                        'width' => '120',
                        'prop'=>'PV_3L',
                    ],
                ],
                'SURPLUS_3L' => [
                    'header' => '三市场结余业绩',
                    'value' => function($row) {
                        return (new Price([
                            'value' => $row['SURPLUS_3L'],
                        ]))->result();
                    },
                    'headerOther' => [
                        'width' => '120',
                        'prop'=>'SURPLUS_3L',
                    ],
                ],
//                'UP_SURPLUS_3L' => [
//                    'header' => '当前三市场结余业绩',
//                    'value' => function($row) {
//                        return (new Price([
//                            'value' => $row['UP_SURPLUS_3L'],
//                        ]))->result();
//                    },
//                    'headerOther' => [
//                        'width' => '120',
//                        'prop'=>'UP_SURPLUS_3L',
//                    ],
//                ],
                'BONUS_BD' => [
                    'header' => '服务奖',
                    'value' => function($row) {
                        return (new Price([
                            'value' => $row['BONUS_BD'],
                        ]))->result();
                    },
                    'headerOther' => [
                        'width' => '120',
                        'prop'=>'BONUS_BD',
                    ],
                ],
                'ORI_BONUS_BD' => [
                    'header' => '服务奖原金额',
                    'value' => function($row) {
                        return (new Price([
                            'value' => $row['ORI_BONUS_BD'],
                        ]))->result();
                    },
                    'headerOther' => [
                        'width' => '120',
                        'prop'=>'ORI_BONUS_BD',
                    ],
                ],
                'BONUS_TG' => [
                    'header' => '推广奖',
                    'value' => function($row) {
                        return (new Price([
                            'value' => $row['BONUS_TG'],
                        ]))->result();
                    },
                    'headerOther' => [
                        'width' => '120',
                        'prop'=>'BONUS_TG',
                    ],
                ],
                'ORI_BONUS_TG' => [
                    'header' => '推广奖原金额',
                    'value' => function($row) {
                        return (new Price([
                            'value' => $row['ORI_BONUS_TG'],
                        ]))->result();
                    },
                    'headerOther' => [
                        'width' => '120',
                        'prop'=>'ORI_BONUS_TG',
                    ],
                ],
                'BONUS_XF' => [
                    'header' => '消费奖',
                    'value' => function($row) {
                        return (new Price([
                            'value' => $row['BONUS_XF'],
                        ]))->result();
                    },
                    'headerOther' => [
                        'width' => '120',
                        'prop'=>'BONUS_XF',
                    ],
                ],
                'ORI_BONUS_XF' => [
                    'header' => '消费奖原金额',
                    'value' => function($row) {
                        return (new Price([
                            'value' => $row['ORI_BONUS_XF'],
                        ]))->result();
                    },
                    'headerOther' => [
                        'width' => '120',
                        'prop'=>'ORI_BONUS_XF',
                    ],
                ],
                'BONUS_YJ' => [
                    'header' => '业绩奖',
                    'value' => function($row) {
                        return (new Price([
                            'value' => $row['BONUS_YJ'],
                        ]))->result();
                    },
                    'headerOther' => [
                        'width' => '120',
                        'prop'=>'BONUS_YJ',
                    ],
                ],
                'ORI_BONUS_YJ' => [
                    'header' => '业绩奖原金额',
                    'value' => function($row) {
                        return (new Price([
                            'value' => $row['ORI_BONUS_YJ'],
                        ]))->result();
                    },
                    'headerOther' => [
                        'width' => '120',
                        'prop'=>'ORI_BONUS_YJ',
                    ],
                ],
                'BONUS_GX' => [
                    'header' => '共享奖',
                    'value' => function($row) {
                        return (new Price([
                            'value' => $row['BONUS_GX'],
                        ]))->result();
                    },
                    'headerOther' => [
                        'width' => '120',
                        'prop'=>'BONUS_GX',
                    ],
                ],
                'ORI_BONUS_GX' => [
                    'header' => '共享奖原金额',
                    'value' => function($row) {
                        return (new Price([
                            'value' => $row['ORI_BONUS_GX'],
                        ]))->result();
                    },
                    'headerOther' => [
                        'width' => '120',
                        'prop'=>'ORI_BONUS_GX',
                    ],
                ],
                'BONUS_GL' => [
                    'header' => '管理奖',
                    'value' => function($row) {
                        return (new Price([
                            'value' => $row['BONUS_GL'],
                        ]))->result();
                    },
                    'headerOther' => [
                        'width' => '120',
                        'prop'=>'BONUS_GL',
                    ],
                ],
                'ORI_BONUS_GL' => [
                    'header' => '管理奖原金额',
                    'value' => function($row) {
                        return (new Price([
                            'value' => $row['ORI_BONUS_GL'],
                        ]))->result();
                    },
                    'headerOther' => [
                        'width' => '120',
                        'prop'=>'ORI_BONUS_GL',
                    ],
                ],
                'BONUS_QY' => [
                    'header' => '团队奖',
                    'value' => function($row) {
                        return (new Price([
                            'value' => $row['BONUS_QY'],
                        ]))->result();
                    },
                    'headerOther' => [
                        'width' => '120',
                        'prop'=>'BONUS_QY',
                    ],
                ],
                'ORI_BONUS_QY' => [
                    'header' => '团队奖原金额',
                    'value' => function($row) {
                        return (new Price([
                            'value' => $row['ORI_BONUS_QY'],
                        ]))->result();
                    },
                    'headerOther' => [
                        'width' => '120',
                        'prop'=>'ORI_BONUS_QY',
                    ],
                ],
                'ORI_BONUS_QY_BD' => [
                    'header' => '报单团队奖原金额',
                    'value' => function($row) {
                        return (new Price([
                            'value' => $row['ORI_BONUS_QY_BD'],
                        ]))->result();
                    },
                    'headerOther' => [
                        'width' => '120',
                        'prop'=>'ORI_BONUS_QY_BD',
                    ],
                ],
                'BONUS_YC' => [
                    'header' => '荣衔奖',
                    'value' => function($row) {
                        return (new Price([
                            'value' => $row['BONUS_YC'],
                        ]))->result();
                    },
                    'headerOther' => [
                        'width' => '120',
                        'prop'=>'BONUS_YC',
                    ],
                ],
                'ORI_BONUS_YC' => [
                    'header' => '荣衔奖原金额',
                    'value' => function($row) {
                        return (new Price([
                            'value' => $row['ORI_BONUS_YC'],
                        ]))->result();
                    },
                    'headerOther' => [
                        'width' => '120',
                        'prop'=>'ORI_BONUS_YC',
                    ],
                ],
                'BONUS_TOTAL' => [
                    'header' => '总奖金',
                    'value' => function($row) {
                        return (new Price([
                            'value' => $row['BONUS_TOTAL'],
                        ]))->result();
                    },
                    'headerOther' => [
                        'width' => '120',
                        'prop'=>'BONUS_TOTAL',
                    ],
                ],
                'BONUS_INCOME' => [
                    'header' => '总收入',
                    'value' => function($row) {
                        return (new Price([
                            'value' => $row['BONUS_INCOME'],
                        ]))->result();
                    },
                    'headerOther' => [
                        'width' => '120',
                        'prop'=>'BONUS_INCOME',
                    ],
                ],
                'ORI_BONUS_STANDARD' => [
                    'header' => '团队成长奖',
                    'value' => function($row) {
                        return (new Price([
                            'value' => $row['ORI_BONUS_STANDARD'],
                        ]))->result();
                    },
                    'headerOther' => [
                        'width' => '120',
                        'prop'=>'ORI_BONUS_STANDARD',
                    ],
                ],
                'CALC_MONTH' => [
                    'header' => '结算月',
                    'headerOther' => [
                        'width' => '100',
                    ],
                ],
            ];
        }
        return $this->columns;
    }

    /**
     * 前台用于筛选的类型集合
     * @return mixed
     */
    public function getFilterTypes()
    {
        if(!$this->filterTypes){
            $this->filterTypes = [
                'LAST_USER_NAME'=> ['isUserTable'=> false, 'name'=> '会员编号'],
                'LAST_REAL_NAME'=> ['isUserTable'=> false, 'name'=> '会员姓名'],
                'LAST_DEC_LV_NAME'=> ['isUserTable'=> false, 'name'=> '结算时会员级别', 'other'=> 'decLevel'],
                'LAST_EMP_LV_NAME'=> ['isUserTable'=> false, 'name'=> '结算时会员聘级', 'other'=> 'empLevel'],
                'LAST_MOBILE'=> ['isUserTable'=> false, 'name'=> '手机号'],
                'LAST_PERIOD_AT'=> ['isUserTable'=> false, 'name'=> '加入期数'],
                'LAST_CREATED_AT'=> ['isUserTable'=> false, 'name'=> '加入时间', 'other'=> 'date'],
                'LAST_REC_USER_NAME'=> ['isUserTable'=> false, 'name'=> '开拓者编号'],
                'LAST_REC_REAL_NAME'=> ['isUserTable'=> false, 'name'=> '开拓者姓名'],
                'LAST_CON_USER_NAME'=> ['isUserTable'=> false, 'name'=> '上级编号'],
                'LAST_CON_REAL_NAME'=> ['isUserTable'=> false, 'name'=> '上级姓名'],
                'PV_1L'=> ['isUserTable'=> false, 'name'=> '一市场新增业绩'],
                'SURPLUS_1L'=> ['isUserTable'=> false, 'name'=> '一市场结余业绩'],
                'PV_2L'=> ['isUserTable'=> false, 'name'=> '二市场新增业绩'],
                'SURPLUS_2L'=> ['isUserTable'=> false, 'name'=> '二市场结余业绩'],
                'PV_3L'=> ['isUserTable'=> false, 'name'=> '三市场新增业绩'],
                'SURPLUS_3L'=> ['isUserTable'=> false, 'name'=> '三市场结余业绩'],
                'BONUS_BD'=> ['isUserTable'=> false, 'name'=> '服务奖'],
                'BONUS_TG'=> ['isUserTable'=> false, 'name'=> '推广奖'],
                'BONUS_XF'=> ['isUserTable'=> false, 'name'=> '消费奖'],
                'BONUS_YJ'=> ['isUserTable'=> false, 'name'=> '业绩奖'],
                'BONUS_GX'=> ['isUserTable'=> false, 'name'=> '共享奖'],
                'BONUS_GL'=> ['isUserTable'=> false, 'name'=> '管理奖'],
                'BONUS_QY'=> ['isUserTable'=> false, 'name'=> '团队奖'],
                'BONUS_YC'=> ['isUserTable'=> false, 'name'=> '荣衔奖'],
                'ORI_BONUS_STANDARD'=> ['isUserTable'=> false, 'name'=> '团队成长奖'],
                'CALC_MONTH'=> ['isUserTable'=> false, 'name'=> '结算月'],
            ];
        }
        return $this->filterTypes;
    }
}