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

namespace backendApi\modules\v1\controllers;

use backendApi\modules\v1\models\AdminCountry;
use backendApi\modules\v1\models\AdminRole;
use backendApi\modules\v1\models\exportForms\BonusExportForm;
use backendApi\modules\v1\models\lists\bonus\BalanceList;
use backendApi\modules\v1\models\lists\bonus\FlowBalanceList;
use backendApi\modules\v1\models\lists\bonus\FlowBonusList;
use backendApi\modules\v1\models\lists\bonus\FlowExchangePointsList;
use backendApi\modules\v1\models\lists\bonus\FlowReconsumePointsList;
use backendApi\modules\v1\models\lists\bonus\FlowWalletList;
use backendApi\modules\v1\models\lists\bonus\PerfMonthList;
use backendApi\modules\v1\models\lists\bonus\PerfOrderList;
use backendApi\modules\v1\models\lists\bonus\PerfPeriodList;
use backendApi\modules\v1\models\lists\bonus\PerfStandardList;
use backendApi\modules\v1\models\lists\bonus\PeriodBonusList;
use backendApi\modules\v1\models\lists\bonus\ScoreMonthList;
use backendApi\modules\v1\models\lists\bonus\TraceDownBdList;
use backendApi\modules\v1\models\lists\bonus\TraceDownGlList;
use backendApi\modules\v1\models\lists\bonus\TraceDownGxList;
use backendApi\modules\v1\models\lists\bonus\TraceDownQyList;
use backendApi\modules\v1\models\lists\bonus\TraceDownTgList;
use backendApi\modules\v1\models\lists\bonus\TraceDownXfList;
use backendApi\modules\v1\models\lists\bonus\TraceDownYcList;
use backendApi\modules\v1\models\lists\bonus\TraceDownYjList;
use backendApi\modules\v1\models\lists\bonus\TraceUpBdList;
use backendApi\modules\v1\models\lists\bonus\TraceUpGlList;
use backendApi\modules\v1\models\lists\bonus\TraceUpGxList;
use backendApi\modules\v1\models\lists\bonus\TraceUpQyList;
use backendApi\modules\v1\models\lists\bonus\TraceUpTgList;
use backendApi\modules\v1\models\lists\bonus\TraceUpXfList;
use backendApi\modules\v1\models\lists\bonus\TraceUpYcList;
use backendApi\modules\v1\models\lists\bonus\TraceUpYjList;
use backendApi\modules\v1\models\lists\bonus\UserPerfList;
use common\helpers\Cache;
use common\helpers\Tool;
use common\helpers\user\Info;
use common\helpers\user\Perf;
use common\models\CalcBonus;
use common\models\CFLXAudit;
use common\models\forms\PerfAdjustmentForm;
use common\models\forms\ResendQYForm;
use common\models\forms\SendCFAndLXForm;
use common\models\PerfMonth;
use common\models\PerfOrder;
use common\models\PerfPeriod;
use common\models\ResendQYAudit;
use common\models\User;
use common\models\UserInfo;
use common\models\UserNetwork;
use common\models\UserPerf;
use Yii;
use common\helpers\Form;
use common\models\FlowBonus;
use common\models\forms\PeriodForm;
use common\models\forms\UserBonusForm;
use common\models\Period;
use yii\db\Exception;
use yii\web\HttpException;

class BonusController extends BaseController {
    public $modelClass = FlowBonus::class;

    public function behaviors() {
        return parent::behaviors();
    }

    /**
     * 期数列表
     * @return mixed
     * @throws \yii\base\InvalidConfigException
     * @throws \yii\httpclient\Exception
     * @throws HttpException
     * @throws Exception
     */
    public function actionPeriod() {
        $filter = $this->filterCondition([
            'periodNum' => 'PERIOD_NUM',
            'year' => 'CALC_YEAR',
            'month' => 'CALC_MONTH',
        ]);
        $condition = $filter['condition'];
        $params = $filter['params'];
        $periodObj = Period::instance();
        $nowPeriodNum = $periodObj->getNowPeriodNum();
//        $condition .= ' AND PERIOD_NUM<=:PERIOD_NUM AND STATUS=1';
        $condition .= ' AND STATUS=1';
//        $params[':PERIOD_NUM'] = $nowPeriodNum;
        $data = Period::lists($condition, $params, ['orderBy' => 'PERIOD_NUM ASC']);
        foreach ($data['list'] as $key => $value) {
            // 结算月
            $data['list'][$key]['CALC_YEAR_MONTH'] = $periodObj->getYearMonth($value['PERIOD_NUM']);
            // 结算
            $data['list'][$key]['IS_CAN_CALC'] = $periodObj->isCanCalc($value['PERIOD_NUM']);
            // 挂网
            $data['list'][$key]['IS_CAN_SENT'] = $periodObj->isCanSend($value['PERIOD_NUM']);
            // 日志
            $data['list'][$key]['IS_SHOW_LOG'] = $value['CALCULATED_AT'] > 0;
        }
        return static::notice($data);
    }

    /**
     * 结算
     * @return mixed
     * @throws HttpException
     */
    public function actionCalcPeriod() {
        $periodNum = \Yii::$app->request->get('periodNum');
        if (!$periodNum) {
            return static::notice('期数不存在', 400);
        }
        $formModel = new PeriodForm();
        $formModel->scenario = 'calc';
        if ($formModel->load(Yii::$app->request->get(), '') && $formModel->calcWebToAsync()) {
            return static::notice('结算已开始处理，请等待');
        } else {
            return static::notice(Form::formatErrorsForApi($formModel->getErrors()), 400);
        }
    }

    /**
     * 挂网
     * @return mixed
     * @throws HttpException
     */
    public function actionSendPeriod() {
        $periodNum = \Yii::$app->request->get('periodNum');
        if (!$periodNum) {
            return static::notice('期数不存在', 400);
        }
        $formModel = new PeriodForm();
        $formModel->scenario = 'send';
        if ($formModel->load(Yii::$app->request->get(), '') && $formModel->sendWebToAsync()) {
            return static::notice('挂网已开始处理，请等待');
        } else {
            return static::notice(Form::formatErrorsForApi($formModel->getErrors()), 400);
        }
    }

    /**
     * 查看业绩
     * @return mixed
     * @throws HttpException
     */
    public function actionPerf() {
        // 获取当前年月
        $period = Period::instance();
        $yearMonth = $period->getNowYearMonth();
        $yearMonth = Yii::$app->request->get('yearMonth', $yearMonth);
        //$filter = $this->filterCondition(Perf::tableName(), ['periodNum'=>'P.PERIOD_NUM', 'userName'=>'UI.USER_NAME']);
        $condition = '';
        $params = [];
        $data = PerfPeriod::lists($condition, $params, [
            'select' => 'P.*, UI.USER_NAME',
            'yearMonth' => $yearMonth,
            'from' => PerfPeriod::tableName() . ' AS P',
            'join' => [
                ['LEFT JOIN', UserInfo::tableName() . ' AS UI', 'P.USER_ID=UI.USER_ID']
            ],
            'orderBy' => 'P.PERIOD_NUM DESC',
        ]);
        $data['request'] = [];
        $data['request']['yearMonth'] = $yearMonth;
        return static::notice($data);
    }

    /**
     * 奖金流水
     * @return mixed
     * @throws \yii\base\Exception
     * @throws HttpException
     */
    public function actionFlowBonus() {
        $filter = $this->filterCondition([
            'USER_NAME' => 'U.USER_NAME',
            'REAL_NAME' => 'U.REAL_NAME',
            'IS_DEC' => 'U.IS_DEC',
            'userIds' => 'USER_ID',
            'CREATED_AT' => 'FB.CREATED_AT',
            'PERIOD_NUM' => 'PERIOD_NUM',
            'CALC_MONTH' => 'CALC_MONTH',
            'DEAL_TYPE_NAME' => 'DEAL_TYPE_ID',
            'LAST_DEC_LV_NAME' => 'LAST_DEC_LV',
            'LAST_EMP_LV_NAME' => 'LAST_EMP_LV',
            'AMOUNT' => 'AMOUNT',
            'TOTAL' => 'TOTAL',
            'ADMIN_NAME' => 'ADMIN_NAME',
            'REMARK' => 'REMARK',
            'TRANSFER_SN' => 'TRANSFER_SN',
        ]);
        $condition = $filter['condition'];
        $params = $filter['params'];

        $isSuper = AdminRole::isSuperAdmin(\Yii::$app->getUser()->getUserInfo()['roleId']);
        if (!$isSuper) {
            $adminId = Yii::$app->getUser()->getUserInfo()['id'];
            $adminCountry = AdminCountry::getCountry($adminId);
            $quotedAdminCountry = array_map(function($item) {
                return "'" . addslashes($item) . "'";
            }, $adminCountry);

            $condition .= " AND U.COUNTRY_ID IN (" . implode(',', $quotedAdminCountry) . ")";
        }

        $listObj = new FlowBonusList();
        $data = $listObj->getList(['condition'=>$condition, 'params'=>$params]);
        return static::notice($data);
    }

    /**
     * 奖金流水
     * @return mixed
     * @throws \yii\db\Exception
     * @throws HttpException
     */
    public function actionFlowBonusExport() {
        $filter = $this->filterCondition([
            'USER_NAME' => 'U.USER_NAME',
            'REAL_NAME' => 'U.REAL_NAME',
            'IS_DEC' => 'U.IS_DEC',
            'userIds' => 'USER_ID',
            'CREATED_AT' => 'FB.CREATED_AT',
            'PERIOD_NUM' => 'PERIOD_NUM',
            'CALC_MONTH' => 'CALC_MONTH',
            'DEAL_TYPE_NAME' => 'DEAL_TYPE_ID',
            'LAST_DEC_LV_NAME' => 'LAST_DEC_LV',
            'LAST_EMP_LV_NAME' => 'LAST_EMP_LV',
            'LAST_STATUS_NAME' => 'LAST_STATUS',
            'AMOUNT' => 'AMOUNT',
            'TOTAL' => 'TOTAL',
            'ADMIN_NAME' => 'ADMIN_NAME',
            'REMARK' => 'REMARK',
            'TRANSFER_SN' => 'TRANSFER_SN',
        ]);

        $condition = $filter['condition'];
        $params = $filter['params'];

        $isSuper = AdminRole::isSuperAdmin(\Yii::$app->getUser()->getUserInfo()['roleId']);
        if (!$isSuper) {
            $adminId = Yii::$app->getUser()->getUserInfo()['id'];
            $adminCountry = AdminCountry::getCountry($adminId);
            $quotedAdminCountry = array_map(function($item) {
                return "'" . addslashes($item) . "'";
            }, $adminCountry);

            $condition .= " AND U.COUNTRY_ID IN (" . implode(',', $quotedAdminCountry) . ")";
        }

        $form = new BonusExportForm();
        $result = $form->run(['condition'=>$condition, 'params'=>$params], 'Bonus_Flow'); // 奖金流水
        if (!$result) {
            return static::notice(Form::formatErrorsForApi($form->getErrors()), 400);
        }
        return static::notice(Yii::t('ctx', 'startExporting')); // 导出开始，请到文件管理-导出文件查看
    }

    /**
     * 复消积分流水
     * @return mixed
     * @throws \yii\base\Exception
     * @throws HttpException
     */
    public function actionFlowRp() {
        $filter = $this->filterCondition([
            'userIds' => 'USER_ID',
            'CREATED_AT' => 'CREATED_AT',
            'PERIOD_NUM' => 'PERIOD_NUM',
            'CALC_MONTH' => 'CALC_MONTH',
            'DEAL_TYPE_NAME' => 'DEAL_TYPE_ID',
            'LAST_DEC_LV_NAME' => 'LAST_DEC_LV',
            'LAST_EMP_LV_NAME' => 'LAST_EMP_LV',
            'AMOUNT' => 'AMOUNT',
            'TOTAL' => 'TOTAL',
            'ADMIN_NAME' => 'ADMIN_NAME',
            'REMARK' => 'REMARK',
        ]);
        $condition = $filter['condition'];
        $params = $filter['params'];
        $listObj = new FlowReconsumePointsList();
        $data = $listObj->getList(['condition'=>$condition, 'params'=>$params]);
        return static::notice($data);
    }

    /**
     * 复消积分流水导出
     * @return mixed
     * @throws \yii\db\Exception
     * @throws HttpException
     */
    public function actionFlowRpExport() {
        $filter = $this->filterCondition([
            'userIds' => 'USER_ID',
            'CREATED_AT' => 'CREATED_AT',
            'PERIOD_NUM' => 'PERIOD_NUM',
            'CALC_MONTH' => 'CALC_MONTH',
            'DEAL_TYPE_NAME' => 'DEAL_TYPE_ID',
            'LAST_DEC_LV_NAME' => 'LAST_DEC_LV',
            'LAST_EMP_LV_NAME' => 'LAST_EMP_LV',
            'AMOUNT' => 'AMOUNT',
            'TOTAL' => 'TOTAL',
            'ADMIN_NAME' => 'ADMIN_NAME',
            'REMARK' => 'REMARK',
        ]);
        $form = new BonusExportForm();
        $result = $form->run($filter, '复消积分流水');
        if (!$result) {
            return static::notice(Form::formatErrorsForApi($form->getErrors()), 400);
        }
        return static::notice(Yii::t('ctx', 'startExporting')); // 导出开始，请到文件管理-导出文件查看
    }


    /**
     * 会员余额流水
     * @return mixed
     * @throws \yii\base\Exception
     * @throws HttpException
     */
    public function actionFlowBalance() {
        $filter = $this->filterCondition([
            'USER_NAME' => 'USER_NAME',
            'REAL_NAME' => 'REAL_NAME',
            'CALC_MONTH' => 'CALC_MONTH',
            'PERIOD_NUM' => 'PERIOD_NUM',
            'TRANSFER_SN' => 'TRANSFER_SN',
            'CREATED_AT' => 'FW.CREATED_AT',
            'AMOUNT' => 'AMOUNT',
            'TOTAL' => 'TOTAL',
            'REMARK' => 'REMARK',
        ]);
        $condition = $filter['condition'];
        $params = $filter['params'];



        $listObj = new FlowBalanceList();
        $data = $listObj->getList(['condition'=>$condition, 'params'=>$params]);
        return static::notice($data);
    }

    /**
     * 会员余额流水导出
     * @return mixed
     * @throws \yii\db\Exception
     * @throws HttpException
     */
    public function actionFlowBalanceExport() {
        $filter = $this->filterCondition([
            'USER_NAME' => 'USER_NAME',
            'REAL_NAME' => 'REAL_NAME',
            'CALC_MONTH' => 'CALC_MONTH',
            'PERIOD_NUM' => 'PERIOD_NUM',
            'TRANSFER_SN' => 'TRANSFER_SN',
            'CREATED_AT' => 'FW.CREATED_AT',
            'AMOUNT' => 'AMOUNT',
            'TOTAL' => 'TOTAL',
            'REMARK' => 'REMARK',
        ]);
        $form = new BonusExportForm();
        $result = $form->run($filter, '会员余额流水');
        if (!$result) {
            return static::notice(Form::formatErrorsForApi($form->getErrors()), 400);
        }
        return static::notice(Yii::t('ctx', 'startExporting')); // 导出开始，请到文件管理-导出文件查看
    }


