OrderDeliveryBatchLogic.php 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. <?php
  2. namespace app\adminapi\logic\order;
  3. use app\adminapi\validate\order\OrderValidate;
  4. use app\common\enum\DeliveryEnum;
  5. use app\common\enum\OrderEnum;
  6. use app\common\model\AddressLibrary;
  7. use app\common\model\Order;
  8. use app\common\model\DeliveryBatch;
  9. use app\common\model\DeliveryBatchInfo;
  10. use app\common\model\Express;
  11. use app\common\model\OrderGoods;
  12. use think\facade\Db;
  13. class OrderDeliveryBatchLogic
  14. {
  15. static function lists(array $params)
  16. {
  17. $lists = DeliveryBatch::page($params['page_no'] ?? 1)->order('id desc')->limit($params['page_size'] ?? 10)->select()->toArray();
  18. $count = DeliveryBatch::count();
  19. return [ 'count' => $count, 'lists' => $lists ];
  20. }
  21. static function getFailInfoLists($id)
  22. {
  23. return DeliveryBatchInfo::where('batch_id', $id)->where('status', 2)->select()->toArray();
  24. }
  25. static function detail($id)
  26. {
  27. $detail = DeliveryBatch::where('id', $id)->append([ 'progress' ])->find();
  28. return $detail ? $detail->toArray() : [];
  29. }
  30. static function delivery($detail)
  31. {
  32. if ($detail['status'] == 1) {
  33. return true;
  34. }
  35. $infoLists = DeliveryBatchInfo::where('batch_id', $detail['id'])->select()->toArray();
  36. foreach ($infoLists as $info) {
  37. // 订单
  38. $order = Order::where('sn', $info['sn'])->find();
  39. if (empty($order['id'])) {
  40. DeliveryBatch::update([ 'fail' => Db::raw('fail+1') ], [ [ 'id' ,'=', $detail['id'] ] ]);
  41. DeliveryBatchInfo::update([ 'status' => 2, 'fail_content' => '订单不存在' ], [ [ 'id', '=', $info['id'] ] ]);
  42. continue;
  43. }
  44. // 部分发货订单不能批量发货
  45. if ($order['express_status'] == DeliveryEnum::PART_SHIPPED) {
  46. DeliveryBatch::update([ 'fail' => Db::raw('fail+1') ], [ [ 'id' ,'=', $detail['id'] ] ]);
  47. DeliveryBatchInfo::update([ 'status' => 2, 'fail_content' => '订单已部分发货,不能批量发货' ], [ [ 'id', '=', $info['id'] ] ]);
  48. continue;
  49. }
  50. // 快递
  51. $express = Express::where('name', $info['express_name'])->find();
  52. if (empty($express['id'])) {
  53. DeliveryBatch::update([ 'fail' => Db::raw('fail+1') ], [ [ 'id' ,'=', $detail['id'] ] ]);
  54. DeliveryBatchInfo::update([ 'status' => 2, 'fail_content' => '快递公司不存在' ], [ [ 'id', '=', $info['id'] ] ]);
  55. continue;
  56. }
  57. // 检测
  58. $validate = new OrderValidate;
  59. if ($order['order_status'] == OrderEnum::STATUS_WAIT_DELIVERY) {
  60. // 待发货
  61. $orderGoods = OrderGoods::where(['order_id'=>$order['id']])->select()->toArray();
  62. $orderGoodsInfo = [];
  63. foreach ($orderGoods as $value) {
  64. $orderGoodsInfo[] = [
  65. 'order_goods_id' => $value['id'],
  66. 'delivery_num' => $value['goods_num']
  67. ];
  68. }
  69. $data = [
  70. 'id' => $order['id'],
  71. 'send_type' => 1,
  72. 'admin_id' => request()->adminInfo['admin_id'],
  73. 'delivery_address_id' => AddressLibrary::order(['is_deliver_default'=>'desc','id'=>'asc'])->value('id'),
  74. 'return_address_id' => AddressLibrary::order(['is_return_default'=>'desc','id'=>'asc'])->value('id'),
  75. 'parcel' => [
  76. [
  77. 'express_id' => $express['id'],
  78. 'invoice_no' => $info['express_no'],
  79. 'order_goods_info' => $orderGoodsInfo,
  80. ]
  81. ],
  82. 'order_goods_ids' => array_column($orderGoodsInfo,'order_goods_id')
  83. ];
  84. if (true !== $validate->scene('delivery')->check($data)) {
  85. DeliveryBatch::update([ 'fail' => Db::raw('fail+1') ], [ [ 'id' ,'=', $detail['id'] ] ]);
  86. DeliveryBatchInfo::update([ 'status' => 2, 'fail_content' => $validate->getError() ], [ [ 'id', '=', $info['id'] ] ]);
  87. continue;
  88. }
  89. // 发货
  90. if(! (new OrderLogic)->delivery($data)) {
  91. DeliveryBatch::update([ 'fail' => Db::raw('fail+1') ], [ [ 'id' ,'=', $detail['id'] ] ]);
  92. DeliveryBatchInfo::update([ 'status' => 2, 'fail_content' => OrderLogic::getError() ], [ [ 'id', '=', $info['id'] ] ]);
  93. continue;
  94. }
  95. } else if ($order['order_status'] == OrderEnum::STATUS_WAIT_RECEIVE) {
  96. // 已发货 修改单号
  97. $data = [
  98. 'id' => $order['id'],
  99. 'express_id' => $express['id'],
  100. 'invoice_no' => $info['express_no'],
  101. 'delivery_content' => '',
  102. ];
  103. $re_result = (new OrderLogic)->changeDelivery($data);
  104. if($re_result !== true) {
  105. DeliveryBatch::update([ 'fail' => Db::raw('fail+1') ], [ [ 'id' ,'=', $detail['id'] ] ]);
  106. DeliveryBatchInfo::update([ 'status' => 2, 'fail_content' => (string) $re_result ], [ [ 'id', '=', $info['id'] ] ]);
  107. continue;
  108. }
  109. } else {
  110. DeliveryBatch::update([ 'fail' => Db::raw('fail+1') ], [ [ 'id' ,'=', $detail['id'] ] ]);
  111. DeliveryBatchInfo::update([ 'status' => 2, 'fail_content' => '订单不允许操作发货' ], [ [ 'id', '=', $info['id'] ] ]);
  112. continue;
  113. }
  114. // 成功
  115. DeliveryBatch::update([ 'success' => Db::raw('success+1') ], [ [ 'id' ,'=', $detail['id'] ] ]);
  116. DeliveryBatchInfo::update([ 'status' => 1 ], [ [ 'id', '=', $info['id'] ] ]);
  117. }
  118. // 更新 已执行导入
  119. DeliveryBatch::update([ 'status' => 1 ], [ [ 'id' ,'=', $detail['id'] ] ]);
  120. return true;
  121. }
  122. static function importLists(array $lists, $filename)
  123. {
  124. try {
  125. Db::startTrans();
  126. array_shift($lists);
  127. $lists = array_values($lists);
  128. $nums = count($lists);
  129. $batch = DeliveryBatch::create([
  130. 'filename' => $filename,
  131. 'nums' => $nums,
  132. 'success' => 0,
  133. 'fail' => 0,
  134. ]);
  135. $batchInfoArrays = [];
  136. foreach ($lists as $info) {
  137. $batchInfoArrays[] = [
  138. 'batch_id' => $batch->id,
  139. 'sn' => trim($info['A']),
  140. 'express_name' => trim($info['B']),
  141. 'express_no' => trim($info['C']),
  142. 'status' => 0,
  143. 'fail_content' => '',
  144. ];
  145. }
  146. (new DeliveryBatchInfo())->saveAll($batchInfoArrays);
  147. Db::commit();
  148. return [ 'id' => $batch->id ];
  149. } catch(\Throwable $e) {
  150. Db::rollback();
  151. return $e->getMessage();
  152. }
  153. }
  154. }