<?php



namespace app\controller\merchant\store;

use app\common\repositories\store\ExcelRepository;
use ln\exceptions\UploadException;
use ln\services\ExcelService;
use think\App;
use ln\basic\BaseController;

class Excel extends BaseController
{

    protected $repository;

    public function __construct(App $app, ExcelRepository $repository)
    {
        parent::__construct($app);
        $this->repository = $repository;
    }

    /**
     * TODO
     * @return mixed
     * @author Qinii
     * @day 2020-08-15
     */
    public function lst()
    {
        $admin = $this->request->adminInfo();
        if($admin['level']) $where['admin_id'] = $this->request->adminId();
        [$page, $limit] = $this->getPage();
        $where['type'] = $this->request->param('type','');
        $where['mer_id'] = $this->request->merId();
        return app('json')->success($this->repository->getList($where,$page,$limit));
    }

    /**
     * TODO 下载文件
     * @param $id
     * @return \think\response\File
     * @author Qinii
     * @day 2020-07-30
     */
    public function download($id)
    {
        try{
            if($id == 'express'){
                $file['name'] = 'express';
                $path = app()->getRootPath().'extend/express.xlsx';
                if(!$file || !file_exists($path)) return app('json')->fail('文件不存在');
                return download($path,$file['name']);
            }

            $file = $this->repository->getWhere(['excel_id' => $id,'mer_id' => $this->request->merId()]);
            $path = app()->getRootPath().'public'.$file['path'];
            if(!$file || !file_exists($path)) return app('json')->fail('文件不存在');

            return download($path,$file['name']);
        }catch (UploadException $e){
            return app('json')->fail('下载失败');
        }
    }

    /**
     * TODO 所有类型
     * @return \think\response\Json
     * @author Qinii
     * @day 7/2/21
     */
    public function type()
    {
        $data = [
            ['key'  => 'order',             'value' => '订单列表'],
            ['key'  => 'delivery',          'value' => '待发货订单'],
            ['key'  => 'searchLog',         'value' => '搜索记录'],
            ['key'  => 'financial',         'value' => '流水记录'],
            ['key'  => 'refundOrder',       'value' => '退款单'],
            ['key'  => 'integralLog',       'value' => '积分日志'],
            ['key'  => 'importDelivery',    'value' => '发货导入'],
            ['key'  => 'exportFinancial',   'value' => '日/月账单'],
        ];
//        $data['data'] = [
//            'order'             => '订单列表',
//            'delivery'          => '待发货订单',
//            'searchLog'         => '搜索记录',
//            'financial'         => '流水记录',
//            'refundOrder'       => '退款单',
//            'integralLog'       => '积分日志',
//            'importDelivery'    => '发货导入',
//            'exportFinancial'   => '日/月账单',
//        ];
        return app('json')->success($data);
    }

}
