ImportShopOrderWuLiuJob.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. <?php
  2. /*
  3. * This file is part of the Jiannei/lumen-api-starter.
  4. *
  5. * (c) Jiannei <longjian.huang@foxmail.com>
  6. *
  7. * This source file is subject to the MIT license that is bundled
  8. * with this source code in the file LICENSE.
  9. */
  10. namespace App\Jobs\Dwbs;
  11. use App\Jobs\Job;
  12. use App\Repositories\Enums\Base\TaskMakeStatusEnum;
  13. use App\Repositories\Enums\Dwbs\ShopOrderStatusEnum;
  14. use App\Repositories\Models\Base\Task;
  15. use App\Repositories\Models\Dwbs\ShopOrder;
  16. use Illuminate\Support\Facades\Storage;
  17. class ImportShopOrderWuLiuJob extends Job
  18. {
  19. public $timeout = 1200;
  20. private $path = '';
  21. private $disk = '';
  22. private $resource = '';
  23. private $login_admin_id = '';
  24. /**
  25. * Create a new job instance.
  26. */
  27. public function __construct($resource, $login_admin_id)
  28. {
  29. $this->resource = $resource;
  30. $this->disk = $resource['disk'];
  31. $this->path = $resource['path'];
  32. $this->login_admin_id = $login_admin_id;
  33. }
  34. /**
  35. * Execute the job.
  36. */
  37. public function handle()
  38. {
  39. $task = Task::importTask($this->resource, ShopOrder::class, $this->login_admin_id);
  40. if (!Storage::disk($this->disk)->exists($this->path)) {
  41. $task->make_status = TaskMakeStatusEnum::FAIL;
  42. $task->result = '找不到文件';
  43. $task->save();
  44. return false;
  45. }
  46. try {
  47. $config = ['path' => ''];
  48. $excel = new \Vtiful\Kernel\Excel($config);
  49. $excel->openFile(Storage::disk($this->disk)->path($this->path))
  50. ->openSheet()
  51. ->setSkipRows(1);
  52. } catch (\Exception $exception) {
  53. $task->make_status = TaskMakeStatusEnum::FAIL;
  54. $task->result = '读取文件失败:' . $exception->getMessage();
  55. $task->save();
  56. return false;
  57. }
  58. $data = [];
  59. $total = 0;
  60. $r_total = 0;
  61. $w_total = 0;
  62. try {
  63. while (($row = $excel->nextRow()) !== NULL) {
  64. if (count($row) < 3) continue;
  65. $order_no = $row[0];
  66. $wuliu_no = $row[1];
  67. $wuliu_type = $row[2];
  68. if ($order_no == '订单号') continue;
  69. if (empty($order_no)) continue;
  70. if (empty($wuliu_no)) continue;
  71. if (empty($wuliu_type)) continue;
  72. $order_no = rk($order_no);
  73. $wuliu_no = rk($wuliu_no);
  74. $wuliu_type = rk($wuliu_type);
  75. $data[] = compact('order_no', 'wuliu_no', 'wuliu_type');
  76. $total++;
  77. $r_total++;
  78. }
  79. } catch (\Exception $exception) {
  80. $task->make_status = TaskMakeStatusEnum::FAIL;
  81. $task->result = '读取文件数据失败:' . $exception->getMessage();
  82. $task->save();
  83. return false;
  84. }
  85. try {
  86. $msg = [];
  87. foreach ($data as $da) {
  88. $order = ShopOrder::query()->where('order_no', $da['order_no'])->first();
  89. if ($order) {
  90. $order->fill([
  91. 'status' => ShopOrderStatusEnum::wait_shou,
  92. 'wuliu_no' => $da['wuliu_no'],
  93. 'wuliu_type' => $da['wuliu_type'],
  94. ]);
  95. $order->save();
  96. $w_total++;
  97. continue;
  98. }
  99. $msg[] = $da['order_no'];
  100. }
  101. $msg = "订单号找不到:" . arr2str($msg);
  102. $task->make_status = TaskMakeStatusEnum::OK;
  103. $task->result = "共{$total}条,读取{$r_total}条,导入{$w_total}条。{$msg}";
  104. $task->save();
  105. } catch (\Exception $exception) {
  106. $task->make_status = TaskMakeStatusEnum::FAIL;
  107. $task->result = '读取文件失败:' . $exception->getMessage();
  108. $task->save();
  109. return false;
  110. }
  111. return true;
  112. }
  113. }