<?php
/**
 * Created by PhpStorm.
 * User: leo
 * Date: 2018/3/9
 * Time: 上午11:56
 */
namespace console\controllers;

use backendApi\modules\v1\models\Admin;
use backendApi\modules\v1\models\AdminCountry;
use backendApi\modules\v1\models\AdminRole;
use backendApi\modules\v1\models\lists\bonus\BalanceList;
use backendApi\modules\v1\models\lists\bonus\CfLxAuditList;
use backendApi\modules\v1\models\lists\bonus\FlowBalanceList;
use backendApi\modules\v1\models\lists\bonus\FlowBonusList;
use backendApi\modules\v1\models\lists\bonus\FlowCfList;
use backendApi\modules\v1\models\lists\bonus\FlowExchangePointsList;
use backendApi\modules\v1\models\lists\bonus\FlowLxList;
use backendApi\modules\v1\models\lists\bonus\FlowReconsumePointsList;
use backendApi\modules\v1\models\lists\bonus\OtherPeriodBonusList;
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\ResendQyAuditList;
use backendApi\modules\v1\models\lists\bonus\ResendQyList;
use backendApi\modules\v1\models\lists\bonus\ScoreMonthList;
use backendApi\modules\v1\models\lists\bonus\TraceBtList;
use backendApi\modules\v1\models\lists\bonus\TraceDownBdList;
use backendApi\modules\v1\models\lists\bonus\TraceDownFxList;
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\TraceFlList;
use backendApi\modules\v1\models\lists\bonus\TraceFwList;
use backendApi\modules\v1\models\lists\bonus\TraceUpBdList;
use backendApi\modules\v1\models\lists\bonus\TraceUpFxList;
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\Excel;
use common\helpers\Form;
use common\helpers\http\RemoteUploadApi;
use common\helpers\Log;
use common\helpers\LoggerTool;
use common\libs\export\module\BonusExport;
use common\models\forms\PeriodForm;
use common\models\forms\UserPerformanceForm;
use common\models\forms\WithdrawForm;
use common\models\LogAsync;
use common\models\Withdraw;
use common\models\UserBonus;
use Yii;
use yii\db\Exception;

class BonusController extends BaseController
{

    /**
     * 生成业绩单并计算业绩
     * @param $taskKey
     * @throws Exception
     * @throws \yii\base\Exception
     */
    public function actionPerf($taskKey){
        $params = Cache::getAsyncParams($taskKey);
        $formModel = new PeriodForm();
        $handleResult = false;
        if($formModel->load($params, '') && $formModel->perfAsync($params['handleUserId'])){
            $handleResult = true;
            \Yii::$app->swooleAsyncTimer->pushAsyncResultToAdmin($params['handleUserId'], "第{$params['periodNum']}期生成业绩单更新成功");
        } else {
            \Yii::$app->swooleAsyncTimer->pushAsyncResultToAdmin($params['handleUserId'], "第{$params['periodNum']}期生成业绩单更新失败,原因：".Form::formatErrorsForApi($formModel->getErrors()), false);
        }
        if(!$handleResult){
            echo "第{$params['periodNum']}期生成业绩单更新失败，原因：".Form::formatErrorsForApi($formModel->getErrors());
        }
    }

    /**
     * 结算奖金
     * @param $taskKey
     * @throws Exception
     * @throws \yii\base\Exception
     */
    public function actionCalc($taskKey){
        $params = Cache::getAsyncParams($taskKey);
        $formModel = new PeriodForm();
        $handleResult = false;
        if($formModel->load($params, '') && $formModel->calcAsync($params['handleUserId'])){
            $handleResult = true;
            \Yii::$app->swooleAsyncTimer->pushAsyncResultToAdmin($params['handleUserId'], "第{$params['periodNum']}期奖金结算更新成功");
        } else {
            \Yii::$app->swooleAsyncTimer->pushAsyncResultToAdmin($params['handleUserId'], "第{$params['periodNum']}期奖金结算更新失败,原因：".Form::formatErrorsForApi($formModel->getErrors()), false);
        }
        if(!$handleResult){
            echo "第{$params['periodNum']}期奖金结算更新失败，原因：".Form::formatErrorsForApi($formModel->getErrors());
        }
    }

