فهرست منبع

缓存架出库逻辑BUG修复

Zhouzhendong 4 سال پیش
والد
کامیت
103ed4bdf7

+ 1 - 1
app/Http/Controllers/StorageController.php

@@ -233,7 +233,7 @@ sql;
                     "track_num" => request("track"),
                 ]);
                 //亮灯
-                app("CacheShelfService")->lightUp($station->code,'3','2');
+                app("CacheShelfService")->lightUp($station->code,'3','0');
                 app("StationService")->locationOccupy($station->code);//占用库位
                 Cache::forever("CACHE_SHELF_OCCUPANCY_{$station->id}",true);
                 DB::commit();

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

@@ -11,8 +11,14 @@ use App\Owner;
 use App\OwnerFeeDetail;
 use App\OwnerPriceOperation;
 use App\RejectedBill;
+use App\Services\ForeignHaiRoboticsService;
+use App\Station;
+use App\StationTask;
+use App\StationTaskMaterialBox;
+use App\TaskTransaction;
 use App\Unit;
 use Illuminate\Http\Request;
+use Illuminate\Support\Facades\Cache;
 use Illuminate\Support\Facades\DB;
 use Illuminate\Support\Facades\Http;
 
@@ -33,7 +39,7 @@ class TestController extends Controller
     }
     public function lightUp()
     {
-        app("CacheShelfService")->lightUp('HAIB1-02-02','3','2');
+        app("CacheShelfService")->lightUp('HAIB1-02-02','3','0');
     }
     public function lightOff()
     {
@@ -45,9 +51,53 @@ class TestController extends Controller
         $response = Http::post(config('api.haiq.storage.light'), $params);
         return json_decode($response->body());
     }
+    private $key = "CACHE_SHELF_AVAILABLE";
     public function test()
     {
-        dd(!!Unit::query()->first());
+        TaskTransaction::query()->where("id",23)->update([
+            "to_station_id" => 10,
+            "status" => 0,
+            "updated_at" => date("Y-m-d H:i:s")
+        ]);
+        TaskTransaction::query()->where("id",27)->update([
+            "to_station_id" => 24,
+            "status" => 0,
+            "updated_at" => date("Y-m-d H:i:s")
+        ]);
+        dd("OK");
+        $ar = StationTaskMaterialBox::query()->whereIn("id",[66124,66125,66126,66127,66128])->get();
+        foreach ($ar as $stationTaskMaterialBox){
+            $stationTaskMaterialBox->materialBox['status']='在缓存架';
+            $stationTaskMaterialBox->materialBox->update();
+            $stationTaskMaterialBox->loadMissing("station");    //提前加载站,后续都需要站信息来处理
+            app("StorageService")->putCacheShelf($stationTaskMaterialBox);
+            app("StorageService")->releaseOccupation($stationTaskMaterialBox); //释放库位占用
+            $this->set($stationTaskMaterialBox,[
+                'id' => $stationTaskMaterialBox['station_id'],
+                'status' => '完成',
+            ]);
+            if (!$stationTaskMaterialBox->station_task_id)return;
+            $task = StationTaskMaterialBox::query()->select(DB::raw(1))
+                ->where("station_task_id",$stationTaskMaterialBox->station_task_id)
+                ->where("status","!=","完成")->first();
+            if (!$task)StationTask::query()->where("id",$stationTaskMaterialBox->station_task_id)
+                ->update(["status"=>"完成"]);
+        }
+    }
+
+    function set($target,array $kvPairs){
+        $update = $target->update($kvPairs);
+        Cache::forget($this->cachingKey($kvPairs));
+        if(isset($kvPairs['id'])){
+            Cache::forget($this->cachingKey(['id'=>$kvPairs['id']]));
+        }
+        return $update;
+    }
+    protected $modelClass=StationTaskMaterialBox::class;
+    protected function cachingKey(array $kvPairs): string
+    {
+        ksort($kvPairs);
+        return $this->modelClass.md5(json_encode($kvPairs));
     }
 
 }

+ 7 - 4
app/Jobs/CacheShelfTaskJob.php

