ScreenController.php 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524
  1. <?php
  2. namespace App\Http\Controllers\Api;
  3. use App\Model\Box;
  4. use App\Model\BoxDetail;
  5. use App\Model\DispatchSub;
  6. use App\Model\OrdersProductProcess;
  7. use App\Model\Process;
  8. use App\Model\SaleOrdersProduct;
  9. use App\Model\SystemL;
  10. use App\Service\ReportFormsService;
  11. use Carbon\Carbon;
  12. use Illuminate\Http\Request;
  13. use Illuminate\Support\Facades\Cache;
  14. use Illuminate\Support\Facades\DB;
  15. /**
  16. * 大屏数据展示
  17. * Class ScreenController
  18. * @package App\Http\Controllers\Api
  19. */
  20. class ScreenController extends BaseController
  21. {
  22. /**
  23. * 产值数据全览
  24. * @param Request $request
  25. * @return array
  26. */
  27. public function output_value(Request $request){
  28. $currentYear = Carbon::now()->year;
  29. $lastYear = $currentYear - 1;
  30. $currentMonth = Carbon::now()->month;
  31. $totalValueAllTime = SaleOrdersProduct::where('del_time',0)->sum('finished_num');
  32. $totalValueLastYear = SaleOrdersProduct::where('del_time',0)->whereRaw("YEAR(FROM_UNIXTIME(crt_time)) = $lastYear")->sum('finished_num');
  33. $totalValueCurrentYearMonth = SaleOrdersProduct::where('del_time',0)->whereRaw("YEAR(FROM_UNIXTIME(crt_time)) = $currentYear AND MONTH(FROM_UNIXTIME(crt_time)) = $currentMonth")->sum('finished_num');
  34. return $this->json_return(200,'',['total_time'=>$totalValueAllTime, 'total_last_year'=>$totalValueLastYear, 'total_current_month'=>$totalValueCurrentYearMonth]);
  35. }
  36. /**
  37. * 项目进度
  38. * @param Request $request
  39. * @return array
  40. */
  41. public function order_process1(Request $request) {
  42. $result = SaleOrdersProduct::where('del_time',0)
  43. ->select(DB::raw('sum(order_quantity) as total'), DB::raw('sum(finished_num) as finished_num'),'out_order_no as Code','customer_name as CustomerName')
  44. ->groupBy('out_order_no')
  45. ->get()->toArray();
  46. if(! empty($result)){
  47. foreach ($result as $key => $value){
  48. $result[$key]['rate'] = number_format($value['finished_num'] / $value['total'],2) * 100;
  49. unset($result[$key]['total']);
  50. unset($result[$key]['finished_num']);
  51. }
  52. $rate = array_column($result, 'rate'); // 提取列作为排序依据
  53. array_multisort($rate, SORT_DESC,$result);
  54. }
  55. return $this->json_return(200,'',$result);
  56. }
  57. /**
  58. * 历史项目 在手项目
  59. * @param Request $request
  60. * @return array
  61. */
  62. public function project_region(Request $request){
  63. $all = SaleOrdersProduct::where('del_time',0)
  64. ->whereColumn('order_quantity','=','finished_num')
  65. ->select('out_order_no')
  66. ->groupBy('out_order_no')
  67. ->get()
  68. ->toArray();
  69. $all = array_column($all,'out_order_no');
  70. $not_all = SaleOrdersProduct::where('del_time',0)
  71. ->whereColumn('order_quantity','<>','finished_num')
  72. ->select('out_order_no')
  73. ->groupBy('out_order_no')
  74. ->get()
  75. ->toArray();
  76. $not_all = array_column($not_all,'out_order_no');
  77. $all = array_diff($all, $not_all);
  78. $arr = [
  79. "all_num" => count($all),
  80. "num" => count($not_all),
  81. ];
  82. return $this->json_return(200,'',$arr);
  83. }
  84. /**
  85. * 本月质量
  86. * @param Request $request
  87. * @return array
  88. */
  89. public function output_value_month1(Request $request){
  90. $date = date('Ymd',time());
  91. $startDate = strtotime(date('Y-m-01 00:00:00', time()));
  92. $endDate = strtotime(date('Y-m-t 23:59:59', time()));
  93. //工序-----------------------------
  94. $model = new OrdersProductProcess(['channel' => $date]);//当前季度的数据
  95. $data = $model->where('del_time',0)
  96. ->where('status',4)
  97. ->select('crt_time')
  98. ->where('crt_time','>=',$startDate)
  99. ->where('crt_time','<=',$endDate)
  100. ->get()->toArray();
  101. $return = [];
  102. if(! empty($data)){
  103. foreach ($data as $value){
  104. $crt_time = date('Y-m-d',$value['crt_time']);
  105. if(isset($return[$crt_time])){
  106. $return[$crt_time]['num'] += 1;
  107. }else{
  108. $return[$crt_time] = [
  109. 'num' => 1,
  110. 'value' => $crt_time
  111. ];
  112. }
  113. }
  114. }
  115. $return = array_values($return);
  116. return $this->json_return(200,'',$return);
  117. }
  118. /**
  119. * 产量趋势图
  120. * @param Request $request
  121. * @return array
  122. */
  123. public function output_value_efficiency1(Request $request){
  124. // 获取当前时间戳
  125. $currentTimestamp = time();
  126. // 输出过去两周的起止时间(包括当前日期)
  127. $timestamp = strtotime("-13 days", $currentTimestamp);
  128. $startOfDay = strtotime(date('Y-m-d 00:00:00', $timestamp));
  129. $endOfDay = strtotime(date('Y-m-d 23:59:59', $currentTimestamp));
  130. $box = Box::where('del_time',0)
  131. ->where('crt_time','>=',$startOfDay)
  132. ->where('crt_time','<=',$endOfDay)
  133. ->select('crt_time','top_order_no','order_no')
  134. ->orderBy('crt_time','desc')
  135. ->get()->toArray();
  136. $result = [];
  137. if(! empty($box)){
  138. foreach ($box as $value){
  139. $model = new BoxDetail(['channel' => $value['top_order_no']]);
  140. $map = $model->where('del_time',0)
  141. ->where('order_no',$value['order_no'])
  142. ->select('order_no',DB::raw('sum(num) as num'))
  143. ->groupBy('order_no')
  144. ->pluck('num','order_no')
  145. ->toArray();
  146. $output = $map[$value['order_no']] ?? 0;
  147. $times = date('Y-m-d',$value['crt_time']);
  148. if(isset($result[$times])){
  149. $result[$times]['output'] += $output;
  150. }else{
  151. $result[$times] = [
  152. 'time' => $times,
  153. 'output' => $output
  154. ];
  155. }
  156. }
  157. }
  158. $result = array_values($result);
  159. return $this->json_return(200,'',$result);
  160. }
  161. //产量趋势图(完工)
  162. public function output_value_efficiency(Request $request){
  163. // 获取当前时间
  164. $date = date('Ymd',time());
  165. $model = new OrdersProductProcess(['channel' => $date]);//当前季度的数据
  166. $data = $model->where('del_time',0)
  167. ->where('status',2)
  168. ->select('finished_time')
  169. ->get()->toArray();
  170. $result = [];
  171. if(! empty($data)){
  172. foreach ($data as $value){
  173. $time = date('Y-m-d',$value['finished_time']);
  174. if(isset($result[$time])){
  175. $result[$time]['output'] += 1;
  176. }else{
  177. $result[$time] = [
  178. 'time' => $time,
  179. 'output' => 1
  180. ];
  181. }
  182. }
  183. ksort($result);
  184. }
  185. $result = array_values($result);
  186. return $this->json_return(200,'',$result);
  187. }
  188. /**
  189. * 工序负荷全览
  190. * @param Request $request
  191. * @return array
  192. */
  193. public function capacity(Request $request){
  194. $date = date('Ymd',time());
  195. //工序-----------------------------
  196. $model = new OrdersProductProcess(['channel' => $date]);//当前季度的数据
  197. $data = $model->where('del_time',0)
  198. ->where('status',2)
  199. ->select('finished_time')
  200. ->orderBy('finished_time','desc')
  201. ->get()->toArray();
  202. $return = [];
  203. if(! empty($data)){
  204. foreach ($data as $value){
  205. $finished_time = date('Ymd',$value['finished_time']);
  206. if(isset($return[$finished_time])){
  207. $return[$finished_time] += 1;
  208. }else{
  209. $return[$finished_time] = 1;
  210. }
  211. }
  212. }
  213. $maxValue = empty($return) ? 0 : max($return);
  214. $today = $return[$date] ?? 0;
  215. $rate = $maxValue ? intval($today/$maxValue * 100) : 0;
  216. //工序-----------------------------
  217. //包装-----------------------------
  218. $model = new BoxDetail(['channel' => $date]);//当前季度的数据
  219. $data = $model->where('del_time',0)
  220. ->select('crt_time','num')
  221. ->get()->toArray();
  222. $return = [];
  223. if(! empty($data)){
  224. foreach ($data as $value){
  225. $crt_time = date('Ymd',$value['crt_time']);
  226. if(isset($return[$crt_time])){
  227. $return[$crt_time] += $value['num'];
  228. }else{
  229. $return[$crt_time] = $value['num'];
  230. }
  231. }
  232. }
  233. $maxValue = empty($return) ? 0 : max($return);
  234. $today = $return[$date] ?? 0;
  235. $rate2 = $maxValue ? intval($today/$maxValue*100) : 0;
  236. //包装-----------------------------
  237. $arr = [
  238. [
  239. [
  240. "title"=> "压贴",
  241. "rate"=> $rate
  242. ],
  243. [
  244. "title"=> "包装",
  245. "rate"=> $rate2
  246. ]
  247. ],
  248. ];
  249. return $this->json_return(200,'',['data' => $arr]);
  250. }
  251. /**
  252. * 设备信息
  253. * @param Request $request
  254. * @return array
  255. */
  256. public function product_num(Request $request){
  257. //数据模型
  258. $models = [];
  259. foreach (SystemL::$device as $k => $v){
  260. $models[$k] = [
  261. "machine_day_num"=> 0,
  262. "machine_month_num"=> 0,
  263. "machine_week_num"=> 0,
  264. "break_day_num"=> 0,
  265. "break_month_num"=> 0,
  266. "break_week_num"=> 0,
  267. "start_time"=> '',
  268. "day_num"=> 0,
  269. "week_num"=> 0,
  270. "month_num"=> 0,
  271. "rate"=> 0
  272. ];
  273. }
  274. //当天的开始与结束时间戳
  275. $timestamp_today_start = strtotime(date("Y-m-d 00:00:00",time()));
  276. $timestamp_today_end = strtotime(date("Y-m-d 23:59:59",time()));
  277. //查询当日
  278. $today = SystemL::where('time','>=',$timestamp_today_start * 1000)
  279. ->where('time','<=',$timestamp_today_end * 1000)
  280. ->select('device_name','data_point_name','time')
  281. ->whereIn('data_point_name',[SystemL::run,SystemL::work,SystemL::stop,SystemL::standBy])
  282. ->get()->toArray();
  283. //组织当日数据
  284. $this->fillData($today,1,$models);
  285. //上周时间
  286. $previousWeekStartDate = Carbon::now()->subWeek()->startOfWeek();
  287. $previousWeekEndDate = Carbon::now()->subWeek()->endOfWeek();
  288. //上周开始结束日期
  289. $start_week = $previousWeekStartDate->toDateString();
  290. $end_week = $previousWeekEndDate->toDateString();
  291. $last_week_key = $start_week . $end_week;
  292. list($status, $return_last_week) = $this->getRedisData($last_week_key);
  293. if(! $status){
  294. $previousWeekStartTimeStamp = $previousWeekStartDate->timestamp * 1000;
  295. $previousWeekEndTimeStamp = $previousWeekEndDate->timestamp * 1000;
  296. //获取上周数据
  297. $list_week = SystemL::where('time','>=',$previousWeekStartTimeStamp)
  298. ->where('time','<=',$previousWeekEndTimeStamp)
  299. ->select('device_name','data_point_name')
  300. ->whereIn('data_point_name',[SystemL::run,SystemL::work,SystemL::stop,SystemL::standBy])
  301. ->get()->toArray();
  302. //组织上周数据
  303. $this->fillData($list_week,2,$models);
  304. //缓存
  305. Cache::put($last_week_key,json_encode($list_week),86400);
  306. }else{
  307. //有缓存 填充数据 组织上周数据
  308. $this->fillData($return_last_week,2,$models);
  309. }
  310. //上月时间
  311. $previousMonthStartDate = Carbon::now()->subMonth()->startOfMonth();
  312. $previousMonthEndDate = Carbon::now()->subMonth()->endOfMonth();
  313. //上月开始结束日期
  314. $start_month = $previousMonthStartDate->toDateString();
  315. $end_month = $previousMonthEndDate->toDateString();
  316. $last_month_key = $start_month . $end_month;
  317. list($status, $return_last_month) = $this->getRedisData($last_month_key);
  318. if(! $status){
  319. $previousMonthStartTimeStamp = $previousMonthStartDate->timestamp * 1000;
  320. $previousMonthEndTimeStamp = $previousMonthEndDate->timestamp * 1000;
  321. //获取上月数据
  322. $list_month = SystemL::where('time','>=',$previousMonthStartTimeStamp)
  323. ->where('time','<=',$previousMonthEndTimeStamp)
  324. ->select('device_name','data_point_name')
  325. ->whereIn('data_point_name',[SystemL::run,SystemL::work,SystemL::stop,SystemL::standBy])
  326. ->get()->toArray();
  327. //组织上月数据
  328. $this->fillData($list_month,3,$models);
  329. //缓存
  330. Cache::put($last_month_key,json_encode($list_month),86400);
  331. }else{
  332. //有缓存 填充数据 组织上周数据
  333. $this->fillData($return_last_month,3,$models);
  334. }
  335. $return = [];
  336. foreach ($models as $key => $value){
  337. $value['device_name'] = $key;
  338. $return[] = $value;
  339. }
  340. return $this->json_return(200,'',$return);
  341. }
  342. //在制工单
  343. public function work_order(Request $request){
  344. // 获取当前时间戳
  345. $currentTimestamp = time();
  346. $timestamp = strtotime("-3 months", $currentTimestamp);
  347. $startOfDay = strtotime(date('Y-m-d 00:00:00', $timestamp));
  348. $endOfDay = strtotime(date('Y-m-d 23:59:59', $currentTimestamp));
  349. $result = DispatchSub::where('del_time',0)
  350. ->where('crt_time',">=", $startOfDay)
  351. ->where('crt_time',"<=", $endOfDay)
  352. ->whereColumn('dispatch_quantity','>','finished_num')
  353. ->select('dispatch_no as order_no','process_id','product_title','dispatch_quantity as product_num','finished_num as finish_num')
  354. ->get()->toArray();
  355. if(! empty($result)){
  356. $process_id = array_unique(array_column($result,'process_id'));
  357. $processMap = Process::whereIn('id',$process_id)
  358. ->pluck('title','id')
  359. ->toArray();
  360. foreach ($result as $key => $value){
  361. $result[$key]['procedure'] = $processMap[$value['process_id']] ?? '';
  362. }
  363. }
  364. return $this->json_return(200,'',$result);
  365. }
  366. /**
  367. * 待加工
  368. * @param Request $request
  369. * @return array
  370. */
  371. public function nu_work_order(Request $request){
  372. // 获取当前时间戳
  373. $currentTimestamp = time();
  374. $startOfDay = strtotime(date('Y-m-d 00:00:00', $currentTimestamp));
  375. $result = DispatchSub::where('del_time',0)
  376. ->where('dispatch_time_start',">=", $startOfDay)
  377. ->where('finished_num',0)
  378. ->select('dispatch_no as order_no','product_title','dispatch_quantity as product_num','process_id')
  379. ->get()->toArray();
  380. if(! empty($result)){
  381. $process_id = array_unique(array_column($result,'process_id'));
  382. $processMap = Process::whereIn('id',$process_id)
  383. ->pluck('title','id')
  384. ->toArray();
  385. foreach ($result as $key => $value){
  386. $result[$key]['procedure'] = $processMap[$value['process_id']] ?? '';
  387. }
  388. }
  389. return $this->json_return(200,'',$result);
  390. }
  391. //获取缓存
  392. public function getRedisData($cacheKey){
  393. if(Cache::has($cacheKey)){
  394. return [true,json_decode(Cache::get($cacheKey),true)];
  395. }
  396. return [false, []];
  397. }
  398. /**
  399. * 数据填充
  400. * @param $list
  401. * @param $type
  402. * @param $models
  403. */
  404. public function fillData($list,$type,&$models){
  405. if(empty($list)) return;
  406. $run_time = $process_time = $fault = $start_time = [];
  407. foreach ($list as $value){
  408. if($type == 1 && ! isset($start_time[$value['device_name']])){
  409. $start_time_tmp = date("Y-m-d H:i:s", $value['time'] / 1000);
  410. $start_time[$value['device_name']] = $start_time_tmp;
  411. }
  412. if($value['data_point_name'] == SystemL::run || $value['data_point_name'] == SystemL::standBy){
  413. //运行次数
  414. if(isset($run_time[$value['device_name']])){
  415. $run_time[$value['device_name']] += 1;
  416. }else{
  417. $run_time[$value['device_name']] = 1;
  418. }
  419. }
  420. if($value['data_point_name'] == SystemL::standBy){
  421. //工作次数
  422. if(isset($process_time[$value['device_name']])){
  423. $process_time[$value['device_name']] += 1;
  424. }else{
  425. $process_time[$value['device_name']] = 1;
  426. }
  427. }
  428. if($value['data_point_name'] == SystemL::stop){
  429. //故障次数
  430. if(isset($fault[$value['device_name']])){
  431. $fault[$value['device_name']] += 1;
  432. }else{
  433. $fault[$value['device_name']] = 1;
  434. }
  435. }
  436. }
  437. foreach (SystemL::$device as $key => $value){
  438. //运行次数
  439. $run_num = $run_time[$key] ?? 0;
  440. //工作次数
  441. $process_num = $process_time[$key] ?? 0;
  442. //故障次数
  443. $fault_tmp = $fault[$key] ?? 0;
  444. //运行时间
  445. $run_time_tmp = (new ReportFormsService())->calTimeReturnMin($run_num);
  446. //工作时间
  447. $process_time_tmp = (new ReportFormsService())->calTimeReturnMin($process_num);
  448. //故障时间
  449. $fault_time_tmp = (new ReportFormsService())->calTimeReturnMin($fault_tmp);
  450. //计划运行时间 工作时间 - 计划停机 (没有计划停机)
  451. //实际运行时间 计划运行时间 -故障停机 - 设备调整(没有设备调整
  452. $true_process_time = $process_time_tmp - $fault_time_tmp;
  453. //有效率 实际/计划运行 时间
  454. $efficient = $process_time_tmp > 0 ? number_format($true_process_time / $process_time_tmp,2) : 0;
  455. //表现性 加工数量/实际运行时间
  456. $expressive = $true_process_time > 0 ? number_format($process_num / $true_process_time,2) : 0;
  457. //质量指数 加工数量- 废品数量 / 加工数量
  458. $quality_index = $process_num > 0 ? number_format(($process_num - $fault_tmp) / $process_num,2) : 0;
  459. //OEE
  460. $oee = number_format($efficient * $expressive * $quality_index,2);
  461. if($type == 1){
  462. $models[$key]['machine_day_num'] = $run_num;
  463. $models[$key]['day_num'] = $process_num;
  464. $models[$key]['break_day_num'] = $fault_tmp;
  465. $models[$key]['rate'] = $oee;
  466. $models[$key]['start_time'] = $start_time[$key] ?? '暂未开机';
  467. }elseif($type == 2){
  468. $models[$key]['machine_week_num'] = $run_num;
  469. $models[$key]['week_num'] = $process_num;
  470. $models[$key]['break_week_num'] = $fault_tmp;
  471. }elseif($type == 3){
  472. $models[$key]['machine_month_num'] = $run_num;
  473. $models[$key]['month_num'] = $process_num;
  474. $models[$key]['break_month_num'] = $fault_tmp;
  475. }
  476. }
  477. }
  478. }