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

use backendApi\modules\v1\models\lists\bonus\PerfOrderList;
use backendApi\modules\v1\models\lists\user\BindList;
use backendApi\modules\v1\models\lists\user\GroupList;
use backendApi\modules\v1\models\lists\user\IndexList;
use backendApi\modules\v1\models\lists\user\BaUserList;
use backendApi\modules\v1\models\lists\user\MoveList;
use backendApi\modules\v1\models\lists\user\StatusAuditList;
use backendApi\modules\v1\models\lists\user\SystemList;
use backendApi\modules\v1\models\lists\user\TeamworkList;
use backendApi\modules\v1\models\lists\user\YearHighestEmpLvList;
use common\helpers\Cache;
use common\libs\export\BaseExport;
use common\libs\export\module\BaUserExport;
use common\libs\export\module\BonusExport;
use common\libs\export\module\UserExport;
use common\models\forms\NetMoveForm;
use common\helpers\Form;
use Yii;
use yii\db\Exception;

class UserController extends BaseController
{

    /**
     * 安置网络移网
     * @param $taskKey
     * @throws \Exception
     */
    public function actionMoveNetwork($taskKey){
        $params = Cache::getAsyncParams($taskKey);
        $formModel = new NetMoveForm();
        $formModel->scenario = 'async';
        $handleResult = false;
        if($formModel->load($params, '') && $formModel->moveAsync()){
            $handleResult = true;
            echo sprintf("时间:[%s]移网, 会员安置网络【%s】移网成功".PHP_EOL, date('Y-m-d H:i:s', time()), $params['moveUserName']);
            //\Yii::$app->swooleAsyncTimer->pushAsyncResultToAdmin($params['handleUserId'], "会员安置网络{$params['moveUserName']}移网成功");
        } else {
            echo sprintf("时间:[%s]移网, 会员安置网络【%s】移网失败".PHP_EOL, date('Y-m-d H:i:s', time()), $params['moveUserName']);
            //\Yii::$app->swooleAsyncTimer->pushAsyncResultToAdmin($params['handleUserId'], "会员安置网络{$params['moveUserName']}移网失败,原因：".Form::formatErrorsForApi($formModel->getErrors()), false);
        }
        if(!$handleResult){
            // Log::async([
//                'type' => LogAsync::TYPE_USER,
//                'route' => 'user/move-network',
//                'title' => "会员安置网络{$params['moveUserName']}移网失败",
//                'detail' => "会员安置网络{$params['moveUserName']}移网失败,原因：".Form::formatErrorsForApi($formModel->getErrors()),
//                'status' => 0,
//            ]);
        }
    }

    /**
     * 移开拓网络
     * @param $taskKey
     * @throws Exception
     */
    public function actionMoveRelation($taskKey){
        $params = Cache::getAsyncParams($taskKey);
        $formModel = new NetMoveForm();
        $formModel->scenario = 'async';
        $handleResult = false;
        if($formModel->load($params, '') && $formModel->moveAsync()){
            $handleResult = true;
            echo sprintf("时间:[%s]移网, 会员开拓网络【%s】移网成功".PHP_EOL, date('Y-m-d H:i:s', time()), $params['moveUserName']);
            //\Yii::$app->swooleAsyncTimer->pushAsyncResultToAdmin($params['handleUserId'], "会员开拓网络{$params['moveUserName']}移网成功");
        } else {
            echo sprintf("时间:[%s]移网, 会员开拓网络【%s】移网失败".PHP_EOL, date('Y-m-d H:i:s', time()), $params['moveUserName']);
            //\Yii::$app->swooleAsyncTimer->pushAsyncResultToAdmin($params['handleUserId'], "会员开拓网络{$params['moveUserName']}移网失败,原因：".Form::formatErrorsForApi($formModel->getErrors()), false);
        }
        if(!$handleResult){
            // Log::async([
//                'type' => LogAsync::TYPE_USER,
//                'route' => 'user/move-relation',
//                'title' => "会员开拓网络{$params['moveUserName']}移网失败",
//                'detail' => "会员开拓网络{$params['moveUserName']}移网失败,原因：".Form::formatErrorsForApi($formModel->getErrors()),
//                'status' => 0,
//            ]);
        }
    }

    /**
     * 会员列表导出
     * @param $taskId
     * @return bool
     */
    public function actionIndexExport($taskId){
        return $this->_export($taskId, IndexList::class, 'Member_list');
    }

    /**
     * 会员绑定列表导出
     * @param $taskId
     * @return bool
     */
    public function actionBindExport($taskId){
        return $this->_export($taskId, BindList::class, '会员绑定列表');
    }

    /**
     * 点位合作导出
     * @param $taskId
     * @return bool
     */
    public function actionTeamworkExport($taskId){
        return $this->_export($taskId, TeamworkList::class, '点位合作');
    }

    /**
     * 体系管理导出
     * @param $taskId
     * @return bool
     */
    public function actionSystemExport($taskId){
        return $this->_export($taskId, SystemList::class, '体系管理');
    }

    /**
     * 团队领导人列表导出
     * @param $taskId
     * @return bool
     */
    public function actionGroupExport($taskId){
        return $this->_export($taskId, GroupList::class, '团队领导人列表');
    }

    /**
     * 移网管理导出
     * @param $taskId
     * @return bool
     */
    public function actionMoveExport($taskId){
        return $this->_export($taskId, MoveList::class, '移网管理');
    }

    /**
     * 会员状态管理导出
     * @param $taskId
     * @return bool
     */
    public function actionStatusAuditExport($taskId){
        return $this->_export($taskId, StatusAuditList::class, '会员状态管理');
    }

    /**
     * 会员历史年度最高聘级表导出
     * @param $taskId
     * @return bool
     */
    public function actionYearHighestEmpLvExport($taskId){
        return $this->_export($taskId, YearHighestEmpLvList::class, '会员历史年度最高聘级表');
    }

    /**
     * 导出方法
     * @param $taskId
     * @param $className
     * @param $listName
     * @return bool
     */
    private function _export($taskId, $className, $listName){
        $factory = UserExport::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;
    }

    /**
     * BA会员列表导出
     * @param $taskId
     * @return bool
     */
    public function actionBaUserListExport($taskId) {
        return $this->_exportBA($taskId, BaUserList::class, 'Brand_Ambassador_List');
    }

    /**
     * 导出方法
     * @param $taskId
     * @param $className
     * @param $listName
     * @return bool
     */
    private function _exportBA($taskId, $className, $listName){
        $factory = BaUserExport::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;
    }
}