StationController.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. <?php
  2. namespace App\Http\Controllers;
  3. use App\Components\AsyncResponse;
  4. use App\Station;
  5. use App\StationTypeBinMonitor;
  6. use Illuminate\Database\Eloquent\Builder;
  7. use Illuminate\Support\Facades\Cache;
  8. use Illuminate\Support\Facades\DB;
  9. class StationController extends Controller
  10. {
  11. use AsyncResponse;
  12. public function monitorIndex()
  13. {
  14. $stations = Station::query()->with('stationType:name','parent:name')->whereHas('stationType',function($query){
  15. /** @var Builder $query */
  16. $query->where('name','料箱监视器');
  17. })->paginate(100);
  18. return view('station.monitor.index',compact('stations'));
  19. }
  20. public function monitorShow(Station $station)
  21. {
  22. $station->loadMissing([
  23. "stationTasks_today.stationTaskCommodities.commodity.barcodes",
  24. "stationTasks_today.stationTaskCommodities.stationTaskMaterialBox",
  25. "stationTasks_today.stationTaskBatches.batch.owner",
  26. "stationTasks_today.stationTaskMaterialBoxes.materialBox",
  27. "stationTypeBinMonitor",
  28. ]);
  29. if (!$station['stationTypeBinMonitor']){
  30. StationTypeBinMonitor::query()->create([
  31. 'station_id' => $station['id'],
  32. 'bin_row_length' => 4,
  33. 'bin_column_length' => 5,
  34. 'bin_wall_amount' => 2,
  35. ]);
  36. $station->load("stationTypeBinMonitor");
  37. }
  38. return view('station.monitor.show',compact('station'));
  39. }
  40. public function getVisual()
  41. {
  42. $arr = \request("arr");
  43. $cacheKey = "loadBoxMonitor_".$arr[0][0];
  44. if (Cache::tags("loadBoxMonitor")->has($cacheKey))$this->success(Cache::tags("loadBoxMonitor")->get($cacheKey));
  45. $res = [];
  46. foreach ($arr as $item){
  47. $query = DB::connection("mysql_haiRobotics")->table("ks_bin")->select("ks_bin_code");
  48. foreach ($item as $value)$query->orWhere("ks_bin_space_code",'like',$value);
  49. $codes = [];
  50. $query->get()->each(function ($bin)use(&$codes){
  51. $codes[] = "'".$bin->ks_bin_code."'";
  52. });
  53. if (!$codes){$res[] = 100;continue;}
  54. $len = count($codes);
  55. $codes = "(".implode(",",$codes).")";
  56. $sql = <<<SQL
  57. SELECT COUNT(*) count FROM (SELECT SUM(QTY) qty FROM INV_LOT_LOC_ID WHERE
  58. TRACEID = '*' AND LOCATIONID IN {$codes} GROUP BY LOCATIONID) inv WHERE INV.qty!=0
  59. SQL;
  60. $count = DB::connection("oracle")->selectOne(DB::raw($sql))->count;
  61. //$res[] = intval($count/$len*1000)/10;
  62. $res[] = [$len,$count];
  63. }
  64. Cache::tags("loadBoxMonitor")->put($cacheKey,$res,1800);
  65. $this->success($res);
  66. }
  67. public function getDetail()
  68. {
  69. $arr = \request("arr");
  70. $cacheKey = "loadBoxMonitorDetail_".$arr[0];
  71. if (Cache::tags("loadBoxMonitor")->has($cacheKey))$this->success(Cache::tags("loadBoxMonitor")->get($cacheKey));
  72. $ks = DB::connection("mysql_haiRobotics")->table("ks_bin")->select("ks_bin_code","ks_bin_space_code")
  73. ->whereIn("ks_bin_space_code",$arr)->get();
  74. $res = [];
  75. $codes = [];
  76. foreach ($ks as $k){
  77. $res[$k->ks_bin_code] = $k->ks_bin_space_code;
  78. $codes[] = $k->ks_bin_code;
  79. }
  80. $locations = [];
  81. DB::connection("oracle")->table("INV_LOT_LOC_ID")->selectRaw("locationid,SUM(qty) qty")
  82. ->where("traceid","*")
  83. ->whereIn("locationid",$codes)->groupByRaw("locationid")->get()->each(function ($inv)use(&$locations,&$res){
  84. $locations[] = [
  85. "location" => $res[$inv->locationid],
  86. "status" => $inv->qty>0,
  87. ];
  88. unset($res[$inv->locationid]);
  89. });
  90. if ($res)foreach ($res as $item)$locations[] = [
  91. "location" => $item,
  92. "status" => false,
  93. ];
  94. Cache::tags("loadBoxMonitor")->put($cacheKey,$locations,1800);
  95. $this->success($locations);
  96. }
  97. public function getBoxes()
  98. {
  99. $arr = \request("arr");
  100. if (!$arr)$this->error("无可用箱");
  101. $ks = DB::connection("mysql_haiRobotics")->table("ks_bin")->select("ks_bin_code")
  102. ->whereIn("ks_bin_space_code",$arr)->pluck("ks_bin_code")->toArray();
  103. $this->success(implode(" ",$ks));
  104. }
  105. }