    /**
     * 奖金挂网
     * @param $taskKey
     * @throws Exception
     * @throws \yii\base\Exception
     */
    public function actionSend($taskKey){
        $params = Cache::getAsyncParams($taskKey);
        $formModel = new PeriodForm();
        $handleResult = false;
        if($formModel->load($params, '') && $formModel->sendAsync($params['handleUserId'])){
            $handleResult = true;
            \Yii::$app->swooleAsyncTimer->pushAsyncResultToAdmin($params['handleUserId'], "第{$params['periodNum']}期挂网成功");
        } else {
            \Yii::$app->swooleAsyncTimer->pushAsyncResultToAdmin($params['handleUserId'], "第{$params['periodNum']}期挂网失败,原因：".Form::formatErrorsForApi($formModel->getErrors()), false);
        }
        if(!$handleResult){
            echo "第{$params['periodNum']}期挂网失败，原因：".Form::formatErrorsForApi($formModel->getErrors());
        }
    }

    /**
     * 后台手动封期时调用的封期后操作
     * @param $taskKey
     * @throws Exception
     */
    public function actionAfterClose($taskKey){
        $params = Cache::getAsyncParams($taskKey);
        $formModel = new PeriodForm();
        $handleResult = false;
        if($formModel->afterCloseAsync($params['periodNum'], $params['closedAt'])){
            $handleResult = true;
        }
        if(!$handleResult){

        }
    }

    /**
     * 奖金余额导出
     * @param $taskId
     * @return bool
     */
    public function actionBalanceExport($taskId){
        return $this->_export($taskId, BalanceList::class, '会员奖金余额列表');
    }

    /**
     * 车房养老奖和领袖分红年度发放审核导出
     * @param $taskId
     * @return bool
     */
    public function actionCfLxAuditExport($taskId){
        return $this->_export($taskId, CfLxAuditList::class, '年度奖金发放审核列表');
    }

    /**
     * 最新奖金列表导出
     * @param $taskId
     * @return bool
     */
    public function actionNewPeriodBonusExport($taskId){
        return $this->_export($taskId, PeriodBonusList::class, '最新奖金列表');
    }

    /**
     * 往期奖金列表导出
     * @param $taskId
     * @return bool
     */
    public function actionOtherPeriodBonusExport($taskId){
        return $this->_export($taskId, PeriodBonusList::class, '往期奖金列表');
    }

    /**
     * 奖金流水列表导出
     * @param $taskId
     * @return bool
     */
    public function actionFlowBonusExport($taskId){
        return $this->_export($taskId, FlowBonusList::class, '奖金流水列表');
    }

    /**
     * 余额流水导出
     * @param $taskId
     * @return bool
     */
    public function actionFlowBalanceExport($taskId){
        return $this->_export($taskId, FlowBalanceList::class, '复消积分流水列表');
    }

    /**
     * 复消积分流水导出
     * @param $taskId
     * @return bool
     */
    public function actionFlowRpExport($taskId){
        return $this->_export($taskId, FlowReconsumePointsList::class, '复消积分流水列表');
    }

    /**
     * 兑换积分流水导出
     * @param $taskId
     * @return bool
     */
    public function actionFlowExchangeExport($taskId){
        return $this->_export($taskId, FlowExchangePointsList::class, '兑换积分流水列表');
    }

    /**
     * 车房养老奖流水导出
     * @param $taskId
     * @return bool
     */
    public function actionFlowCfExport($taskId){
        return $this->_export($taskId, FlowCfList::class, '车房养老奖流水列表');
    }

    /**
     * 领袖分红奖导出
     * @param $taskId
     * @return bool
     */
    public function actionFlowLxExport($taskId){
        return $this->_export($taskId, FlowLxList::class, '领袖分红奖流水列表');
    }

    /**
     * 区域津贴补发列表
     * @param $taskId
     * @return bool
     */
    public function actionResendQyListExport($taskId){
        return $this->_export($taskId, ResendQyList::class, '区域津贴补发列表');
    }