@@ -48,7 +48,7 @@ class CacheShelfTaskJob implements ShouldQueue
                 try {
                     $available = Cache::get($this->key,function (){return [];});
                     if ($this->count!==count($available))return;
-                    $tasks = TaskTransaction::query()->with("task")
+                    $tasks = TaskTransaction::query()->selectRaw("task_id,GROUP_CONCAT(id) AS ids,id")->with("task")
                         ->where("type","出库")->whereHas("task",function ($query){
                             $query->where("status","待处理");
                         })->where("status",3)->lockForUpdate()
@@ -70,7 +70,10 @@ class CacheShelfTaskJob implements ShouldQueue
                         $task->push($obj->task);
                         unset($available[$loc]);
                         $updateTask[] = ["id"=>$obj->task->id,"station_id"=>$map[$loc],"updated_at"=>$time];
-                        $updateTransaction[] = ["id"=>$obj->id,"to_station_id"=>$map[$loc],"status"=>0,"updated_at"=>$time];
+                        if ($obj->ids!=$obj->id){
+                            $ids = explode(",",$obj->ids);
+                            foreach ($ids as $id)$updateTransaction[] = ["id"=>$id,"to_station_id"=>$map[$loc],"status"=>0,"updated_at"=>$time];
+                        }else $updateTransaction[] = ["id"=>$obj->id,"to_station_id"=>$map[$loc],"status"=>0,"updated_at"=>$time];
                     }
                     app("BatchUpdateService")->batchUpdate("station_task_material_boxes",$updateTask);
                     app("BatchUpdateService")->batchUpdate("task_transactions",$updateTransaction);
@@ -78,7 +81,7 @@ class CacheShelfTaskJob implements ShouldQueue
                     if ($result){
                         Cache::forever($this->key,$available);
                         foreach ($toLocation as $value){
-                            app("CacheShelfService")->lightUp($value,'3','2');
+                            app("CacheShelfService")->lightUp($value,'3','0');
                             Cache::forever("CACHE_SHELF_OCCUPANCY_{$map[$value]}",true);
                         }
                         app("StationService")->locationOccupyMulti($toLocation->toArray());
@@ -89,7 +92,7 @@ class CacheShelfTaskJob implements ShouldQueue
                     }
                 }catch (\Exception $e){
                     DB::rollBack();
-                    $this->push(__METHOD__."->".__LINE__,"出库队列执行错误",$e->getMessage());
+                    $this->push(__METHOD__."->".__LINE__,"出库队列执行错误",$e->getMessage()." | 当前任务数:".$this->count." | 缓存信息:".(isset($available) ? json_encode($available) : ''));
                 }
                 break;
             default://入库呼叫

+ 1 - 1
app/Services/CacheShelfService.php

@@ -52,7 +52,7 @@ class   CacheShelfService
         $station = Station::query()->with('materialBox')->where('code', $locCode)->first();
         if (Cache::has("CACHE_SHELF_OCCUPANCY_{$station->id}")){
             //缓存存在 不允许灭灯 灭了再点开
-            app("CacheShelfService")->lightUp($station->code,'3','2',["title"=>"库位预定,禁止操作"]);
+            app("CacheShelfService")->lightUp($station->code,'3','0',["title"=>"库位预定,禁止操作"]);
             return ['success' => true];
         }
         //站存在 站为缓存架

+ 1 - 1
app/Services/StationService.php

@@ -155,7 +155,7 @@ class StationService
     {
         $mapping = [];
         foreach (Station::query()->whereIn("code",$codes)->get() as $station){
-            $station[$station->code] = $station->id;
+            $mapping[$station->code] = $station->id;
         }
         return $mapping;
     }

+ 1 - 1
app/Services/StationTaskBatchService.php

@@ -197,7 +197,7 @@ class StationTaskBatchService
                         $isFetchedFromRobotics = $this->foreignHaiRoboticsService->
                         fetchGroup_multiLocation($toLocation, $taskMaterialBoxes, $groupPrefix, '立架出至缓存架',20);
                         foreach ($toLocation as $value){
-                            app("CacheShelfService")->lightUp($value,'3','2');
+                            app("CacheShelfService")->lightUp($value,'3','0');
                             Cache::forever("CACHE_SHELF_OCCUPANCY_{$map[$value]}",true);
                         }
                         app("StationService")->locationOccupyMulti($toLocation->toArray());

+ 1 - 1
app/Services/StorageService.php

@@ -123,7 +123,7 @@ class StorageService
             ->where("status",0)->first();
         if (!$task)return;
         //黄灯闪烁
-        if ($task->type == '入库' && $task->mark == 1)app("CacheShelfService")->lightUp($stationTaskMaterialBox->station->code,'3','2',["title"=>'机器人取箱中,禁止操作',]);
+        if ($task->type == '入库' && $task->mark == 1)app("CacheShelfService")->lightUp($stationTaskMaterialBox->station->code,'3','0',["title"=>'机器人取箱中,禁止操作',]);
     }
 
     /**