BatchService.php 8.0 KB

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