    /**
     * 补发区域津贴审核列表
     * @param $taskId
     * @return bool
     */
    public function actionResendQyAuditListExport($taskId){
        return $this->_export($taskId, ResendQyAuditList::class, '补发区域津贴审核列表');
    }

    /**
     * 团队奖向下追溯导出
     * @param $taskId
     * @return bool
     */
    public function actionTraceDownQyExport($taskId){
        return $this->_export($taskId, TraceDownQyList::class, '团队奖向下追溯导出列表');
    }

    /**
     * 团队奖向上追溯导出
     * @param $taskId
     * @return bool
     */
    public function actionTraceUpQyExport($taskId){
        return $this->_export($taskId, TraceUpQyList::class, '团队奖向上追溯导出列表');
    }

    /**
     * 荣衔奖向下追溯导出
     * @param $taskId
     * @return bool
     */
    public function actionTraceDownYcExport($taskId){
        return $this->_export($taskId, TraceDownYcList::class, '荣衔奖向下追溯导出列表');
    }

    /**
     * 荣衔奖向上追溯导出
     * @param $taskId
     * @return bool
     */
    public function actionTraceUpYcExport($taskId){
        return $this->_export($taskId, TraceUpYcList::class, '荣衔奖向上追溯导出列表');
    }

    /**
     * 服务奖向下追溯导出
     * @param $taskId
     * @return bool
     */
    public function actionTraceDownBdExport($taskId){
        return $this->_export($taskId, TraceDownYcList::class, '服务奖向下追溯导出列表');
    }

    /**
     * 服务奖向上追溯导出
     * @param $taskId
     * @return bool
     */
    public function actionTraceUpBdExport($taskId){
        return $this->_export($taskId, TraceUpYcList::class, '服务奖向上追溯导出列表');
    }

    /**
     * 推广奖向下追溯导出
     * @param $taskId
     * @return bool
     */
    public function actionTraceDownTgExport($taskId){
        return $this->_export($taskId, TraceDownTgList::class, '推广奖向下追溯导出列表');
    }


    /**
     * 推广奖向上追溯导出
     * @param $taskId
     * @return bool
     */
    public function actionTraceUpTgExport($taskId){
        return $this->_export($taskId, TraceUpTgList::class, '推广奖向上追溯导出列表');
    }


    /**
     * 消费奖向下追溯导出
     * @param $taskId
     * @return bool
     */
    public function actionTraceDownXfExport($taskId){
        return $this->_export($taskId, TraceDownXfList::class, '消费奖向下追溯导出列表');
    }


    /**
     * 消费奖向上追溯导出
     * @param $taskId
     * @return bool
     */
    public function actionTraceUpXfExport($taskId){
        return $this->_export($taskId, TraceUpXfList::class, '消费奖向上追溯导出列表');
    }


    /**
     * 业绩奖向下追溯导出
     * @param $taskId
     * @return bool
     */
    public function actionTraceDownYjExport($taskId){
        return $this->_export($taskId, TraceDownYjList::class, '业绩奖向下追溯导出列表');
    }

    /**
     * 业绩奖向上追溯导出
     * @param $taskId
     * @return bool
     */
    public function actionTraceUpYjExport($taskId){
        return $this->_export($taskId, TraceUpYjList::class, '业绩奖向上追溯导出列表');
    }

    /**
     * 管理奖向下追溯导出
     * @param $taskId
     * @return bool
     */
    public function actionTraceDownGlExport($taskId){
        return $this->_export($taskId, TraceDownGlList::class, '管理奖向下追溯导出列表');
    }

    /**
     * 管理奖向上追溯导出
     * @param $taskId
     * @return bool
     */
    public function actionTraceUpGlExport($taskId){
        return $this->_export($taskId, TraceUpGlList::class, '管理奖向上追溯导出列表');
    }


    /**
     * 共享奖向下追溯导出
     * @param $taskId
     * @return bool
     */
    public function actionTraceDownGxExport($taskId){
        return $this->_export($taskId, TraceDownGxList::class, '共享奖向下追溯导出列表');
    }

    /**
     * 共享奖向上追溯导出
     * @param $taskId
     * @return bool
     */
    public function actionTraceUpBdGxport($taskId){
        return $this->_export($taskId, TraceUpGxList::class, '共享奖向上追溯导出列表');
    }


