StationService.php 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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. broadcast(new BroadcastToStation($station_id, $json));
  58. }
  59. function broadcastBinMonitor($station_id, ?StationTask $stationTask){
  60. $this->instant($this->stationTaskService,'StationTaskService');
  61. if($stationTask['status']=='完成')
  62. $stationTask=$this->stationTaskService->getCurrent_shouldProcess_ByStationId($stationTask['station_id']);
  63. if($stationTask)
  64. $stationTask->loadMissing([
  65. "stationTaskCommodities.commodity.barcodes",
  66. "stationTaskCommodities.materialBox",
  67. "stationTaskBatches.batch",
  68. "stationTaskMaterialBoxes.materialBox",
  69. ]);
  70. $this->broadcast($station_id, $stationTask);
  71. }
  72. }