ajun 5 lat temu
rodzic
commit
c6dfe01cd4

+ 7 - 42
app/Http/Controllers/CacheShelfController.php

@@ -3,12 +3,8 @@
 namespace App\Http\Controllers;
 
 use App\Components\AsyncResponse;
-use App\Exceptions\ErrorException;
-use App\MaterialBox;
 use App\Services\CacheShelfService;
-use App\Services\StationCacheShelfGridService;
 use App\Station;
-use App\StationCacheShelfGrid;
 use Illuminate\Contracts\Foundation\Application;
 use Illuminate\Contracts\View\Factory;
 use Illuminate\Database\Eloquent\Builder;
@@ -42,54 +38,23 @@ class CacheShelfController extends Controller
     public function getTasksApi(Request $request,string $id,CacheShelfService $service)
     {
         /** @var Station $station */
-        $station = Station::query()->where('id',$id)->first();
-
-        $service->getTasks($station);
+        $service->getChildStation($id);
         $this->success($station['grids']);
     }
 
     /**
      * 缓存架亮灯
      * @param Request $request
-     * @param StationCacheShelfGridService $gridService
+     * @param CacheShelfService $service
+     * @return mixed
      */
-    public function lightOnApi(Request $request,StationCacheShelfGridService $gridService)
+    public function lightOnApi(Request $request,CacheShelfService $service)
     {
-        $grid_id = $request['index'];
-        /** @var Station $station */
-        $station = Station::query()->where('id',$request['id'])->first();
 
-        $grid = StationCacheShelfGrid::query()->firstOrCreate(['station_id'=>$station['id'],'grid_id'=>$grid_id]);
-        $materialBox = MaterialBox::query()->firstOrCreate(['code'=>$request['code']]);
+        $stationCode = $request['stationCode'];
+        $materialBoxCode = $request['materialBoxCode'];
 
-        $grid->update(['material_box_id' => $materialBox['id'],'status' => 1]);
-
-        return $gridService->lightOn($station,$request['x'],$request['y']);
+        return $service->createStationTask($stationCode,$materialBoxCode);
     }
 
-//    /**
-//     * 拍灯推送任务
-//     * @param Request $request
-//     * @param CacheShelfService $service
-//     */
-//    public function pushTaskApi(Request $request,CacheShelfService $service)
-//    {
-//        /**
-//         * @var Station $station
-//         * @var MaterialBox $materialBox
-//         * @var StationCacheShelfGrid $grid
-//         */
-//        $station = Station::query()->where('id',$request['id'])->first();
-//        $materialBox = MaterialBox::query()->where('code',$request['code'])->first();
-//        $grid = StationCacheShelfGrid::query()->where(['station_id'=>$station['id'],'grid_id'=>$request['index']])->first();
-//
-//        try {
-//            $bool = $service->putBinToStore($station, $materialBox, $grid);
-//            if($bool)$this->success();
-//            else $this->error('推送任务异常');
-//        } catch (ErrorException $e) {
-//            $this->error($e->getMessage());
-//        }
-//    }
-
 }

+ 130 - 54
app/Services/CacheShelfService.php

@@ -2,13 +2,16 @@
 
 namespace App\Services;
 
+use App\Events\BroadcastToStation;
 use App\Exceptions\ErrorException;
 use App\MaterialBox;
 use App\Station;
-use App\StationCacheShelfGrid;
 use App\StationTaskMaterialBox;
+use App\StationType;
 use App\Traits\ServiceAppAop;
 use Illuminate\Support\Facades\Http;
