Browse Source

添加缓存架格口计算

ajun 5 năm trước cách đây
mục cha
commit
011db07c65
1 tập tin đã thay đổi với 27 bổ sung6 xóa
  1. 27 6
      app/StationCacheShelfGrid.php

+ 27 - 6
app/StationCacheShelfGrid.php

@@ -10,7 +10,7 @@ class StationCacheShelfGrid extends Model
 {
     use ModelLogChanging;
 
-    protected $fillable = ['station_id','material_box_id','grid_id','status'];
+    protected $fillable = ['station_id', 'material_box_id', 'grid_id', 'status'];
 
     public static $status = [
         '0' => '无',
@@ -27,13 +27,34 @@ class StationCacheShelfGrid extends Model
         return $this->belongsTo(MaterialBox::class);
     }
 
-    public static function getLocation(Station $station,StationCacheShelfGrid $grid)
+    /**
+     * 根据格口计算位置
+     * @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- ($grid_id / 3) +1;
-        $col = 2- ($grid_id % 3) +1;
-        return 'HAI'.$code.'-0'.$row.'-0'.$col;
+        $row = 2 - round($grid_id / 3) + 1;
+        $col = 2 - ($grid_id % 3) + 1;
+        return 'HAI' . $code . '-0' . $row . '-0' . $col;
+    }
+
+    /**
+     * 根据位置计算 grid_id 和 station code
+     * @param $code
+     * @return array|false[]
+     */
+    public static function getGridByCode($code): array
+    {
+        $bool = preg_match_all('/^HAI([A-Z0-9]+)-[0]([0-9]+)-[0]([0-9]+)/',$code,$arr);
+        if(!$bool)return [false,false];
+        $stationCode =$arr[1][0] ?? false;
+        $col = $arr[2][0] ?? 0;  // 列
+        $row = $arr[3][0] ?? 0;  // 行
+        $gridId = ($row-1)*3 + $col;
+        return [$stationCode,$gridId];
     }
 }