    /**
     * 兑换积分流水
     * @return mixed
     * @throws \yii\base\Exception
     * @throws HttpException
     */
    public function actionFlowExchange() {
        $filter = $this->filterCondition([
            'userIds' => 'USER_ID',
            'CREATED_AT' => 'CREATED_AT',
            'PERIOD_NUM' => 'PERIOD_NUM',
            'CALC_MONTH' => 'CALC_MONTH',
            'DEAL_TYPE_NAME' => 'DEAL_TYPE_ID',
            'LAST_DEC_LV_NAME' => 'LAST_DEC_LV',
            'LAST_EMP_LV_NAME' => 'LAST_EMP_LV',
            'AMOUNT' => 'AMOUNT',
            'TOTAL' => 'TOTAL',
            'ADMIN_NAME' => 'ADMIN_NAME',
            'REMARK' => 'REMARK',
        ]);
        $condition = $filter['condition'];
        $params = $filter['params'];
        $listObj = new FlowExchangePointsList();
        $data = $listObj->getList(['condition'=>$condition, 'params'=>$params]);
        return static::notice($data);
    }

    /**
     * 兑换积分流水导出
     * @return mixed
     * @throws \yii\db\Exception
     * @throws HttpException
     */
    public function actionFlowExchangeExport() {
        $filter = $this->filterCondition([
            'userIds' => 'USER_ID',
            'CREATED_AT' => 'CREATED_AT',
            'PERIOD_NUM' => 'PERIOD_NUM',
            'CALC_MONTH' => 'CALC_MONTH',
            'DEAL_TYPE_NAME' => 'DEAL_TYPE_ID',
            'LAST_DEC_LV_NAME' => 'LAST_DEC_LV',
            'LAST_EMP_LV_NAME' => 'LAST_EMP_LV',
            'AMOUNT' => 'AMOUNT',
            'TOTAL' => 'TOTAL',
            'ADMIN_NAME' => 'ADMIN_NAME',
            'REMARK' => 'REMARK',
        ]);
        $form = new BonusExportForm();
        $result = $form->run($filter, '兑换积分流水');
        if (!$result) {
            return static::notice(Form::formatErrorsForApi($form->getErrors()), 400);
        }
        return static::notice(Yii::t('ctx', 'startExporting')); // 导出开始，请到文件管理-导出文件查看
    }

    /**
     * 现金钱包流水
     * @return mixed
     * @throws \yii\base\Exception
     * @throws HttpException
     */
    public function actionFlowWallet() {
        $filter = $this->filterCondition([
            'USER_NAME' => 'U.USER_NAME',
            'REAL_NAME' => 'U.REAL_NAME',
            'IS_DEC' => 'U.IS_DEC',
            'userIds' => 'USER_ID',
            'CREATED_AT' => 'CREATED_AT',
            'PERIOD_NUM' => 'PERIOD_NUM',
            'CALC_MONTH' => 'CALC_MONTH',
            'AMOUNT' => 'AMOUNT',
            'TOTAL' => 'TOTAL',
            'ADMIN_NAME' => 'ADMIN_NAME',
            'REMARK' => 'REMARK',
        ]);
        $condition = $filter['condition'];
        $params = $filter['params'];
        $listObj = new FlowWalletList();
        $data = $listObj->getList(['condition'=>$condition, 'params'=>$params]);
        return static::notice($data);
    }

    /**
     * 现金钱包流水导出
     * @return mixed
     * @throws \yii\db\Exception
     * @throws HttpException
     */
    public function actionFlowWalletExport() {
        $filter = $this->filterCondition([
            'USER_NAME' => 'U.USER_NAME',
            'REAL_NAME' => 'U.REAL_NAME',
            'IS_DEC' => 'U.IS_DEC',
            'userIds' => 'USER_ID',
            'CREATED_AT' => 'CREATED_AT',
            'PERIOD_NUM' => 'PERIOD_NUM',
            'CALC_MONTH' => 'CALC_MONTH',
            'AMOUNT' => 'AMOUNT',
            'TOTAL' => 'TOTAL',
            'ADMIN_NAME' => 'ADMIN_NAME',
            'REMARK' => 'REMARK',
        ]);
        $form = new BonusExportForm();
        $result = $form->run($filter, '现金钱包流水');
        if (!$result) {
            return static::notice(Form::formatErrorsForApi($form->getErrors()), 400);
        }
        return static::notice(Yii::t('ctx', 'startExporting')); // 导出开始，请到文件管理-导出文件查看
    }

    /**
     * 车房流水
     * @return mixed
     * @throws \yii\base\Exception
     * @throws HttpException
     */
    public function actionFlowCf() {
        $filter = $this->filterCondition([
            'userIds' => 'USER_ID',
            'CREATED_AT' => 'CREATED_AT',
            'PERIOD_NUM' => 'PERIOD_NUM',
            'CALC_MONTH' => 'CALC_MONTH',
            'DEAL_TYPE_NAME' => 'DEAL_TYPE_ID',
            'LAST_DEC_LV_NAME' => 'LAST_DEC_LV',
            'LAST_EMP_LV_NAME' => 'LAST_EMP_LV',
            'LAST_STATUS_NAME' => 'LAST_STATUS',
            'AMOUNT' => 'AMOUNT',
            'TOTAL' => 'TOTAL',
            'ADMIN_NAME' => 'ADMIN_NAME',
            'REMARK' => 'REMARK',
        ]);
        $condition = $filter['condition'];
        $params = $filter['params'];
        $listObj = new FlowCfList();
        $data = $listObj->getList(['condition'=>$condition, 'params'=>$params]);
        return static::notice($data);
    }

    /**
     * 车房养老奖流水导出
     * @return mixed
     * @throws \yii\db\Exception
     * @throws HttpException
     */
    public function actionFlowCfExport() {
        $filter = $this->filterCondition([
            'userIds' => 'USER_ID',
            'CREATED_AT' => 'CREATED_AT',
            'PERIOD_NUM' => 'PERIOD_NUM',
            'CALC_MONTH' => 'CALC_MONTH',
            'DEAL_TYPE_NAME' => 'DEAL_TYPE_ID',
            'LAST_DEC_LV_NAME' => 'LAST_DEC_LV',
            'LAST_EMP_LV_NAME' => 'LAST_EMP_LV',
            'LAST_STATUS_NAME' => 'LAST_STATUS',
            'AMOUNT' => 'AMOUNT',
            'TOTAL' => 'TOTAL',
            'ADMIN_NAME' => 'ADMIN_NAME',
            'REMARK' => 'REMARK',
        ]);
        $form = new BonusExportForm();
        $result = $form->run($filter, '车房养老奖流水');
        if (!$result) {
            return static::notice(Form::formatErrorsForApi($form->getErrors()), 400);
        }
        return static::notice(Yii::t('ctx', 'startExporting')); // 导出开始，请到文件管理-导出文件查看
    }

    /**
     * 领袖分红流水
     * @return mixed
     * @throws \yii\base\Exception
     * @throws HttpException
     */
    public function actionFlowLx() {
        $filter = $this->filterCondition([
            'userIds' => 'USER_ID',
            'CREATED_AT' => 'CREATED_AT',
            'PERIOD_NUM' => 'PERIOD_NUM',
            'CALC_MONTH' => 'CALC_MONTH',
            'DEAL_TYPE_NAME' => 'DEAL_TYPE_ID',
            'LAST_DEC_LV_NAME' => 'LAST_DEC_LV',
            'LAST_EMP_LV_NAME' => 'LAST_EMP_LV',
            'LAST_STATUS_NAME' => 'LAST_STATUS',
            'AMOUNT' => 'AMOUNT',
            'TOTAL' => 'TOTAL',
            'ADMIN_NAME' => 'ADMIN_NAME',
            'REMARK' => 'REMARK',
        ]);
        $condition = $filter['condition'];
        $params = $filter['params'];
        $listObj = new FlowLxList();
        $data = $listObj->getList(['condition'=>$condition, 'params'=>$params]);
        return static::notice($data);
    }

    /**
     * 导出
     * @return mixed
     * @throws \yii\db\Exception
     * @throws HttpException
     */
    public function actionFlowLxExport() {
        $filter = $this->filterCondition([
            'userIds' => 'USER_ID',
            'CREATED_AT' => 'CREATED_AT',
            'PERIOD_NUM' => 'PERIOD_NUM',
            'CALC_MONTH' => 'CALC_MONTH',
            'DEAL_TYPE_NAME' => 'DEAL_TYPE_ID',
            'LAST_DEC_LV_NAME' => 'LAST_DEC_LV',
            'LAST_EMP_LV_NAME' => 'LAST_EMP_LV',
            'LAST_STATUS_NAME' => 'LAST_STATUS',
            'AMOUNT' => 'AMOUNT',
            'TOTAL' => 'TOTAL',
            'ADMIN_NAME' => 'ADMIN_NAME',
            'REMARK' => 'REMARK',
        ]);
        $form = new BonusExportForm();
        $result = $form->run($filter, '领袖分红奖流水');
        if (!$result) {
            return static::notice(Form::formatErrorsForApi($form->getErrors()), 400);
        }
        return static::notice(Yii::t('ctx', 'startExporting')); // 导出开始，请到文件管理-导出文件查看
    }

    /**
     * 车房养老奖和领袖分红年度发放审核列表
     * @return mixed
     * @throws \yii\base\Exception
     * @throws HttpException
     */
    public function actionCfLxAuditList() {
        $filter = $this->filterCondition([
            'userIds' => 'CFLXA.USER_ID',
            'CF' => 'CFLXA.CF',
            'LX' => 'CFLXA.LX',
            'CREATE_ADMIN_NAME' => 'ADM.ADMIN_NAME',
            'CREATED_AT' => 'CFLXA.CREATED_AT',
            'AUDIT_ADMIN_NAME' => 'ADMA.ADMIN_NAME',
            'AUDITED_AT' => 'CFLXA.AUDITED_AT',
            'CREATE_REMARK' => 'CFLXA.CREATE_REMARK',
            'filterStatus' => 'CFLXA.AUDIT_STATUS',
        ]);
        $condition = $filter['condition'];
        $params = $filter['params'];
        $listObj = new CfLxAuditList();
        $data = $listObj->getList(['condition'=>$condition, 'params'=>$params]);
        return static::notice($data);
    }

    /**
     * 车房养老奖和领袖分红年度发放审核导出
     * @return mixed
     * @throws \yii\db\Exception
     * @throws HttpException
     */
    public function actionCfLxAuditExport() {
        $filter = $this->filterCondition([
            'userIds' => 'CFLXA.USER_ID',
            'CF' => 'CFLXA.CF',
            'LX' => 'CFLXA.LX',
            'CREATE_ADMIN_NAME' => 'ADM.ADMIN_NAME',
            'CREATED_AT' => 'CFLXA.CREATED_AT',
            'AUDIT_ADMIN_NAME' => 'ADMA.ADMIN_NAME',
            'AUDITED_AT' => 'CFLXA.AUDITED_AT',
            'CREATE_REMARK' => 'CFLXA.CREATE_REMARK',
            'filterStatus' => 'CFLXA.AUDIT_STATUS',
        ]);
        $form = new BonusExportForm();
        $result = $form->run($filter, '年度奖金发放审核');
        if (!$result) {
            return static::notice(Form::formatErrorsForApi($form->getErrors()), 400);
        }
        return static::notice(Yii::t('ctx', 'startExporting')); // 导出开始，请到文件管理-导出文件查看
    }

    /**
     * 申请发放车房养老奖和领袖分红
     * @return mixed
     * @throws \yii\db\Exception
     * @throws HttpException
     */
    public function actionCfLxApply() {
        if (Yii::$app->request->isPost) {
            $formModel = new SendCFAndLXForm();
            $formModel->scenario = 'apply';
            if ($formModel->load(Yii::$app->request->post(), '') && $result = $formModel->apply()) {
                if(!empty($result['logs'])){
                    return static::notice('申请成功，请在年度奖管理中审核');
                }else{
                    return static::notice('申请失败，无符合发放条件会员',400);
                }
            } else {
                return static::notice(Form::formatErrorsForApi($formModel->getErrors()), 400);
            }
        }
        return static::notice('Illegal request', 400); // 非法请求
    }

    /**
     * 审核车房养老奖和领袖分红
     * @return mixed
     * @throws \yii\db\Exception
     * @throws HttpException
     */
    public function actionCfLxAudit() {
        $formModel = new SendCFAndLXForm();
        $formModel->scenario = 'audit';
        if ($formModel->load(Yii::$app->request->post(), '') && $result = $formModel->audit()) {
            return static::notice('审核年度奖申请完成');
        }
        return static::notice(Form::formatErrorsForApi($formModel->getErrors()), 400);
    }

    /**
     * 删除车房领袖审核信息
     * @return mixed
     * @throws \yii\db\Exception
     * @throws HttpException
     */
    public function actionCfLxAuditDelete() {
        $sendCFAndLXForm = new SendCFAndLXForm();
        $result = static::delete(CFLXAudit::class, function ($selected) use ($sendCFAndLXForm) {
            $sendCFAndLXForm->beforeDelete($selected);
            // Log::adminHandle('删除审核会员余额信息');
        }, function ($selected) use ($sendCFAndLXForm) {
            $sendCFAndLXForm->delete($selected);
            // Log::adminHandle('删除审核会员余额信息');
        }, true);
        return $result;
    }

    /**
     * 查看所传期数的各项奖金
     * @return mixed
     * @throws \yii\db\Exception
     * @throws HttpException
     */
    public function actionPeriodDetail() {
        $periodNum = \Yii::$app->request->get('periodNum');
        $period = Period::instance();
        $periodInfo = $period->setPeriodNum($periodNum);
        $yearMonth = $period->getYearMonth($periodNum);
//        $filter = $this->filterCondition('UT', [
//            'userName' => 'UI.USER_NAME',
//        ]);
        $condition = '';
        $params = [];
        $condition .= ' AND C.PERIOD_NUM=:PERIOD_NUM';
        $params[':PERIOD_NUM'] = $periodNum;
        if (!$period->isClosed($periodNum)) {
            return static::notice('Cannot view this period'); // 该期不能查看
        }
        $data = CalcBonus::lists($condition, $params, [
            'yearMonth' => $yearMonth,
            'select' => 'C.*,UI.USER_NAME',
            'from' => CalcBonus::tableName() . ' AS C',
            'join' => [
                ['LEFT JOIN', UserInfo::tableName() . ' AS UI', 'C.USER_ID=UI.USER_ID'],
            ],
            'orderBy' => 'C.CREATED_AT DESC',
        ]);
        $data['periodInfo'] = $periodInfo;
        return static::notice($data);
    }