+use Illuminate\Database\Eloquent\Builder;
+use Illuminate\Database\Eloquent\Collection;
 
 class CacheShelfService
 {
@@ -21,28 +24,40 @@ class CacheShelfService
     private $stationCacheShelfGridService;
     /** @var ForeignHaiRoboticsService $foreignHaiRoboticsService */
     private $foreignHaiRoboticsService;
+    /** @var StationTaskService $stationTaskService */
+    private $stationTaskService;
 
     /**
-     * 获取现有的缓存架任务
-     * @param Station $station
+     * 获取缓存架上子货架当前任务
+     * @param $id
+     * @return Builder[]|Collection
      */
-    public function getTasks(Station $station)
+    public function getChildStation($id)
     {
-        $grids = StationCacheShelfGrid::query()->with('materialBox')->where('station_id', $station['id'])->where('status', 1)->orderBy('grid_id')->get();
-        $station->setRelation('grids', $grids);
+        return Station::query()->where('parent_id',$id)->with('parent','currentStationTask.stationTaskMaterialBoxes.materialBox')->get();
     }
 
+
+    /**
+     * 拍灯触任务
+     * @param $locCode
+     * @param $PTLAction
+     * @return array|bool[]
+     */
     public function lightOffTask($locCode, $PTLAction): array
     {
         $this->instant($this->stationCacheShelfGridService, 'StationCacheShelfGridService');
-        list($stationCode, $gridId, $row, $col) = StationCacheShelfGrid::getGridByCode($locCode);
-        $station = Station::query()->where('code', $stationCode)->first();
-        $grid = StationCacheShelfGrid::query()->where('station_id', $station['id'])->where('grid_id', $gridId)->first();
-        $materialBox = MaterialBox::query()->where('id', $grid['material_box_id'])->first();
+
+        $station = Station::query()->where('code', $locCode)->with('parent','currentStationTask.stationTaskMaterialBoxes.materialBox')->first();
         try {
-            $bool = $this->putBinToStore($station, $materialBox, $grid);                    // 推送任务
-            if($bool)$this->stationCacheShelfGridService->lightOff($locCode, $PTLAction);    // 灭灯广播
-            return ['success' => $bool];
+            $bool = $this->putBinToStore($station);                            // 推送任务
+            if($bool){
+                $this->_stationCacheBroadCast($locCode,$PTLAction);        // 灭灯广播
+                $this->_stationCacheLightOff($locCode);
+                return ['success' => true];
+            }else{
+                return ['success' => false,'errMsg' => '机器人推送失败'];
+            }
         } catch (ErrorException $e) {
             LogService::log(__FUNCTION__,'缓存架推送任务失败',json_encode($e->getMessage()));
             return ['success' => false,'errMsg' => $e->getMessage()];
@@ -50,59 +65,120 @@ class CacheShelfService
     }
 
     /**
-     * 推任务
+     * 推任务至海柔机器人
      * @param  $station
-     * @param  $materialBox
-     * @param  $grid
      * @return bool
      * @throws ErrorException
      */
-    public function putBinToStore($station, $materialBox, $grid): bool
+    public function putBinToStore($station): bool
     {
         $this->instant($this->stationTaskMaterialBoxService, 'StationTaskMaterialBoxService');
         $this->instant($this->stationCacheShelfGridService, 'StationCacheShelfGridService');
         $this->instant($this->foreignHaiRoboticsService, 'ForeignHaiRoboticsService');
 
         /** @var StationTaskMaterialBox $stationTaskMaterialBox */
-        $stationTaskMaterialBox = $this->stationTaskMaterialBoxService->createByStationMaterialBox($station, $materialBox);
-        $this->stationCacheShelfGridService->processGrid($grid, $station, $materialBox);
+        $stationTaskMaterialBox = $station['currentStationTask']['stationTaskMaterialBoxes'];
+        return $this->foreignHaiRoboticsService->putBinToStore_fromCacheShelf($stationTaskMaterialBox);
+    }
+
+    /**
+     * 创建站任务和料箱任务
+     * @param $stationCode
+     * @param $materialBoxCode
+     * @return array
+     */
+    public function createStationTask($stationCode,$materialBoxCode): array
+    {
+        $this->instant($this->stationTaskService, 'StationTaskService');
+        $this->instant($this->stationTaskMaterialBoxService,'stationTaskMaterialBoxService');
 
-        $station->setRelation('grids', $grid);
-        $stationTaskMaterialBox->setRelation('station', $station);
-        $stationTaskMaterialBox->setRelation('materialBox', $materialBox);
+//        $station = Station::query()->with('currentPendingTask.stationTaskMaterialBoxes.materialBox')->firstOrCreate(['code' => $stationCode]);
+        $station = Station::query()->where('code' , $stationCode)->first();
+        if(!$station){
+            $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']]);
+        }
 
-        return $this->foreignHaiRoboticsService->putBinToStore_fromCacheShelf($stationTaskMaterialBox);
+        $materialBox = MaterialBox::query()->firstOrCreate(['code' => $materialBoxCode]);
+        if($station['currentPendingTask']['stationTaskMaterialBoxes']['materialBox'] ?? false){
+            return ['success' => false,'message' => '当前已有未完成的站任务'];
+        }
+        $stationTask = $this->stationTaskService->create(1);                          // 生成站任务
+        $this->stationTaskMaterialBoxService->createByStationMaterialBox($station,$materialBox);     // 创建料箱任务
+        $this->stationTaskService->registerStations($stationTask,[$station['id']]);                  // 注册站任务站
+        return['success'=>true];
+    }
+
+    /**
+     * 控制格口亮灯
+     * @param $locCode
+     * @param string $title
+     * @param null $materialCode
+     * @return mixed
+     */
+    public function _stationCacheLightOn($locCode,$title = 'title',$materialCode = null)
+    {
+        $params = [
+            "areaCode" => "1004",
+            'locCode' => $locCode,
+            'PTLAction' => 1,
+            'PTLSettings' => [
+                'color'=> 1,
+                'frequency'  =>1
+                ],
+            "displayInfo" => [
+                "detail01" => $materialCode,
+                "detail02" => "detail02",
+                "detail03" => "detail03",
+                "qrCode" => "qrCode",
+                "qty00" => "11",
+                "qty01" => 1,
+                "qty02" => 2,
+                "title" => $title,
+                "uomDesc01" => "uo",
+                "uomDesc02" => "uo"
+            ],
+        ];
+        $response = Http::post(config('api.haiq.storage.light'), $params);
+        return json_decode($response->body());
+    }
+
+    /**
+     * 控制格口灭灯
+     * @param $locCode
+     * @return mixed
+     */
+    public function _stationCacheLightOff($locCode){
+        $params = [
+            "areaCode" => "1004",
+            'locCode' => $locCode,
+            'PTLAction' => 0,
+        ];
+        $response = Http::post(config('api.haiq.storage.light'), $params);
+        return json_decode($response->body());
+    }
+
+    /**
+     * 广播 通知货物被取走
+     * @param $locCode
+     * @param $PTLAction
+     */
+    public function _stationCacheBroadCast($locCode,$PTLAction)
+    {
+        if($PTLAction == 0){
+            $station = Station::query()->with('parent')->where('code',$locCode)->first();
+            $json = json_encode( [
+                'station_id' => $station['parent']['id'],
+                'code'  => $station['parent']['code'],
+                'gird_id' => $station['id'],
+                'grid_code' => $station['code'],
+            ]);
+            broadcast(new  BroadcastToStation($station['id'],$json));
+        }
     }
 
-//    /**
-//     * 入库任务完成
-//     * @param $params
-//     */
-//    public function putBinToStoreFinish($params)
-//    {
-//        $this->instant($this->stationTaskMaterialBoxService,'StationTaskMaterialBoxService');
-//
-//        $locCode = $params['locCode'];
-//
-//        list($stationCode, $gridId) = StationCacheShelfGrid::getGridByCode($locCode);
-//        $station = Station::query()->where('code', $stationCode)->first();
-//
-//        $stationCacheShelfGrid = StationCacheShelfGrid::query()->with('materialBox')->where('station_id', $station)->where('grid_id', $gridId)->first();
-//        $stationCacheShelfGrid->update(['status' => '0', 'material_box_id' => null]);
-//
-//        $StationTaskMaterialBox = StationTaskMaterialBox::query()->where('station_id', $station['id'])->where('material_box_id', $stationCacheShelfGrid['$stationCacheShelfGrid'])->first();
-//        $this->stationTaskMaterialBoxService->set($StationTaskMaterialBox, ['status' => '已完成']);
-//    }
-
-//    /**
-//     * 取消格口任务
-//     * @param Station $station
-//     * @param array $girds
-//     */
-//    public function cancelTask(Station $station, array $girds = [])
-//    {
-//        $gridQuery = StationCacheShelfGrid::query()->where('station_id', $station['id']);
-//        if (count($girds) > 0) $gridQuery->whereIn('grid_id', $girds);
-//        $this->stationCacheShelfGridService->cancelTask($gridQuery->get());
-//    }
 }

+ 1 - 1
app/Services/ForeignHaiRoboticsService.php

@@ -245,7 +245,7 @@ class ForeignHaiRoboticsService
         $this->instant($this->stationService,'StationService');
 
         // 缓存架格口
-        $formLocation = StationCacheShelfGrid::getLocation($stationTaskMaterialBox['station'],$stationTaskMaterialBox['station']['grids']->first());
+        $formLocation = $stationTaskMaterialBox['station']['code'];
 
         /** 创建料箱 从缓存架 到 立架任务 */
         $dataToPost=$this->makeJson_move(

+ 2 - 2
app/Services/PackageStatisticsService.php

@@ -27,8 +27,8 @@ class PackageStatisticsService
                 ->selectRaw('orders.owner_id,logistic_id');
 
         $columnQueryRules=[
-            'created_at_start' => ['alias' => 'created_at','startDate' => ":00"],
-            'created_at_end' => ['alias' => 'created_at','endDate' => ":59"],
+            'created_at_start' => ['alias' => 'order_packages.created_at','startDate' => ":00"],
+            'created_at_end' => ['alias' => 'order_packages.created_at','endDate' => ":59"],
             'logistic_id' => ['multi' => ','],
             'owner_id' => ['multi' => ','],
         ];

+ 0 - 1
app/Services/StationCacheShelfGridService.php

@@ -63,7 +63,6 @@ class StationCacheShelfGridService
                 'grid_id' => $gridId,
             ]);
             broadcast(new BroadcastToStation($station['id'] ?? '', $json));
