BatchService.php 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. <?php
  2. namespace App\Services;
  3. use App\Batch;
  4. use App\Events\BroadcastToStation;
  5. use App\Exceptions\ErrorException;
  6. use App\Jobs\BatchTaskJob;
  7. use App\OracleActAllocationDetails;
  8. use App\Order;
  9. use App\OrderBin;
  10. use App\OrderCommodity;
  11. use App\Owner;
  12. use App\Station;
  13. use Exception;
  14. use Illuminate\Support\Collection;
  15. use Illuminate\Support\Facades\Cache;
  16. use Illuminate\Support\Facades\DB;
  17. use Illuminate\Support\Facades\Http;
  18. use App\Traits\ServiceAppAop;
  19. use Illuminate\Support\Facades\Log;
  20. class BatchService
  21. {
  22. use ServiceAppAop;
  23. protected $modelClass=Batch::class;
  24. /** @var StationTaskBatchService $stationTaskBatchService */
  25. private $stationTaskBatchService;
  26. /** @var StationRuleBatchService $stationRuleBatchService */
  27. private $stationRuleBatchService;
  28. /** @var StationTaskMaterialBoxService $stationTaskMaterialBoxService */
  29. private $stationTaskMaterialBoxService;
  30. /** @var StationTaskCommodityService $stationTaskCommodityService */
  31. private $stationTaskCommodityService;
  32. /** @var StationTaskService $stationTaskService */
  33. private $stationTaskService;
  34. public function __construct(){
  35. $this->stationTaskBatchService=null;
  36. $this->stationRuleBatchService=null;
  37. $this->stationTaskMaterialBoxService=null;
  38. $this->stationTaskCommodityService=null;
  39. $this->stationTaskService=null;
  40. }
  41. public function get(array $params)
  42. {
  43. $query = Batch::query();
  44. foreach ($params as $column=>$param){
  45. if (is_array($param))$query->whereIn($column,$param);
  46. else $query->where($column,$param);
  47. }
  48. return $query->get();
  49. }
  50. public function updateWhereIn($key,$values,$updateKeyValues){
  51. Batch::query()->whereIn($key,$values)->update($updateKeyValues);
  52. }
  53. /**
  54. * 为波次附加任务,已附加的重复任务不影响
  55. * @param $batches
  56. * @throws Exception
  57. */
  58. public function assignTasks($batches)
  59. {
  60. app('LogService')->log('海柔','assignTasks1',json_encode($batches));
  61. try{
  62. $batches = collect($batches);
  63. $this->instant($this->stationTaskBatchService,'StationTaskBatchService');
  64. $this->instant($this->stationRuleBatchService,'StationRuleBatchService');
  65. $this->instant($this->stationTaskService,'StationTaskService');
  66. $this->instant($this->stationTaskCommodityService,'StationTaskCommodityService');
  67. $this->instant($this->stationTaskMaterialBoxService,'StationTaskMaterialBoxService');
  68. $stationTasks=null;
  69. $batches_shouldProcess = $this->stationRuleBatchService->getBatches_shouldProcess($batches); //按规则过滤需要的波次
  70. if($batches_shouldProcess->isEmpty()) return;
  71. app('LogService')->log('海柔','assignTasks3',json_encode($batches_shouldProcess));
  72. DB::transaction(function ()use($batches,&$batches_shouldProcess,&$stationTasks){
  73. $stationTasks = $this->stationTaskService->create($batches_shouldProcess->count()); //生成总任务
  74. $this->stationTaskBatchService->createByBatches($batches_shouldProcess,$stationTasks); //注册波次任务
  75. $this->stationTaskMaterialBoxService->createByBatches($batches_shouldProcess,$stationTasks); //注册料箱任务
  76. $this->stationTaskCommodityService->createByBatches($batches_shouldProcess,$stationTasks); //注册商品任务
  77. });
  78. foreach ($stationTasks as &$stationTask){
  79. $stationTask->loadMissing([
  80. "stationTaskCommodities.commodity.barcodes",
  81. "stationTaskCommodities.stationTaskMaterialBox",
  82. "stationTaskBatches.batch.owner",
  83. "stationTaskMaterialBoxes.materialBox",
  84. ]);
  85. }
  86. $jsonStationTasks=json_encode($stationTasks,true);
  87. broadcast(new BroadcastToStation(BroadcastToStation::ALL_STATION, $jsonStationTasks));
  88. }catch(Exception $e){
  89. $batchesJson='';
  90. foreach ($batches as $batch){
  91. $batchesJson.=json_encode($batch->code);
  92. Cache::tags(['波次防重叠'.$batch['id']])->flush();
  93. }
  94. Log::error("注册任务失败",["msg"=>$e->getMessage(),"json"=>$batchesJson]);
  95. throw new Exception('注册任务失败: '. $batchesJson);
  96. }
  97. }
  98. public function getBatchByCodes($codes)
  99. {
  100. if(empty($codes))return collect();
  101. if(count($codes) == 0)return collect();
  102. return Batch::query()->whereIn('code',$codes)->get();
  103. }
  104. /**
  105. * 检查批次订单信息,存在库位异常 直接删除所有所属商品 重新拉取
  106. *
  107. * @param Collection|array $batches
  108. */
  109. public function checkBatchOrderInfo($batches)
  110. {
  111. if (!is_array($batches))$batches = $batches->toArray();
  112. $ids = array_column($batches,"id");
  113. $batches = Batch::query()->whereIn("id",$ids)->with("orders.orderCommodities")->get();
  114. foreach ($batches as $batch){
  115. if (!$batch->orders)continue;
  116. foreach ($batch->orders as $order){
  117. if (!$order->orderCommodities)app("OrderService")->notExistToRecover($order);
  118. else{
  119. $mark = false;
  120. foreach ($order->orderCommodities as $orderCommodity){
  121. if (!$orderCommodity->location){
  122. $mark = true;break;
  123. }
  124. }
  125. if ($mark){
  126. OrderCommodity::query()->where("order_id",$order->id)->delete();
  127. app("OrderService")->notExistToRecover($order);
  128. }
  129. }
  130. }
  131. }
  132. }
  133. /**
  134. * 修复波次
  135. */
  136. public function repairBatch(string $code):bool
  137. {
  138. if (!$code)return false;
  139. $wave = DB::connection("oracle")->selectOne(DB::raw("select * from DOC_WAVE_HEADER where WAVENO = ?"),[$code]);
  140. if (!$wave)return false;
  141. $owner = app("OwnerService")->codeGetOwner($wave->customerid);
  142. $obj = [
  143. "wms_status" => Batch::WMS_STATUS[$wave->wavestatus] ?? $wave->wavestatus,
  144. "wms_type"=>$wave->descr,
  145. "created_at"=>date("Y-m-d H:i:s"),
  146. "wms_created_at"=>$wave->addtime,
  147. "updated_at"=>$wave->edittime,
  148. "owner_id"=>$owner->id,
  149. ];
  150. $wave = Batch::query()->where("code",$code)->first();
  151. if (!$wave) $wave = Batch::query()->create($obj);
  152. else Batch::query()->where("code",$code)->update($obj);
  153. $orderNos = array_column(DB::connection("oracle")->select(DB::raw("select orderno from DOC_WAVE_DETAILS where WAVENO = ?"),[$code]),"orderno");
  154. $count = Order::query()->whereIn("code",$orderNos)->count();
  155. if (count($orderNos)!=$count)app("OrderService")->syncOrderByCodes($orderNos);
  156. Order::query()->whereIn("code",$orderNos)->update(["batch_id"=>$wave->id]);
  157. $orders = Order::query()->with(["batch","bin"])->whereIn("code",$orderNos)->get();
  158. if (count($orderNos)!=count($orders))return false;
  159. foreach ($orders as $order){
  160. if ($order->bin)continue;
  161. $bin = DB::connection("oracle")->selectOne(DB::raw("select seqno from DOC_WAVE_DETAILS where waveno = ? and orderno = ?"),
  162. [$order->batch->code,$order->code]);
  163. if ($bin) OrderBin::query()->create([
  164. 'order_id' => $order->id,
  165. 'number' => $bin->seqno,
  166. ]);
  167. }
  168. return true;
  169. }
  170. }