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

use backendApi\modules\v1\models\lists\bonus\FlowBonusList;
use backendApi\modules\v1\models\lists\log\UserLoginList;
use common\helpers\Cache;
use common\libs\export\module\BonusExport;
use console\libs\logging\OperateAsync;

class LogController extends BaseController
{

    /**
     * 异步保存日志
     * @param $taskKey
     */
    public function actionSave($taskKey){
        $params = Cache::getAsyncParams($taskKey);
        $operate = new OperateAsync([], $params);
        if(!$operate->asyncSave()){
            // 记录错误系统日志
        }
    }

    /**
     * 奖金流水列表导出
     * @param $taskId
     * @return bool
     */
    public function actionUserLoginExport($taskId){
        return $this->_export($taskId, UserLoginList::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;
    }

}