-            StationCacheShelfGrid::query()->where('station_id',$station['id'])->where('grid_id', $gridId)->update(['material_box_id' => null, 'status' => 0]);
         }
     }
 

+ 1 - 1
app/Services/StationTaskMaterialBoxService.php

@@ -153,6 +153,7 @@ class StationTaskMaterialBoxService
         }
     }
 
+    // TODO 料箱处理
     function markHasTaken($stationTaskMaterialBox)
     {
         //TODO: 标记 料箱位置(需要其字段存在)$stationTaskMaterialBox['materialBox']['position']
@@ -168,7 +169,6 @@ class StationTaskMaterialBoxService
                 'code' => $stationTaskMaterialBox['materialBox']['code'],
                 'status' => '完成'
             ]);
-            $grids = StationCacheShelfGrid::query()->where(['station_id'=>$stationTaskMaterialBox['station_id'],'material_box_id'=>$stationTaskMaterialBox['material_box_id']])->get();
             $this->stationCacheShelfGridService->cancelTask($grids);
             broadcast(new BroadcastToStation($stationTaskMaterialBox['station_id'], $json));
             $this->markProcessed($stationTaskMaterialBox);

+ 4 - 2
app/Station.php

@@ -5,6 +5,7 @@ namespace App;
 use App\Traits\ModelTimeFormat;
 use Illuminate\Database\Eloquent\Model;
 use Illuminate\Database\Eloquent\Relations\HasMany;