    /**
     * 获取最新一期结算的奖金
     * @return mixed
     * @throws \yii\base\Exception
     * @throws \yii\db\Exception
     * @throws HttpException
     */
    public function actionNewPeriodBonus() {
        $periodNum = Period::calculatedMaxPeriodNum();
        $data = $this->_periodBonus($periodNum);
        return static::notice($data);
    }

    /**
     * 最新一期奖金导出
     * @return mixed
     * @throws \yii\db\Exception
     * @throws HttpException
     */
    public function actionNewPeriodBonusExport() {
        $filter = $this->filterCondition([
            'LAST_USER_NAME' => 'CB.LAST_USER_NAME',
            'LAST_REAL_NAME' => 'CB.LAST_REAL_NAME',
            'LAST_DEC_LV_NAME' => 'CB.LAST_DEC_LV',
            'LAST_EMP_LV_NAME' => 'CB.LAST_EMP_LV',
            'LAST_MOBILE' => 'CB.LAST_MOBILE',
            'LAST_PERIOD_AT' => 'CB.LAST_PERIOD_AT',
            'LAST_CREATED_AT' => 'CB.LAST_CREATED_AT',
            'LAST_REC_USER_NAME' => 'CB.LAST_REC_USER_NAME',
            'LAST_REC_REAL_NAME' => 'CB.LAST_REC_REAL_NAME',
            'LAST_CON_USER_NAME' => 'CB.LAST_CON_USER_NAME',
            'LAST_CON_REAL_NAME' => 'CB.LAST_CON_REAL_NAME',
            'BONUS_BD' => 'CB.BONUS_BD',
            'BONUS_TG' => 'CB.BONUS_TG',
            'BONUS_YJ' => 'CB.BONUS_YJ',
            'BONUS_GX' => 'CB.BONUS_GX',
            // 'BONUS_GL' => 'CB.BONUS_BS',
            'BONUS_GL' => 'CB.BONUS_GL',
            'BONUS_QY' => 'CB.BONUS_QY',
            'BONUS_YC' => 'CB.BONUS_YC',
            'PV_1L' => 'CB.PV_1L',
            'SURPLUS_1L' => 'CB.SURPLUS_1L',
            'PV_2L' => 'CB.PV_2L',
            'SURPLUS_2L' => 'CB.SURPLUS_2L',
            'PV_3L' => 'CB.PV_3L',
            'SURPLUS_3L' => 'CB.SURPLUS_3L',
            'CALCULATED_AT' => 'CB.CALCULATED_AT',
            'CALC_MONTH' => 'CB.CALC_MONTH',
            'BONUS_QY_MONTH' => 'CB.BONUS_QY_MONTH',
        ]);
        $periodNum = Period::calculatedMaxPeriodNum();
        $period = Period::instance();
        $yearMonth = $period->getYearMonth($periodNum);
        $filter['condition'] .= ' AND PERIOD_NUM=:PERIOD_NUM';
        $filter['params'][':PERIOD_NUM'] = $periodNum;
        $form = new BonusExportForm();
        $result = $form->run(array_merge($filter, ['others'=>['yearMonth'=>$yearMonth]]), '最新奖金');
        if (!$result) {
            return static::notice(Form::formatErrorsForApi($form->getErrors()), 400);
        }
        return static::notice(Yii::t('ctx', 'startExporting')); // 导出开始，请到文件管理-导出文件查看
    }

    /**
     * 获取往期已结算的奖金
     * @return mixed
     * @throws \yii\db\Exception
     * @throws HttpException
     */
    public function actionOtherPeriodBonus() {
        $periodNumRequest = \Yii::$app->request->get('periodNum');
        if(!$periodNumRequest){
            $periodNum = Period::calculatedMaxPeriodNum();
        }else{
            $periodNum = explode(",", $periodNumRequest)[1];
        }
        $data = $this->_periodBonus($periodNum,false);
        if( isset($data['filterTypes']) && is_array($data['filterTypes']) ) {
            $data['filterTypes'] = array_merge(['periodNum'=>['isUserTable'=> false, 'name'=> \Yii::t('ctx', 'payCycle')]], $data['filterTypes']);
        }
        return static::notice($data);
    }

    /**
     * 往期奖金导出
     * @return mixed
     * @throws \yii\db\Exception
     * @throws HttpException
     */
    public function actionOtherPeriodBonusExport() {
        $filter = $this->filterCondition([
            'periodNum' => 'CB.PERIOD_NUM',
            'LAST_USER_NAME' => 'CB.LAST_USER_NAME',
            'LAST_REAL_NAME' => 'CB.LAST_REAL_NAME',
            'LAST_DEC_LV_NAME' => 'CB.LAST_DEC_LV',
            'LAST_EMP_LV_NAME' => 'CB.LAST_EMP_LV',
            'LAST_MOBILE' => 'CB.LAST_MOBILE',
            'LAST_PERIOD_AT' => 'CB.LAST_PERIOD_AT',
            'LAST_CREATED_AT' => 'CB.LAST_CREATED_AT',
            'LAST_REC_USER_NAME' => 'CB.LAST_REC_USER_NAME',
            'LAST_REC_REAL_NAME' => 'CB.LAST_REC_REAL_NAME',
            'LAST_CON_USER_NAME' => 'CB.LAST_CON_USER_NAME',
            'LAST_CON_REAL_NAME' => 'CB.LAST_CON_REAL_NAME',
            'BONUS_BD' => 'CB.BONUS_BD',
            'BONUS_TG' => 'CB.BONUS_TG',
            'BONUS_YJ' => 'CB.BONUS_YJ',
            'BONUS_GX' => 'CB.BONUS_GX',
            'BONUS_GL' => 'CB.BONUS_GL',
            'BONUS_QY' => 'CB.BONUS_QY',
            'BONUS_YC' => 'CB.BONUS_YC',
            'PV_1L' => 'CB.PV_1L',
            'SURPLUS_1L' => 'CB.SURPLUS_1L',
            'PV_2L' => 'CB.PV_2L',
            'SURPLUS_2L' => 'CB.SURPLUS_2L',
            'PV_3L' => 'CB.PV_3L',
            'SURPLUS_3L' => 'CB.SURPLUS_3L',
            'CALCULATED_AT' => 'CB.CALCULATED_AT',
            'CALC_MONTH' => 'CB.CALC_MONTH',
            'BONUS_QY_MONTH' => 'CB.BONUS_QY_MONTH',
        ]);
        $periodNumRequest = \Yii::$app->request->get('periodNum');
        if(!$periodNumRequest){
            $periodNum = Period::calculatedMaxPeriodNum();
        }else{
            $periodNum = explode(",", $periodNumRequest)[1];
        }
        $period = Period::instance();
        $yearMonth = $period->getYearMonth($periodNum);

        $condition = $filter['condition'];
        $params = $filter['params'];

        $isSuper = AdminRole::isSuperAdmin(\Yii::$app->getUser()->getUserInfo()['roleId']);
        if (!$isSuper) {
            $adminId = Yii::$app->getUser()->getUserInfo()['id'];
            $adminCountry = AdminCountry::getCountry($adminId);
            $quotedAdminCountry = array_map(function($item) {
                return "'" . addslashes($item) . "'";
            }, $adminCountry);

            $condition .= " AND CU.COUNTRY_ID IN (" . implode(',', $quotedAdminCountry) . ")";
        }

        $form = new BonusExportForm();
        $result = $form->run(array_merge(['condition'=>$condition, 'params'=>$params, 'others'=>['yearMonth'=>$yearMonth]]), 'Member Bonus'); // 往期奖金
        if (!$result) {
            return static::notice(Form::formatErrorsForApi($form->getErrors()), 400);
        }
        return static::notice(Yii::t('ctx', 'startExporting')); // 导出开始，请到文件管理-导出文件查看
    }

    /**
     * 获取筛选类型
     * @return mixed
     * @throws HttpException
     */
    public function actionOtherPeriodBonusFilterTypes(){
        $listObj = new PeriodBonusList();
        $filterTypes = $listObj->getFilterTypes();
        $filterTypes = array_merge(['periodNum'=>['isUserTable'=> false, 'name'=> \Yii::t('ctx', 'payCycle')]], $filterTypes);
        return static::notice($filterTypes);
    }

    /**
     * 期数对应的奖金
     * @param $periodNum
     * @param $periodFilter
     * @return array
     * @throws \yii\base\Exception
     * @throws \yii\db\Exception
     */
    private function _periodBonus($periodNum,$periodFilter = true) {
        $filter = $this->filterCondition([
            'periodNum' => 'CB.PERIOD_NUM',
            'LAST_USER_NAME' => 'CB.LAST_USER_NAME',
            'LAST_REAL_NAME' => 'CB.LAST_REAL_NAME',
            'LAST_DEC_LV_NAME' => 'CB.LAST_DEC_LV',
            'LAST_EMP_LV_NAME' => 'CB.LAST_EMP_LV',
            'LAST_ELITE_LV_NAME' => 'CB.LAST_ELITE_LV',
            'LAST_PERIOD_AT' => 'CB.LAST_PERIOD_AT',
            'LAST_CREATED_AT' => 'CB.LAST_CREATED_AT',
            'CALC_MONTH' => 'CB.CALC_MONTH',
            'BONUS_TOTAL' => 'CB.BONUS_TOTAL',
            'BONUS_E' => 'CB.BONUS_E',
            'BONUS_PE' => 'CB.BONUS_PE',
            'BONUS_SE' => 'CB.BONUS_SE',
            'BONUS_LB' => 'CB.BONUS_LB',
            'BONUS_ST' => 'CB.BONUS_ST',
            'BONUS_WB' => 'CB.BONUS_WB',
            'BONUS_CAR' => 'CB.BONUS_CAR',
            'BONUS_PB' => 'CB.BONUS_PB',
        ]);
        $period = Period::instance();
        $yearMonth = $period->getYearMonth($periodNum);
        $condition = $filter['condition'];
        $params = $filter['params'];
        if($periodFilter){
            $condition .= ' AND CB.PERIOD_NUM=:PERIOD_NUM';
            $params[':PERIOD_NUM'] = $periodNum;
        }

        $isSuper = AdminRole::isSuperAdmin(\Yii::$app->getUser()->getUserInfo()['roleId']);
        if (!$isSuper) {
            $adminId = Yii::$app->getUser()->getUserInfo()['id'];
            $adminCountry = AdminCountry::getCountry($adminId);
            $quotedAdminCountry = array_map(function($item) {
                return "'" . addslashes($item) . "'";
            }, $adminCountry);

            $condition .= " AND CU.COUNTRY_ID IN (" . implode(',', $quotedAdminCountry) . ")";
        }

        $listObj = new PeriodBonusList();
        return $listObj->getList(['condition'=>$condition, 'params'=>$params, 'others'=>['yearMonth'=>$yearMonth]]);
    }

    /**
     * 团队奖奖金向下追溯
     * @return mixed
     * @throws \yii\base\Exception
     * @throws \yii\db\Exception
     * @throws HttpException
     */
    public function actionTraceDownQy() {
        $periodNum = Yii::$app->request->get('periodNum');
        if (!$periodNum) return static::notice(\Yii::t('ctx', 'requirePeriods'), 400);
        $userName = Yii::$app->request->get('userName');
        if (!$userName) return static::notice(\Yii::t('ctx', 'requireMemberNumber'), 400);
        $period = Period::instance();
        if (!$period->isCalculated($periodNum)) {
            return static::notice([]);
        }
        $yearMonth = $period->getYearMonth($periodNum);

        $filter = $this->filterCondition([
            'decType' => 'PO.DEC_TYPE',
            'periodNum' => 'PO.PERIOD_NUM',
        ]);
        $qyFilter = $this->_qyFilterCondition($filter['request']);
        $filter = array_merge($filter, $qyFilter);
        $condition = $filter['condition'];
        $params = $filter['params'];
        $listObj = new TraceDownQyList();
        $data = $listObj->getList(['condition'=>$condition, 'params'=>$params, 'others'=>['yearMonth'=>$yearMonth, 'periodNum'=>$periodNum]]);
        return static::notice($data);
    }

    /**
     * 获取
     * @param $request
     * @return array|string[]
     */
    private function _qyFilterCondition($request) {
        //根据USER_NAME获取USER_ID
        $user = User::find()->select('ID')->where('USER_NAME=:USER_NAME', ['USER_NAME'=>$request['userName']])->asArray()->one();
        if( !$user ) return [];

        //找到这个会员的LOCATION_TAG
        $userNetwork = UserNetwork::find()->select(['LOCATION_TAG'])->where('USER_ID=:USER_ID', ['USER_ID'=>$user['ID']])->asArray()->one();
        $searchLocationTag = $userNetwork['LOCATION_TAG'];
        if( isset($request['location']) && $request['location'] ) {
            $searchLocationTag .= $request['location'];
        }

        return [
            'LOCATION_TAG' => 'LIKE ' . $searchLocationTag . '%'
        ];
    }

    /**
     * 团队奖向下追溯导出
     * @return mixed
     * @throws \yii\db\Exception
     * @throws HttpException
     */
    public function actionTraceDownQyExport() {
        $periodNum = Yii::$app->request->get('periodNum');
        if (!$periodNum) return static::notice('请填写期数', 400);
        $userName = Yii::$app->request->get('userName');
        if (!$userName) return static::notice('请填写会员编号', 400);
        $period = Period::instance();
        if (!$period->isCalculated($periodNum)) {
            return static::notice([]);
        }
        $yearMonth = $period->getYearMonth($periodNum);
        $filter = $this->filterCondition([
            'decType' => 'PO.DEC_TYPE',
            'periodNum' => 'PO.PERIOD_NUM',
        ]);
        $qyFilter = $this->_qyFilterCondition($filter['request']);
        $filter = array_merge($filter, $qyFilter);

        $form = new BonusExportForm();
        $result = $form->run(array_merge($filter, ['others'=>['yearMonth'=>$yearMonth, 'periodNum'=>$periodNum]]), '团队奖向下追溯');
        if (!$result) {
            return static::notice(Form::formatErrorsForApi($form->getErrors()), 400);
        }
        return static::notice(Yii::t('ctx', 'startExporting')); // 导出开始，请到文件管理-导出文件查看
    }

    /**
     * 团队奖向上追溯
     * @return mixed
     * @throws \yii\base\Exception
     * @throws HttpException
     */
    public function actionTraceUpQy() {
        $periodNum = Yii::$app->request->get('periodNum');
        if (!$periodNum) return static::notice('请填写期数', 400);
        if (!$userName = Yii::$app->request->get('userName')) return static::notice('请填写会员编号', 400);
        if(!$userId = Info::getUserIdByUserName($userName))  return static::notice('Member does not exist', 400); // 会员不存在
        //会员是否有业绩单
        if(!PerfOrder::find()->where('USER_ID=:USER_ID AND PERIOD_NUM=:PERIOD_NUM',[':USER_ID'=>$userId,':PERIOD_NUM'=>$periodNum])->exists()){
            return static::notice('该会员本期没有业绩单', 400);
        }
        $listObj = new TraceUpQyList();
        $data = $listObj->getList(['condition'=>'', 'params'=>[], 'others'=>['userId'=>$userId, 'periodNum'=>$periodNum]]);
        return static::notice($data);
    }

