StationService.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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\Support\Facades\Cache;
  9. use App\Traits\ServiceAppAop;
  10. class StationService
  11. {
  12. use ServiceAppAop;
  13. protected $modelClass=Station::class;
  14. /** @var $stationTaskService StationTaskService */
  15. protected $stationTaskService;
  16. /**
  17. * @param string $typeName
  18. * @return Station
  19. * @throws Exception
  20. */
  21. function getStation_byType(string $typeName):Station{
  22. $station= Cache::remember('Station_typeName_'.$typeName,
  23. config('cache.expirations.rarelyChange'),
  24. function ()use($typeName) {
  25. $stationType= StationType::query()->where('name',$typeName)->orderBy('id')->get('id')->first();
  26. if(!$stationType) throw new Exception('指定站类型获取不到');
  27. return Station::query()->where('station_type_id',$stationType['id'])->first();
  28. });
  29. if(!$station)throw new Exception('默认站获取不到');
  30. return $station;
  31. }
  32. function getULineEntrance(Station $station):?Station{
  33. $station->loadMissing(['stationType','child']);
  34. if (($station['stationType']['name']??'')=='料箱出货口'){
  35. return $station;
  36. }
  37. if (($station['child']['stationType']['name']??'')=='料箱出货口'){
  38. return $station['child'];
  39. }
  40. return null;
  41. }
  42. function getULineExit(Station $station):?Station{
  43. $station->loadMissing(['stationType','child']);
  44. if ($station['stationType']['name']??''=='料箱入货口'){
  45. return $station;
  46. }
  47. if ($station['child']['stationType']['name']??''=='料箱入货口'){
  48. return $station['child'];
  49. }
  50. return null;
  51. }
  52. function broadcast($station_id, ?StationTask $stationTask){
  53. if($stationTask)
  54. $json = $stationTask->toJson();
  55. else
  56. $json =[];
  57. LogService::log('海柔请求','broadcastBinMonitor',
  58. $station_id.'|'.$json);
  59. broadcast(new BroadcastToStation($station_id, $json));
  60. }
  61. function broadcastBinMonitor($station_id, ?StationTask $stationTask){
  62. if(!$stationTask)return;
  63. $this->instant($this->stationTaskService,'StationTaskService');
  64. if($stationTask['status']=='完成')
  65. $stationTask=$this->stationTaskService->getCurrent_shouldProcess_ByStationId($stationTask['station_id']);
  66. if($stationTask)
  67. $stationTask->loadMissing([
  68. "stationTaskCommodities.commodity.barcodes",
  69. "stationTaskCommodities.materialBox",
  70. "stationTaskBatches.batch",
  71. "stationTaskMaterialBoxes.materialBox",
  72. ]);
  73. $this->broadcast($station_id, $stationTask);
  74. }
  75. }