+use Illuminate\Database\Eloquent\Relations\HasOne;
 use App\Traits\ModelLogChanging;
 
 class Station extends Model
@@ -31,8 +32,9 @@ class Station extends Model
         return $this->hasOne(StationTypeBinMonitor::class);
     }
 
-    public function grids(): HasMany
+    public function currentPendingTask(): HasOne
     {
-        return $this->hasMany(StationCacheShelfGrid::class);
+        return $this->hasOne(StationTask::class)
+            ->where('status' ,'=', '待处理');
     }
 }

+ 0 - 61
app/StationCacheShelfGrid.php

@@ -1,61 +0,0 @@
-<?php
-
-namespace App;
-
-use Illuminate\Database\Eloquent\Model;
-use Illuminate\Database\Eloquent\Relations\BelongsTo;
-use App\Traits\ModelLogChanging;
-
-class StationCacheShelfGrid extends Model
-{
-    use ModelLogChanging;
-
-    protected $fillable = ['station_id', 'material_box_id', 'grid_id', 'status'];
-
-    public static $status = [
-        '0' => '无',
-        '1' => '待处理',
-    ];
-
-    public function station(): BelongsTo
-    {
-        return $this->belongsTo(Station::class);
-    }
-
-    public function materialBox(): BelongsTo
-    {
-        return $this->belongsTo(MaterialBox::class);
-    }
-
-    /**
-     * 根据格口计算位置
-     * @param Station $station
-     * @param StationCacheShelfGrid $grid
-     * @return string
-     */
-    public static function getLocation(Station $station, StationCacheShelfGrid $grid): string
-    {
-        $code = $station['code'];
-        $grid_id = $grid['grid_id'];
-        $row = 2 - round($grid_id / 3) + 1;
-        $col = 2 - ($grid_id % 3) + 1;
-        return 'HAI' . $code . '-0' . $col . '-0' . $row;
-    }
-
-    /**
-     * 根据位置计算 grid_id 和 station code
-     * @param $code
-     * @return array|false[]
-     */
-    public static function getGridByCode($code): array
-    {
-        $arr = [];
-        preg_match('/^HAI([\w\.\ ]+)-0([0-9]+)-0([0-9]+)/',$code,$arr);
-        if(count($arr)==0)return [false,false,null,null];
-        $stationCode =$arr[1] ?? false;
-        $col = $arr[2] ?? 0;  // 列
-        $row = $arr[3] ?? 0;  // 行
-        $gridId = ($row-1)*3 + (3-$col);
-        return [$stationCode,$gridId,$row,$col];
-    }
-}

