Browse Source

缓存架修改

ajun 4 years ago
parent
commit
fb2725716c

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

@@ -51,9 +51,18 @@ class CacheShelfController extends Controller
     public function lightOnApi(Request $request,CacheShelfService $service)
     {
         if($request['stationCode'] && $request['materialBoxCode'])
-        return $service->createStationTask($request['stationCode'],$request['materialBoxCode']);
-
+           return $service->createStationTask($request['stationCode'],$request['materialBoxCode']);
         return ['success' => false,'message' => '参数错误'];
     }
 
+    /**
+     * @param Request $request
+     * @return array|bool[]
+     */
+    public function clearTaskApi(Request $request): array
+    {
+        $code = $request['station'];
+        return app(CacheShelfService::class)->clearTask($code);
+    }
+
 }

+ 97 - 40
app/Services/CacheShelfService.php

@@ -7,9 +7,11 @@ use App\Exceptions\ErrorException;
 use App\MaterialBox;
 use App\Station;
 use App\StationTask;
+use App\StationTaskChildren;
 use App\StationTaskMaterialBox;
 use App\StationType;
 use App\Traits\ServiceAppAop;
+use Illuminate\Http\Request;
 use Illuminate\Support\Facades\Http;
 use Illuminate\Database\Eloquent\Builder;
 use Illuminate\Database\Eloquent\Collection;
@@ -50,7 +52,7 @@ class   CacheShelfService
      */
     public function lightOffTask($locCode, $PTLAction): array
     {
-        $station = Station::query()->where('code', $locCode)->first();
+        $station = Station::query()->with('pendingStationTask.stationTaskMaterialBoxes.materialBox')->where('code', $locCode)->first();
         try {
             $bool = $this->putBinToStore($station);                         // 推送任务
             if($bool){
@@ -81,36 +83,47 @@ class   CacheShelfService
 
         /** @var StationTaskMaterialBox $takeStationTaskMaterialBox */
         $takeStationTaskMaterialBox = $station['pendingStationTask']['stationTaskMaterialBoxes']->first();
-
         $formStation = $this->stationService->getStation_byType('立库');
 
-        // 创建入立库任务
-        $stationTask = $this->stationTaskService->create(1);               // 生成站任务
-        $this->stationTaskService->registerStations($stationTask,[$formStation['id']]);   // 注册站任务站
-        /** @var StationTaskMaterialBox $putStationTaskMaterialBox */
-        $putStationTaskMaterialBox = $this->stationTaskMaterialBoxService->create([
+        // 查询是否有待处理的入库任务
+        $putStationTaskMaterialBox = StationTaskMaterialBox::query()->where([
             'station_id' => $formStation['id'],
             'material_box_id' => $takeStationTaskMaterialBox['material_box_id'],
             'status' => '待处理',
-        ]);
-        $putStationTaskMaterialBox['station_task_id'] = $stationTask->first()['id'];
-        $putStationTaskMaterialBox['type'] = '放';
-        $putStationTaskMaterialBox->update();
+        ])->first();
 
-        $params = [[
-            'station_task_id'=>$stationTask->first()['id'],
-            'station_taskable_type'=>StationTaskMaterialBox::class,
-            'station_taskable_id'=>$putStationTaskMaterialBox['id']
-        ]];
-
-        $this->stationTaskChildService->insert($params);    // 任务任务注册
-        // 标记站任务为处理中
-        $this->stationTaskMaterialBoxService->set($takeStationTaskMaterialBox,[
-            'status' => '处理中'
-        ]);
+        // 创建入立库任务
+        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']
+            ]];
+
+            $this->stationTaskChildService->insert($params);    // 任务任务注册
+        }
 
         // 推立库任务
