ScreenController.php 19 KB

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