+ 28 - 0
database/migrations/2021_04_29_133306_drop_station_cache_shelf_grips_table.php

@@ -0,0 +1,28 @@
+<?php
+
+use Illuminate\Database\Migrations\Migration;
+use Illuminate\Database\Schema\Blueprint;
+use Illuminate\Support\Facades\Schema;
+
+class DropStationCacheShelfGripsTable extends Migration
+{
+    /**
+     * Run the migrations.
+     *
+     * @return void
+     */
+    public function up()
+    {
+        Schema::dropIfExists('station_cache_shelf_grids');
+    }
+
+    /**
+     * Reverse the migrations.
+     *
+     * @return void
+     */
+    public function down()
+    {
+        //
+    }
+}

+ 11 - 8
resources/views/station/cachingShelf/list/index.blade.php

@@ -24,6 +24,7 @@
                 materialBoxCode: null,      // 料箱编码
                 currentStation: {           // 当前Station
                     id: null,               // Station->id
+                    code:'',
                     data: [[], [], []],     // 格口
                 },
                 boxClass: 'modal-sm',
@@ -39,13 +40,15 @@
             methods: {
                 /** 1初始化缓存架格口 */
                 initCacheShelfBox() {
+                    let _code = this.currentStation.code;
                     this.currentStation.data.forEach(function (item, index, self) {
                         let boxRow = new Array(3);
                         for (let i = 0; i < boxRow.length; i++) {
                             let row =  2 - index + 1;
                             let col =  2 - i + 1;
                             let myIndex = index * 3 + i + 1;
-                            boxRow[i] = {x: index, y: i, row: row, col: col, code: null, index: myIndex};
+                            let code = 'HAI'+_code+'-0'+col+'-0'+row;
+                            boxRow[i] = {x: index, y: i, row: row, col: col, code: null, index: myIndex,stationCode:code};
                         }
                         self[index] = boxRow;
                     });
@@ -60,6 +63,7 @@
                 /** 展示modal */
                 showFeedBox(station) {
                     this.currentStation.id = station['id'];
+                    this.currentStation.code = station['code'];
                     this.broadcastName = "{{config('database.redis.options.prefix')}}" + 'station-' + station['id'];
                     this.initCacheGrid();
                     $('#box').modal('show');
@@ -82,13 +86,12 @@
                         if (res.data.data) {
                             if (res.data.data.length === 0) return;
                             for (let i = 0; i < res.data.data.length; i++) {
-                                if(res.data.data.length === i)break;
-                                let grid = res.data.data[i];
-                                let gridIndex = grid['grid_id'];
-                                let row = Math.round(gridIndex / 3);
-                                let col = gridIndex % 3 -1;
-                                let code = grid['material_box'] ? grid['material_box']['code'] : null;
-                                if(grid['status'] === 1) this.$set(this.currentStation.data[row][col], 'code', code);
+                                let stationCode = res.data.data[i]['code'];
+                                let codes  = /^HAI([\w]+)-0([\w])-0([\w])/.exec(stationCode);
+                                let row = codes[3];
+                                let col = codes[2];
+                                let materialBoxCode = res.data.data[i]['currentStationTask']['stationTaskMaterialBoxes']['materialBox'];
+                                this.$set(this.currentStation.data[row][col],'code',materialBoxCode);
                             }
                             this.$forceUpdate();
                             return;

+ 33 - 0
tests/Services/CacheShelfService/StationCacheLightOffTest.php

@@ -0,0 +1,33 @@
+<?php
+
+
+namespace Tests\Services\CacheShelfService;
+
+
+use App\Services\CacheShelfService;
+use Tests\TestCase;
+
+class StationCacheLightOffTest extends TestCase
+{
+    /** @var CacheShelfService $service */
+    protected $service;
+    public $data = [];
+
+    protected function setUp(): void
+    {
+        parent::setUp(); // TODO: Change the autogenerated stub
+        $this->data['code'] = 'HAIB1-01-01';
+        $this->service = app(CacheShelfService::class);
+    }
+
+    public function testLightOff()
+    {
+        $body = $this->service->_stationCacheLightOff($this->data['code']);
+        $this->assertEquals($body->code,200);
+    }
+
+    protected function tearDown(): void
+    {
+        parent::tearDown(); // TODO: Change the autogenerated stub
+    }
+}

+ 32 - 0
tests/Services/CacheShelfService/StationCacheLightOnTest.php

@@ -0,0 +1,32 @@
+<?php
+
+
+namespace Tests\Services\CacheShelfService;
+
+use App\Services\CacheShelfService;
+use Tests\TestCase;
+
+class StationCacheLightOnTest extends TestCase
+{
+    /** @var CacheShelfService $service */
+    protected $service;
+    public $data = [];
+
+    protected function setUp(): void
+    {
+        parent::setUp(); // TODO: Change the autogenerated stub
+        $this->data['code'] = 'HAIB1-01-01';
+        $this->service = app(CacheShelfService::class);
+    }
+
+    public function testLightOn()
+    {
+        $body = $this->service->_stationCacheLightOn($this->data['code'], 'IDE0005665');
+        $this->assertEquals($body->code, 200);
+    }
+
+    protected function tearDown(): void
+    {
+        parent::tearDown(); // TODO: Change the autogenerated stub
+    }
+}