    /**
     * 团队奖向上追溯导出
     * @return mixed
     * @throws \yii\db\Exception
     * @throws HttpException
     */
    public function actionTraceUpQyExport() {
        $periodNum = Yii::$app->request->get('periodNum');
        if (!$periodNum) return static::notice('请填写期数', 400);
        if (!$userName = Yii::$app->request->get('userName')) return static::notice('请填写会员编号', 400);
        if(!$userId = Info::getUserIdByUserName($userName))  return static::notice('Member does not exist', 400); // 会员不存在
        //会员是否有业绩单
        if(!PerfOrder::find()->where('USER_ID=:USER_ID AND PERIOD_NUM=:PERIOD_NUM',[':USER_ID'=>$userId,':PERIOD_NUM'=>$periodNum])->exists()){
            return static::notice('该会员本期没有业绩单', 400);
        }
        $form = new BonusExportForm();
        $result = $form->run(array_merge(['condition'=>'', 'params'=>[]], ['others'=>['userId'=>$userId, 'periodNum'=>$periodNum]]), '团队奖向上追溯');
        if (!$result) {
            return static::notice(Form::formatErrorsForApi($form->getErrors()), 400);
        }
        return static::notice(Yii::t('ctx', 'startExporting')); // 导出开始，请到文件管理-导出文件查看
    }

    /**
     * 荣衔奖向下追溯
     * @return mixed
     * @throws \yii\base\Exception
     * @throws \yii\db\Exception
     * @throws HttpException
     */
    public function actionTraceDownYc() {
        $periodNum = Yii::$app->request->get('periodNum');
        if (!$periodNum) return static::notice([]);
        $period = Period::instance();
        $yearMonth = $period->getYearMonth($periodNum);

        $filter = $this->filterCondition([
            'userName' => 'UI.USER_NAME',
            'periodNum' => 'CBY.PERIOD_NUM',
        ]);
        $condition = $filter['condition'];
        $params = $filter['params'];

        $listObj = new TraceDownYcList();
        $data = $listObj->getList(['condition'=>$condition, 'params'=>$params, 'others'=>['yearMonth'=>$yearMonth, 'periodNum'=>$periodNum]]);

        return static::notice($data);
    }

    /**
     * 荣衔奖向下追溯导出
     * @return mixed
     * @throws \yii\db\Exception
     * @throws HttpException
     */
    public function actionTraceDownYcExport() {
        $periodNum = Yii::$app->request->get('periodNum');
        if (!$periodNum) return static::notice([]);
        $period = Period::instance();
        $yearMonth = $period->getYearMonth($periodNum);

        $filter = $this->filterCondition([
            'userName' => 'UI.USER_NAME',
            'periodNum' => 'CBY.PERIOD_NUM',
        ]);
        $form = new BonusExportForm();
        $result = $form->run(array_merge($filter, ['others'=>['yearMonth'=>$yearMonth, 'periodNum'=>$periodNum]]), '荣衔奖向下追溯');
        if (!$result) {
            return static::notice(Form::formatErrorsForApi($form->getErrors()), 400);
        }
        return static::notice(Yii::t('ctx', 'startExporting')); // 导出开始，请到文件管理-导出文件查看
    }

    /**
     * 服务奖向下追溯
     * @return mixed
     * @throws \yii\base\Exception
     * @throws \yii\db\Exception
     * @throws HttpException
     */
    public function actionTraceDownBd() {
        $periodNum = Yii::$app->request->get('periodNum');
        if (!$periodNum) return static::notice([]);
        $period = Period::instance();
        $yearMonth = $period->getYearMonth($periodNum);

        $filter = $this->filterCondition([
            'userName' => 'UI.USER_NAME',
            'periodNum' => 'CBB.PERIOD_NUM',
        ]);
        $condition = $filter['condition'];
        $params = $filter['params'];

        $listObj = new TraceDownBdList();
        $data = $listObj->getList(['condition'=>$condition, 'params'=>$params, 'others'=>['yearMonth'=>$yearMonth, 'periodNum'=>$periodNum]]);

        return static::notice($data);
    }

    /**
     * 服务奖向下追溯导出
     * @return mixed
     * @throws \yii\db\Exception
     * @throws HttpException
     */
    public function actionTraceDownBdExport() {
        $periodNum = Yii::$app->request->get('periodNum');
        if (!$periodNum) return static::notice([]);
        $period = Period::instance();
        $yearMonth = $period->getYearMonth($periodNum);

        $filter = $this->filterCondition([
            'userName' => 'UI.USER_NAME',
            'periodNum' => 'CBB.PERIOD_NUM',
        ]);
        $form = new BonusExportForm();
        $result = $form->run(array_merge($filter, ['others'=>['yearMonth'=>$yearMonth, 'periodNum'=>$periodNum]]), '服务奖向下追溯');
        if (!$result) {
            return static::notice(Form::formatErrorsForApi($form->getErrors()), 400);
        }
        return static::notice(Yii::t('ctx', 'startExporting')); // 导出开始，请到文件管理-导出文件查看
    }


    /**
     * 推广奖向下追溯
     * @return mixed
     * @throws \yii\base\Exception
     * @throws \yii\db\Exception
     * @throws HttpException
     */
    public function actionTraceDownTg() {
        $periodNum = Yii::$app->request->get('periodNum');
        if (!$periodNum) return static::notice([]);
        $period = Period::instance();
        $yearMonth = $period->getYearMonth($periodNum);

        $filter = $this->filterCondition([
            'userName' => 'UI.USER_NAME',
            'periodNum' => 'CTG.PERIOD_NUM',
        ]);
        $condition = $filter['condition'];
        $params = $filter['params'];

        $listObj = new TraceDownTgList();
        $data = $listObj->getList(['condition'=>$condition, 'params'=>$params, 'others'=>['yearMonth'=>$yearMonth, 'periodNum'=>$periodNum]]);

        return static::notice($data);
    }

    /**
     * 推广奖向下追溯导出
     * @return mixed
     * @throws \yii\db\Exception
     * @throws HttpException
     */
    public function actionTraceDownTgExport() {
        $periodNum = Yii::$app->request->get('periodNum');
        if (!$periodNum) return static::notice([]);
        $period = Period::instance();
        $yearMonth = $period->getYearMonth($periodNum);

        $filter = $this->filterCondition([
            'userName' => 'UI.USER_NAME',
            'periodNum' => 'CTG.PERIOD_NUM',
        ]);
        $form = new BonusExportForm();
        $result = $form->run(array_merge($filter, ['others'=>['yearMonth'=>$yearMonth, 'periodNum'=>$periodNum]]), '推广奖向下追溯');
        if (!$result) {
            return static::notice(Form::formatErrorsForApi($form->getErrors()), 400);
        }
        return static::notice(Yii::t('ctx', 'startExporting')); // 导出开始，请到文件管理-导出文件查看
    }

    /**
     * 消费奖向下追溯
     * @return mixed
     * @throws \yii\base\Exception
     * @throws \yii\db\Exception
     * @throws HttpException
     */
    public function actionTraceDownXf() {
        $periodNum = Yii::$app->request->get('periodNum');
        if (!$periodNum) return static::notice([]);
        $period = Period::instance();
        $yearMonth = $period->getYearMonth($periodNum);

        $filter = $this->filterCondition([
            'userName' => 'UI.USER_NAME',
            'periodNum' => 'CXF.PERIOD_NUM',
        ]);
        $condition = $filter['condition'];
        $params = $filter['params'];

        $listObj = new TraceDownXfList();
        $data = $listObj->getList(['condition'=>$condition, 'params'=>$params, 'others'=>['yearMonth'=>$yearMonth, 'periodNum'=>$periodNum]]);

        return static::notice($data);
    }

    /**
     * 消费奖向下追溯导出
     * @return mixed
     * @throws \yii\db\Exception
     * @throws HttpException
     */
    public function actionTraceDownXfExport() {
        $periodNum = Yii::$app->request->get('periodNum');
        if (!$periodNum) return static::notice([]);
        $period = Period::instance();
        $yearMonth = $period->getYearMonth($periodNum);

        $filter = $this->filterCondition([
            'userName' => 'UI.USER_NAME',
            'periodNum' => 'CXF.PERIOD_NUM',
        ]);
        $form = new BonusExportForm();
        $result = $form->run(array_merge($filter, ['others'=>['yearMonth'=>$yearMonth, 'periodNum'=>$periodNum]]), '推广奖向下追溯');
        if (!$result) {
            return static::notice(Form::formatErrorsForApi($form->getErrors()), 400);
        }
        return static::notice(Yii::t('ctx', 'startExporting')); // 导出开始，请到文件管理-导出文件查看
    }

    /**
     * 业绩奖向下追溯
     * @return mixed
     * @throws \yii\base\Exception
     * @throws \yii\db\Exception
     * @throws HttpException
     */
    public function actionTraceDownYj() {
        $periodNum = Yii::$app->request->get('periodNum');
        if (!$periodNum) return static::notice([]);
        $period = Period::instance();
        $yearMonth = $period->getYearMonth($periodNum);

        $filter = $this->filterCondition([
            'userName' => 'UI.USER_NAME',
            'periodNum' => 'CYJ.PERIOD_NUM',
        ]);
        $condition = $filter['condition'];
        $params = $filter['params'];

        $listObj = new TraceDownYjList();
        $data = $listObj->getList(['condition'=>$condition, 'params'=>$params, 'others'=>['yearMonth'=>$yearMonth, 'periodNum'=>$periodNum]]);

        return static::notice($data);
    }

    /**
     * 业绩奖向下追溯导出
     * @return mixed
     * @throws \yii\db\Exception
     * @throws HttpException
     */
    public function actionTraceDownYjExport() {
        $periodNum = Yii::$app->request->get('periodNum');
        if (!$periodNum) return static::notice([]);
        $period = Period::instance();
        $yearMonth = $period->getYearMonth($periodNum);

        $filter = $this->filterCondition([
            'userName' => 'UI.USER_NAME',
            'periodNum' => 'CYJ.PERIOD_NUM',
        ]);
        $form = new BonusExportForm();
        $result = $form->run(array_merge($filter, ['others'=>['yearMonth'=>$yearMonth, 'periodNum'=>$periodNum]]), '业绩奖向下追溯');
        if (!$result) {
            return static::notice(Form::formatErrorsForApi($form->getErrors()), 400);
        }
        return static::notice(Yii::t('ctx', 'startExporting')); // 导出开始，请到文件管理-导出文件查看
    }

    /**
     * 共享奖向下追溯
     * @return mixed
     * @throws \yii\base\Exception
     * @throws \yii\db\Exception
     * @throws HttpException
     */
    public function actionTraceDownGx() {
        $periodNum = Yii::$app->request->get('periodNum');
        if (!$periodNum) return static::notice([]);
        $period = Period::instance();
        $yearMonth = $period->getYearMonth($periodNum);

        $filter = $this->filterCondition([
            'userName' => 'UI.USER_NAME',
            'periodNum' => 'CGX.PERIOD_NUM',
        ]);
        $condition = $filter['condition'];
        $params = $filter['params'];

        $listObj = new TraceDownGxList();
        $data = $listObj->getList(['condition'=>$condition, 'params'=>$params, 'others'=>['yearMonth'=>$yearMonth, 'periodNum'=>$periodNum]]);

        return static::notice($data);
    }

    /**
     * 共享奖向下追溯导出
     * @return mixed
     * @throws \yii\db\Exception
     * @throws HttpException
     */
    public function actionTraceDownGxExport() {
        $periodNum = Yii::$app->request->get('periodNum');
        if (!$periodNum) return static::notice([]);
        $period = Period::instance();
        $yearMonth = $period->getYearMonth($periodNum);

        $filter = $this->filterCondition([
            'userName' => 'UI.USER_NAME',
            'periodNum' => 'CGX.PERIOD_NUM',
        ]);
        $form = new BonusExportForm();
        $result = $form->run(array_merge($filter, ['others'=>['yearMonth'=>$yearMonth, 'periodNum'=>$periodNum]]), '共享奖向下追溯');
        if (!$result) {
            return static::notice(Form::formatErrorsForApi($form->getErrors()), 400);
        }
        return static::notice(Yii::t('ctx', 'startExporting')); // 导出开始，请到文件管理-导出文件查看
    }

    /**
     * 管理奖向下追溯
     * @return mixed
     * @throws \yii\base\Exception
     * @throws \yii\db\Exception
     * @throws HttpException
     */
    public function actionTraceDownGl() {
        $periodNum = Yii::$app->request->get('periodNum');
        if (!$periodNum) return static::notice([]);
        $period = Period::instance();
        $yearMonth = $period->getYearMonth($periodNum);

        $filter = $this->filterCondition([
            'userName' => 'UI.USER_NAME',
            'periodNum' => 'CGL.PERIOD_NUM',
        ]);
        $condition = $filter['condition'];
        $params = $filter['params'];

        $listObj = new TraceDownGlList();
        $data = $listObj->getList(['condition'=>$condition, 'params'=>$params, 'others'=>['yearMonth'=>$yearMonth, 'periodNum'=>$periodNum]]);

        return static::notice($data);
    }

    /**
     * 管理奖向下追溯导出
     * @return mixed
     * @throws \yii\db\Exception
     * @throws HttpException
     */
    public function actionTraceDownGlExport() {
        $periodNum = Yii::$app->request->get('periodNum');
        if (!$periodNum) return static::notice([]);
        $period = Period::instance();
        $yearMonth = $period->getYearMonth($periodNum);

        $filter = $this->filterCondition([
            'userName' => 'UI.USER_NAME',
            'periodNum' => 'CGL.PERIOD_NUM',
        ]);
        $form = new BonusExportForm();
        $result = $form->run(array_merge($filter, ['others'=>['yearMonth'=>$yearMonth, 'periodNum'=>$periodNum]]), '管理奖向下追溯');
        if (!$result) {
            return static::notice(Form::formatErrorsForApi($form->getErrors()), 400);
        }
        return static::notice(Yii::t('ctx', 'startExporting')); // 导出开始，请到文件管理-导出文件查看
    }

    /**
     * 荣衔奖向上追溯
     * @return mixed
     * @throws \yii\base\Exception
     * @throws \yii\db\Exception
     * @throws HttpException
     */
    public function actionTraceUpYc() {
        $periodNum = Yii::$app->request->get('periodNum');
        if (!$periodNum) return static::notice([]);
        $period = Period::instance();
        $yearMonth = $period->getYearMonth($periodNum);

        $filter = $this->filterCondition([
            'userName' => 'UI.USER_NAME',
            'periodNum' => 'CBY.PERIOD_NUM',
        ]);
        $condition = $filter['condition'];
        $params = $filter['params'];

        $listObj = new TraceUpYcList();
        $data = $listObj->getList(['condition'=>$condition, 'params'=>$params, 'others'=>['yearMonth'=>$yearMonth, 'periodNum'=>$periodNum]]);

        return static::notice($data);
    }

