StationRuleBatchService.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. <?php
  2. namespace App\Services;
  3. use App\Batch;
  4. use App\StationRuleBatch;
  5. use App\StationTaskBatch;
  6. use App\StationType;
  7. use Illuminate\Support\Collection;
  8. use Illuminate\Support\Facades\Cache;
  9. use App\Traits\ServiceAppAop;
  10. use Illuminate\Support\Facades\DB;
  11. class StationRuleBatchService
  12. {
  13. use ServiceAppAop;
  14. protected $modelClass=StationRuleBatch::class;
  15. function getByBatch(?Batch $batch): ?StationRuleBatch
  16. {
  17. LogService::log(__METHOD__,'getByBatch','波次任务分配1.21:'.json_encode($batch));
  18. $batchType = $batch['type'] ?? 'null';
  19. $ownerId = $batch['owner_id'] ?? 'null';
  20. LogService::log(__METHOD__,'getByBatch','波次任务分配1.22:'.($this->isLocationOfRobot($batch)));
  21. $batch->loadMissing('stationTaskBatch');
  22. // if($batch['stationTaskBatch'])return null;//? 这行有啥用?
  23. return Cache::remember('stationRuleBatch_batchType_'.$batchType.'_ownerId_'.$ownerId, config('cache.expirations.rarelyChange'),function()use($batch){
  24. $builder= StationRuleBatch::query()->with('stationType')
  25. ->where('owner_id',$batch['owner_id']);
  26. if($batch['type']
  27. && $batch['type']!='无'){
  28. $builder=$builder->where('batch_type',$batch['type']);
  29. }
  30. LogService::log(__METHOD__,'getByBatch','波次任务分配1.23:');
  31. return $builder->first();
  32. });
  33. }
  34. function isLocationOfRobot(?Batch $batch): bool
  35. {
  36. LogService::log(__METHOD__,'getByBatch','波次任务分配1.21I1:'.json_encode($batch));
  37. if(!$batch)return false;
  38. LogService::log(__METHOD__,'getByBatch','波次任务分配1.22I2:'.json_encode($batch));
  39. $sql = "select count(*) as count from order_commodities where location like 'IDE%' and order_id in (select id from orders where batch_id in (select id from batches where id = ?))";
  40. LogService::log(__METHOD__,'getByBatch','波次任务分配1.22I3:');
  41. LogService::log(__METHOD__,'getByBatch','波次任务分配1.22I3.2:'.$sql);
  42. $billDetails = DB::select(DB::raw($sql),[$batch['id']]);
  43. LogService::log(__METHOD__,'getByBatch','波次任务分配1.22I4:'.json_encode($batch).json_encode($billDetails).($billDetails[0]->count > 0));
  44. return $billDetails[0]->count > 0;
  45. }
  46. function getStationType_toBeTask(Batch $batch): ?StationType{
  47. $stationRuleBatch=$this->getByBatch($batch);
  48. if(!$stationRuleBatch)return null;
  49. return $stationRuleBatch['stationType'];
  50. }
  51. /**
  52. * @param Collection $batches
  53. * @return Collection Batches
  54. */
  55. function getBatches_shouldProcess(Collection $batches): Collection
  56. {
  57. LogService::log(__METHOD__,'shouldProcess','波次任务分配1.1:'.json_encode($batches));
  58. $batches_toProcess=collect();
  59. $batches_inTask=StationTaskBatch::query()->whereIn('batch_id',data_get($batches,'*.id'))->get();
  60. LogService::log(__METHOD__,'shouldProcess','波次任务分配1.2:'.json_encode($batches_inTask));
  61. $batches=$batches->whereNotIn('id',data_get($batches_inTask,'*.id')??[]);
  62. foreach ($batches as $batch){
  63. $stationRuleBatch=$this->getByBatch($batch);
  64. if($stationRuleBatch){
  65. // if(!$this->isLocationOfRobot($batch))continue;
  66. $batches_toProcess->push($batch);
  67. }
  68. }
  69. LogService::log(__METHOD__,'shouldProcess','波次任务分配1.3:'.json_encode($batches_inTask));
  70. return $batches_toProcess;
  71. }
  72. }