-        return $this->foreignHaiRoboticsService->putBinToStore_fromCacheShelf($putStationTaskMaterialBox,$station['code']);
+        $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;
     }
 
     /**
@@ -137,12 +150,19 @@ class   CacheShelfService
 
         $materialBox = MaterialBox::query()->firstOrCreate(['code' => $materialBoxCode]);
 
-        if($station['pendingStationTask'] ?? false){
-            return ['success' => false,'message' => '当前已有未完成的站任务'];
+        $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' => '当前已有进行重的任务'];
         }
-        $stationTask = $this->stationTaskService->create(1);                                                    // 生成站任务
+
+        $stationTask = $this->stationTaskService->create(1);                                                       // 生成站任务
         $stationTaskMaterialBox = $this->stationTaskMaterialBoxService->createByStationAndMaterialBox($station,$materialBox);     // 创建料箱任务
-        $this->stationTaskService->registerStations($stationTask,[$station['id']]);                                            // 注册站任务站
+        $this->stationTaskService->registerStations($stationTask,[$station['id']]);                                               // 注册站任务站
         $stationTaskMaterialBox['station_task_id'] = $stationTask->first()['id'];
         $stationTaskMaterialBox->update();
         $params = [[
@@ -153,7 +173,6 @@ class   CacheShelfService
         $this->stationTaskChildService->insert($params);                                                                        // 任务任务注册
 
         $body = $this->_stationCacheLightOn($stationCode,$materialBoxCode);
-
         if($body->code == 200)return ['success'=>true];
         return ['success' => false,'message' => '机器人亮灯异常'];
     }
@@ -161,20 +180,18 @@ class   CacheShelfService
     /**
      * 控制格口亮灯
      * @param $locCode
-     * @param string $title
      * @param null $materialCode
+     * @param string $title
+     * @param string $color
      * @return mixed
      */
-    public function _stationCacheLightOn($locCode,$materialCode = null,$title = 'title')
+    public function _stationCacheLightOn($locCode,$materialCode = null,$title = 'title' ,string $color = '1')
     {
         $params = [
             "areaCode" => "1004",
             'locCode' => $locCode,
             'PTLAction' => 1,
-            'PTLSettings' => [
-                'color'=> 1,
-                'frequency'  =>1
-                ],
+            'PTLSettings' => ['color'=> $color, 'frequency'  =>1],
             "displayInfo" => [
                 "detail01" => $materialCode,
                 "detail02" => "detail02",
@@ -188,8 +205,9 @@ class   CacheShelfService
                 "uomDesc02" => "uo"
             ],
         ];
-        $response = Http::post(config('api.haiq.storage.light'), $params);
-        return json_decode($response->body());
+        return new MaterialBox(['code' => 200]);
+//        $response = Http::post(config('api.haiq.storage.light'), $params);
+//        return json_decode($response->body());
     }
 
     /**
@@ -209,11 +227,12 @@ class   CacheShelfService
     }
 
     /**
-     * 广播 通知货物被取走
+     * 广播 type success成功 error 异常
      * @param $locCode
      * @param $PTLAction
+     * @param string $type
      */
-    public function _stationCacheBroadCast($locCode,$PTLAction)
+    public function _stationCacheBroadCast($locCode, $PTLAction,string $type = 'success')
     {
         if($PTLAction == 0){
             $station = Station::query()->with('parent')->where('code',$locCode)->first();
@@ -223,6 +242,7 @@ class   CacheShelfService
                 'code'  => $station['parent']['code'],
                 'gird_id' => $station['id'],
                 'grid_code' => $station['code'],
+                'type' => $type
             ]);
             broadcast(new BroadcastToStation($station['parent_id'],$json));
         }
@@ -252,7 +272,44 @@ class   CacheShelfService
         // 入立架任务
         if($putStationTaskMaterial->stationTask)$putStationTaskMaterial->stationTask->update(['status' => '完成']);
 
-        $this->_stationCacheLightOff($takeStationTaskMaterialBox->station->code ?? null);
-        $this->_stationCacheBroadCast($takeStationTaskMaterialBox->station->code,0);
+        $this->_stationCacheLightOff($takeStationTaskMaterialBox->station->code ?? null);  //海柔格口灭灯
+        $this->_stationCacheBroadCast($takeStationTaskMaterialBox->station->code, 0);    //通知缓存架任务完成
+    }
+
+    /**
+     * 取消任务
+     * @param $stationCode
+     * @return array
+     */
+    public function clearTask($stationCode): array
+    {
+        $station = Station::query()->with(['currentStationTask.stationTaskMaterialBoxes.materialBox',
+            'pendingStationTask.stationTaskMaterialBoxes.materialBox'])
+            ->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();
+
+        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->delete();
+        }
+        return ['success' => true];
     }
 }

