ScreenController.php 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523
  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_efficiency(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_efficiency1(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. ->get()->toArray();
  201. $return = [];
  202. if(! empty($data)){
  203. foreach ($data as $value){
  204. $finished_time = date('Ymd',$value['finished_time']);
  205. if(isset($return[$finished_time])){
  206. $return[$finished_time] += 1;
  207. }else{
  208. $return[$finished_time] = 1;
  209. }
  210. }
  211. }
  212. $maxValue = empty($return) ? 0 : max($return);
  213. $today = $return[$date] ?? 0;
  214. $rate = $maxValue ? intval($today/$maxValue) : 0;
  215. //工序-----------------------------
  216. //包装-----------------------------
  217. $model = new BoxDetail(['channel' => $date]);//当前季度的数据
  218. $data = $model->where('del_time',0)
  219. ->select('crt_time','num')
  220. ->get()->toArray();
  221. $return = [];
  222. if(! empty($data)){
  223. foreach ($data as $value){
  224. $crt_time = date('Ymd',$value['crt_time']);
  225. if(isset($return[$crt_time])){
  226. $return[$crt_time] += $value['num'];
  227. }else{
  228. $return[$crt_time] = $value['num'];
  229. }
  230. }
  231. }
  232. $maxValue = empty($return) ? 0 : max($return);
  233. $today = $return[$date] ?? 0;
  234. $rate2 = $maxValue ? intval($today/$maxValue) : 0;
  235. //包装-----------------------------
  236. $arr = [
  237. [
  238. [
  239. "title"=> "压贴",
  240. "rate"=> $rate
  241. ],
  242. [
  243. "title"=> "包装",
  244. "rate"=> $rate2
  245. ]
  246. ],
  247. ];
  248. return $this->json_return(200,'',['data' => $arr]);
  249. }
  250. /**
  251. * 设备信息
  252. * @param Request $request
  253. * @return array
  254. */
  255. public function product_num(Request $request){
  256. //数据模型
  257. $models = [];
  258. foreach (SystemL::$device as $k => $v){
  259. $models[$k] = [
  260. "machine_day_num"=> 0,
  261. "machine_month_num"=> 0,
  262. "machine_week_num"=> 0,
  263. "break_day_num"=> 0,
  264. "break_month_num"=> 0,
  265. "break_week_num"=> 0,
  266. "start_time"=> '',
  267. "day_num"=> 0,
  268. "week_num"=> 0,
  269. "month_num"=> 0,
  270. "rate"=> 0
  271. ];
  272. }
  273. //当天的开始与结束时间戳
  274. $timestamp_today_start = strtotime(date("Y-m-d 00:00:00",time()));
  275. $timestamp_today_end = strtotime(date("Y-m-d 23:59:59",time()));
  276. //查询当日
  277. $today = SystemL::where('time','>=',$timestamp_today_start * 1000)
  278. ->where('time','<=',$timestamp_today_end * 1000)
  279. ->select('device_name','data_point_name','time')
  280. ->whereIn('data_point_name',[SystemL::run,SystemL::work,SystemL::stop,SystemL::standBy])
  281. ->get()->toArray();
  282. //组织当日数据
  283. $this->fillData($today,1,$models);
  284. //上周时间
  285. $previousWeekStartDate = Carbon::now()->subWeek()->startOfWeek();
  286. $previousWeekEndDate = Carbon::now()->subWeek()->endOfWeek();
  287. //上周开始结束日期
  288. $start_week = $previousWeekStartDate->toDateString();
  289. $end_week = $previousWeekEndDate->toDateString();
  290. $last_week_key = $start_week . $end_week;
  291. list($status, $return_last_week) = $this->getRedisData($last_week_key);
  292. if(! $status){
  293. $previousWeekStartTimeStamp = $previousWeekStartDate->timestamp * 1000;
  294. $previousWeekEndTimeStamp = $previousWeekEndDate->timestamp * 1000;
  295. //获取上周数据
  296. $list_week = SystemL::where('time','>=',$previousWeekStartTimeStamp)
  297. ->where('time','<=',$previousWeekEndTimeStamp)
  298. ->select('device_name','data_point_name')
  299. ->whereIn('data_point_name',[SystemL::run,SystemL::work,SystemL::stop,SystemL::standBy])
  300. ->get()->toArray();
  301. //组织上周数据
  302. $this->fillData($list_week,2,$models);
  303. //缓存
  304. Cache::put($last_week_key,json_encode($list_week),86400);
  305. }else{
  306. //有缓存 填充数据 组织上周数据
  307. $this->fillData($return_last_week,2,$models);
  308. }
  309. //上月时间
  310. $previousMonthStartDate = Carbon::now()->subMonth()->startOfMonth();
  311. $previousMonthEndDate = Carbon::now()->subMonth()->endOfMonth();
  312. //上月开始结束日期
  313. $start_month = $previousMonthStartDate->toDateString();
  314. $end_month = $previousMonthEndDate->toDateString();
  315. $last_month_key = $start_month . $end_month;
  316. list($status, $return_last_month) = $this->getRedisData($last_month_key);
  317. if(! $status){
  318. $previousMonthStartTimeStamp = $previousMonthStartDate->timestamp * 1000;
  319. $previousMonthEndTimeStamp = $previousMonthEndDate->timestamp * 1000;
  320. //获取上月数据
  321. $list_month = SystemL::where('time','>=',$previousMonthStartTimeStamp)
  322. ->where('time','<=',$previousMonthEndTimeStamp)
  323. ->select('device_name','data_point_name')
  324. ->whereIn('data_point_name',[SystemL::run,SystemL::work,SystemL::stop,SystemL::standBy])
  325. ->get()->toArray();
  326. //组织上月数据
  327. $this->fillData($list_month,3,$models);
  328. //缓存
  329. Cache::put($last_month_key,json_encode($list_month),86400);
  330. }else{
  331. //有缓存 填充数据 组织上周数据
  332. $this->fillData($return_last_month,3,$models);
  333. }
  334. $return = [];
  335. foreach ($models as $key => $value){
  336. $value['device_name'] = $key;
  337. $return[] = $value;
  338. }
  339. return $this->json_return(200,'',$return);
  340. }
  341. //在制工单
  342. public function work_order(Request $request){
  343. // 获取当前时间戳
  344. $currentTimestamp = time();
  345. $timestamp = strtotime("-3 months", $currentTimestamp);
  346. $startOfDay = strtotime(date('Y-m-d 00:00:00', $timestamp));
  347. $endOfDay = strtotime(date('Y-m-d 23:59:59', $currentTimestamp));
  348. $result = DispatchSub::where('del_time',0)
  349. ->where('crt_time',">=", $startOfDay)
  350. ->where('crt_time',"<=", $endOfDay)
  351. ->whereColumn('dispatch_quantity','>','finished_num')
  352. ->select('dispatch_no as order_no','process_id','product_title','dispatch_quantity as product_num','finished_num as finish_num')
  353. ->get()->toArray();
  354. if(! empty($result)){
  355. $process_id = array_unique(array_column($result,'process_id'));
  356. $processMap = Process::whereIn('id',$process_id)
  357. ->pluck('title','id')
  358. ->toArray();
  359. foreach ($result as $key => $value){
  360. $result[$key]['procedure'] = $processMap[$value['process_id']] ?? '';
  361. }
  362. }
  363. return $this->json_return(200,'',$result);
  364. }
  365. /**
  366. * 待加工
  367. * @param Request $request
  368. * @return array
  369. */
  370. public function nu_work_order(Request $request){
  371. $date = date('Ymd',time());
  372. //工序-----------------------------
  373. $model = new OrdersProductProcess(['channel' => $date]);//当前季度的数据
  374. $result = $model->where('del_time',0)
  375. ->where('status',0)
  376. ->select('production_no as order_no','product_title','process_id',DB::raw('count(id) as product_num'))
  377. ->groupBy('order_product_id')
  378. ->get()->toArray();
  379. if(! empty($result)){
  380. $process_id = array_unique(array_column($result,'process_id'));
  381. $processMap = Process::whereIn('id',$process_id)
  382. ->pluck('title','id')
  383. ->toArray();
  384. foreach ($result as $key => $value){
  385. $result[$key]['procedure'] = $processMap[$value['process_id']] ?? '';
  386. }
  387. }
  388. return $this->json_return(200,'',$result);
  389. }
  390. //获取缓存
  391. public function getRedisData($cacheKey){
  392. if(Cache::has($cacheKey)){
  393. return [true,json_decode(Cache::get($cacheKey),true)];
  394. }
  395. return [false, []];
  396. }
  397. /**
  398. * 数据填充
  399. * @param $list
  400. * @param $type
  401. * @param $models
  402. */
  403. public function fillData($list,$type,&$models){
  404. if(empty($list)) return;
  405. $run_time = $process_time = $fault = $start_time = [];
  406. foreach ($list as $value){
  407. if($type == 1 && ! isset($start_time[$value['device_name']])){
  408. $start_time_tmp = date("Y-m-d H:i:s", $value['time'] / 1000);
  409. $start_time[$value['device_name']] = $start_time_tmp;
  410. }
  411. if($value['data_point_name'] == SystemL::run || $value['data_point_name'] == SystemL::standBy){
  412. //运行次数
  413. if(isset($run_time[$value['device_name']])){
  414. $run_time[$value['device_name']] += 1;
  415. }else{
  416. $run_time[$value['device_name']] = 1;
  417. }
  418. }
  419. if($value['data_point_name'] == SystemL::standBy){
  420. //工作次数
  421. if(isset($process_time[$value['device_name']])){
  422. $process_time[$value['device_name']] += 1;
  423. }else{
  424. $process_time[$value['device_name']] = 1;
  425. }
  426. }
  427. if($value['data_point_name'] == SystemL::stop){
  428. //故障次数
  429. if(isset($fault[$value['device_name']])){
  430. $fault[$value['device_name']] += 1;
  431. }else{
  432. $fault[$value['device_name']] = 1;
  433. }
  434. }
  435. }
  436. foreach (SystemL::$device as $key => $value){
  437. //运行次数
  438. $run_num = $run_time[$key] ?? 0;
  439. //工作次数
  440. $process_num = $process_time[$key] ?? 0;
  441. //故障次数
  442. $fault_tmp = $fault[$key] ?? 0;
  443. //运行时间
  444. $run_time_tmp = (new ReportFormsService())->calTimeReturnMin($run_num);
  445. //工作时间
  446. $process_time_tmp = (new ReportFormsService())->calTimeReturnMin($process_num);
  447. //故障时间
  448. $fault_time_tmp = (new ReportFormsService())->calTimeReturnMin($fault_tmp);
  449. //计划运行时间 工作时间 - 计划停机 (没有计划停机)
  450. //实际运行时间 计划运行时间 -故障停机 - 设备调整(没有设备调整
  451. $true_process_time = $process_time_tmp - $fault_time_tmp;
  452. //有效率 实际/计划运行 时间
  453. $efficient = $process_time_tmp > 0 ? number_format($true_process_time / $process_time_tmp,2) : 0;
  454. //表现性 加工数量/实际运行时间
  455. $expressive = $true_process_time > 0 ? number_format($process_num / $true_process_time,2) : 0;
  456. //质量指数 加工数量- 废品数量 / 加工数量
  457. $quality_index = $process_num > 0 ? number_format(($process_num - $fault_tmp) / $process_num,2) : 0;
  458. //OEE
  459. $oee = number_format($efficient * $expressive * $quality_index,2);
  460. if($type == 1){
  461. $models[$key]['machine_day_num'] = $run_num;
  462. $models[$key]['day_num'] = $process_num;
  463. $models[$key]['break_day_num'] = $fault_tmp;
  464. $models[$key]['rate'] = $oee;
  465. $models[$key]['start_time'] = $start_time[$key] ?? '暂未开机';
  466. }elseif($type == 2){
  467. $models[$key]['machine_week_num'] = $run_num;
  468. $models[$key]['week_num'] = $process_num;
  469. $models[$key]['break_week_num'] = $fault_tmp;
  470. }elseif($type == 3){
  471. $models[$key]['machine_month_num'] = $run_num;
  472. $models[$key]['month_num'] = $process_num;
  473. $models[$key]['break_month_num'] = $fault_tmp;
  474. }
  475. }
  476. }
  477. }