Przeglądaj źródła

缓存架修改

ajun 4 lat temu
rodzic
commit
52e54c4c58

+ 11 - 10
app/Http/Controllers/CacheShelfController.php

@@ -30,16 +30,15 @@ class CacheShelfController extends Controller
     }
 
     /**
-     * 获取缓存货架上的任务列表
+     * 获取缓存货架上的料箱
      * @param Request $request
      * @param string $id
      * @param CacheShelfService $service
      */
-    public function getTasksApi(Request $request,string $id,CacheShelfService $service)
+    public function getChildStationApi(Request $request,string $id,CacheShelfService $service)
     {
-        /** @var Station $station */
-        $station = $service->getChildStation($id);
-        $this->success($station);
+        $stations = $service->getChildStation($id);
+        $this->success($stations);
     }
 
     /**
@@ -48,21 +47,23 @@ class CacheShelfController extends Controller
      * @param CacheShelfService $service
      * @return mixed
      */
-    public function lightOnApi(Request $request,CacheShelfService $service)
+    public function lightOnApi(Request $request,CacheShelfService $service): array
     {
-        if($request['stationCode'] && $request['materialBoxCode'])
-           return $service->createStationTask($request['stationCode'],$request['materialBoxCode']);
+        if($request['stationCode'] && $request['materialBoxCode']){
+            return  $service->bindMaterialBox($request['stationCode'],$request['materialBoxCode']);
+        }
         return ['success' => false,'message' => '参数错误'];
     }
 
     /**
      * @param Request $request
+     * @param CacheShelfService $service
      * @return array|bool[]
      */
-    public function clearTaskApi(Request $request): array
+    public function clearTaskApi(Request $request,CacheShelfService $service): array
     {
         $code = $request['station'];
-        return app(CacheShelfService::class)->clearTask($code);
+        return $service->clearTask($code);
     }
 
 }

+ 2 - 7
app/Http/Controllers/TestController.php

@@ -49,6 +49,7 @@ use App\ProcurementTotalBill;
 use App\RejectedBillItem;
 use App\Services\BatchService;
 use App\Services\CacheService;
+use App\Services\CacheShelfService;
 use App\Services\CommodityService;
 use App\Services\common\BatchUpdateService;
 use App\Services\common\DataHandlerService;
@@ -1377,7 +1378,6 @@ TEXT;
 
         $orderPackages = OrderPackage::query()->selectRaw('count(1) counts,logistic_number')->where('created_at','>=','2020-01-01')->groupBy('logistic_number')->having('counts','>','1')->get();
 
-
         $updateParams = [];
         $deleteParams = [];
         foreach ($orderPackages as $orderPackage) {
@@ -1427,14 +1427,9 @@ TEXT;
             $deleteParams[]  = $orderPackages_filter;
             $saveOrderPackage->save();
         }
-        dd($updateParams,$deleteParams);
+        dd('任务完成');
     }
 
