ScreenController.php 20 KB

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