    /**
     * 荣衔奖向上追溯导出
     * @return mixed
     * @throws \yii\db\Exception
     * @throws HttpException
     */
    public function actionTraceUpYcExport() {
        $periodNum = Yii::$app->request->get('periodNum');
        if (!$periodNum) return static::notice([]);
        $period = Period::instance();
        $yearMonth = $period->getYearMonth($periodNum);

        $filter = $this->filterCondition([
            'userName' => 'UI.USER_NAME',
            'periodNum' => 'CBY.PERIOD_NUM',
        ]);
        $form = new BonusExportForm();
        $result = $form->run(array_merge($filter, ['others'=>['yearMonth'=>$yearMonth, 'periodNum'=>$periodNum]]), '荣衔奖向上追溯');
        if (!$result) {
            return static::notice(Form::formatErrorsForApi($form->getErrors()), 400);
        }
        return static::notice(Yii::t('ctx', 'startExporting')); // 导出开始，请到文件管理-导出文件查看
    }


    /**
     * 服务奖向上追溯
     * @return mixed
     * @throws \yii\base\Exception
     * @throws \yii\db\Exception
     * @throws HttpException
     */
    public function actionTraceUpBd() {
        $periodNum = Yii::$app->request->get('periodNum');
        if (!$periodNum) return static::notice([]);
        $period = Period::instance();
        $yearMonth = $period->getYearMonth($periodNum);

        $filter = $this->filterCondition([
            'userName' => 'UI.USER_NAME',
            'periodNum' => 'CBB.PERIOD_NUM',
        ]);
        $condition = $filter['condition'];
        $params = $filter['params'];

        $listObj = new TraceUpBdList();
        $data = $listObj->getList(['condition'=>$condition, 'params'=>$params, 'others'=>['yearMonth'=>$yearMonth, 'periodNum'=>$periodNum]]);

        return static::notice($data);
    }

    /**
     * 服务奖向上追溯导出
     * @return mixed
     * @throws \yii\db\Exception
     * @throws HttpException
     */
    public function actionTraceUpBdExport() {
        $periodNum = Yii::$app->request->get('periodNum');
        if (!$periodNum) return static::notice([]);
        $period = Period::instance();
        $yearMonth = $period->getYearMonth($periodNum);

        $filter = $this->filterCondition([
            'userName' => 'UI.USER_NAME',
            'periodNum' => 'CBY.PERIOD_NUM',
        ]);
        $form = new BonusExportForm();
        $result = $form->run(array_merge($filter, ['others'=>['yearMonth'=>$yearMonth, 'periodNum'=>$periodNum]]), '荣衔奖向上追溯');
        if (!$result) {
            return static::notice(Form::formatErrorsForApi($form->getErrors()), 400);
        }
        return static::notice(Yii::t('ctx', 'startExporting')); // 导出开始，请到文件管理-导出文件查看
    }

    /**
     * 推广奖向上追溯
     * @return mixed
     * @throws \yii\base\Exception
     * @throws \yii\db\Exception
     * @throws HttpException
     */
    public function actionTraceUpTg() {
        $periodNum = Yii::$app->request->get('periodNum');
        if (!$periodNum) return static::notice([]);
        $period = Period::instance();
        $yearMonth = $period->getYearMonth($periodNum);

        $filter = $this->filterCondition([
            'userName' => 'UI.USER_NAME',
            'periodNum' => 'CTG.PERIOD_NUM',
        ]);
        $condition = $filter['condition'];
        $params = $filter['params'];

        $listObj = new TraceUpTgList();
        $data = $listObj->getList(['condition'=>$condition, 'params'=>$params, 'others'=>['yearMonth'=>$yearMonth, 'periodNum'=>$periodNum]]);

        return static::notice($data);
    }

    /**
     * 推广奖向上追溯导出
     * @return mixed
     * @throws \yii\db\Exception
     * @throws HttpException
     */
    public function actionTraceUpTgExport() {
        $periodNum = Yii::$app->request->get('periodNum');
        if (!$periodNum) return static::notice([]);
        $period = Period::instance();
        $yearMonth = $period->getYearMonth($periodNum);

        $filter = $this->filterCondition([
            'userName' => 'UI.USER_NAME',
            'periodNum' => 'CTG.PERIOD_NUM',
        ]);
        $form = new BonusExportForm();
        $result = $form->run(array_merge($filter, ['others'=>['yearMonth'=>$yearMonth, 'periodNum'=>$periodNum]]), '荣衔奖向上追溯');
        if (!$result) {
            return static::notice(Form::formatErrorsForApi($form->getErrors()), 400);
        }
        return static::notice(Yii::t('ctx', 'startExporting')); // 导出开始，请到文件管理-导出文件查看
    }

    /**
     * 消费奖向上追溯
     * @return mixed
     * @throws \yii\base\Exception
     * @throws \yii\db\Exception
     * @throws HttpException
     */
    public function actionTraceUpXf() {
        $periodNum = Yii::$app->request->get('periodNum');
        if (!$periodNum) return static::notice([]);
        $period = Period::instance();
        $yearMonth = $period->getYearMonth($periodNum);

        $filter = $this->filterCondition([
            'userName' => 'UI.USER_NAME',
            'periodNum' => 'CXF.PERIOD_NUM',
        ]);
        $condition = $filter['condition'];
        $params = $filter['params'];

        $listObj = new TraceUpXfList();
        $data = $listObj->getList(['condition'=>$condition, 'params'=>$params, 'others'=>['yearMonth'=>$yearMonth, 'periodNum'=>$periodNum]]);

        return static::notice($data);
    }

    /**
     * 推广奖向上追溯导出
     * @return mixed
     * @throws \yii\db\Exception
     * @throws HttpException
     */
    public function actionTraceUpXfExport() {
        $periodNum = Yii::$app->request->get('periodNum');
        if (!$periodNum) return static::notice([]);
        $period = Period::instance();
        $yearMonth = $period->getYearMonth($periodNum);

        $filter = $this->filterCondition([
            'userName' => 'UI.USER_NAME',
            'periodNum' => 'CXF.PERIOD_NUM',
        ]);
        $form = new BonusExportForm();
        $result = $form->run(array_merge($filter, ['others'=>['yearMonth'=>$yearMonth, 'periodNum'=>$periodNum]]), '荣衔奖向上追溯');
        if (!$result) {
            return static::notice(Form::formatErrorsForApi($form->getErrors()), 400);
        }
        return static::notice(Yii::t('ctx', 'startExporting')); // 导出开始，请到文件管理-导出文件查看
    }

    /**
     * 业绩奖向上追溯
     * @return mixed
     * @throws \yii\base\Exception
     * @throws \yii\db\Exception
     * @throws HttpException
     */
    public function actionTraceUpYj() {
        $periodNum = Yii::$app->request->get('periodNum');
        if (!$periodNum) return static::notice([]);
        $period = Period::instance();
        $yearMonth = $period->getYearMonth($periodNum);

        $filter = $this->filterCondition([
            'userName' => 'UI.USER_NAME',
            'periodNum' => 'CYJ.PERIOD_NUM',
        ]);
        $condition = $filter['condition'];
        $params = $filter['params'];

        $listObj = new TraceUpYjList();
        $data = $listObj->getList(['condition'=>$condition, 'params'=>$params, 'others'=>['yearMonth'=>$yearMonth, 'periodNum'=>$periodNum]]);

        return static::notice($data);
    }

    /**
     * 业绩奖向上追溯导出
     * @return mixed
     * @throws \yii\db\Exception
     * @throws HttpException
     */
    public function actionTraceUpYjExport() {
        $periodNum = Yii::$app->request->get('periodNum');
        if (!$periodNum) return static::notice([]);
        $period = Period::instance();
        $yearMonth = $period->getYearMonth($periodNum);

        $filter = $this->filterCondition([
            'userName' => 'UI.USER_NAME',
            'periodNum' => 'CYJ.PERIOD_NUM',
        ]);
        $form = new BonusExportForm();
        $result = $form->run(array_merge($filter, ['others'=>['yearMonth'=>$yearMonth, 'periodNum'=>$periodNum]]), '荣衔奖向上追溯');
        if (!$result) {
            return static::notice(Form::formatErrorsForApi($form->getErrors()), 400);
        }
        return static::notice(Yii::t('ctx', 'startExporting')); // 导出开始，请到文件管理-导出文件查看
    }

    /**
     * 共享奖向上追溯
     * @return mixed
     * @throws \yii\base\Exception
     * @throws \yii\db\Exception
     * @throws HttpException
     */
    public function actionTraceUpGx() {
        $periodNum = Yii::$app->request->get('periodNum');
        if (!$periodNum) return static::notice([]);
        $period = Period::instance();
        $yearMonth = $period->getYearMonth($periodNum);

        $filter = $this->filterCondition([
            'userName' => 'UI.USER_NAME',
            'periodNum' => 'CGX.PERIOD_NUM',
        ]);
        $condition = $filter['condition'];
        $params = $filter['params'];

        $listObj = new TraceUpGxList();
        $data = $listObj->getList(['condition'=>$condition, 'params'=>$params, 'others'=>['yearMonth'=>$yearMonth, 'periodNum'=>$periodNum]]);

        return static::notice($data);
    }

    /**
     * 共享奖向上追溯导出
     * @return mixed
     * @throws \yii\db\Exception
     * @throws HttpException
     */
    public function actionTraceUpGxExport() {
        $periodNum = Yii::$app->request->get('periodNum');
        if (!$periodNum) return static::notice([]);
        $period = Period::instance();
        $yearMonth = $period->getYearMonth($periodNum);

        $filter = $this->filterCondition([
            'userName' => 'UI.USER_NAME',
            'periodNum' => 'CGX.PERIOD_NUM',
        ]);
        $form = new BonusExportForm();
        $result = $form->run(array_merge($filter, ['others'=>['yearMonth'=>$yearMonth, 'periodNum'=>$periodNum]]), '荣衔奖向上追溯');
        if (!$result) {
            return static::notice(Form::formatErrorsForApi($form->getErrors()), 400);
        }
        return static::notice(Yii::t('ctx', 'startExporting')); // 导出开始，请到文件管理-导出文件查看
    }

    /**
     * 管理奖向上追溯
     * @return mixed
     * @throws \yii\base\Exception
     * @throws \yii\db\Exception
     * @throws HttpException
     */
    public function actionTraceUpGl() {
        $periodNum = Yii::$app->request->get('periodNum');
        if (!$periodNum) return static::notice([]);
        $period = Period::instance();
        $yearMonth = $period->getYearMonth($periodNum);

        $filter = $this->filterCondition([
            'userName' => 'UI.USER_NAME',
            'periodNum' => 'CGL.PERIOD_NUM',
        ]);
        $condition = $filter['condition'];
        $params = $filter['params'];

        $listObj = new TraceUpGlList();
        $data = $listObj->getList(['condition'=>$condition, 'params'=>$params, 'others'=>['yearMonth'=>$yearMonth, 'periodNum'=>$periodNum]]);

        return static::notice($data);
    }

    /**
     * 管理奖向上追溯导出
     * @return mixed
     * @throws \yii\db\Exception
     * @throws HttpException
     */
    public function actionTraceUpGlExport() {
        $periodNum = Yii::$app->request->get('periodNum');
        if (!$periodNum) return static::notice([]);
        $period = Period::instance();
        $yearMonth = $period->getYearMonth($periodNum);

        $filter = $this->filterCondition([
            'userName' => 'UI.USER_NAME',
            'periodNum' => 'CGL.PERIOD_NUM',
        ]);
        $form = new BonusExportForm();
        $result = $form->run(array_merge($filter, ['others'=>['yearMonth'=>$yearMonth, 'periodNum'=>$periodNum]]), '荣衔奖向上追溯');
        if (!$result) {
            return static::notice(Form::formatErrorsForApi($form->getErrors()), 400);
        }
        return static::notice(Yii::t('ctx', 'startExporting')); // 导出开始，请到文件管理-导出文件查看
    }


    /**
     * 复销奖向下追溯
     * @return mixed
     * @throws \yii\base\Exception
     * @throws \yii\db\Exception
     * @throws HttpException
     */
    public function actionTraceDownFx() {
        $periodNum = Yii::$app->request->get('periodNum');
        if (!$periodNum) return static::notice([]);
        $period = Period::instance();
        $yearMonth = $period->getYearMonth($periodNum);

        $filter = $this->filterCondition([
            'userName' => 'UI.USER_NAME',
            'periodNum' => 'CBF.PERIOD_NUM',
        ]);
        $condition = $filter['condition'];
        $params = $filter['params'];

        $listObj = new TraceDownFxList();
        $data = $listObj->getList(['condition'=>$condition, 'params'=>$params, 'others'=>['yearMonth'=>$yearMonth, 'periodNum'=>$periodNum]]);

        return static::notice($data);
    }

    /**
     * 复销奖向下追溯导出
     * @return mixed
     * @throws \yii\db\Exception
     * @throws HttpException
     */
    public function actionTraceDownFxExport() {
        $periodNum = Yii::$app->request->get('periodNum');
        if (!$periodNum) return static::notice([]);
        $period = Period::instance();
        $yearMonth = $period->getYearMonth($periodNum);

        $filter = $this->filterCondition([
            'userName' => 'UI.USER_NAME',
            'periodNum' => 'CBF.PERIOD_NUM',
        ]);
        $form = new BonusExportForm();
        $result = $form->run(array_merge($filter, ['others'=>['yearMonth'=>$yearMonth, 'periodNum'=>$periodNum]]), '复销奖向下追溯');
        if (!$result) {
            return static::notice(Form::formatErrorsForApi($form->getErrors()), 400);
        }
        return static::notice(Yii::t('ctx', 'startExporting')); // 导出开始，请到文件管理-导出文件查看
    }

    /**
     * 复销奖向上追溯
     * @return mixed
     * @throws \yii\base\Exception
     * @throws \yii\db\Exception
     * @throws HttpException
     */
    public function actionTraceUpFx() {
        $periodNum = Yii::$app->request->get('periodNum');
        if (!$periodNum) return static::notice([]);
        $period = Period::instance();
        $yearMonth = $period->getYearMonth($periodNum);

        $filter = $this->filterCondition([
            'userName' => 'UI.USER_NAME',
            'periodNum' => 'CBF.PERIOD_NUM',
        ]);
        $condition = $filter['condition'];
        $params = $filter['params'];

        $listObj = new TraceUpFxList();
        $data = $listObj->getList(['condition'=>$condition, 'params'=>$params, 'others'=>['yearMonth'=>$yearMonth, 'periodNum'=>$periodNum]]);

        return static::notice($data);
    }

