ScreenController.php 22 KB

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