| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177 |
- <?php
- namespace App\Services;
- use App\Batch;
- use App\Exceptions\ErrorException;
- use App\MaterialBox;
- use App\OrderCommodity;
- use App\StationTask;
- use App\StationTaskMaterialBox;
- use Illuminate\Support\Collection;
- use Illuminate\Support\Facades\Cache;
- use App\Traits\ServiceAppAop;
- class StationTaskMaterialBoxService
- {
- use ServiceAppAop;
- protected $modelClass=StationTaskMaterialBox::class;
- /** @var StationService $stationService */
- private $stationService;
- /** @var StationTypeService $stationTypeService */
- private $stationTypeService;
- /** @var StationTaskService $stationTaskService */
- private $stationTaskService;
- /** @var StationTaskBatchService $stationTaskBatchService */
- private $stationTaskBatchService;
- /** @var StationTaskCommodityService $stationTaskCommodityService */
- private $stationTaskCommodityService;
- /** @var MaterialBoxService $materialBoxService */
- private $materialBoxService;
- public function __construct(){
- $this->stationService=null;
- $this->stationTypeService=null;
- $this->stationTaskService=null;
- $this->materialBoxService=null;
- $this->stationTaskBatchService=null;
- $this->stationTaskCommodityService=null;
- }
- function createByBatches(Collection $batches,Collection $stationTasks_toAttach): Collection
- {
- $this->instant($this->stationTaskService,'StationTaskService');
- $stationTaskMaterialBoxes = (function () use ($batches) {
- $stationTaskMaterialBoxes_listByBatch = new Collection();
- foreach ($batches as $batch) {
- $stationTaskMaterialBoxes_listByBatch=
- $stationTaskMaterialBoxes_listByBatch->merge(
- $this->createByBatch($batch)
- );
- }
- return $stationTaskMaterialBoxes_listByBatch;
- })();
- $this->stationTaskService
- ->registerSubTasks(
- $stationTasks_toAttach,
- [$stationTaskMaterialBoxes]);
- return $stationTaskMaterialBoxes;
- }
- function createByBatch(Batch $batch): ?Collection
- {
- $this->instant($this->materialBoxService,'MaterialBoxService');
- $this->instant($this->stationTypeService,'StationTypeService');
- $this->instant($this->stationService,'StationService');
- $stationMaterialBoxes_toCreate=new Collection();
- $order_ids=data_get($batch['orders'],'*.id');
- $orderCommodities=OrderCommodity::query()->with('orderBin')->whereIn('order_id',$order_ids)->get();
- if($orderCommodities->isEmpty())return $stationMaterialBoxes_toCreate;
- $stationType=$this->stationTypeService->getForMaterialBox_onBatchProcess();
- foreach ($orderCommodities as $orderCommodity){
- $station=$this->stationService->getStation_byType($stationType['name']);
- $materialBox=$this->materialBoxService->firstOrCreate(['code' => $orderCommodity['location']]);
- $stationMaterialBoxes_toCreate->push([
- 'station_id'=>$station['id'],
- 'material_box_id'=>$materialBox['id'],
- 'status'=>'待处理'
- ]);
- }
- return $this->insert($stationMaterialBoxes_toCreate->toArray());
- }
- function get(array $kvPairs){
- ksort($kvPairs);
- return Cache::remember('StationTaskMaterialBox'.md5(json_encode($kvPairs)), config('cache.expirations.fastChange'), function ()use($kvPairs) {
- $query = StationTaskMaterialBox::query();
- foreach ($kvPairs as $column => $value){
- if (is_array($value))$query->whereIn($column,$value);
- else $query->where($column,$value);
- }
- return $query->get();
- });
- }
- public function insert(array $stationTaskMaterialBoxes): Collection
- {
- $inserted=collect();
- foreach ($stationTaskMaterialBoxes as $stationTaskMaterialBox){
- $inserted->push(StationTaskMaterialBox::query()->create($stationTaskMaterialBox));
- }
- return $inserted;
- }
- function markHasPut(StationTaskMaterialBox $stationTaskMaterialBox){
- try{
- $taskType=$this->getServingTaskType($stationTaskMaterialBox);
- switch ($taskType){
- case '分波次':
- $this->markProcessing($stationTaskMaterialBox);
- $this->stationTaskBatchService->markProcessing($stationTaskMaterialBox['stationTaskBatch']);
- $this->stationTaskCommodityService->markProcessing($stationTaskMaterialBox['stationTaskCommodities']);
- break;
- case '入立库':
- break;
- case '入库':break;
- }
- }catch (\Exception $e){
- throw new ErrorException('放置料箱出错');
- }
- }
- function markTaken($stationTaskMaterialBox){
- //TODO: 标记 料箱位置(需要其字段存在)$stationTaskMaterialBox['materialBox']['position']
- }
- function markProcessed(StationTaskMaterialBox $stationTaskMaterialBox){
- $stationTaskMaterialBox['status'] = '完成';
- $stationTaskMaterialBox->save();
- $this->stationService->broadcastBinMonitor($stationTaskMaterialBox['station_id'],$stationTaskMaterialBox['stationTask']);
- }
- function markProcessing($stationTaskMaterialBox)
- {
- if (get_class($stationTaskMaterialBox)==StationTaskMaterialBox::class){
- $stationTaskMaterialBox = collect($stationTaskMaterialBox);
- }
- StationTaskMaterialBox::query()
- ->whereIn('id', data_get($stationTaskMaterialBox, '*.id'))
- ->update(['status'=>'处理中']);
- $this->stationTaskService
- ->markProcessing_byId(
- data_get($stationTaskMaterialBox, '*.station_id')
- );
- }
- function excepted($stationTaskMaterialBox_orBox){
- switch (get_class($stationTaskMaterialBox_orBox)){
- case MaterialBox::class:
- case StationTaskMaterialBox::class:
- throw new ErrorException('料箱异常'.json_encode($stationTaskMaterialBox_orBox->toJson()));
- }
- }
- function getServingTaskType(StationTaskMaterialBox $stationTaskMaterialBox): string
- {
- $stationTaskMaterialBox->loadMissing('station.stationType');
- if($isBatching=(
- $stationTaskMaterialBox['station_task_batch_id'] &&
- $stationTaskMaterialBox['station']['stationType'] == '料箱监视器')
- ){
- return '分波次';
- }
- if($isPuttingBack=(
- !$stationTaskMaterialBox &&
- $stationTaskMaterialBox['station']['stationType'] == '立库')
- ){
- return '入立库';
- }
- if($isStoring=false){
- return '入库';
- }
- throw new ErrorException('当前类型找不到');
- }
- }
|