    /**
     * 复销奖向上追溯导出
     * @return mixed
     * @throws \yii\db\Exception
     * @throws HttpException
     */
    public function actionTraceUpFxExport() {
        $periodNum = Yii::$app->request->get('periodNum');
        if (!$periodNum) return static::notice([]);
        $period = Period::instance();
        $yearMonth = $period->getYearMonth($periodNum);

        $filter = $this->filterCondition([
            'userName' => 'UI.USER_NAME',
            'periodNum' => 'CBF.PERIOD_NUM',
        ]);
        $form = new BonusExportForm();
        $result = $form->run(array_merge($filter, ['others'=>['yearMonth'=>$yearMonth, 'periodNum'=>$periodNum]]), '复销奖向上追溯');
        if (!$result) {
            return static::notice(Form::formatErrorsForApi($form->getErrors()), 400);
        }
        return static::notice(Yii::t('ctx', 'startExporting')); // 导出开始，请到文件管理-导出文件查看
    }

    /**
     * 核算会员区域津贴
     * @return mixed
     * @throws \yii\db\Exception
     * @throws HttpException
     */
    public function actionCalcQy() {
        $periodNum = Yii::$app->request->get('periodNum');
        $userName = Yii::$app->request->get('userName');
        $period = Period::instance();
        $yearMonth = $period->getYearMonth($periodNum);
        if (!$userId = Info::getUserIdByUserName($userName)) {
            return static::notice('Member does not exist', 400); // 会员不存在
        }
        $data = CalcBonus::findUseSlaves()->yearMonth($yearMonth)->where('PERIOD_NUM=:PERIOD_NUM AND USER_ID=:USER_ID', [':PERIOD_NUM' => $periodNum, ':USER_ID' => $userId])->asArray()->one();
        if ($data) {
            $data['REAL_NAME'] = Info::getUserRealNameByUserId($userId);
            $data['LAST_DEC_LV_NAME'] = Cache::getDecLevelConfig()[$data['LAST_DEC_LV']]['LEVEL_NAME']??'';
            $data['LAST_EMP_LV_NAME'] = Cache::getEmpLevelConfig()[$data['LAST_EMP_LV']]['LEVEL_NAME']??'';
            $data['LAST_STATUS_NAME'] = Tool::paramConvert(Yii::$app->params['userStatus'])[$data['LAST_STATUS']]['label']??'';
            $data['AUDIT_STATUS'] = false;
            if ($resendQy = ResendQYAudit::find()->where('USER_ID=:USER_ID AND PERIOD_NUM=:PERIOD_NUM AND (AUDIT_STATUS=:AUDIT_STATUS_UN OR AUDIT_STATUS=:AUDIT_STATUS_TRUE)', [':USER_ID' => $userId, ':PERIOD_NUM' => $periodNum, ':AUDIT_STATUS_UN' => \Yii::$app->params['auditStatus']['un']['value'], ':AUDIT_STATUS_TRUE' => \Yii::$app->params['auditStatus']['true']['value']])->asArray()->one()) {
                $data['AUDIT_STATUS'] = $resendQy['AUDIT_STATUS'];
            }
        }
        return static::notice($data);
    }

    /**
     * 补发区域津贴列表
     * @return mixed
     * @throws \yii\base\Exception
     * @throws HttpException
     */
    public function actionResendQyList() {
        $filter = $this->filterCondition([
            'userIds' => 'USER_ID',
            'filterStatus' => 'RESEND_STATUS',
            'PERIOD_NUM' => 'PERIOD_NUM',
            'LAST_DEC_LV_NAME' => 'LAST_DEC_LV',
            'LAST_EMP_LV_NAME' => 'LAST_EMP_LV',
            'LAST_STATUS_NAME' => 'LAST_STATUS',
            'LAST_SUB_COM_NAME' => 'LAST_SUB_COM_ID',
            'LAST_AREA_NAME' => [
                'FIELD' => ['LAST_PROVINCE', 'LAST_CITY', 'LAST_COUNTY'],
                'BIND' => ['LAST_PROVINCE', 'LAST_CITY', 'LAST_COUNTY'],
            ],
            'LAST_SYSTEM_NAME' => 'LAST_SYSTEM_ID',
            'BONUS_QY' => 'BONUS_QY',
            'SHOULD_QY' => 'SHOULD_QY',
            'QY_1L' => 'QY_1L',
            'SURPLUS_1L' => 'SURPLUS_1L',
            'QY_2L' => 'QY_2L',
            'SURPLUS_2L' => 'SURPLUS_2L',
            'QY_3L' => 'QY_3L',
            'SURPLUS_3L' => 'SURPLUS_3L',
            'QY_4L' => 'QY_4L',
            'SURPLUS_4L' => 'SURPLUS_4L',
            'QY_5L' => 'QY_5L',
            'SURPLUS_5L' => 'SURPLUS_5L',
            'SURPLUS_LS' => 'SURPLUS_LS',
            'QY_LS' => 'QY_LS',
        ]);
        $condition = $filter['condition'];
        $params = $filter['params'];
        $listObj = new ResendQyList();
        $data = $listObj->getList(['condition'=>$condition, 'params'=>$params]);
        return static::notice($data);
    }

    /**
     * 区域津贴补发列表导出
     * @return mixed
     * @throws \yii\db\Exception
     * @throws HttpException
     */
    public function actionResendQyListExport() {
        $filter = $this->filterCondition([
            'userIds' => 'USER_ID',
            'filterStatus' => 'RESEND_STATUS',
            'PERIOD_NUM' => 'PERIOD_NUM',
            'LAST_DEC_LV_NAME' => 'LAST_DEC_LV',
            'LAST_EMP_LV_NAME' => 'LAST_EMP_LV',
            'LAST_STATUS_NAME' => 'LAST_STATUS',
            'LAST_SUB_COM_NAME' => 'LAST_SUB_COM_ID',
            'LAST_AREA_NAME' => [
                'FIELD' => ['LAST_PROVINCE', 'LAST_CITY', 'LAST_COUNTY'],
                'BIND' => ['LAST_PROVINCE', 'LAST_CITY', 'LAST_COUNTY'],
            ],
            'LAST_SYSTEM_NAME' => 'LAST_SYSTEM_ID',
            'BONUS_QY' => 'BONUS_QY',
            'SHOULD_QY' => 'SHOULD_QY',
            'QY_1L' => 'QY_1L',
            'SURPLUS_1L' => 'SURPLUS_1L',
            'QY_2L' => 'QY_2L',
            'SURPLUS_2L' => 'SURPLUS_2L',
            'QY_3L' => 'QY_3L',
            'SURPLUS_3L' => 'SURPLUS_3L',
            'QY_4L' => 'QY_4L',
            'SURPLUS_4L' => 'SURPLUS_4L',
            'QY_5L' => 'QY_5L',
            'SURPLUS_5L' => 'SURPLUS_5L',
            'SURPLUS_LS' => 'SURPLUS_LS',
            'QY_LS' => 'QY_LS',
        ]);
        $form = new BonusExportForm();
        $result = $form->run($filter, '区域津贴补发列表');
        if (!$result) {
            return static::notice(Form::formatErrorsForApi($form->getErrors()), 400);
        }
        return static::notice(Yii::t('ctx', 'startExporting')); // 导出开始，请到文件管理-导出文件查看
    }

    /**
     * 补发区域津贴
     * @return mixed
     * @throws \yii\db\Exception
     * @throws HttpException
     */
    public function actionResendQy() {
        if (Yii::$app->request->isPost) {
            $formModel = new ResendQYForm();
            $formModel->scenario = 'apply';
            if ($formModel->load(Yii::$app->request->post(), '') && $result = $formModel->apply()) {
                if($result['logs']){
                    /*foreach ($result['logs'] as $k => $value) {
                        $userName = Info::getUserNameByUserId($value);
                        // Log::adminHandle('申请为会员' . $userName . '发放补发区域津贴', 0, $value, $userName);
                    }*/
                    return static::notice('申请成功，请在补发区域津贴审核管理中审核');
                }else{
                    return static::notice('没有符合补发条件的记录',400);
                }
            } else {
                return static::notice(Form::formatErrorsForApi($formModel->getErrors()), 400);
            }
        }
        return static::notice('Illegal request', 400); // 非法请求
    }

    /**
     * 补发审核表
     * @return mixed
     * @throws \yii\base\Exception
     * @throws HttpException
     */
    public function actionResendQyAuditList() {
        $filter = $this->filterCondition([
            'userIds' => 'RQYA.USER_ID',
            'filterStatus' => 'RQYA.AUDIT_STATUS',
            'SHOULD_BONUS' => 'RQYA.SHOULD_BONUS',
            'PERIOD_NUM' => 'RQYA.PERIOD_NUM',
            'CREATED_AT' => 'RQYA.CREATED_AT',
            'AUDITED_AT' => 'RQYA.AUDITED_AT',
            'RESEND_REMARK' => 'RQYA.RESEND_REMARK',
            'CREATE_ADMIN_NAME' => 'ADMC.ADMIN_NAME',
            'AUDIT_ADMIN_NAME' => 'ADMA.ADMIN_NAME',
        ]);
        $condition = $filter['condition'];
        $params = $filter['params'];
        $listObj = new ResendQyAuditList();
        $data = $listObj->getList(['condition'=>$condition, 'params'=>$params]);
        return static::notice($data);
    }

    /**
     * 补发审核表导出
     * @return mixed
     * @throws \yii\db\Exception
     * @throws HttpException
     */
    public function actionResendQyAuditListExport() {
        $filter = $this->filterCondition([
            'userIds' => 'RQYA.USER_ID',
            'filterStatus' => 'RQYA.AUDIT_STATUS',
            'SHOULD_BONUS' => 'RQYA.SHOULD_BONUS',
            'PERIOD_NUM' => 'RQYA.PERIOD_NUM',
            'CREATED_AT' => 'RQYA.CREATED_AT',
            'AUDITED_AT' => 'RQYA.AUDITED_AT',
            'RESEND_REMARK' => 'RQYA.RESEND_REMARK',
            'CREATE_ADMIN_NAME' => 'ADMC.ADMIN_NAME',
            'AUDIT_ADMIN_NAME' => 'ADMA.ADMIN_NAME',
        ]);
        $form = new BonusExportForm();
        $result = $form->run($filter, '补发区域津贴审核列表');
        if (!$result) {
            return static::notice(Form::formatErrorsForApi($form->getErrors()), 400);
        }
        return static::notice(Yii::t('ctx', 'startExporting')); // 导出开始，请到文件管理-导出文件查看
    }

    /**
     * 审核补发区域津贴
     * @return mixed
     * @throws \yii\db\Exception
     * @throws HttpException
     */
    public function actionResendQyAudit() {
        $formModel = new ResendQYForm();
        $formModel->scenario = 'audit';
        if ($formModel->load(Yii::$app->request->post(), '') && $result = $formModel->audit()) {
            /*foreach ($result['logs'] as $k => $value) {
                $userName = Info::getUserNameByUserId($value);
                // Log::adminHandle('审核为会员' . $userName . '补发区域津贴', 1, $value, $userName);
            }*/
            return static::notice('审核补发区域津贴成功');
        }
        return static::notice(Form::formatErrorsForApi($formModel->getErrors()), 400);
    }

    /**
     * 删除补发区域津贴审核信息
     * @return mixed
     * @throws \yii\db\Exception
     * @throws HttpException
     */
    public function actionResendQyAuditDelete() {
        $resendQYForm = new ResendQYForm();
        $result = static::delete(ResendQYAudit::class, function ($selected) use ($resendQYForm) {
            $resendQYForm->beforeDelete($selected);
            // Log::adminHandle('删除审核会员余额信息');
        }, function ($selected) use ($resendQYForm) {
            $resendQYForm->delete($selected);
            // Log::adminHandle('删除审核会员余额信息');
        }, true);
        return $result;
    }

    /**
     * 余额列表
     * @return mixed
     * @throws \yii\base\Exception
     * @throws HttpException
     */
    public function actionBalanceList() {
        $filter = $this->filterCondition([
            'USER_NAME' => 'UI.USER_NAME',
            'REAL_NAME' => 'UI.REAL_NAME',
            'IS_DEC' => 'UI.IS_DEC',
        ]);
        $condition = $filter['condition'];
        $params = $filter['params'];

        $isSuper = AdminRole::isSuperAdmin(\Yii::$app->getUser()->getUserInfo()['roleId']);
        if (!$isSuper) {
            $adminId = Yii::$app->getUser()->getUserInfo()['id'];
            $adminCountry = AdminCountry::getCountry($adminId);
            $quotedAdminCountry = array_map(function($item) {
                return "'" . addslashes($item) . "'";
            }, $adminCountry);

            $condition .= " AND UI.COUNTRY_ID IN (" . implode(',', $quotedAdminCountry) . ")";
        }

        $listObj = new BalanceList();
        $data = $listObj->getList(['condition'=>$condition, 'params'=>$params]);
        return static::notice($data);
    }

    /**
     * 会员奖金余额导出
     * @return mixed
     * @throws \yii\db\Exception
     * @throws HttpException
     */
    public function actionBalanceExport() {
        $filter = $this->filterCondition([
            'USER_NAME' => 'UI.USER_NAME',
            'REAL_NAME' => 'UI.REAL_NAME',
            'IS_DEC' => 'UI.IS_DEC',
        ]);

        $condition = $filter['condition'];
        $params = $filter['params'];

        $isSuper = AdminRole::isSuperAdmin(\Yii::$app->getUser()->getUserInfo()['roleId']);
        if (!$isSuper) {
            $adminId = Yii::$app->getUser()->getUserInfo()['id'];
            $adminCountry = AdminCountry::getCountry($adminId);
            $quotedAdminCountry = array_map(function($item) {
                return "'" . addslashes($item) . "'";
            }, $adminCountry);

            $condition .= " AND UI.COUNTRY_ID IN (" . implode(',', $quotedAdminCountry) . ")";
        }

        $form = new BonusExportForm();
        $result = $form->run(['condition'=>$condition, 'params'=>$params], '会员奖金余额');
        if (!$result) {
            return static::notice(Form::formatErrorsForApi($form->getErrors()), 400);
        }
        return static::notice(Yii::t('ctx', 'startExporting')); // 导出开始，请到文件管理-导出文件查看
    }

    /**
     * 报单中心店补/补贴追溯
     * @return mixed
     * @throws \yii\base\Exception
     * @throws HttpException
     */
    public function actionTraceBt() {
        $startPeriodNum = Yii::$app->request->get('startPeriodNum');
        $endPeriodNum = Yii::$app->request->get('endPeriodNum');
        if (!$startPeriodNum || !$endPeriodNum) return static::notice([]);
        $filterStatus = Yii::$app->request->get('filterStatus');
        $filter = $this->filterCondition([
            'userName' => 'UI.USER_NAME',
        ]);
        $condition = $filter['condition'];
        $params = $filter['params'];
        if ($startPeriodNum) {
            $condition .= ' AND CBT.PERIOD_NUM>=:START_PERIOD_NUM';
            $params[':START_PERIOD_NUM'] = $startPeriodNum;
        }
        if ($endPeriodNum) {
            $condition .= ' AND CBT.PERIOD_NUM<=:END_PERIOD_NUM';
            $params[':END_PERIOD_NUM'] = $endPeriodNum;
        }
        if ($filterStatus) {
            $condition .= ' AND CBT.BT_TYPE=:BT_TYPE';
            $params[':BT_TYPE'] = $filterStatus;
        }
        $listObj = new TraceBtList();
        $data = $listObj->getList(['condition'=>$condition, 'params'=>$params]);

        return static::notice($data);
    }

