StationTaskMaterialBoxService.php 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. <?php
  2. namespace App\Services;
  3. use App\Batch;
  4. use App\Exceptions\ErrorException;
  5. use App\MaterialBox;
  6. use App\OrderCommodity;
  7. use App\StationTask;
  8. use App\StationTaskMaterialBox;
  9. use Illuminate\Support\Collection;
  10. use Illuminate\Support\Facades\Cache;
  11. use App\Traits\ServiceAppAop;
  12. class StationTaskMaterialBoxService
  13. {
  14. use ServiceAppAop;
  15. /** @var StationService $stationService */
  16. private $stationService;
  17. /** @var StationTypeService $stationTypeService */
  18. private $stationTypeService;
  19. /** @var StationTaskService $stationTaskService */
  20. private $stationTaskService;
  21. /** @var StationTaskBatchService $stationTaskBatchService */
  22. private $stationTaskBatchService;
  23. /** @var StationTaskCommodityService $stationTaskCommodityService */
  24. private $stationTaskCommodityService;
  25. /** @var MaterialBoxService $materialBoxService */
  26. private $materialBoxService;
  27. public function __construct(){
  28. $this->stationService=null;
  29. $this->stationTypeService=null;
  30. $this->stationTaskService=null;
  31. $this->materialBoxService=null;
  32. $this->stationTaskBatchService=null;
  33. $this->stationTaskCommodityService=null;
  34. }
  35. function createByBatches(Collection $batches,Collection $stationTasks_toAttach): Collection
  36. {
  37. $stationTaskMaterialBoxes = (function () use ($batches) {
  38. $stationTaskMaterialBoxes_listByBatch = new Collection();
  39. foreach ($batches as $batch) {
  40. $stationTaskMaterialBoxes_listByBatch->push(
  41. $this->createByBatch($batch)
  42. );
  43. }
  44. return $stationTaskMaterialBoxes_listByBatch;
  45. })();
  46. $this->stationTaskService
  47. ->registerSubTasks(
  48. $stationTasks_toAttach,
  49. $stationTaskMaterialBoxes);
  50. return $stationTaskMaterialBoxes;
  51. }
  52. function createByBatch(Batch $batch): Collection
  53. {
  54. $this->instant($this->materialBoxService,'MaterialBoxService');
  55. $this->instant($this->stationTypeService,'StationTypeService');
  56. $this->instant($this->stationService,'StationService');
  57. $stationMaterialBoxes_toCreate=new Collection();
  58. $order_ids=data_get($batch['orders'],'*.id');
  59. $orderCommodities=OrderCommodity::query()->with('orderBin')->whereIn('order_id',$order_ids)->get();
  60. if($orderCommodities->isEmpty())return ;
  61. $stationType=$this->stationTypeService->getForMaterialBox_onBatchProcess();
  62. foreach ($orderCommodities as $orderCommodity){
  63. $station=$this->stationService->getStation_byType($stationType['name']);
  64. $materialBox=$this->materialBoxService->firstOrCreate(['code' => $orderCommodity['location']]);
  65. $stationMaterialBoxes_toCreate->push([
  66. 'station_id'=>$station['id'],
  67. 'material_box_id'=>$materialBox['id'],
  68. 'status'=>'待处理'
  69. ]);
  70. }
  71. $this->insert($stationMaterialBoxes_toCreate->toArray());
  72. }
  73. function get(array $kvPairs){
  74. ksort($kvPairs);
  75. return Cache::remember('StationTaskMaterialBox'.md5(json_encode($kvPairs)), config('cache.expirations.fastChange'), function ()use($kvPairs) {
  76. $query = StationTaskMaterialBox::query();
  77. foreach ($kvPairs as $column => $value){
  78. if (is_array($value))$query->whereIn($column,$value);
  79. else $query->where($column,$value);
  80. }
  81. return $query->get();
  82. });
  83. }
  84. public function insert(array $stationTaskMaterialBoxes): bool
  85. {
  86. $inserted = StationTaskMaterialBox::query()->insert($stationTaskMaterialBoxes);
  87. LogService::log(__METHOD__,__FUNCTION__,json_encode($stationTaskMaterialBoxes).
  88. '||'.json_encode(array_slice(debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS),0,3)));
  89. return $inserted;
  90. }
  91. function markHasPut(StationTaskMaterialBox $stationTaskMaterialBox){
  92. try{
  93. $taskType=$this->getServingTaskType($stationTaskMaterialBox);
  94. switch ($taskType){
  95. case '分波次':
  96. $this->markProcessing($stationTaskMaterialBox);
  97. $this->stationTaskBatchService->markProcessing($stationTaskMaterialBox['stationTaskBatch']);
  98. $this->stationTaskCommodityService->markProcessing($stationTaskMaterialBox['stationTaskCommodities']);
  99. break;
  100. case '入立库':
  101. break;
  102. case '入库':break;
  103. }
  104. }catch (\Exception $e){
  105. throw new ErrorException('放置料箱出错');
  106. }
  107. }
  108. function markTaken($stationTaskMaterialBox){
  109. }
  110. function markProcessed(StationTaskMaterialBox $stationTaskMaterialBox){
  111. $stationTaskMaterialBox['status'] = '完成';
  112. $stationTaskMaterialBox->save();
  113. $this->stationService->broadcastBinMonitor($stationTaskMaterialBox['station_id'],$stationTaskMaterialBox['stationTask']);
  114. }
  115. function markProcessing($stationTaskMaterialBox)
  116. {
  117. if (get_class($stationTaskMaterialBox)==StationTaskMaterialBox::class){
  118. $stationTaskMaterialBox = collect($stationTaskMaterialBox);
  119. }
  120. StationTaskMaterialBox::query()
  121. ->whereIn('id', data_get($stationTaskMaterialBox, '*.id'))
  122. ->update(['status'=>'处理中']);
  123. $this->stationTaskService
  124. ->markProcessing_byId(
  125. data_get($stationTaskMaterialBox, '*.station_id')
  126. );
  127. }
  128. function excepted($stationTaskMaterialBox_orBox){
  129. switch (get_class($stationTaskMaterialBox_orBox)){
  130. // case MaterialBox::class:
  131. // case StationTaskMaterialBox::class:
  132. }
  133. }
  134. function getServingTaskType(StationTaskMaterialBox $stationTaskMaterialBox): string
  135. {
  136. $stationTaskMaterialBox->loadMissing('station.stationType');
  137. if($isBatching=(
  138. $stationTaskMaterialBox['station_task_batch_id'] &&
  139. $stationTaskMaterialBox['station']['stationType'] == '料箱监视器')
  140. ){
  141. return '分波次';
  142. }
  143. if($isPuttingBack=(
  144. !$stationTaskMaterialBox &&
  145. $stationTaskMaterialBox['station']['stationType'] == '立库')
  146. ){
  147. return '入立库';
  148. }
  149. if($isStoring=false){
  150. return '入库';
  151. }
  152. throw new ErrorException('当前类型找不到');
  153. }
  154. }