<?php

namespace App\Http\Controllers;

use App\Imports\UpdatePickZone;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\Gate;
use Maatwebsite\Excel\Facades\Excel;

class OrderCommodityAssignController extends Controller
{
    public function index()
    {
        if(!Gate::allows('订单管理-指定分配-查询')){ return redirect(url('denied'));  }
        $assigns = app("OrderCommodityAssignService")->paginate();
        return view("order.index.index",compact("assigns"));
    }

    public function import(Request $request)
    {
        if(!Gate::allows('订单管理-指定分配-编辑')){ return ["success"=>false,"data"=>"无权操作！"];  }
        $fileSuffix=$request->file('file')->getClientOriginalExtension();
        if ($fileSuffix != 'xlsx' && $fileSuffix != 'xls' && $fileSuffix != 'csv')
            return ['success'=>false,'data'=>'不支持该文件类型'];
        ini_set('max_execution_time',2500);
        ini_set('memory_limit','1526M');
        $fileSuffix = ucwords($fileSuffix);

        Excel::import(new UpdatePickZone(),$request->file('file')->path(),null,$fileSuffix);
        if (Cache::has('commodityAssign'))return Cache::pull('commodityAssign');

        return ["success"=>false,"data"=>"导入发生错误，数据无响应"];
    }
}
