StationService.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. <?php
  2. namespace App\Services;
  3. use App\Events\BroadcastToStation;
  4. use App\Station;
  5. use App\StationTask;
  6. use App\StationType;
  7. use Exception;
  8. use Illuminate\Database\Eloquent\Model;
  9. use Illuminate\Support\Facades\Cache;
  10. use App\Traits\ServiceAppAop;
  11. class StationService
  12. {
  13. use ServiceAppAop;
  14. protected $modelClass=Station::class;
  15. /** @var $stationTaskService StationTaskService */
  16. protected $stationTaskService;
  17. /**
  18. * @param string $typeName
  19. * @return Station
  20. * @throws Exception
  21. */
  22. function getStation_byType(string $typeName):Station{
  23. $station= Cache::remember('Station_typeName_'.$typeName,
  24. config('cache.expirations.rarelyChange'),
  25. function ()use($typeName) {
  26. $stationType= StationType::query()->where('name',$typeName)->orderBy('id')->get('id')->first();
  27. if(!$stationType) throw new Exception('指定站类型获取不到');
  28. return Station::query()->where('station_type_id',$stationType['id'])->first();
  29. });
  30. if(!$station)throw new Exception('默认站获取不到');
  31. return $station;
  32. }
  33. function getULineEntrance(Station $station):?Station{
  34. $station->loadMissing(['stationType','child']);
  35. if (($station['stationType']['name']??'')=='料箱出货口'){
  36. return $station;
  37. }
  38. if (($station['child']['stationType']['name']??'')=='料箱出货口'){
  39. return $station['child'];
  40. }
  41. return null;
  42. }
  43. function getULineExit(Station $station):?Station{
  44. $station->loadMissing(['stationType','child']);
  45. if ($station['stationType']['name']??''=='料箱入货口'){
  46. return $station;
  47. }
  48. if ($station['child']['stationType']['name']??''=='料箱入货口'){
  49. return $station['child'];
  50. }
  51. return null;
  52. }
  53. function broadcast($station_id, ?StationTask $stationTask){
  54. LogService::log('海柔请求in2','broadcast',
  55. $station_id.'|'.json_encode($stationTask));
  56. if($stationTask)
  57. $json = $stationTask->toJson();
  58. else
  59. $json =[];
  60. LogService::log('海柔请求done2','broadcast',
  61. $station_id.'|'.$json);
  62. broadcast(new BroadcastToStation($station_id, $json));
  63. }
  64. function broadcastBinMonitor($station_id, ?StationTask $stationTask){
  65. LogService::log('海柔请求in1','broadcastBinMonitor',
  66. $station_id.'|'.json_encode($stationTask));
  67. if(!$stationTask)return;
  68. $this->instant($this->stationTaskService,'StationTaskService');
  69. if($stationTask['status']=='完成')
  70. $stationTask=$this->stationTaskService->getCurrent_shouldProcess_ByStationId($stationTask['station_id']);
  71. if($stationTask)
  72. $stationTask->loadMissing([
  73. "stationTaskCommodities.commodity.barcodes",
  74. "stationTaskCommodities.stationTaskMaterialBox",
  75. "stationTaskBatches.batch",
  76. "stationTaskMaterialBoxes.materialBox",
  77. ]);
  78. LogService::log('海柔请求done1','broadcastBinMonitor',
  79. $station_id.'|'.json_encode($stationTask));
  80. $this->broadcast($station_id, $stationTask);
  81. }
  82. /**
  83. * 获取镜像映射库位
  84. *
  85. * @param string $mirrorLocation
  86. *
  87. * @return Model|null
  88. */
  89. public function getMirrorMappingLocation($mirrorLocation)
  90. {
  91. if (!$mirrorLocation)return null;
  92. return Station::query()->where("code",substr($mirrorLocation,2))->first();
  93. }
  94. /**
  95. * 是否为半箱缓存架位置
  96. *
  97. * @param Station|null|\stdClass $station
  98. * @return bool
  99. */
  100. public function isHalfBoxLocation(?Station $station):bool
  101. {
  102. if (!$station)return false;
  103. return $station->parent_id==7;
  104. }
  105. /**
  106. * 是否为缓存架位置
  107. *
  108. * @param Station|\stdClass $station
  109. *
  110. * @return bool
  111. */
  112. public function isCacheShelfLocation(Station $station):bool
  113. {
  114. return $station->station_type_id == 5;
  115. }
  116. }