ShareOrderJob.php 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. <?php
  2. namespace app\jobs\order;
  3. use app\jobs\supplier\SupplierFinanceJob;
  4. use app\services\order\StoreOrderCartInfoServices;
  5. use app\services\order\StoreOrderServices;
  6. use app\services\order\StoreOrderSplitServices;
  7. use app\services\store\SystemStoreServices;
  8. use app\services\supplier\SystemSupplierServices;
  9. use crmeb\basic\BaseJobs;
  10. use crmeb\traits\QueueTrait;
  11. use think\facade\Log;
  12. /**
  13. * 分配订单
  14. * Class ShareOrderJob
  15. * @package app\jobs
  16. */
  17. class ShareOrderJob extends BaseJobs
  18. {
  19. use QueueTrait;
  20. /**
  21. * 门店分配订单
  22. * @param $orderInfo
  23. * @return bool
  24. */
  25. public function doJob($orderInfo)
  26. {
  27. if (!$orderInfo) {
  28. return true;
  29. }
  30. //整单分给门店
  31. /** @var StoreOrderServices $storeOrderServices */
  32. $storeOrderServices = app()->make(StoreOrderServices::class);
  33. $order = $storeOrderServices->get($orderInfo['id']);
  34. if (!$order) {
  35. return true;
  36. }
  37. $orderInfo = $order->toArray();
  38. //已经分配或者门店自提
  39. if (!isset($orderInfo['shipping_type']) || $orderInfo['shipping_type'] != 1 || $orderInfo['store_id'] > 0) {
  40. //分配给门店
  41. SpliteStoreOrderJob::dispatchDo('splitAfter', [$orderInfo]);
  42. return true;
  43. }
  44. try {
  45. $id = (int)$orderInfo['id'];
  46. /** @var StoreOrderCartInfoServices $storeOrderCartInfoServices */
  47. $storeOrderCartInfoServices = app()->make(StoreOrderCartInfoServices::class);
  48. //订单下原商品信息
  49. $cartInfo = $storeOrderCartInfoServices->getCartColunm(['oid' => $id, 'split_status' => [0, 1]], 'cart_id,type,relation_id,is_gift,cart_num,split_surplus_num', 'cart_id');
  50. if (!$cartInfo) {
  51. return true;
  52. }
  53. $suppplierIds = $storeIds = [];
  54. foreach ($cartInfo as $cart) {
  55. $type = $cart['type'] ?? 0;
  56. switch ($type) {
  57. case 0://兼容之前供应商商品
  58. if ($cart['relation_id']) {
  59. $suppplierIds[] = $cart['relation_id'];
  60. }
  61. break;
  62. case 1:
  63. $storeIds[] = $cart['relation_id'];
  64. break;
  65. case 2:
  66. $suppplierIds[] = $cart['relation_id'];
  67. break;
  68. }
  69. }
  70. if ($suppplierIds) {//验证供应商状态(关闭|删除不分配)
  71. /** @var SystemSupplierServices $supplierServices */
  72. $supplierServices = app()->make(SystemSupplierServices::class);
  73. $suppplierIds = $supplierServices->getColumn([['id', 'in', $suppplierIds], ['is_show', '=', 1], ['is_del', '=', 0]], 'id');
  74. }
  75. if ($storeIds) {//验证门店状态(关闭|删除不分配)
  76. $storeServices = app()->make(SystemStoreServices::class);
  77. $storeIds = $storeServices->getColumn([['id', 'in', $storeIds], ['is_show', '=', 1], ['is_del', '=', 0]], 'id');
  78. }
  79. $cart_ids = [];
  80. $other_cart_ids = [];
  81. //分配给供应商、门店
  82. if ($suppplierIds || $storeIds) {
  83. $suppplier_id = $suppplierIds[0] ?? 0;
  84. $store_id = $storeIds[0] ?? 0;
  85. $updateData = [];
  86. //先拆分供应商
  87. if ($suppplier_id) {
  88. foreach ($cartInfo as $cart_id => $cart) {
  89. if ($cart['type'] == 2 && $cart['relation_id'] == $suppplier_id) {//拆分
  90. $cart_ids[] = ['cart_id' => $cart_id, 'cart_num' => $cart['cart_num']];
  91. } else {
  92. $other_cart_ids[] = ['cart_id' => $cart_id, 'cart_num' => $cart['cart_num']];
  93. }
  94. }
  95. $updateData['supplier_id'] = $suppplier_id;
  96. } elseif ($store_id) {
  97. foreach ($cartInfo as $cart_id => $cart) {
  98. if ($cart['type'] == 1 && $cart['relation_id'] == $store_id) {//拆分
  99. $cart_ids[] = ['cart_id' => $cart_id, 'cart_num' => $cart['cart_num']];
  100. } else {
  101. $other_cart_ids[] = ['cart_id' => $cart_id, 'cart_num' => $cart['cart_num']];
  102. }
  103. }
  104. $updateData['store_id'] = $store_id;
  105. }
  106. //下单商品都是某一个供应商|| 门店商品,不用拆分
  107. if (!$other_cart_ids && (count($suppplierIds) == 1 || count($storeIds) == 1)) {
  108. $storeOrderServices->update(['id' => $id], $updateData);
  109. if (isset($updateData['store_id'])) {//拆分门店订单
  110. $orderInfo['store_id'] = $store_id;
  111. SpliteStoreOrderJob::dispatchDo('splitAfter', [$orderInfo]);
  112. } elseif (isset($updateData['supplier_id'])) {
  113. //供应商账单流水
  114. SupplierFinanceJob::dispatch([$id, 1]);
  115. }
  116. } else {
  117. //分配订单
  118. /** @var StoreOrderSplitServices $storeOrderSplitServices */
  119. $storeOrderSplitServices = app()->make(StoreOrderSplitServices::class);
  120. $splitResult = $storeOrderSplitServices->equalSplit($id, $cart_ids);
  121. $otherOrder = [];
  122. if ($splitResult) {//拆分供应商订单
  123. [$orderInfo, $otherOrder] = $splitResult;
  124. }
  125. $storeOrderServices->update(['id' => $orderInfo['id']], $updateData);
  126. if (isset($updateData['store_id'])) {//拆分门店订单
  127. $orderInfo['store_id'] = $updateData['store_id'];
  128. SpliteStoreOrderJob::dispatchDo('splitAfter', [$orderInfo]);
  129. } elseif (isset($updateData['supplier_id'])) {
  130. //供应商账单流水
  131. SupplierFinanceJob::dispatch([$orderInfo['id'], 1]);
  132. }
  133. //还有商品
  134. if ($other_cart_ids && $otherOrder) {
  135. //还有其他供应商 || 门店 继续分配
  136. if (count($suppplierIds) >= 1 || count($storeIds) >= 1) {
  137. ShareOrderJob::dispatch([$otherOrder]);
  138. }
  139. }
  140. }
  141. } else {//平台配送
  142. SpliteStoreOrderJob::dispatchDo('splitAfter', [$orderInfo]);
  143. }
  144. } catch (\Throwable $e) {
  145. Log::error('自动拆分供应商订单失败,原因:' . $e->getMessage() . $e->getFile() . $e->getLine());
  146. }
  147. return true;
  148. }
  149. }