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

use backendApi\modules\v1\models\lists\shop\DecOrderList;
use backendApi\modules\v1\models\lists\shop\GoodsList;
use backendApi\modules\v1\models\lists\shop\OrderList;
use backendApi\modules\v1\models\lists\shop\BaOrderList;
use common\libs\export\module\BaShopExport;
use common\libs\export\module\ShopExport;
use Yii;

class ShopController extends BaseController
{

    /**
     * 报单列表导出
     * @param $taskId
     * @return bool
     */
    public function actionDecOrderListExport($taskId){
        $factory = ShopExport::factory($taskId);
        $factory->listModelClass = DecOrderList::class;
        try {
            if ($factory->generate()) {
                Yii::$app->swooleAsyncTimer->pushAsyncResultToAdmin($factory->getUserId(), '报单列表导出成功');
            }
            unset($factory);
            return true;
        } catch (\Exception $e) {
            echo '报单列表导出失败。详情：' . $e->getMessage();
            Yii::$app->swooleAsyncTimer->pushAsyncResultToAdmin($factory->getUserId(), '报单列表导出失败。详情：' . $e->getMessage(), false);
        }
        unset($factory);
        return false;
    }

    /**
     * 订单列表导出
     * @param $taskId
     * @return bool
     */
    public function actionOrderListExport($taskId){
        $factory = ShopExport::factory($taskId);
        $factory->listModelClass = OrderList::class;
        try {
            if ($factory->generateOrderExcel()) {
                Yii::$app->swooleAsyncTimer->pushAsyncResultToAdmin($factory->getUserId(), '订单列表导出成功');
            }
            unset($factory);
            return true;
        } catch (\Exception $e) {
            echo '订单列表导出失败。详情：' . $e->getMessage();
            Yii::$app->swooleAsyncTimer->pushAsyncResultToAdmin($factory->getUserId(), '订单列表导出失败。详情：' . $e->getMessage(), false);
        }

        unset($factory);
        return false;
    }

    /**
     * 商品列表导出
     * @param $taskId
     * @return bool
     */
    public function actionGoodsListExport($taskId){
        $factory = ShopExport::factory($taskId);
        $factory->listModelClass = GoodsList::class;
        try {
            if ($factory->generate()) {
                Yii::$app->swooleAsyncTimer->pushAsyncResultToAdmin($factory->getUserId(), '商品列表导出成功');
            }
            unset($factory);
            return true;
        } catch (\Exception $e) {
            echo '商品列表导出失败。详情：' . $e->getMessage();
            Yii::$app->swooleAsyncTimer->pushAsyncResultToAdmin($factory->getUserId(), '商品列表导出失败。详情：' . $e->getMessage(), false);
        }
        unset($factory);
        return false;
    }

    /**
     * 订单列表导出PDF
     * @param $taskId
     * @return bool
     */
    public function actionOrderListExportPdf($taskId)
    {
        $factory = ShopExport::factory($taskId);
        $factory->listModelClass = OrderList::class;
        try {
            if ($factory->generateOrderPDF()) {
                Yii::$app->swooleAsyncTimer->pushAsyncResultToAdmin($factory->getUserId(), '订单列表导出成功');
            }
            unset($factory);
            return true;
        } catch (\Exception $e) {
            echo '订单列表导出失败。详情：' . $e->getMessage();
            Yii::$app->swooleAsyncTimer->pushAsyncResultToAdmin($factory->getUserId(), '订单列表导出失败。详情：' . $e->getMessage(), false);
        }

        unset($factory);
        return false;
    }

    /**
     * 订单列表导出PDF
     * @param $taskId
     * @return bool
     */
    public function actionOrderInvoiceExport($taskId)
    {
        $factory = ShopExport::factory($taskId);
        $factory->listModelClass = OrderList::class;
        try {
            if ($factory->actionOrderInvoiceExport()) {
                Yii::$app->swooleAsyncTimer->pushAsyncResultToAdmin($factory->getUserId(), '导出成功');
            }
            unset($factory);
            return true;
        } catch (\Exception $e) {
            echo '导出失败。详情：' . $e->getMessage() . ' ' . $e->getLine() . ' ' . $e->getFile();
            Yii::$app->swooleAsyncTimer->pushAsyncResultToAdmin($factory->getUserId(), '导出失败。详情：' . $e->getMessage() . ' ' . $e->getLine() . ' ' . $e->getFile(), false);
        }

        unset($factory);
        return false;
    }

    /**
     * 订单列表导出PDF
     * @param $taskId
     * @return bool
     */
    public function actionDecOrderListExportPdf($taskId)
    {
        $factory = ShopExport::factory($taskId);
        $factory->listModelClass = OrderList::class;
        try {
            if ($factory->generateDecOrderPDF()) {
                Yii::$app->swooleAsyncTimer->pushAsyncResultToAdmin($factory->getUserId(), '订单列表导出成功');
            }
            unset($factory);
            return true;
        } catch (\Exception $e) {
            echo '订单列表导出失败。详情：' . $e->getMessage();
            Yii::$app->swooleAsyncTimer->pushAsyncResultToAdmin($factory->getUserId(), '订单列表导出失败。详情：' . $e->getMessage(), false);
        }

        unset($factory);
        return false;
    }

    /**
     * BA订单列表导出
     * @param $taskId
     * @return bool
     */
    public function actionBaOrderListExport($taskId){
        $factory = BaShopExport::factory($taskId);
        $factory->listModelClass = BaOrderList::class;
        try {
            if ($factory->generateBaOrderExcel()) {
                Yii::$app->swooleAsyncTimer->pushAsyncResultToAdmin($factory->getUserId(), 'BA订单列表导出成功');
            }
            unset($factory);
            return true;
        } catch (\Exception $e) {
            echo '订单列表导出失败。详情：' . $e->getMessage();
            Yii::$app->swooleAsyncTimer->pushAsyncResultToAdmin($factory->getUserId(), 'BA订单列表导出失败。详情：' . $e->getMessage(), false);
        }

        unset($factory);
        return false;
    }

    /**
     * 订单列表导出PDF
     * @param $taskId
     * @return bool
     */
    public function actionBaOrderListExportPdf($taskId)
    {
        $factory = BaShopExport::factory($taskId);
        $factory->listModelClass = BaOrderList::class;
        try {
            if ($factory->generateBaOrderPDF()) {
                Yii::$app->swooleAsyncTimer->pushAsyncResultToAdmin($factory->getUserId(), '订单列表导出成功');
            }
            unset($factory);
            return true;
        } catch (\Exception $e) {
            echo '订单列表导出失败。详情：' . $e->getMessage();
            Yii::$app->swooleAsyncTimer->pushAsyncResultToAdmin($factory->getUserId(), '订单列表导出失败。详情：' . $e->getMessage(), false);
        }

        unset($factory);
        return false;
    }
}