    /**
     * 报单中心补贴追溯导出
     * @return mixed
     * @throws \yii\db\Exception
     * @throws HttpException
     */
    public function actionTraceBtExport() {
        $startPeriodNum = Yii::$app->request->get('startPeriodNum');
        $endPeriodNum = Yii::$app->request->get('endPeriodNum');
        if (!$startPeriodNum || !$endPeriodNum) return static::notice([]);
        $filterStatus = Yii::$app->request->get('filterStatus');
        $filter = $this->filterCondition([
            'userName' => 'UI.USER_NAME',
        ]);
        $condition = $filter['condition'];
        $params = $filter['params'];
        if ($startPeriodNum) {
            $condition .= ' AND CBT.PERIOD_NUM>=:START_PERIOD_NUM';
            $params[':START_PERIOD_NUM'] = $startPeriodNum;
        }
        if ($endPeriodNum) {
            $condition .= ' AND CBT.PERIOD_NUM<=:END_PERIOD_NUM';
            $params[':END_PERIOD_NUM'] = $endPeriodNum;
        }
        if ($filterStatus) {
            $condition .= ' AND CBT.BT_TYPE=:BT_TYPE';
            $params[':BT_TYPE'] = $filterStatus;
        }
        $form = new BonusExportForm();
        $result = $form->run(['condition'=>$condition, 'params'=>$params], '报单中心补贴追溯');
        if (!$result) {
            return static::notice(Form::formatErrorsForApi($form->getErrors()), 400);
        }
        return static::notice(Yii::t('ctx', 'startExporting')); // 导出开始，请到文件管理-导出文件查看
    }

    /**
     * 报单中心货补追溯
     * @return mixed
     * @throws \yii\base\Exception
     * @throws HttpException
     */
    public function actionTraceFl() {
        $startPeriodNum = Yii::$app->request->get('startPeriodNum');
        $endPeriodNum = Yii::$app->request->get('endPeriodNum');
        if (!$startPeriodNum || !$endPeriodNum) return static::notice([]);
        $filter = $this->filterCondition([
            'userName' => 'UI.USER_NAME',
        ]);
        $condition = $filter['condition'];
        $params = $filter['params'];
        if ($startPeriodNum) {
            $condition .= ' AND CFL.PERIOD_NUM>=:START_PERIOD_NUM';
            $params[':START_PERIOD_NUM'] = $startPeriodNum;
        }
        if ($endPeriodNum) {
            $condition .= ' AND CFL.PERIOD_NUM<=:END_PERIOD_NUM';
            $params[':END_PERIOD_NUM'] = $endPeriodNum;
        }
        $listObj = new TraceFlList();
        $data = $listObj->getList(['condition'=>$condition, 'params'=>$params]);

        return static::notice($data);
    }

    /**
     * 报单中心货补追溯导出
     * @return mixed
     * @throws \yii\db\Exception
     * @throws HttpException
     */
    public function actionTraceFlExport() {
        $startPeriodNum = Yii::$app->request->get('startPeriodNum');
        $endPeriodNum = Yii::$app->request->get('endPeriodNum');
        if (!$startPeriodNum || !$endPeriodNum) return static::notice([]);
        $filter = $this->filterCondition([
            'userName' => 'UI.USER_NAME',
        ]);
        $condition = $filter['condition'];
        $params = $filter['params'];
        if ($startPeriodNum) {
            $condition .= ' AND CFL.PERIOD_NUM>=:START_PERIOD_NUM';
            $params[':START_PERIOD_NUM'] = $startPeriodNum;
        }
        if ($endPeriodNum) {
            $condition .= ' AND CFL.PERIOD_NUM<=:END_PERIOD_NUM';
            $params[':END_PERIOD_NUM'] = $endPeriodNum;
        }
        $form = new BonusExportForm();
        $result = $form->run(['condition'=>$condition, 'params'=>$params], '报单中心货补追溯');
        if (!$result) {
            return static::notice(Form::formatErrorsForApi($form->getErrors()), 400);
        }
        return static::notice(Yii::t('ctx', 'startExporting')); // 导出开始，请到文件管理-导出文件查看
    }

    /**
     * 区域业绩提成追溯
     * @return mixed
     * @throws \yii\base\Exception
     * @throws HttpException
     */
    public function actionTraceFw() {
        $startPeriodNum = Yii::$app->request->get('startPeriodNum');
        $endPeriodNum = Yii::$app->request->get('endPeriodNum');
        if (!$startPeriodNum || !$endPeriodNum) return static::notice([]);
        $filter = $this->filterCondition([
            'userName' => 'UI.USER_NAME',
        ]);
        $condition = $filter['condition'];
        $params = $filter['params'];
        if ($startPeriodNum) {
            $condition .= ' AND CFW.PERIOD_NUM>=:START_PERIOD_NUM';
            $params[':START_PERIOD_NUM'] = $startPeriodNum;
        }
        if ($endPeriodNum) {
            $condition .= ' AND CFW.PERIOD_NUM<=:END_PERIOD_NUM';
            $params[':END_PERIOD_NUM'] = $endPeriodNum;
        }
        $listObj = new TraceFwList();
        $data = $listObj->getList(['condition'=>$condition, 'params'=>$params]);

        return static::notice($data);
    }

    /**
     * 区域业绩提成导出
     * @return mixed
     * @throws \yii\db\Exception
     * @throws HttpException
     */
    public function actionTraceFwExport() {
        $startPeriodNum = Yii::$app->request->get('startPeriodNum');
        $endPeriodNum = Yii::$app->request->get('endPeriodNum');
        if (!$startPeriodNum || !$endPeriodNum) return static::notice([]);
        $filter = $this->filterCondition([
            'userName' => 'UI.USER_NAME',
        ]);
        $condition = $filter['condition'];
        $params = $filter['params'];
        if ($startPeriodNum) {
            $condition .= ' AND CFW.PERIOD_NUM>=:START_PERIOD_NUM';
            $params[':START_PERIOD_NUM'] = $startPeriodNum;
        }
        if ($endPeriodNum) {
            $condition .= ' AND CFW.PERIOD_NUM<=:END_PERIOD_NUM';
            $params[':END_PERIOD_NUM'] = $endPeriodNum;
        }
        $form = new BonusExportForm();
        $result = $form->run(['condition'=>$condition, 'params'=>$params], '区域业绩提成追溯');
        if (!$result) {
            return static::notice(Form::formatErrorsForApi($form->getErrors()), 400);
        }
        return static::notice(Yii::t('ctx', 'startExporting')); // 导出开始，请到文件管理-导出文件查看
    }

    /**
     * 业绩单
     * @return mixed
     * @throws \yii\base\Exception
     * @throws HttpException
     */
    public function actionPerfOrder(){
        $filter = $this->filterCondition([
            'PERIOD_NUM' => 'PO.PERIOD_NUM',
            'SN' => 'PO.SN',
            'DEC_STATUS_NAME' => 'PO.DEC_STATUS',
            'USER_NAME' => 'U.USER_NAME',
            'REAL_NAME' => 'U.REAL_NAME',
            'userIds' => 'PO.USER_ID',
            'filterType' => 'PO.DEC_TYPE',
            'LAST_REC_USER_NAME' => 'PO.LAST_REC_USER_NAME',
            'LAST_REC_REAL_NAME' => 'PO.LAST_REC_REAL_NAME',
            'PV' => 'PO.PV',
            'DEC_AMOUNT' => 'PO.DEC_AMOUNT',
            'DEC_SN' => 'PO.DEC_SN',
            'CREATED_AT' => 'PO.CREATED_AT',
        ]);
        $condition = $filter['condition'];
        $params = $filter['params'];

        $isSuper = AdminRole::isSuperAdmin(\Yii::$app->getUser()->getUserInfo()['roleId']);
        if (!$isSuper) {
            $adminId = Yii::$app->getUser()->getUserInfo()['id'];
            $adminCountry = AdminCountry::getCountry($adminId);
            $quotedAdminCountry = array_map(function($item) {
                return "'" . addslashes($item) . "'";
            }, $adminCountry);

            $condition .= " AND U.COUNTRY_ID IN (" . implode(',', $quotedAdminCountry) . ")";
        }

        $listObj = new PerfOrderList();
        $data = $listObj->getList(['condition'=>$condition, 'params'=>$params]);

        return static::notice($data);
    }

    /**
     * 业绩单导出
     * @return mixed
     * @throws \yii\db\Exception
     * @throws HttpException
     */
    public function actionPerfOrderExport() {
        $filter = $this->filterCondition([
            'PERIOD_NUM' => 'PO.PERIOD_NUM',
            'SN' => 'PO.SN',
            'DEC_STATUS_NAME' => 'PO.DEC_STATUS',
            'userIds' => 'PO.USER_ID',
            'filterType' => 'PO.DEC_TYPE',
            'LAST_REC_USER_NAME' => 'PO.LAST_REC_USER_NAME',
            'LAST_REC_REAL_NAME' => 'PO.LAST_REC_REAL_NAME',
            'PV' => 'PO.PV',
            'DEC_AMOUNT' => 'PO.DEC_AMOUNT',
            'DEC_SN' => 'PO.DEC_SN',
            'LAST_SUB_COM_NAME' => 'PO.LAST_SUB_COM_ID',
            'LAST_SYSTEM_NAME' => 'PO.LAST_SYSTEM_ID',
            'LAST_AREA' => [
                'FIELD' => ['PO.LAST_PROVINCE', 'PO.LAST_CITY', 'PO.LAST_COUNTY'],
                'BIND' => ['LAST_PROVINCE', 'LAST_CITY', 'LAST_COUNTY'],
            ],
            'DEC_USER_NAME' => 'DUI.USER_NAME',
            'LAST_DEC_DEC_LV_NAME' => 'PO.LAST_DEC_DEC_LV',
            'LAST_DEC_SUB_COM_NAME' => 'PO.LAST_DEC_SUB_COM_ID',
            'LAST_DEC_AREA' => [
                'FIELD' => ['PO.LAST_DEC_PROVINCE', 'PO.LAST_DEC_CITY', 'PO.LAST_DEC_COUNTY'],
                'BIND' => ['LAST_DEC_PROVINCE', 'LAST_DEC_CITY', 'LAST_DEC_COUNTY'],
            ],
            'CREATED_AT' => 'PO.CREATED_AT',
            'CLOSED_AT' => 'PO.CLOSED_AT',
        ]);
        $condition = $filter['condition'];
        $params = $filter['params'];

        $isSuper = AdminRole::isSuperAdmin(\Yii::$app->getUser()->getUserInfo()['roleId']);
        if (!$isSuper) {
            $adminId = Yii::$app->getUser()->getUserInfo()['id'];
            $adminCountry = AdminCountry::getCountry($adminId);
            $quotedAdminCountry = array_map(function($item) {
                return "'" . addslashes($item) . "'";
            }, $adminCountry);

            $condition .= " AND U.COUNTRY_ID IN (" . implode(',', $quotedAdminCountry) . ")";
        }

        $form = new BonusExportForm();
        $result = $form->run(['condition'=>$condition, 'params'=>$params], '业绩单');
        if (!$result) {
            return static::notice(Form::formatErrorsForApi($form->getErrors()), 400);
        }
        return static::notice(Yii::t('ctx', 'startExporting')); // 导出开始，请到文件管理-导出文件查看
    }

    /**
     * 月积分
     * @return mixed
     * @throws \yii\base\Exception
     * @throws HttpException
     */
    public function actionScoreMonth(){
        $filter = $this->filterCondition([
            'USER_NAME' => 'U.USER_NAME',
            'userIds' => 'SM.USER_ID',
            'CALC_MONTH' => 'SM.CALC_MONTH',
            'CREATED_AT' => 'SM.CREATED_AT',
        ]);
        $condition = $filter['condition'];
        $params = $filter['params'];
        $listObj = new ScoreMonthList();
        $data = $listObj->getList(['condition'=>$condition, 'params'=>$params]);

        return static::notice($data);
    }

    /**
     * 月积分导出
     * @return mixed
     * @throws \yii\db\Exception
     * @throws HttpException
     */
    public function actionScoreMonthExport() {
        $filter = $this->filterCondition([
            'USER_NAME' => 'U.USER_NAME',
            'userIds' => 'SM.USER_ID',
            'CALC_MONTH' => 'SM.CALC_MONTH',
            'CREATED_AT' => 'SM.CREATED_AT',
        ]);
        $condition = $filter['condition'];
        $params = $filter['params'];
        $form = new BonusExportForm();
        $result = $form->run(['condition'=>$condition, 'params'=>$params], '月积分');
        if (!$result) {
            return static::notice(Form::formatErrorsForApi($form->getErrors()), 400);
        }
        return static::notice(Yii::t('ctx', 'startExporting')); // 导出开始，请到文件管理-导出文件查看
    }

    /**
     * 月业绩
     * @return mixed
     * @throws \yii\base\Exception
     * @throws HttpException
     */
    public function actionPerfMonth(){
        $filter = $this->filterCondition([
            'USER_NAME' => 'U.USER_NAME',
            'REC_USER_NAME' => 'RU.USER_NAME',
            'userIds' => 'PM.USER_ID',
            'CALC_MONTH' => 'PM.CALC_MONTH',
            'CREATED_AT' => 'PM.CREATED_AT',
        ]);
        $condition = $filter['condition'];
        $params = $filter['params'];

        $isSuper = AdminRole::isSuperAdmin(\Yii::$app->getUser()->getUserInfo()['roleId']);
        if (!$isSuper) {
            $adminId = Yii::$app->getUser()->getUserInfo()['id'];
            $adminCountry = AdminCountry::getCountry($adminId);
            $quotedAdminCountry = array_map(function($item) {
                return "'" . addslashes($item) . "'";
            }, $adminCountry);

            $condition .= " AND U.COUNTRY_ID IN (" . implode(',', $quotedAdminCountry) . ")";
        }

        $listObj = new PerfMonthList();
        $data = $listObj->getList(['condition'=>$condition, 'params'=>$params]);

        return static::notice($data);
    }

