StationCacheShelfGrid.php 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. <?php
  2. namespace App;
  3. use Illuminate\Database\Eloquent\Model;
  4. use Illuminate\Database\Eloquent\Relations\BelongsTo;
  5. use App\Traits\ModelLogChanging;
  6. class StationCacheShelfGrid extends Model
  7. {
  8. use ModelLogChanging;
  9. protected $fillable = ['station_id', 'material_box_id', 'grid_id', 'status'];
  10. public static $status = [
  11. '0' => '无',
  12. '1' => '待处理',
  13. ];
  14. public function station(): BelongsTo
  15. {
  16. return $this->belongsTo(Station::class);
  17. }
  18. public function materialBox(): BelongsTo
  19. {
  20. return $this->belongsTo(MaterialBox::class);
  21. }
  22. /**
  23. * 根据格口计算位置
  24. * @param Station $station
  25. * @param StationCacheShelfGrid $grid
  26. * @return string
  27. */
  28. public static function getLocation(Station $station, StationCacheShelfGrid $grid): string
  29. {
  30. $code = $station['code'];
  31. $grid_id = $grid['grid_id'];
  32. $row = 2 - round($grid_id / 3) + 1;
  33. $col = 2 - ($grid_id % 3) + 1;
  34. return 'HAI' . $code . '-0' . $col . '-0' . $row;
  35. }
  36. /**
  37. * 根据位置计算 grid_id 和 station code
  38. * @param $code
  39. * @return array|false[]
  40. */
  41. public static function getGridByCode($code): array
  42. {
  43. $arr = [];
  44. preg_match('/^HAI([\w\.\ ]+)-0([0-9]+)-0([0-9]+)/',$code,$arr);
  45. if(count($arr)==0)return [false,false,null,null];
  46. $stationCode =$arr[1] ?? false;
  47. $col = $arr[2] ?? 0; // 列
  48. $row = $arr[3] ?? 0; // 行
  49. $gridId = ($row-1)*3 + (3-$col);
  50. return [$stationCode,$gridId,$row,$col];
  51. }
  52. }