ImportOrder.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. <?php
  2. namespace app\queue\redis\slow;
  3. use app\model\saas\SaasChatMsg;
  4. use app\model\saas\SaasOrder;
  5. use app\model\saas\SaasOrderExpress;
  6. use app\model\system\SystemImport;
  7. use app\model\system\SystemUser;
  8. use PhpOffice\PhpSpreadsheet\IOFactory;
  9. use Webman\RedisQueue\Consumer;
  10. class ImportOrder implements Consumer
  11. {
  12. /**
  13. * 订单导入发货
  14. * 更新云打印件
  15. * @var string
  16. */
  17. public string $queue = "import-order";
  18. /**
  19. * 连接配置
  20. * @var string
  21. */
  22. public string $connection = "default";
  23. public function consume($data): bool
  24. {
  25. try {
  26. if(empty($data['logId'])) return true;
  27. if(empty($data['poi_id'])) return true;
  28. $path = public_path().$data['path'];
  29. $spreadsheet = IOFactory::load($path);
  30. $worksheet = $spreadsheet->getActiveSheet();
  31. $sheetData = $worksheet->toArray(null, true, true, true);
  32. // 5. 现在 $sheetData 就是一个二维数组,你可以遍历它处理每一行数据
  33. $orderMode = (new SaasOrder);
  34. $excelData= [];
  35. $expressData = [];
  36. foreach ($sheetData as $rowIndex => $row) {
  37. if ($rowIndex > 0 && !empty($row['A'])) {
  38. $excelData[$rowIndex] = $row;
  39. }
  40. }
  41. print_r($excelData);
  42. if (!empty($excelData)) {
  43. foreach (array_values(array_filter($excelData)) as $key=>$val) {
  44. $order = $orderMode->where("order_sn",trim($val['A']))->findOrEmpty();
  45. if ($order->isEmpty()) {
  46. continue;
  47. }
  48. $order->express_status = 2;
  49. $order->save();
  50. $express = (new SaasOrderExpress)->where("express_sn",trim($val['C']))->findOrEmpty();
  51. if ($express->isEmpty()) {
  52. // 发送一条发货消息给用户
  53. $lastMsg = (new SaasChatMsg)->where(['openid' => $order['openid'],'poi_id' => $order['poi_id']])->order("id desc")->findOrEmpty();
  54. if ($lastMsg->isEmpty()) {
  55. $serviceUser = (new SystemUser)->where(['store_id' => $order['poi_id'],"type" => 3])->order("last_msg_at","asc")->findOrEmpty();
  56. $lastServiceId = $serviceUser['id'];
  57. } else {
  58. $lastServiceId = $lastMsg['service_id'];
  59. }
  60. $expressName = "";
  61. if (isset($val['D'])) {
  62. $expressName = "({$val['D']})";
  63. }
  64. (new SaasChatMsg)->insertGetId([
  65. 'openid' => $order['openid'],
  66. 'poi_id' => $order['poi_id'],
  67. "source" => 2,
  68. "service_id" => $lastServiceId,
  69. "msgId" => md5($order['openid'].time()),
  70. "content" => "您的订单({$val['A']})预计3日内到你手中,快递单号:{$val['C']} {$expressName}",
  71. "type" => "text"
  72. ]);
  73. $expressData[$key] = [
  74. "order_sn" => trim($val['A']),
  75. "express_sn" => trim($val['C']),
  76. "mobile" => trim($val['B']),
  77. "express_name" => $val['D']?trim($val['D']):'',
  78. "last_at" => getDateFull()
  79. ];
  80. }
  81. }
  82. }
  83. if (!empty($expressData)) {
  84. (new SaasOrderExpress)->insertAll(array_values($expressData));
  85. }
  86. (new SystemImport)->where("id",$data['logId'])->update(['status' => 1,"last_at" => getDateFull()]);
  87. return true;
  88. } catch (\Throwable $throwable) {
  89. echo getDateFull()."==导入订单报错===\n";
  90. echo $throwable->getFile()."\n";
  91. echo $throwable->getLine()."\n";
  92. echo $throwable->getMessage()."\n";
  93. }
  94. return true;
  95. }
  96. }