-    public function endStationTask()
-    {
-        $stationTask = StationTask::query()->where('id',711)->first();
-        $stationTask->update(['status'=>'完成']);
-    }
 
     public function order_package_exception_type_counting_record_init()
     {

+ 83 - 141
app/Services/CacheShelfService.php

@@ -7,9 +7,11 @@ use App\Exceptions\ErrorException;
 use App\MaterialBox;
 use App\Station;
 use App\StationTask;
+use App\StationTaskChild;
 use App\StationTaskChildren;
 use App\StationTaskMaterialBox;
 use App\StationType;
+use App\Storage;
 use App\Traits\ServiceAppAop;
 use Illuminate\Http\Request;
 use Illuminate\Support\Facades\Http;
@@ -40,7 +42,7 @@ class   CacheShelfService
      */
     public function getChildStation($id)
     {
-        return Station::query()->where('parent_id',$id)->with('parent','pendingStationTask.stationTaskMaterialBoxes.materialBox')->get();
+        return Station::query()->where('parent_id', $id)->with('storage.materialBox')->get();
     }
 
 
@@ -49,134 +51,95 @@ class   CacheShelfService
      * @param $locCode
      * @param $PTLAction
      * @return array|bool[]
+     * @throws \Exception
      */
     public function lightOffTask($locCode, $PTLAction): array
     {
-        $station = Station::query()->with('pendingStationTask.stationTaskMaterialBoxes.materialBox')->where('code', $locCode)->first();
+        $station = Station::query()->with('storage.materialBox')->where('code', $locCode)->first();
         try {
             $bool = $this->putBinToStore($station);                         // 推送任务
-            if($bool){
-                LogService::log(__CLASS__,'lightOffTask','code' .' true'. $locCode.json_encode($station));
+            if ($bool) {
+                LogService::log(__CLASS__, 'lightOffTask', 'code' . ' true' . $locCode . json_encode($station));
                 return ['success' => true];
-            }else{
-                return ['success' => false,'errMsg' => '机器人推送失败'];
+            } else {
+                return ['success' => false, 'errMsg' => '机器人推送失败'];
             }
         } catch (ErrorException $e) {
-            LogService::log(__FUNCTION__,'缓存架推送任务失败',json_encode($e->getMessage()));
-            return ['success' => false,'errMsg' => $e->getMessage()];
+            LogService::log(__FUNCTION__, '缓存架推送任务失败', json_encode($e->getMessage()));
+            return ['success' => false, 'errMsg' => $e->getMessage()];
         }
     }
 
     /**
      * 推任务至海柔机器人
      * @param  $station
-     * @return bool
+     * @return array
      * @throws ErrorException
+     * @throws \Exception
      */
-    public function putBinToStore($station): bool
+    public function putBinToStore($station): array
     {
-        $this->instant($this->stationTaskMaterialBoxService, 'StationTaskMaterialBoxService');
         $this->instant($this->foreignHaiRoboticsService, 'ForeignHaiRoboticsService');
         $this->instant($this->stationService, 'StationService');
-        $this->instant($this->stationTaskService, 'StationTaskService');
-        $this->instant($this->stationTaskChildService, 'StationTaskChildService');
 
-        /** @var StationTaskMaterialBox $takeStationTaskMaterialBox */
-        $takeStationTaskMaterialBox = $station['pendingStationTask']['stationTaskMaterialBoxes']->first();
-        $formStation = $this->stationService->getStation_byType('立库');
+        /** @var MaterialBox $materialbox */
+        $materialBox = $station->storage->materialbox;
+
+        $formStation = $this->stationService->getStation_byType('立库');          // 立库
+
+        $stationTask = StationTask::query()->create(['station_id' => $formStation['id'], 'status' => '待处理']);   // 生成任务
 
-        // 查询是否有待处理的入库任务
-        $putStationTaskMaterialBox = StationTaskMaterialBox::query()->where([
+        /** @var StationTaskMaterialBox $stationTaskMaterialBox */
+        $stationTaskMaterialBox = StationTaskMaterialBox::query()->create([
             'station_id' => $formStation['id'],
-            'material_box_id' => $takeStationTaskMaterialBox['material_box_id'],
+            'material_box_id' => $materialBox['id'],
             'status' => '待处理',
-        ])->first();
-
-        // 创建入立库任务
-        if(!$putStationTaskMaterialBox){
-            $stationTask = $this->stationTaskService->create(1);               // 生成站任务
-            $this->stationTaskService->registerStations($stationTask,[$formStation['id']]);   // 注册站任务站
-            /** @var StationTaskMaterialBox $putStationTaskMaterialBox */
-            $putStationTaskMaterialBox = $this->stationTaskMaterialBoxService->create([
-                'station_id' => $formStation['id'],
-                'material_box_id' => $takeStationTaskMaterialBox['material_box_id'],
-                'status' => '待处理',
-            ]);
-            $putStationTaskMaterialBox['station_task_id'] = $stationTask->first()['id'];
-            $putStationTaskMaterialBox['type'] = '放';
-            $putStationTaskMaterialBox->update();
-
-            $params = [[
-                'station_task_id'=>$stationTask->first()['id'],
-                'station_taskable_type'=>StationTaskMaterialBox::class,
-                'station_taskable_id'=>$putStationTaskMaterialBox['id']
-            ]];
+            'station_task_id' => $stationTask['id'],
+            'type' => '放',
+        ]);
 
-            $this->stationTaskChildService->insert($params);    // 任务任务注册
-        }
+        StationTaskChildren::query()->create([
+            'station_task_id' => $stationTask['id'],
+            'station_taskable_type' => StationTaskMaterialBox::class,
+            'station_taskable_id' => $stationTaskMaterialBox['id']
+        ]);
 
-        // 推立库任务
-        $isSuccess =  $this->foreignHaiRoboticsService->putBinToStore_fromCacheShelf($putStationTaskMaterialBox,$station['code']);
-        if($isSuccess) $this->stationTaskMaterialBoxService->set($takeStationTaskMaterialBox,['status' => '处理中']);  // 任务推送成功 标记站任务为处理中
-        else {
-            $materialBoxCode = $station['pendingStationTask']['stationTaskMaterialBoxes']->first()->code ?? '';
-            $this->_stationCacheBroadCast($station->code,0,'error');
-            $this->_stationCacheLightOn($station->code,$materialBoxCode,'拍灯重试任务');
-        }
-        return $isSuccess;
+        $bool = $this->foreignHaiRoboticsService->putBinToStore_fromCacheShelf($stationTaskMaterialBox, $station['code']);
+        return $bool ? ['success' => true] : ['success' => false];
     }
 
     /**
-     * 创建站任务和料箱任务
+     * 缓存架和料箱的绑定
      * @param $stationCode
      * @param $materialBoxCode
      * @return array
      */
-    public function createStationTask($stationCode,$materialBoxCode): array
+    public function bindMaterialBox($stationCode, $materialBoxCode): array
     {
-        $this->instant($this->stationTaskService, 'StationTaskService');
-        $this->instant($this->stationTaskMaterialBoxService,'StationTaskMaterialBoxService');
-        $this->instant($this->stationTaskChildService,'StationTaskChildService');
+        $station = Station::query()->with('storage')->where('code', $stationCode)->first();
 
-        $station = Station::query()->where('code' , $stationCode)->first();
-        if(!$station){
+        if (!$station) {
             $arr = [];
-            preg_match('/^HAI([\w]+)/',$stationCode,$arr);
+            preg_match('/^HAI([\w]+)/', $stationCode, $arr);
             $parentCode = $arr[1] ?? '';
-            $stationType = StationType::query()->where('name','缓存架')->first();
-            $parentStation = Station::query()->firstOrCreate(['code'=>$parentCode],['station_type_id'=>$stationType['id']]);
-            $station = Station::query()->firstOrCreate(['code' => $stationCode,'parent_id'=>$parentStation['id']],['name'=>$stationCode,'station_type_id' => $stationType['id']]);
+            $stationType = StationType::query()->where('name', '缓存架')->first();
+            $parentStation = Station::query()->firstOrCreate(['code' => $parentCode], ['station_type_id' => $stationType['id']]);
+            $station = Station::query()->firstOrCreate(['code' => $stationCode, 'parent_id' => $parentStation['id']], ['name' => $stationCode, 'station_type_id' => $stationType['id']]);
         }
 
         $materialBox = MaterialBox::query()->firstOrCreate(['code' => $materialBoxCode]);
 
-        $station->load('pendingStationTask.stationTaskMaterialBoxes.materialBox');
-        if($station->pendingStationTask){
-            if($station['pendingStationTask']['stationTaskMaterialBoxes']->first()->materialBox->code == $materialBoxCode){
-                $response = $this->_stationCacheLightOn($stationCode,$materialBoxCode,'任务重试');
-                if($response->code) return ['success'=>true,'message' =>  '任务重试'];
-                return ['success'=>true,'message' =>  '任务重试失败'];
-            }
-            return ['success' => false,'message' => '当前已有进行重的任务'];
-        }
+        $storage = $station->storage ?? Storage::query()->firstOrCreate(['station_id' => $station['id']]);
 
-        $stationTask = $this->stationTaskService->create(1);                                                       // 生成站任务
-        $stationTaskMaterialBox = $this->stationTaskMaterialBoxService->createByStationAndMaterialBox($station,$materialBox);     // 创建料箱任务
-        $this->stationTaskService->registerStations($stationTask,[$station['id']]);                                               // 注册站任务站
-        $stationTaskMaterialBox['station_task_id'] = $stationTask->first()['id'];
-        $stationTaskMaterialBox->update();
-        $params = [[
-                'station_task_id'=>$stationTask->first()['id'],
-                'station_taskable_type'=>StationTaskMaterialBox::class,
-                'station_taskable_id'=>$stationTaskMaterialBox['id']
-            ]];
-        $this->stationTaskChildService->insert($params);                                                                        // 任务任务注册
-
-        $body = $this->_stationCacheLightOn($stationCode,$materialBoxCode);
-        if($body->code == 200)return ['success'=>true];
-        return ['success' => false,'message' => '机器人亮灯异常'];
+        $result = $this->_stationCacheLightOn($station['code'],$materialBoxCode);
+        if($result['code'] == 200){
+            $storage->update(['material_box_id' => $materialBox['id'],'status' => 1]);
+            return ['success' => true];
+        }
+        return ['success' => false,'message' => $result['errMsg']];
     }
 
+
     /**
      * 控制格口亮灯
      * @param $locCode
@@ -185,13 +148,13 @@ class   CacheShelfService
      * @param string $color
      * @return mixed
      */
-    public function _stationCacheLightOn($locCode,$materialCode = null,$title = 'title' ,string $color = '1')
+    public function _stationCacheLightOn($locCode, $materialCode = null, $title = 'title', string $color = '1')
     {
         $params = [
             "areaCode" => "1004",
             'locCode' => $locCode,
             'PTLAction' => 1,
-            'PTLSettings' => ['color'=> $color, 'frequency'  =>1],
+            'PTLSettings' => ['color' => $color, 'frequency' => 1],
             "displayInfo" => [
                 "detail01" => $materialCode,
                 "detail02" => "detail02",
@@ -214,8 +177,9 @@ class   CacheShelfService
      * @param $locCode
      * @return mixed
      */
-    public function _stationCacheLightOff($locCode){
-        if(!$locCode)return null;
+    public function _stationCacheLightOff($locCode)
+    {
+        if (!$locCode) return null;
         $params = [
             "areaCode" => "1004",
             'locCode' => $locCode,
@@ -231,84 +195,62 @@ class   CacheShelfService
      * @param $PTLAction
      * @param string $type
      */
-    public function _stationCacheBroadCast($locCode, $PTLAction,string $type = 'success')
+    public function _stationCacheBroadCast($locCode, $PTLAction, string $type = 'success')
     {
-        if($PTLAction == 0){
-            $station = Station::query()->with('parent')->where('code',$locCode)->first();
+        if (!$locCode) return;
+        if ($PTLAction == 0) {
+            $station = Station::query()->with('parent')->where('code', $locCode)->first();
 
-            $json = json_encode( [
+            $json = json_encode([
                 'station_id' => $station['parent']['id'],
-                'code'  => $station['parent']['code'],
+                'code' => $station['parent']['code'],
                 'gird_id' => $station['id'],
                 'grid_code' => $station['code'],
-                'type' => $type
+                'type' => $type,
+                'status' => 0
             ]);
-            broadcast(new BroadcastToStation($station['parent_id'],$json));
+            broadcast(new BroadcastToStation($station['parent_id'], $json));
         }
     }
 
     /**
-     * 根据立库任务完成 缓存架任务 和 入立架任务
-     * @param StationTaskMaterialBox $putStationTaskMaterial
+     * 根据立库任务完成对storage进行修改
+     * @param StationTaskMaterialBox $stationTaskMaterial
      */
-    public function putStationTaskMaterialBoxProcess(StationTaskMaterialBox $putStationTaskMaterial)
+    public function putStationTaskMaterialBoxProcess(StationTaskMaterialBox $stationTaskMaterial)
     {
-        $this->instant($this->stationTaskMaterialBoxService,'StationTaskMaterialBoxService');
-
-        $takeStationTaskMaterialBox = StationTaskMaterialBox::query()->with('station','stationTask')->where('material_box_id',$putStationTaskMaterial['material_box_id'])->whereIn('station_id',function($query){
-            $query->from('stations')->selectRaw('id')->whereIn('station_type_id',function($query){
-                $query->from('station_types')->selectRaw('id')->where('name','缓存架');
-            });
-        })->where('status','处理中')->first();
-
-        if(!$takeStationTaskMaterialBox)return ;
-        $this->stationTaskMaterialBoxService->set($takeStationTaskMaterialBox, [
-            'status' => '完成'
-        ]);
+        $this->instant($this->stationTaskMaterialBoxService, 'StationTaskMaterialBoxService');
 
-        // 缓存架任务
-        if($takeStationTaskMaterialBox->stationTask)$takeStationTaskMaterialBox->stationTask->update(['status' => '完成']);
-        // 入立架任务
-        if($putStationTaskMaterial->stationTask)$putStationTaskMaterial->stationTask->update(['status' => '完成']);
+        $storage = Storage::query()->where('material_box_id', $stationTaskMaterial['material_box_id'])->where('status',1)->first();
+        if($storage)$storage->update(['status' => 0, 'material_box_id' => null]);
 
-        $this->_stationCacheLightOff($takeStationTaskMaterialBox->station->code ?? null);  //海柔格口灭灯
-        $this->_stationCacheBroadCast($takeStationTaskMaterialBox->station->code, 0);    //通知缓存架任务完成
+        $this->_stationCacheLightOff($stationTaskMaterial->station->code ?? null);  //海柔格口灭灯
+        $this->_stationCacheBroadCast($stationTaskMaterial->station->code ?? null, 0);    //通知缓存架任务完成
     }
 
     /**
      * 取消任务
      * @param $stationCode
      * @return array
+     * @throws \Exception
      */
     public function clearTask($stationCode): array
     {
-        $station = Station::query()->with(['currentStationTask.stationTaskMaterialBoxes.materialBox',
-            'pendingStationTask.stationTaskMaterialBoxes.materialBox'])
-            ->where('code',$stationCode)->first();
+        $station = Station::query()->with('storage')->where('code', $stationCode)->first();
 
-        if($station->currentStationTask)return ['success' => false,'message' => '当前任务正在执行','data'=>$stationCode];
-        if($station->pendingStationTask->stationTaskMaterialBoxes->count() == 0){
-            $station->pendingStationTask->delete();
-            return ['success' => true];
-        }
-
-        $taskStationTaskMaterialBox = $station->pendingStationTask->stationTaskMaterialBoxes->first() ?? null;
-        $stationTaskMaterialBoxes = StationTaskMaterialBox::query()->with('stationTask')
-            ->where('material_box_id',$taskStationTaskMaterialBox['material_box_id'])
-            ->where('station_id','!=',$station->pendingStationTask['id'])
-            ->get();
+        if (!$station) return ['success' => false, 'message' => '传入参数异常,找不到对应的缓存架记录'];
 
-        foreach ($stationTaskMaterialBoxes as $stationTaskMaterialBox) {
-            if($stationTaskMaterialBox->stationTask){
-                StationTaskChildren::query()->where([
-                    'station_task_id' => $stationTaskMaterialBox->stationTask['id'],
-                    'station_taskable_type'=>StationTaskMaterialBox::class,
-                    'station_taskable_id'=>$stationTaskMaterialBox['id']])->delete();
-
-                $stationTaskMaterialBox->stationTask->delete();
+        $stationTaskMaterialBox = StationTaskMaterialBox::query()->where('material_box_id',$station['storage']['material_box_id'])->first();
+        if($stationTaskMaterialBox ){
+            if($stationTaskMaterialBox->status == '处理中')
+                return ['success' => false, 'message' => '当前缓存架任务正在处理中'];
+            else{
+                $stationTaskMaterialBox->delete();
             }
-            $stationTaskMaterialBox->delete();
         }
+
+        $station->storage->update(['status' => 0,'material_box_id' => null]);
+
         return ['success' => true];
     }
 }

+ 2 - 16
app/Services/ForeignHaiRoboticsService.php

@@ -324,26 +324,12 @@ class ForeignHaiRoboticsService
     public function putBinToStore_fromCacheShelf(?StationTaskMaterialBox $stationTaskMaterialBox,
                                                  string $formLocation): bool
     {
-        LogService::log('海柔请求','putBinToStore_fromCacheShelf1', '');
-        LogService::log('海柔请求','putBinToStore_fromCacheShelf2', json_encode($stationTaskMaterialBox));
-
-        $dataToPost=$this->makeJson_move(
-            collect([$stationTaskMaterialBox]),
-            '缓存架入立架',
-            $formLocation,
-            ''
-        );
-        LogService::log('海柔请求','putBinToStore_fromCacheShelf3', json_encode($dataToPost));
+        $dataToPost=$this->makeJson_move(collect([$stationTaskMaterialBox]), '缓存架入立架', $formLocation, '');
 
         $controlSuccess = $this->controlHaiRobot($dataToPost,collect([$stationTaskMaterialBox]),'缓存架入立架');
-        LogService::log('海柔请求','putBinToStore_fromCacheShelf4', 'controlHaiRobot '. json_encode($controlSuccess));
-
         if($controlSuccess){
             $this->instant($this->stationTaskMaterialBoxService,'StationTaskMaterialBoxService');
-            $this->stationTaskMaterialBoxService->set($stationTaskMaterialBox,[
-                'status' => '处理中',
-            ]);
-            LogService::log('海柔请求','putBinToStore_fromCacheShelf5', 'controlHaiRobot '. json_encode($stationTaskMaterialBox));
+            $this->stationTaskMaterialBoxService->set($stationTaskMaterialBox,['status' => '处理中']);
         }
         return $controlSuccess;
     }

+ 5 - 0
app/Station.php

@@ -49,4 +49,9 @@ class Station extends Model
             ->where('status','=','待处理');
     }
 
+    public function storage(): HasOne
+    {
+        return $this->hasOne(Storage::class);
+    }
+
 }

+ 1 - 0
database/factories/StationTaskFactory.php

@@ -9,5 +9,6 @@ $factory->define(StationTask::class, function (Faker $faker) {
     $status = ['待处理','挂起','处理中','完成','异常','取消'];
     return [
         "status"=>'待处理',
+        'station_id' => 0
     ];
 });

+ 6 - 1
database/factories/StationTaskMaterialBoxFactory.php

@@ -7,6 +7,11 @@ use Faker\Generator as Faker;
 
 $factory->define(StationTaskMaterialBox::class, function (Faker $faker) {
     return [
-        'status'=>'待处理'
+        'station_id' => 0,
+        'material_box_id' => 0,
+        'status'=>'待处理',
+        'type' => 0,
+        'station_task_batch_id' =>0 ,
+        'station_task_id' => 0
     ];
 });

+ 15 - 0
database/factories/StorageFactory.php

@@ -0,0 +1,15 @@
+<?php
+
+/** @var \Illuminate\Database\Eloquent\Factory $factory */
+
+use Faker\Generator as Faker;
+
+$factory->define(\App\Storage::class, function (Faker $faker) {
+    return [
+        "station_id" => rand(0,10),
+        "material_box_id" =>  rand(0,10),
+        "commodity_id" => 0 ,
+        "amount" => 0,
+        "status" => 0,
+    ];
+});

+ 4 - 6
resources/views/station/cachingShelf/list/index.blade.php

@@ -102,7 +102,7 @@
                 // },
                 /** 初始化格口 */
                 initCacheGrid() {
-                    let url = '{{url('apiLocal/station/cacheShelf/getTasks')}}'+'/'+this.currentStation.id;
+                    let url = '{{url('apiLocal/station/cacheShelf/getChildStation')}}'+'/'+this.currentStation.id;
                     window.tempTip.setIndex(1099);
                     window.tempTip.setDuration(3000);
                     $('#alert-box').show();
@@ -110,11 +110,9 @@
                         if (res.data.data) {
                             for (let index = 0; index < res.data.data.length; index++) {
                                 let stationCode = res.data.data[index]['code'];
-                                if(!res.data.data[index]['pending_station_task'])continue;
-                                if(!res.data.data[index]['pending_station_task']['station_task_material_boxes'])continue;
-                                if(!res.data.data[index]['pending_station_task']['station_task_material_boxes'][0])continue;
-                                if(!res.data.data[index]['pending_station_task']['station_task_material_boxes'][0]['material_box'])continue;
-                                let materialBoxCode = res.data.data[index]['pending_station_task']['station_task_material_boxes'][0]['material_box']['code'] ?? null;
+                                if(!res.data.data[index]['storage'])continue;
+                                if(!res.data.data[index]['storage']['material_box'])continue;
+                                let materialBoxCode = res.data.data[index]['storage']['material_box']['code'] ?? null;
                                 this.$set(this.currentStation.grids[stationCode],'materialBoxCode',materialBoxCode);
                             }
                             $('#alert-box').hide();

+ 1 - 1
routes/apiLocal.php

@@ -204,7 +204,7 @@ Route::group(['prefix' => 'station'],function(){
         Route::post('pushTask','CacheShelfController@pushTaskApi')->name('station.cacheShelf.pushTaskApi');
         Route::post('clearTask','CacheShelfController@clearTaskApi')->name('station.cacheShelf.clearTaskApi');
         Route::post('lightOn','CacheShelfController@lightOnApi')->name('station.cacheShelf.lightOnApi');
-        Route::get('getTasks/{id}','CacheShelfController@getTasksApi')->name('station.cacheShelf.getTasksApi');
+        Route::get('getChildStation/{id}','CacheShelfController@getChildStationApi')->name('station.cacheShelf.getChildStationApi');
     });
 
     /** 栈规则 */

+ 57 - 0
tests/Services/CacheShelfService/BindMaterialBoxTest.php

@@ -0,0 +1,57 @@
+<?php
+
+
+namespace Tests\Services\CacheShelfService;
+
+
+use App\MaterialBox;
+use App\Services\CacheShelfService;
+use App\Services\ForeignHaiRoboticsService;
+use App\Services\StationTaskMaterialBoxService;
+use App\Services\StationTaskService;
+use App\Station;
+use App\Storage;
+use App\Traits\TestMockSubServices;
+use Tests\TestCase;
+
+class BindMaterialBoxTest extends TestCase
+{
+    use TestMockSubServices;
+
+
+    /** @var CacheShelfService $cacheShelfService*/
+    public $cacheShelfService;
+    public $data = [];
+
+
+
+    protected function setUp(): void
+    {
+        parent::setUp(); // TODO: Change the autogenerated stub
+        $this->cacheShelfService =  $this->subMock([
+            'class' => CacheShelfService::class,
+            'methods' => [
+                '_stationCacheLightOn' => new MaterialBox(['code' => 200])
+            ],
+        ]);
+        $this->data['station'] = factory(Station::class)->create();
+        $this->data['materialBox'] = factory(MaterialBox::class)->create();
+        $this->data['storage'] = factory(Storage::class)->create(['station_id' => $this->data['station']['id']]);
+    }
+
+    public function testBindMaterialBoxTest()
+    {
+        $result = $this->cacheShelfService->bindMaterialBox($this->data['station']['code'],$this->data['materialBox']['code']);
+        $this->assertTrue($result['success']);
+        $storage = Storage::query()->whereKey($this->data['storage']['id'])->first();
+        $this->assertEquals($this->data['materialBox']['id'],$storage['material_box_id']);
+    }
+
+    protected function tearDown(): void
+    {
+        if($this->data['station'] ?? false) Station::query()->whereKey($this->data['station']['id'])->delete();
+        if($this->data['materialBox'] ?? false) MaterialBox::query()->whereKey($this->data['materialBox']['id'])->delete();
+        if($this->data['storage'] ?? false)
+        parent::tearDown(); // TODO: Change the autogenerated stub
+    }
+}

+ 32 - 47
tests/Services/CacheShelfService/ClearTaskTest.php

@@ -15,6 +15,7 @@ use App\StationTask;
 use App\StationTaskChildren;
 use App\StationTaskMaterialBox;
 use App\StationType;
+use App\Storage;
 use Tests\TestCase;
 
 class ClearTaskTest extends TestCase
@@ -41,73 +42,57 @@ class ClearTaskTest extends TestCase
         $stationType = StationType::query()->firstOrCreate(['name'=>'立库']);
 
         $this->data['LiKuStation'] = factory(Station::class)->create(['station_type_id'=>$stationType['id']]);
-        $this->data['station'] = factory(Station::class)->create();
 
-        $this->data['stationTask'] = [];
-        $this->data['stationTaskMaterialBoxes'] = [];
         $this->data['materialBox'] = factory(MaterialBox::class)->create();
 
-        $this->data['stationTask'] = $this->stationTaskService->create(1);                                                    // 生成站任务
-        $this->data['stationTaskMaterialBoxes'][] = $this->stationTaskMaterialBoxService->createByStationAndMaterialBox($this->data['station'],$this->data['materialBox']);     // 创建料箱任务
-        $this->stationTaskService->registerStations($this->data['stationTask'],[$this->data['station']['id']]);                                            // 注册站任务站
-        $this->data['stationTaskMaterialBoxes'][0]['station_task_id'] = $this->data['stationTask'][0]['id'];
-        $this->data['stationTaskMaterialBoxes'][0]->update();
-        $params = [[
-            'station_task_id'=>$this->data['stationTask'][0]->first()['id'],
-            'station_taskable_type'=>StationTaskMaterialBox::class,
-            'station_taskable_id'=>$this->data['stationTaskMaterialBoxes'][0]['id']
-        ]];
-
-        $this->stationTaskChildService->insert($params);                                                                        // 任务任务注册
+        $this->data['station'] = factory(Station::class)->create();
+
+        $this->data['storage'] = factory(Storage::class)->create(['station_id' => $this->data['station']['id']]);
+
     }
 
     public function testClearTask()
     {
         $result = $this->service->clearTask($this->data['station']->code);
-        $stationTask = StationTask::query()->where('station_id',$this->data['station']['id'])->first();
-        $this->assertEmpty($stationTask);
-        $stationTaskMaterialBox = StationTaskMaterialBox::query()->where('material_box_id',$this->data['materialBox']['id'])->get();
-        $this->assertEmpty($stationTaskMaterialBox);
+        $this->assertTrue($result['success']);
+        $storage = Storage::query()->with('materialBox')->whereKey($this->data['storage'])->first();
+        $this->assertNull($storage->materialBox);
     }
 
     public function testClearTask_taskIsRunning()
     {
-        StationTask::query()->where('station_id',$this->data['station']['id'])->update(['status' => '处理中']);
-        $result = $this->service->clearTask($this->data['station']->code);
+        Storage::query()->where('station_id',$this->data['station']['id'])->update(['material_box_id'=>$this->data['materialBox']['id']]);
 
-        $stationTask = StationTask::query()->where('station_id',$this->data['station']['id'])->first();
+        $this->data['task'] = factory(StationTask::class)->create(['station_id' => $this->data['station']['id']]);
+
+        $this->data['stationTaskMaterialBox'] = StationTaskMaterialBox::query()->create([
+            'station_id' => $this->data['LiKuStation']['id'],
+            'material_box_id' => $this->data['materialBox']['id'],
+            'status' => '处理中',
+            'station_task_id' => $this->data['task']['id'],
+            'type' => '放',
+        ]);
+
+        $this->data['stationTaskChildren'] = StationTaskChildren::query()->create([
+            'station_task_id' => $this->data['task']['id'],
+            'station_taskable_type' => StationTaskMaterialBox::class,
+            'station_taskable_id' => $this->data['stationTaskMaterialBox']['id']
+        ]);
+
+        $result = $this->service->clearTask($this->data['station']->code);
 
-        $this->assertEquals('处理中',$stationTask->status);
         $this->assertFalse($result['success']);
     }
 
 
     protected function tearDown(): void
     {
-        if($this->data['station']){
-            $stationTasks = StationTask::query()->where('station_id',$this->data['station']['id'])->get();
-            foreach ($stationTasks as $stationTask) {
-                $stationTaskMaterialBoxes = $stationTask->stationTaskMaterialBoxes;
-                foreach ($stationTaskMaterialBoxes as $stationTaskMaterialBox) {
-                    StationTaskChildren::query()->where(['station_task_id' => $stationTask['id'],'station_taskable_type'=> StationTaskMaterialBox::class,'station_taskable_id'=>$stationTaskMaterialBox['id']])->delete();
-                    $stationTaskMaterialBox->delete();
-                }
-            }
-            Station::query()->where('id',$this->data['station']['id'])->delete();
-        }
-
-        if($this->data['LiKuStation']){
-            $stationTasks = StationTask::query()->where('station_id',$this->data['LiKuStation']['id'])->get();
-            foreach ($stationTasks as $stationTask) {
-                $stationTaskMaterialBoxes = $stationTask->stationTaskMaterialBoxes;
-                foreach ($stationTaskMaterialBoxes as $stationTaskMaterialBox) {
-                    StationTaskChildren::query()->where(['station_task_id' => $stationTask['id'],'station_taskable_type'=> StationTaskMaterialBox::class,'station_taskable_id'=>$stationTaskMaterialBox['id']])->delete();
-                    $stationTaskMaterialBox->delete();
-                }
-            }
-            Station::query()->where('id',$this->data['LiKuStation']['id'])->delete();
-        }
-        if($this->data['materialBox'])MaterialBox::query()->where('id',$this->data['materialBox'])->delete();
+        if($this->data['materialBox'])MaterialBox::query()->whereKey($this->data['materialBox']['id'])->delete();
+        if($this->data['station'])Station::query()->whereKey($this->data['station']['id'])->delete();
+        if($this->data['storage'])Storage::query()->whereKey($this->data['storage']['id'])->delete();
+        if($this->data['task'] ?? false)StationTask::query()->whereKey($this->data['task']['id'])->delete();
+        if($this->data['stationTaskMaterialBox'] ?? false)StationTaskMaterialBox::query()->whereKey($this->data['stationTaskMaterialBox']['id'])->delete();
+        if($this->data['stationTaskChildren'] ?? false)StationTaskChildren::query()->whereKey($this->data['stationTaskChildren']['id'])->delete();
         parent::tearDown(); // TODO: Change the autogenerated stub
     }
 

+ 0 - 100
tests/Services/CacheShelfService/CreateStationTask.php

@@ -1,100 +0,0 @@
-<?php
-
-
-namespace Tests\Services\CacheShelfService;
-
-
-use App\MaterialBox;
-use App\Services\CacheShelfService;
-use App\Services\ForeignHaiRoboticsService;
-use App\Services\StationTaskMaterialBoxService;
-use App\Services\StationTaskService;
-use App\Station;
-use App\StationTask;
-use App\StationTaskMaterialBox;
-use App\Traits\TestMockSubServices;
-use Tests\TestCase;
-
-class CreateStationTask extends TestCase
-{
-    use TestMockSubServices;
-
-    /** @var CacheShelfService $service */
-    protected $service;
-    /** @var StationTaskService $stationTaskService */
-    protected $stationTaskService;
-
-    protected $data = [];
-
-    /**
-     * createStationTask
-     * 注册站任务
-     * 注册站料箱任务
-     * 注册站任务子任务
-     */
-    protected function setUp(): void
-    {
-        parent::setUp();
-        $this->service = $this->subMock([
-            'class' => CacheShelfService::class,
-            'methods' => [
-                '_stationCacheLightOn' => new MaterialBox(['code' => 200])
-            ],
-            'subService' => [[
-                'serviceName' => 'foreignHaiRoboticsService',
-                'class' => ForeignHaiRoboticsService::class,
-                'methods' => [
-                    'controlHaiRobot' => true
-                ]
-            ], [
-                'serviceName' => 'stationTaskService',
-                'class' => StationTaskService::class
-            ], [
-                'serviceName' => 'stationTaskMaterialBoxService',
-                'class' => StationTaskMaterialBoxService::class,
-            ]
-            ]
-        ]);
-        $this->stationTaskService = $this->subMock([
-            'class' => StationTaskService::class,
-        ]);
-        $this->data['parentStation'] = factory(Station::class)->create();
-        $this->data['station'] = factory(Station::class)->create(['parent_id' => $this->data['parentStation']['id']]);
-        $this->data['materialBox'] = factory(MaterialBox::class)->create();
-    }
-
-    public function testCreatStationTask()
-    {
-        $data = $this->service->createStationTask($this->data['station']['code'], $this->data['materialBox']['code']);
-        $this->assertTrue($data['success']);
-
-        $stationTask = StationTask::query()->where('station_id', $this->data['station']['id'])->with('stationTaskMaterialBoxes')->first();
-        $stationTaskMaterialBox = StationTaskMaterialBox::query()->with('materialBox')->where('station_id', $this->data['station']['id'])->first();
-        $this->assertEquals($stationTask['status'], '待处理');
-        $this->assertEquals($stationTaskMaterialBox['materialBox']['code'], $this->data['materialBox']['code']);
-
-        $station = Station::query()->with(['pendingStationTask.stationTaskMaterialBoxes'])->where('id', $this->data['station']['id'])->first();
-        $this->assertEquals($station['pendingStationTask']['stationTaskMaterialBoxes']->first()['station_id'], $stationTask['station_id']);
-    }
-
-    public function testStationTaskIsExists()
-    {
-        $this->data['stationTask'] = $this->stationTaskService->create(1);
-        $this->stationTaskService->registerStations($this->data['stationTask'], [$this->data['station']['id']]);
-
-        $data = $this->service->createStationTask($this->data['station']['code'], $this->data['materialBox']['code']);
-        $this->assertFalse($data['success']);
-    }
-
-
-    protected function tearDown(): void
-    {
-        Station::query()->where('id', $this->data['station']['id'])->delete();
-        Station::query()->where('id', $this->data['parentStation']['id'])->delete();
-        StationTask::query()->where('station_id', $this->data['station']['id'])->delete();
-        MaterialBox::query()->where('id', $this->data['materialBox']['id'])->delete();
-        StationTaskMaterialBox::query()->where('station_id', $this->data['station']['id'])->delete();
-        if ($this->data['stationTask'] ?? false) StationTask::query()->whereIn('id', data_get($this->data['stationTask'], '*.id'))->delete();
-        parent::tearDown();
-    }
-}

+ 11 - 30
tests/Services/CacheShelfService/GetChildStationTest.php

@@ -10,6 +10,7 @@ use App\StationTask;
 use App\StationTaskChildren;
 use App\StationTaskMaterialBox;
 use App\StationType;
+use App\Storage;
 use Faker\Factory;
 use Illuminate\Database\Eloquent\Collection;
 use Tests\TestCase;
@@ -31,45 +32,25 @@ class GetChildStationTest extends TestCase
         $this->data['parentStation'] = factory(Station::class)->create(['station_type_id' => $stationType['id']]);
         $this->data['station'] = factory(Station::class)->create(['parent_id'=>$this->data['parentStation']['id']]);
         $this->data['materialBox'] = factory(MaterialBox::class)->create();
-        $this->data['stationTask'] = factory(StationTask::class)->create();
-        $this->data['stationTask']['station_id'] = $this->data['station']['id'];
-        $this->data['stationTask']->save();
-
-        $this->data['stationTaskMaterialBox'] = factory(StationTaskMaterialBox::class)->create([
-            'station_id' => $this->data['station']['id'],
-            'material_box_id' => $this->data['materialBox']['id'],
-            'status' => '待处理'
-        ]);
-
-        $this->data['stationTaskMaterialBox']['station_task_id'] = $this->data['stationTask']['id'];
-        $this->data['stationTaskMaterialBox']->save();
-
-        $this->data['stationTaskChildren']  = StationTaskChildren::query()->create([
-            "station_task_id" => $this->data['stationTask']['id'],
-        ]);
+        $this->data['storage'] = factory(Storage::class)->create(['station_id' => $this->data['station']['id'],'material_box_id' => $this->data['materialBox']['id']]);
     }
 
     public function testGetTasks()
     {
-        $grids = $this->service->getChildStation($this->data['station']['id']);
-        $this->assertTrue($grids ? true : false);
+        $childStations = $this->service->getChildStation($this->data['parentStation']['id']);
+        $this->assertTrue($childStations ? true : false);
+        $station = $childStations->first();
+        $this->assertEquals($this->data['storage']['id'],$station->storage['id']);
+        $this->assertEquals($this->data['materialBox']['id'],$station->storage->materialBox['id']);
     }
 
 
     protected function tearDown(): void
     {
-        $materialBox = MaterialBox::query()->where('id', $this->data['materialBox']['id'])->first();
-        if ($materialBox) {
-            $stationTaskMaterialBoxes = StationTaskMaterialBox::query()->where('material_box_id', $materialBox['id'])->get();
-            foreach ($stationTaskMaterialBoxes as $stationTaskMaterialBox) {
-                if($stationTaskMaterialBox->station->parent)$stationTaskMaterialBox->station->parent->delete();
-                if ($stationTaskMaterialBox->station) $stationTaskMaterialBox->station->delete();
-                if ($stationTaskMaterialBox->stationTask) $stationTaskMaterialBox->stationTask->delete();
-                $stationTaskMaterialBox->delete();
-            }
-            $materialBox->delete();
-        }
-        if($this->data['stationTaskChildren'])StationTaskChildren::query()->where('id',$this->data['stationTaskChildren']['id'])->delete();
+        if($this->data['parentStation'])Station::query()->whereKey($this->data['parentStation']['id'])->delete();
+        if($this->data['station'])Station::query()->whereKey($this->data['station']['id'])->delete();
+        if($this->data['materialBox'])MaterialBox::query()->whereKey($this->data['materialBox']['id'])->delete();
+        if($this->data['storage'])Storage::query()->whereKey($this->data['storage']['id'])->delete();
         parent::tearDown(); // TODO: Change the autogenerated stub
     }
 

+ 3 - 4
tests/Services/CacheShelfService/LightOffTaskTest.php

@@ -48,18 +48,17 @@ class LightOffTaskTest extends TestCase
         $this->data['station'] = factory(Station::class)->create(['parent_id'=>$this->data['parentStation']['id'],'code' => $this->data['locCode']]);
         $this->data['materialBox'] = factory(MaterialBox::class)->create();
 
-        $this->data['stationTask'] = factory(StationTask::class)->create();
+        $this->data['stationTask'] = factory(StationTask::class)->create(['station_id' =>$this->data['station']['id']]);
         $this->data['stationTask']['station_id'] = $this->data['station']['id'];
         $this->data['stationTask']->save();
 
         $this->data['stationTaskMaterialBox'] = factory(StationTaskMaterialBox::class)->create([
             'station_id' => $this->data['station']['id'],
             'material_box_id' => $this->data['materialBox']['id'],
-            'status' => '待处理'
+            'status' => '待处理',
+            'station_task_id' => $this->data['stationTask']['id'],
         ]);
 
-        $this->data['stationTaskMaterialBox']['station_task_id'] = $this->data['stationTask']['id'];
-        $this->data['stationTaskMaterialBox']->save();
 
         $this->data['stationTaskChildren']  = StationTaskChildren::query()->create([
             "station_task_id" => $this->data['stationTask']['id'],