ScreenController.php 22 KB

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