ExportOrder.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. <?php
  2. namespace app\queue\redis\slow;
  3. use app\model\saas\SaasOrder;
  4. use app\model\system\SystemExport;
  5. use app\service\saas\OrderService;
  6. use PhpOffice\PhpSpreadsheet\Spreadsheet;
  7. use PhpOffice\PhpSpreadsheet\Writer\Xlsx;
  8. use Webman\RedisQueue\Consumer;
  9. class ExportOrder implements Consumer
  10. {
  11. /**
  12. * 订单导出
  13. * 更新云打印件
  14. * @var string
  15. */
  16. public string $queue = "export-order";
  17. /**
  18. * 连接配置
  19. * @var string
  20. */
  21. public string $connection = "default";
  22. protected array $status = [
  23. "0" => "待支付",
  24. "1" => "待使用",
  25. "2" => "已完成",
  26. "3" => "已退款",
  27. "4" => "退款中",
  28. "5" => "已关闭",
  29. "6" => "部分退款",
  30. "7" => "履约中",
  31. "8" => "退款审核中",
  32. "9" => "退款审核中",
  33. ];
  34. protected array $express = [
  35. "0" => "待订单完成",
  36. "1" => "待发货",
  37. "2" => "已发货",
  38. "3" => "已签收",
  39. ];
  40. public function consume($data): bool
  41. {
  42. try {
  43. if(empty($data['logId'])) return true;
  44. $spreadsheet = new Spreadsheet();
  45. $title = ['店铺名称','下单人昵称','订单编号','订单状态','发货状态', '收件人', '手机号码', '收货地址','商品ID','团单标题','兑换商品', '创建时间'];
  46. $sheet = $spreadsheet->getActiveSheet();
  47. $titCol = 'A';
  48. foreach ($title as $key => $value) {
  49. // 单元格内容写入
  50. $sheet->setCellValue($titCol . '1', $value);
  51. $titCol++;
  52. }
  53. $row = 2; // 从第二行开始
  54. $searchKey = (new OrderService)->searchFilter($data['param']);
  55. $orderData = (new SaasOrder)->where($searchKey)->with(['address' => function($query) {
  56. $query->with(['goods']);
  57. },'poi','user','product'])->order("create_at","desc")->select();
  58. if ($orderData->isEmpty()) {
  59. (new SystemExport)->where("id",$data['logId'])->update(['status' => 2]);
  60. return true;
  61. }
  62. foreach ($orderData as $item) {
  63. $dataCol = 'A';
  64. $address = "-";
  65. if (!empty($item['address'])) {
  66. $address = $item['address']['city_text'].$item['address']['address'];
  67. }
  68. $goodsTitle = $item['product']['product_name']??'-';
  69. if ($item['product']['is_new'] == 1) {
  70. $goodsTitle = "{$item['product']['price']}元代{$item['product']['line_price']}元商场权益券{$item['product']['product_name']}";
  71. }
  72. $newData = [
  73. "shop" => $item['poi']['poi_name']??'-', // 店铺名称
  74. "nickname" => $item['user']['nickname']??'-', // 下单人昵称
  75. "order" => $item['order_sn']??'-', // 订单编号
  76. "status" => $this->status[$item['status']], // 订单状态
  77. "express" => $this->express[$item['express_status']], // 订单状态
  78. "user" => $item['address']['nickname']??'-',
  79. "mobile" => $item['address']['mobile']??'-',
  80. "address" => $address,
  81. "goods_id" => $item['product']['goods_id']??'-',
  82. "goods_name" => $goodsTitle,
  83. "product" => $item['address']['goods']['title']??'-',
  84. "create_at" => $item['create_at'],
  85. ];
  86. foreach ($newData as $value) {
  87. $sheet->setCellValue($dataCol . $row, ' '.$value);
  88. $dataCol++;
  89. }
  90. $row++;
  91. }
  92. $write = new Xlsx($spreadsheet);
  93. $pathName = public_path().DIRECTORY_SEPARATOR."download".DIRECTORY_SEPARATOR.$data['param']['fileName'];
  94. echo $pathName."\n";
  95. (new SystemExport)->where("id",$data['logId'])->update([
  96. "down_url" => "https://tran.jsshuita.cn/download/".$data['param']['fileName'],
  97. "status" => 1
  98. ]);
  99. $write->save($pathName);
  100. } catch (\Throwable $throwable) {
  101. echo getDateFull()."==导出订单报错===\n";
  102. echo $throwable->getFile()."\n";
  103. echo $throwable->getLine()."\n";
  104. echo $throwable->getMessage()."\n";
  105. }
  106. return true;
  107. }
  108. }