    /**
     * 月业绩导出
     * @return mixed
     * @throws \yii\db\Exception
     * @throws HttpException
     */
    public function actionPerfMonthExport() {
        $filter = $this->filterCondition([
            'USER_NAME' => 'U.USER_NAME',
            'REC_USER_NAME' => 'RU.USER_NAME',
            'userIds' => 'PM.USER_ID',
            'CALC_MONTH' => 'PM.CALC_MONTH',
            'CREATED_AT' => 'PM.CREATED_AT',
        ]);
        $condition = $filter['condition'];
        $params = $filter['params'];

        $isSuper = AdminRole::isSuperAdmin(\Yii::$app->getUser()->getUserInfo()['roleId']);
        if (!$isSuper) {
            $adminId = Yii::$app->getUser()->getUserInfo()['id'];
            $adminCountry = AdminCountry::getCountry($adminId);
            $quotedAdminCountry = array_map(function($item) {
                return "'" . addslashes($item) . "'";
            }, $adminCountry);

            $condition .= " AND U.COUNTRY_ID IN (" . implode(',', $quotedAdminCountry) . ")";
        }

        $form = new BonusExportForm();
        $result = $form->run(['condition'=>$condition, 'params'=>$params], '月业绩');
        if (!$result) {
            return static::notice(Form::formatErrorsForApi($form->getErrors()), 400);
        }
        return static::notice(Yii::t('ctx', 'startExporting')); // 导出开始，请到文件管理-导出文件查看
    }

    /**
     * 期业绩
     * @return mixed
     * @throws \yii\base\Exception
     * @throws HttpException
     */
    public function actionPerfPeriodList(){
        $filter = $this->filterCondition([
            'USER_NAME' => 'U.USER_NAME',
            'userIds' => 'PP.USER_ID',
            'PERIOD_NUM' => 'PP.PERIOD_NUM',
            'CREATED_AT' => 'PP.CREATED_AT',
        ]);
        $condition = $filter['condition'];
        $params = $filter['params'];
        $listObj = new PerfPeriodList();
        $data = $listObj->getList(['condition'=>$condition, 'params'=>$params]);

        return static::notice($data);
    }

    /**
     * 期业绩导出
     * @return mixed
     * @throws \yii\db\Exception
     * @throws HttpException
     */
    public function actionPerfPeriodListExport() {
        $filter = $this->filterCondition([
            'USER_NAME' => 'U.USER_NAME',
            'userIds' => 'PP.USER_ID',
            'PERIOD_NUM' => 'PP.PERIOD_NUM',
            'CREATED_AT' => 'PP.CREATED_AT',
        ]);
        $condition = $filter['condition'];
        $params = $filter['params'];
        $form = new BonusExportForm();
        $result = $form->run(['condition'=>$condition, 'params'=>$params], '期业绩');
        if (!$result) {
            return static::notice(Form::formatErrorsForApi($form->getErrors()), 400);
        }
        return static::notice(Yii::t('ctx', 'startExporting')); // 导出开始，请到文件管理-导出文件查看
    }


    /**
     * 达标业绩
     * @return mixed
     * @throws \yii\base\Exception
     * @throws HttpException
     */
    public function actionPerfStandard(){
        $filter = $this->filterCondition([
            'USER_NAME' => 'U.USER_NAME',
            'REC_USER_NAME' => 'RU.USER_NAME',
            'userIds' => 'PS.USER_ID',
            'CALC_MONTH' => 'PS.CALC_MONTH',
            'CREATED_AT' => 'PS.CREATED_AT',
        ]);
        $condition = $filter['condition'];
        $params = $filter['params'];
        $listObj = new PerfStandardList();
        $data = $listObj->getList(['condition'=>$condition, 'params'=>$params]);

        return static::notice($data);
    }

    /**
     * 达标业绩导出
     * @return mixed
     * @throws \yii\db\Exception
     * @throws HttpException
     */
    public function actionPerfStandardExport() {
        $filter = $this->filterCondition([
            'USER_NAME' => 'U.USER_NAME',
            'REC_USER_NAME' => 'RU.USER_NAME',
            'userIds' => 'PS.USER_ID',
            'CALC_MONTH' => 'PS.CALC_MONTH',
            'CREATED_AT' => 'PS.CREATED_AT',
        ]);
        $condition = $filter['condition'];
        $params = $filter['params'];
        $form = new BonusExportForm();
        $result = $form->run(['condition'=>$condition, 'params'=>$params], '达标业绩');
        if (!$result) {
            return static::notice(Form::formatErrorsForApi($form->getErrors()), 400);
        }
        return static::notice(Yii::t('ctx', 'startExporting')); // 导出开始，请到文件管理-导出文件查看
    }


    /**
     * 会员实时业绩
     * @return mixed
     * @throws HttpException
     */
    public function actionRealTimePerf() {
        $userName = Yii::$app->request->get('userName');
        if (!$userId = Info::getUserIdByUserName($userName)) {
            return static::notice('Member does not exist', 400); // 会员不存在
        }
        $period = Period::instance();
        $newPerf = Perf::getPeriodNewPerf($userId);
        $weekData = [['PV_1L' => $newPerf['PV_1L'], 'PV_2L' => $newPerf['PV_2L'], 'PV_3L' => $newPerf['PV_3L'], 'PV_4L' => $newPerf['PV_4L'], 'PV_5L' => $newPerf['PV_5L'], 'PV_PCS'=>Tool::formatPrice($newPerf['PV_PCS']), 'PV_PSS'=>Tool::formatPrice($newPerf['PV_PSS'])]];
        $monthPerf = Perf::getMonthPerf($userId);
        $monthData = [['PV_1L' => Tool::formatPrice($monthPerf['PV_1L']), 'PV_2L' => Tool::formatPrice($monthPerf['PV_2L']), 'PV_3L' => Tool::formatPrice($monthPerf['PV_3L']), 'PV_4L' => Tool::formatPrice($monthPerf['PV_4L']), 'PV_5L' => Tool::formatPrice($monthPerf['PV_5L']), 'PV_PCS'=>Tool::formatPrice($monthPerf['PV_PCS']), 'PV_PSS'=>Tool::formatPrice($monthPerf['PV_PSS'])]];
        $lastMonth = PerfMonth::getMonthPerf($period->getLastMonth()['yearMonth'], $userId);
        $lastData = [['PV_1L' => Tool::formatPrice($lastMonth['PV_1L_TOTAL']), 'PV_2L' => Tool::formatPrice($lastMonth['PV_2L_TOTAL']), 'PV_3L' => Tool::formatPrice($lastMonth['PV_3L_TOTAL']), 'PV_4L' => Tool::formatPrice($lastMonth['PV_4L_TOTAL']), 'PV_5L' => Tool::formatPrice($lastMonth['PV_5L_TOTAL']), 'PV_PCS'=>Tool::formatPrice($lastMonth['PV_PCS']), 'PV_PSS'=>Tool::formatPrice($lastMonth['PV_PSS']), 'PV_PSS_TOTAL'=>Tool::formatPrice($lastMonth['PV_PSS_TOTAL'])]];
        //是否合格
//        $lastChkStatus = '';
//        $lastMonthPerfChk = Cache::getSystemConfig()['lastMonthPerfChk']['VALUE'];
//        $lastArr = [$lastMonth['PV_1L_TOTAL'], $lastMonth['PV_2L_TOTAL'], $lastMonth['PV_3L_TOTAL'], $lastMonth['PV_4L_TOTAL'], $lastMonth['PV_5L_TOTAL']];
//        if (array_sum($lastArr) >= $lastMonthPerfChk) {
//            $lastChkStatus = '已合格';
//        }
        //判断大区
//        $bigLocation = array_search(max($lastArr), $lastArr) + 1;
        return static::notice(['weekData' => $weekData, 'monthData' => $monthData, 'lastData' => $lastData]);
    }

    /**
     * 荣衔业绩
     * @return mixed
     * @throws HttpException
     */
    public function actionYcPerf() {
        $userName = Yii::$app->request->get('userName');
        if (!$userId = Info::getUserIdByUserName($userName)) {
            return static::notice('Member does not exist', 400); // 会员不存在
        }

        $userPerfData = Perf::getUserPerf($userId);
        $ycPerfList = [];
        //查找推荐的子会员
        $childrenList = UserInfo::findUseDbCalc()->select('USER_ID')->where('REC_UID=:REC_UID', [
            'REC_UID' => $userId
        ])->asArray()->all();
        $maxPvPss = 0;
        $childSumPerf = 0;
        foreach ($childrenList as $child) {
            $childBaseUser = Info::getBaseUserById($child['USER_ID']);
            if( !$childBaseUser ) continue;
            $childPerfData = Perf::getUserPerf($child['USER_ID']);
            if( !$childPerfData ) continue;

            $childPvPcs = $childPerfData['PV_PCS_ZC'] + $childPerfData['PV_PCS_FX'];

            $childPvPssTotal = $childPvPcs + $childPerfData['PV_PSS'];

            $childSumPerf += $childPvPssTotal;
            if( $childPvPssTotal >= $maxPvPss ) {
                $maxPvPss = $childPvPssTotal;
            }

            $ycPerfList[] = [
                'USER_NAME'=>$childBaseUser['USER_NAME'],
                'REAL_NAME'=>$childBaseUser['REAL_NAME'],
                'PV_PCS' => $childPerfData['PV_PCS_ZC'] + $childPerfData['PV_PCS_FX'],
                'PV_PSS_TOTAL' => $childPerfData['PV_PSS_TOTAL'],
                'MAX_DEPART_PERF' => '-',
                'OTHER_DEPART_PERF' => '-',
                'PV_PSS_SUM_TOTAL' => round($childPerfData['PV_PCS_ZC'] + $childPerfData['PV_PCS_FX'] + $childPerfData['PV_PSS_TOTAL'],2),
            ];
        }

        if( $userPerfData ) {
            $baseUser = Info::getBaseUserById($userId);
            $ycPerfData = [
                'USER_NAME'=>$baseUser['USER_NAME'],
                'REAL_NAME'=>$baseUser['REAL_NAME'],
                'PV_PCS' => $userPerfData['PV_PCS_ZC'] + $userPerfData['PV_PCS_FX'],
                'PV_PSS_TOTAL' => $userPerfData['PV_PSS_TOTAL'],
                'MAX_DEPART_PERF' => round($maxPvPss, 2),
                'OTHER_DEPART_PERF' => round($childSumPerf-$maxPvPss, 2),
                'PV_PSS_SUM_TOTAL' => round($userPerfData['PV_PCS_ZC'] + $userPerfData['PV_PCS_FX'] + $userPerfData['PV_PSS_TOTAL'],2),
            ];
            array_unshift($ycPerfList, $ycPerfData);
        }

        return static::notice(['ycPerfList' => $ycPerfList]);
    }

    /**
     * 用户业绩
     * @return mixed
     * @throws HttpException
     */
    public function actionUserPerf() {
        $filter = $this->filterCondition([
            'USER_NAME' => 'U.USER_NAME',
        ]);
        $condition = $filter['condition'];
        $params = $filter['params'];

        $isSuper = AdminRole::isSuperAdmin(\Yii::$app->getUser()->getUserInfo()['roleId']);
        if (!$isSuper) {
            $adminId = Yii::$app->getUser()->getUserInfo()['id'];
            $adminCountry = AdminCountry::getCountry($adminId);
            $quotedAdminCountry = array_map(function($item) {
                return "'" . addslashes($item) . "'";
            }, $adminCountry);

            $condition .= " AND U.COUNTRY_ID IN (" . implode(',', $quotedAdminCountry) . ")";
        }

        $listObj = new UserPerfList();
        $data = $listObj->getList(['condition'=>$condition, 'params'=>$params]);

        return static::notice($data);
    }

    /**
     * 月业绩导出
     * @return mixed
     * @throws \yii\db\Exception
     * @throws HttpException
     */
    public function actionUserPerfExport() {
        $filter = $this->filterCondition([
            'USER_NAME' => 'U.USER_NAME',
        ]);
        $condition = $filter['condition'];
        $params = $filter['params'];

        $isSuper = AdminRole::isSuperAdmin(\Yii::$app->getUser()->getUserInfo()['roleId']);
        if (!$isSuper) {
            $adminId = Yii::$app->getUser()->getUserInfo()['id'];
            $adminCountry = AdminCountry::getCountry($adminId);
            $quotedAdminCountry = array_map(function($item) {
                return "'" . addslashes($item) . "'";
            }, $adminCountry);

            $condition .= " AND U.COUNTRY_ID IN (" . implode(',', $quotedAdminCountry) . ")";
        }

        $form = new BonusExportForm();
        $result = $form->run(['condition'=>$condition, 'params'=>$params], '用户业绩');
        if (!$result) {
            return static::notice(Form::formatErrorsForApi($form->getErrors()), 400);
        }
        return static::notice(Yii::t('ctx', 'startExporting')); // 导出开始，请到文件管理-导出文件查看
    }

    /**
     * 自动提现（奖金）
     *
     */
    public function actionAutoWithdraw() {
        Cache::setWithdrawLock(0); // todo
        $withdrawLock = Cache::getWithdrawLock();
        if ($withdrawLock!=0){
            return static::notice(Form::formatErrorsForApi('已有进程在生成'), 400);
        }else{
            $formModel = new UserBonusForm();
            $formModel->scenario = 'autoWithdraw';
            if ($formModel->autoWithdrawWebToAsync()) {
                return static::notice('生成提现单 已开始处理，请等待');
            } else {
                return static::notice(Form::formatErrorsForApi($formModel->getErrors()), 400);
            }
        }
    }

    /**
     * 查询安置网下级会员和业绩、修改业绩
     * @return mixed|void
     * @throws HttpException
     * @throws \yii\base\Exception
     */
    public function actionPerfAdjustment()
    {
        // 查询业绩
        if (\Yii::$app->request->isGet) {
            $memberCode = Yii::$app->request->get('memberCode');
            $userId = Info::getUserIdByUserName(trim($memberCode));

            // 查询传入会员业绩
            $perfMarket = UserPerf::getPerfMarket($userId);
            if ($perfMarket) {
                // 查询传入会员的安置网下级
                $memberList = UserNetwork::getFirstFloorChildren($userId);
                $memberList = array_column($memberList, NULL, 'RELATIVE_LOCATION');
                // 左腿1市场
                $leftPerfUserName = (isset($memberList[1]) && $memberList[1]['USER_ID']) ? Info::getUserNameByUserId($memberList[1]['USER_ID']) : '';
                // 右腿2市场
                $rightPerfUserName = (isset($memberList[2]) && $memberList[2]['USER_ID']) ? Info::getUserNameByUserId($memberList[2]['USER_ID']) : '';

                // 会员编号
                $perfMarket['USER_NAME'] = $memberCode;
                $perfMarket['SURPLUS_1L_USER_NAME'] = $leftPerfUserName;
                $perfMarket['SURPLUS_2L_USER_NAME'] = $rightPerfUserName;

                return static::notice(['allData' => $perfMarket]);
            }

            return static::notice('No data', 400);
        } else if (\Yii::$app->request->isPost) {
            $formModel = new PerfAdjustmentForm();
            $formModel->scenario = 'perfAdjustment';
            $post = \Yii::$app->request->post();
            if ($formModel->load($post, '') && $formModel->perfAdjustment()) {
                return static::notice('Successful');
            } else {
                return static::notice(Form::formatErrorsForApi($formModel->getErrors()), 400);
            }
        } else {
            return static::notice('Illegal request', 400); // 非法请求
        }
    }
}
