StationTaskMaterialBoxService.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. <?php
  2. namespace App\Services;
  3. use App\Batch;
  4. use App\OrderCommodity;
  5. use App\StationTaskMaterialBox;
  6. use Illuminate\Support\Collection;
  7. use Illuminate\Support\Facades\Cache;
  8. use App\Traits\ServiceAppAop;
  9. class StationTaskMaterialBoxService
  10. {
  11. use ServiceAppAop;
  12. /** @var StationService $stationService */
  13. private $stationService;
  14. /** @var StationTypeService $stationTypeService */
  15. private $stationTypeService;
  16. /** @var StationTaskService $stationTaskService */
  17. private $stationTaskService;
  18. /** @var MaterialBoxService $materialBoxService */
  19. private $materialBoxService;
  20. public function __construct(){
  21. $this->stationService=null;
  22. $this->stationTypeService=null;
  23. $this->stationTaskService=null;
  24. $this->materialBoxService=null;
  25. }
  26. function createByBatches(array $batches,Collection $stationTasks_toAttach): Collection
  27. {
  28. $stationTaskMaterialBoxes_listByBatch=new Collection();
  29. foreach ($batches as $batch){
  30. $stationTaskMaterialBoxes_listByBatch->push(
  31. $this->createByBatch($batch)
  32. );
  33. }
  34. $this->stationTaskService->registerSubTasks($stationTasks_toAttach,$stationTaskMaterialBoxes_listByBatch);
  35. }
  36. function createByBatch(Batch $batch): Collection
  37. {
  38. $this->instant($this->materialBoxService,'MaterialBoxService');
  39. $this->instant($this->stationTypeService,'StationTypeService');
  40. $this->instant($this->stationService,'StationService');
  41. $stationMaterialBoxes_toCreate=new Collection();
  42. $order_ids=data_get($batch['orders'],'*.id');
  43. $orderCommodities=OrderCommodity::query()->with('orderBin')->whereIn('order_id',$order_ids)->get();
  44. foreach ($orderCommodities as $orderCommodity){
  45. $stationType=$this->stationTypeService->getForMaterialBox();
  46. $station=$this->stationService->getStation_byType($stationType['name']);
  47. $materialBox=$this->materialBoxService->firstOrCreate(['code' => $orderCommodity['location']]);
  48. $stationMaterialBoxes_toCreate->push([
  49. 'station_id'=>$station['id'],
  50. 'material_box_id'=>$materialBox['id'],
  51. 'status'=>'待处理'
  52. ]);
  53. }
  54. $this->insert($stationMaterialBoxes_toCreate->toArray());
  55. }
  56. function get(array $kvPairs){
  57. ksort($kvPairs);
  58. return Cache::remember('StationTaskMaterialBox'.md5(json_encode($kvPairs)), config('cache.expirations.fastChange'), function ()use($kvPairs) {
  59. $query = StationTaskMaterialBox::query();
  60. foreach ($kvPairs as $column => $value){
  61. if (is_array($value))$query->whereIn($column,$value);
  62. else $query->where($column,$value);
  63. }
  64. return $query->get();
  65. });
  66. }
  67. public function insert(array $stationTaskMaterialBoxes): bool
  68. {
  69. $inserted = StationTaskMaterialBox::query()->insert($stationTaskMaterialBoxes);
  70. LogService::log(__METHOD__,__FUNCTION__,json_encode($stationTaskMaterialBoxes).
  71. '||'.json_encode(array_slice(debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS),0,3)));
  72. return $inserted;
  73. }
  74. }