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

namespace console\controllers;

use backendApi\modules\v1\models\lists\finance\BalanceAuditList;
use backendApi\modules\v1\models\lists\finance\HistoryBonusList;
use backendApi\modules\v1\models\lists\finance\RechargeList;
use backendApi\modules\v1\models\lists\finance\TransferList;
use backendApi\modules\v1\models\lists\finance\WithdrawList;
use common\libs\export\module\FinanceExport;
use common\models\Withdraw;
use Yii;

class FinanceController extends BaseController {

    /**
     * 会员余额调整列表导出
     * @param $taskId
     * @return bool
     */
    public function actionBalanceAuditListExport($taskId) {
        return $this->_export($taskId, BalanceAuditList::class, '会员余额调整列表');
    }

//    /**
//     * 会员业绩调整列表导出
//     * @param $taskId
//     * @return bool
//     */
//    public function actionPerfAuditListExport($taskId) {
//        return $this->_export($taskId, PerfAuditList::class, '会员业绩调整列表');
//    }

    /**
     * 转账列表导出
     * @param $taskId
     * @return bool
     */
    public function actionTransferListExport($taskId) {
        return $this->_export($taskId, TransferList::class, '转账列表');
    }

    /**
     * 历史奖金余额导出
     * @param $taskId
     * @return bool
     */
    public function actionHistoryBonusExport($taskId) {
        return $this->_export($taskId, HistoryBonusList::class, '历史奖金余额');
    }

    /**
     * 提现明细导出到excel
     * @param $taskId
     * @return bool
     */
    public function actionWithdrawExport($taskId) {
        return $this->_export($taskId, WithdrawList::class, '提现明细');
    }

    /**
     * 充值明细导出到excel
     * @param $taskId
     * @return bool
     */
    public function actionRechargeExport($taskId) {
        return $this->_export($taskId, RechargeList::class, '充值明细');
    }

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