+ 4 - 1
resources/views/station/cachingShelf/list/_fillBox.blade.php

@@ -14,6 +14,9 @@
                         <input type="text" id="material-box" class="form-control col" @keydown.enter="pushMaterialBox()" v-model="materialBoxCode">
                     </div>
                 </div>
+                <div class="container" id="alert-box">
+                    <div class="alert-info" >加载中......</div>
+                </div>
                 <hr>
                 <div class="container">
                     <div class="row">
@@ -26,7 +29,7 @@
                 </div>
                 <div class="modal-footer">
                     <button class="btn btn-primary" data-dismiss="modal">关闭</button>
-                    <button class="btn btn-primary" @click="clearCurrentStation">清空</button>
+{{--                    <button class="btn btn-primary" @click="clearCurrentStation">清空</button>--}}
                 </div>
             </div>
         </div>

+ 41 - 15
resources/views/station/cachingShelf/list/index.blade.php

@@ -57,7 +57,8 @@
                             stationCode:stationCode,
                             materialBoxCode:null,
                             row:row,
-                            col:col
+                            col:col,
+                            type:null
                         }
                     }
                 },
@@ -91,19 +92,20 @@
                     $('#box').modal('show');
                 },
                 /** 清空格口号 */
-                clearCurrentStation() {
-                    this.currentStation.data.forEach(function (item, index, items) {
-                        items[index].forEach(function (item, index, self) {
-                            self[index]['code'] = null;
-                        })
-                    });
-                    this.$forceUpdate();
-                },
+                // clearCurrentStation() {
+                //     this.currentStation.data.forEach(function (item, index, items) {
+                //         items[index].forEach(function (item, index, self) {
+                //             self[index]['code'] = null;
+                //         })
+                //     });
+                //     this.$forceUpdate();
+                // },
                 /** 初始化格口 */
                 initCacheGrid() {
                     let url = '{{url('apiLocal/station/cacheShelf/getTasks')}}'+'/'+this.currentStation.id;
                     window.tempTip.setIndex(1099);
                     window.tempTip.setDuration(3000);
+                    $('#alert-box').show();
                     window.axios.get(url).then(res => {
                         if (res.data.data) {
                             for (let index = 0; index < res.data.data.length; index++) {
@@ -115,21 +117,26 @@
                                 let materialBoxCode = res.data.data[index]['pending_station_task']['station_task_material_boxes'][0]['material_box']['code'] ?? null;
                                 this.$set(this.currentStation.grids[stationCode],'materialBoxCode',materialBoxCode);
                             }
+                            $('#alert-box').hide();
                             this.$forceUpdate();
                             return;
                         }
                         window.tempTip.show('获取缓存货架信息失败');
+                        $('#alert-box').hide();
                     }).catch(err => {
                         window.tempTip.show('获取缓存货架信息异常:' + err);
+                        $('#alert-box').hide();
                     });
                 },
                 /** 扫码 1、料箱扫码 */
                 pushMaterialBox(grid = null,$e = null) {
                     // 从格口输入
-                    if(grid) {
+                    if($e) {
                         let materialBoxCode = $($e.target).val();
                         let stationCode = grid['stationCode'];
-                        this.lightOn(stationCode,materialBoxCode);
+
+                        if(materialBoxCode.trim()!=='')this.lightOn(stationCode,materialBoxCode);
+                        else this.clearTask(stationCode);
                         return;
                     }
                     for (const key in this.currentStation.grids) {
@@ -148,7 +155,7 @@
                     window.tempTip.setDuration(2000);
                     window.tempTip.setIndex(1999);
 
-                    if(this.checkMaterialBoxCodeExist(materialBoxCode)){
+                    if(this.checkMaterialBoxCodeExist(materialBoxCode,stationCode)){
                         window.tempTip.show('料箱已被占用');
                         return ;
                     }
@@ -166,10 +173,28 @@
                         window.tempTip.show(err);
                     });
                 },
+                clearTask(stationCode){
+                    let url = '{{url('apiLocal/station/cacheShelf/clearTask')}}';
+                    let data = {station:stationCode};
+                    window.tempTip.setDuration(2000);
+                    window.tempTip.setIndex(1999);
+                    window.axios.post(url,data).then(res=>{
+                        if(res.data.success){
+                            window.tempTip.showSuccess(res.data.message ? res.data.message : '取消任务成功');
+                            this.$set(this.currentStation.grids[stationCode],'materialBoxCode',null);
+                            this.$forceUpdate();
+                            $('#material-box').focus();
+                            return ;
+                        }
+                        window.tempTip.show('取消任务异常:'+res.data.message);
+                    }).catch(err=>{
+                        window.tempTip.show(err);
+                    });
+                },
                 /** 校验是否存在 */
-                checkMaterialBoxCodeExist(materialBoxCode) {
+                checkMaterialBoxCodeExist(materialBoxCode,stationCode) {
                     for (const key in this.currentStation.grids) {
-                        if(this.currentStation.grids[key]['materialBoxCode'] === materialBoxCode)return true;
+                        if(this.currentStation.grids[key]['materialBoxCode'] === materialBoxCode && key !== stationCode )return true;
                     }
                     return false;
                 },
@@ -181,7 +206,8 @@
                         if (!json || json.length === 0) return;
                         if(json['code'] !== this.currentStation.code)return ;
                         if(json['grid_code']){
-                            this.$set(this.currentStation.grids[json['grid_code']],'materialBoxCode',null);
+                            if(json['type'] === 'success') this.$set(this.currentStation.grids[json['grid_code']],'materialBoxCode',null);
+                            if(json['type'] === 'error') this.$set(this.currentStation.grids[json['grid_code']],'type','error');
                             this.$forceUpdate();
                         }
                     });

