ImportOrder.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. <?php
  2. namespace app\queue\redis\slow;
  3. use app\model\saas\SaasOrder;
  4. use app\model\saas\SaasOrderExpress;
  5. use app\model\system\SystemImport;
  6. use PhpOffice\PhpSpreadsheet\IOFactory;
  7. use Webman\RedisQueue\Consumer;
  8. class ImportOrder implements Consumer
  9. {
  10. /**
  11. * 订单导入发货
  12. * 更新云打印件
  13. * @var string
  14. */
  15. public string $queue = "import-order";
  16. /**
  17. * 连接配置
  18. * @var string
  19. */
  20. public string $connection = "default";
  21. public function consume($data): bool
  22. {
  23. try {
  24. if(empty($data['logId'])) return true;
  25. if(empty($data['poi_id'])) return true;
  26. $path = public_path().$data['path'];
  27. $spreadsheet = IOFactory::load($path);
  28. $worksheet = $spreadsheet->getActiveSheet();
  29. $sheetData = $worksheet->toArray(null, true, true, true);
  30. // 5. 现在 $sheetData 就是一个二维数组,你可以遍历它处理每一行数据
  31. $orderMode = (new SaasOrder);
  32. $excelData= [];
  33. $expressData = [];
  34. foreach ($sheetData as $rowIndex => $row) {
  35. if ($rowIndex > 0 && !empty($row['A'])) {
  36. $excelData[$rowIndex] = $row;
  37. }
  38. }
  39. if (!empty($excelData)) {
  40. foreach (array_values(array_filter($excelData)) as $key=>$val) {
  41. $order = $orderMode->where("order_sn",trim($val['A']))->findOrEmpty();
  42. if ($order->isEmpty()) {
  43. continue;
  44. }
  45. $order->express_status = 2;
  46. $order->save();
  47. $express = (new SaasOrderExpress)->where("express_sn",trim($val['C']))->findOrEmpty();
  48. if ($express->isEmpty()) {
  49. $expressData[$key] = [
  50. "order_sn" => trim($val['A']),
  51. "express_sn" => trim($val['C']),
  52. "mobile" => trim($val['B']),
  53. "last_at" => getDateFull()
  54. ];
  55. }
  56. }
  57. }
  58. if (!empty($expressData)) {
  59. (new SaasOrderExpress)->insertAll(array_values($expressData));
  60. }
  61. (new SystemImport)->where("id",$data['logId'])->update(['status' => 1,"last_at" => getDateFull()]);
  62. return true;
  63. } catch (\Throwable $throwable) {
  64. echo getDateFull()."==导入订单报错===\n";
  65. echo $throwable->getFile()."\n";
  66. echo $throwable->getLine()."\n";
  67. echo $throwable->getMessage()."\n";
  68. }
  69. return true;
  70. }
  71. }