StationTaskMaterialBoxService.php 6.3 KB

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