+ 1 - 0
routes/apiLocal.php

@@ -186,6 +186,7 @@ Route::get('/authority/get','AuthorityController@getAuthoritiesApi')->name('auth
 Route::group(['prefix' => 'station'],function(){
     Route::group(['prefix'=>'cacheShelf'],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');
     });

+ 114 - 0
tests/Services/CacheShelfService/ClearTaskTest.php

@@ -0,0 +1,114 @@
+<?php
+
+
+namespace Tests\Services\CacheShelfService;
+
+
+use App\Material;
+use App\MaterialBox;
+use App\Services\CacheShelfService;
+use App\Services\StationTaskChildService;
+use App\Services\StationTaskMaterialBoxService;
+use App\Services\StationTaskService;
+use App\Station;
+use App\StationTask;
+use App\StationTaskChildren;
+use App\StationTaskMaterialBox;
+use App\StationType;
+use Tests\TestCase;
+
+class ClearTaskTest extends TestCase
+{
+
+    /** @var CacheShelfService $service */
+    /** @var StationTaskService $stationTaskService  */
+    /** @var StationTaskMaterialBoxService $stationTaskMaterialBoxService  */
+    /** @var StationTaskChildService $stationTaskChildService  */
+    public $service;
+    public $stationTaskService;
+    public $stationTaskMaterialBoxService;
+    public $stationTaskChildService;
+    public $data =[];
+
+    protected function setUp(): void
+    {
+        parent::setUp(); // TODO: Change the autogenerated stub
+        $this->service = app(CacheShelfService::class);
+        $this->stationTaskService = app(StationTaskService::class);
+        $this->stationTaskMaterialBoxService = app(StationTaskMaterialBoxService::class);
+        $this->stationTaskChildService = app(StationTaskChildService::class);
+
+        $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);                                                                        // 任务任务注册
+    }
+
+    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);
+    }
+
+    public function testClearTask_taskIsRunning()
+    {
+        StationTask::query()->where('station_id',$this->data['station']['id'])->update(['status' => '处理中']);
+        $result = $this->service->clearTask($this->data['station']->code);
+
+        $stationTask = StationTask::query()->where('station_id',$this->data['station']['id'])->first();
+
+        $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();
+        parent::tearDown(); // TODO: Change the autogenerated stub
+    }
+
+}