* * This source file is subject to the MIT license that is bundled * with this source code in the file LICENSE. */ namespace App\Jobs\Dwbs; use App\Jobs\Job; use App\Repositories\Enums\Base\TaskMakeStatusEnum; use App\Repositories\Enums\Dwbs\ShopOrderStatusEnum; use App\Repositories\Models\Base\Task; use App\Repositories\Models\Dwbs\ShopOrder; use Illuminate\Support\Facades\Storage; class ImportShopOrderWuLiuJob extends Job { public $timeout = 1200; private $path = ''; private $disk = ''; private $resource = ''; private $login_admin_id = ''; /** * Create a new job instance. */ public function __construct($resource, $login_admin_id) { $this->resource = $resource; $this->disk = $resource['disk']; $this->path = $resource['path']; $this->login_admin_id = $login_admin_id; } /** * Execute the job. */ public function handle() { $task = Task::importTask($this->resource, ShopOrder::class, $this->login_admin_id); if (!Storage::disk($this->disk)->exists($this->path)) { $task->make_status = TaskMakeStatusEnum::FAIL; $task->result = '找不到文件'; $task->save(); return false; } try { $config = ['path' => '']; $excel = new \Vtiful\Kernel\Excel($config); $excel->openFile(Storage::disk($this->disk)->path($this->path)) ->openSheet() ->setSkipRows(1); } catch (\Exception $exception) { $task->make_status = TaskMakeStatusEnum::FAIL; $task->result = '读取文件失败:' . $exception->getMessage(); $task->save(); return false; } $data = []; $total = 0; $r_total = 0; $w_total = 0; try { while (($row = $excel->nextRow()) !== NULL) { if (count($row) < 3) continue; $order_no = $row[0]; $wuliu_no = $row[1]; $wuliu_type = $row[2]; if ($order_no == '订单号') continue; if (empty($order_no)) continue; if (empty($wuliu_no)) continue; if (empty($wuliu_type)) continue; $order_no = rk($order_no); $wuliu_no = rk($wuliu_no); $wuliu_type = rk($wuliu_type); $data[] = compact('order_no', 'wuliu_no', 'wuliu_type'); $total++; $r_total++; } } catch (\Exception $exception) { $task->make_status = TaskMakeStatusEnum::FAIL; $task->result = '读取文件数据失败:' . $exception->getMessage(); $task->save(); return false; } try { $msg = []; foreach ($data as $da) { $order = ShopOrder::query()->where('order_no', $da['order_no'])->first(); if ($order) { $order->fill([ 'status' => ShopOrderStatusEnum::wait_shou, 'wuliu_no' => $da['wuliu_no'], 'wuliu_type' => $da['wuliu_type'], ]); $order->save(); $w_total++; continue; } $msg[] = $da['order_no']; } $msg = "订单号找不到:" . arr2str($msg); $task->make_status = TaskMakeStatusEnum::OK; $task->result = "共{$total}条,读取{$r_total}条,导入{$w_total}条。{$msg}"; $task->save(); } catch (\Exception $exception) { $task->make_status = TaskMakeStatusEnum::FAIL; $task->result = '读取文件失败:' . $exception->getMessage(); $task->save(); return false; } return true; } }