    /**
     * 复销奖向下追溯导出
     * @param $taskId
     * @return bool
     */
    public function actionTraceDownFxExport($taskId){
        return $this->_export($taskId, TraceDownFxList::class, '复销奖向下追溯导出列表');
    }

    /**
     * 复销奖向上追溯导出
     * @param $taskId
     * @return bool
     */
    public function actionTraceUpFxExport($taskId){
        return $this->_export($taskId, TraceUpFxList::class, '复销奖向上追溯导出列表');
    }

    /**
     * 报单中心补贴追溯导出
     * @param $taskId
     * @return bool
     */
    public function actionTraceBtExport($taskId){
        return $this->_export($taskId, TraceBtList::class, '报单中心补贴追溯列表');
    }

    /**
     * 报单中心货补追溯导出
     * @param $taskId
     * @return bool
     */
    public function actionTraceFlExport($taskId){
        return $this->_export($taskId, TraceFlList::class, '报单中心货补追溯列表');
    }

    /**
     * 区域业绩提成追溯列表导出
     * @param $taskId
     * @return bool
     */
    public function actionTraceFwExport($taskId){
        return $this->_export($taskId, TraceFwList::class, '区域业绩提成追溯列表');
    }

    /**
     * 业绩单导出
     * @param $taskId
     * @return bool
     */
    public function actionPerfOrderExport($taskId){
        return $this->_export($taskId, PerfOrderList::class, '业绩单列表');
    }

    /**
     * 月积分导出
     * @param $taskId
     * @return bool
     */
    public function actionScoreMonthExport($taskId){
        return $this->_export($taskId, ScoreMonthList::class, '月积分列表');
    }

    /**
     * 月业绩导出
     * @param $taskId
     * @return bool
     */
    public function actionPerfMonthExport($taskId){
        return $this->_export($taskId, PerfMonthList::class, '月业绩列表');
    }

    /**
     * 达标业绩导出
     * @param $taskId
     * @return bool
     */
    public function actionPerfStandardExport($taskId){
        return $this->_export($taskId, PerfStandardList::class, '达标业绩列表');
    }

    /**
     * 月业绩导出
     * @param $taskId
     * @return bool
     */
    public function actionUserPerfExport($taskId){
        return $this->_export($taskId, UserPerfList::class, '用户业绩列表');
    }

    /**
     * 期业绩导出
     * @param $taskId
     * @return bool
     */
    public function actionPerfPeriodListExport($taskId){
        return $this->_export($taskId, PerfPeriodList::class, '期业绩列表');
    }

    /**
     * 导出方法
     * @param $taskId
     * @param $className
     * @param $listName
     * @return bool
     */
    private function _export($taskId, $className, $listName){
        $factory = BonusExport::factory($taskId);
        $factory->listModelClass = $className;
        try {
            if ($factory->generate()) {
                \Yii::$app->swooleAsyncTimer->pushAsyncResultToAdmin($factory->getUserId(), $listName.'导出成功');
            }
            unset($factory, $taskId, $className, $listName);
            return true;
        } catch (\Exception $e) {
            echo $listName.'导出失败。详情：' . $e->getMessage();
            \Yii::$app->swooleAsyncTimer->pushAsyncResultToAdmin($factory->getUserId(), $listName.'导出失败。详情：' . $e->getMessage(), false);
        }
        unset($factory, $taskId, $className, $listName);
        return false;
    }

    /**
     * 批量提现
     *
     */
    public function actionAutoWithdraw($taskKey){
        $params = Cache::getAsyncParams($taskKey);

        Cache::setWithdrawLock(1);
        $formModel = new WithdrawForm();
        $formModel->batchWithdraw(5,0, $params);
        Cache::setWithdrawLock(0);
    }

    /**
     * 会员绩效奖金过期巡查
     * @param $taskKey
     * @return void
     * @throws Exception
     */
    public function actionUserPerformanceBonusExpired($taskKey)
    {
        $formModel = new UserPerformanceForm();
        $formModel->actionUserPerformanceBonusExpired();
    }
}