ScreenController.php 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481
  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. class ScreenController extends BaseController
  16. {
  17. //产值数据全览
  18. public function output_value(Request $request){
  19. $currentYear = Carbon::now()->year;
  20. $lastYear = $currentYear - 1;
  21. $currentMonth = Carbon::now()->month;
  22. $totalValueAllTime = SaleOrdersProduct::where('del_time',0)->sum('finished_num');
  23. $totalValueLastYear = SaleOrdersProduct::where('del_time',0)->whereRaw("YEAR(FROM_UNIXTIME(crt_time)) = $lastYear")->sum('finished_num');
  24. $totalValueCurrentYearMonth = SaleOrdersProduct::where('del_time',0)->whereRaw("YEAR(FROM_UNIXTIME(crt_time)) = $currentYear AND MONTH(FROM_UNIXTIME(crt_time)) = $currentMonth")->sum('finished_num');
  25. return $this->json_return(200,'',['total_time'=>$totalValueAllTime, 'total_last_year'=>$totalValueLastYear, 'total_current_month'=>$totalValueCurrentYearMonth]);
  26. }
  27. //项目进度
  28. public function order_process1(Request $request) {
  29. $result = SaleOrdersProduct::where('del_time',0)
  30. ->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')
  31. ->groupBy('out_order_no')
  32. ->get()->toArray();
  33. if(! empty($result)){
  34. foreach ($result as $key => $value){
  35. $result[$key]['rate'] = number_format($value['finished_num'] / $value['total'],2) * 100;
  36. unset($result[$key]['total']);
  37. unset($result[$key]['finished_num']);
  38. }
  39. $rate = array_column($result, 'rate'); // 提取列作为排序依据
  40. array_multisort($rate, SORT_DESC,$result);
  41. }
  42. return $this->json_return(200,'',$result);
  43. }
  44. //历史项目 在手项目
  45. public function project_region(Request $request){
  46. $all = SaleOrdersProduct::where('del_time',0)
  47. ->whereColumn('order_quantity','=','finished_num')
  48. ->select('out_order_no')
  49. ->groupBy('out_order_no')
  50. ->get()
  51. ->toArray();
  52. $all = array_column($all,'out_order_no');
  53. $not_all = SaleOrdersProduct::where('del_time',0)
  54. ->whereColumn('order_quantity','<>','finished_num')
  55. ->select('out_order_no')
  56. ->groupBy('out_order_no')
  57. ->get()
  58. ->toArray();
  59. $not_all = array_column($not_all,'out_order_no');
  60. $all = array_diff($all, $not_all);
  61. $arr = [
  62. "all_num" => count($all),
  63. "num" => count($not_all),
  64. ];
  65. return $this->json_return(200,'',$arr);
  66. }
  67. //本月质量
  68. public function output_value_month1(Request $request){
  69. $date = date('Ymd',time());
  70. $startDate = strtotime(date('Y-m-01 00:00:00', time()));
  71. $endDate = strtotime(date('Y-m-t 23:59:59', time()));
  72. //工序-----------------------------
  73. $model = new OrdersProductProcess(['channel' => $date]);//当前季度的数据
  74. $data = $model->where('del_time',0)
  75. ->where('status',4)
  76. ->select('crt_time')
  77. ->where('crt_time','>=',$startDate)
  78. ->where('crt_time','<=',$endDate)
  79. ->get()->toArray();
  80. $return = [];
  81. if(! empty($data)){
  82. foreach ($data as $value){
  83. $crt_time = date('Y-m-d',$value['crt_time']);
  84. if(isset($return[$crt_time])){
  85. $return[$crt_time]['num'] += 1;
  86. }else{
  87. $return[$crt_time] = [
  88. 'num' => 1,
  89. 'value' => $crt_time
  90. ];
  91. }
  92. }
  93. }
  94. $return = array_values($return);
  95. return $this->json_return(200,'',$return);
  96. }
  97. //产量趋势图(包装)
  98. public function output_value_efficiency1(Request $request){
  99. // 获取当前时间戳
  100. $currentTimestamp = time();
  101. // 输出过去两周的起止时间(包括当前日期)
  102. $timestamp = strtotime("-13 days", $currentTimestamp);
  103. $startOfDay = strtotime(date('Y-m-d 00:00:00', $timestamp));
  104. $endOfDay = strtotime(date('Y-m-d 23:59:59', $currentTimestamp));
  105. $box = Box::where('del_time',0)
  106. ->where('crt_time','>=',$startOfDay)
  107. ->where('crt_time','<=',$endOfDay)
  108. ->select('crt_time','top_order_no','order_no')
  109. ->orderBy('crt_time','desc')
  110. ->get()->toArray();
  111. $result = [];
  112. if(! empty($box)){
  113. foreach ($box as $value){
  114. $model = new BoxDetail(['channel' => $value['top_order_no']]);
  115. $map = $model->where('del_time',0)
  116. ->where('order_no',$value['order_no'])
  117. ->select('order_no',DB::raw('sum(num) as num'))
  118. ->groupBy('order_no')
  119. ->pluck('num','order_no')
  120. ->toArray();
  121. $output = $map[$value['order_no']] ?? 0;
  122. $times = date('Y-m-d',$value['crt_time']);
  123. if(isset($result[$times])){
  124. $result[$times]['output'] += $output;
  125. }else{
  126. $result[$times] = [
  127. 'time' => $times,
  128. 'output' => $output
  129. ];
  130. }
  131. }
  132. }
  133. $result = array_values($result);
  134. return $this->json_return(200,'',$result);
  135. }
  136. //产量趋势图(完工)
  137. public function output_value_efficiency(Request $request){
  138. // 获取当前时间
  139. $date = date('Ymd',time());
  140. $model = new OrdersProductProcess(['channel' => $date]);//当前季度的数据
  141. $data = $model->where('del_time',0)
  142. ->where('status',2)
  143. ->select('finished_time')
  144. ->get()->toArray();
  145. $result = [];
  146. if(! empty($data)){
  147. foreach ($data as $value){
  148. $time = date('Y-m-d',$value['finished_time']);
  149. if(isset($result[$time])){
  150. $result[$time]['output'] += 1;
  151. }else{
  152. $result[$time] = [
  153. 'time' => $time,
  154. 'output' => 1
  155. ];
  156. }
  157. }
  158. ksort($result);
  159. }
  160. $result = array_values($result);
  161. return $this->json_return(200,'',$result);
  162. }
  163. //工序负荷全览
  164. public function capacity(Request $request){
  165. $date = date('Ymd',time());
  166. //工序-----------------------------
  167. $model = new OrdersProductProcess(['channel' => $date]);//当前季度的数据
  168. $data = $model->where('del_time',0)
  169. ->where('status',2)
  170. ->select('finished_time')
  171. ->get()->toArray();
  172. $return = [];
  173. if(! empty($data)){
  174. foreach ($data as $value){
  175. $finished_time = date('Ymd',$value['finished_time']);
  176. if(isset($return[$finished_time])){
  177. $return[$finished_time] += 1;
  178. }else{
  179. $return[$finished_time] = 1;
  180. }
  181. }
  182. }
  183. $maxValue = empty($return) ? 0 : max($return);
  184. $today = $return[$date] ?? 0;
  185. $rate = $maxValue ? intval($today/$maxValue) : 0;
  186. //工序-----------------------------
  187. //包装-----------------------------
  188. $model = new BoxDetail(['channel' => $date]);//当前季度的数据
  189. $data = $model->where('del_time',0)
  190. ->select('crt_time','num')
  191. ->get()->toArray();
  192. $return = [];
  193. if(! empty($data)){
  194. foreach ($data as $value){
  195. $crt_time = date('Ymd',$value['crt_time']);
  196. if(isset($return[$crt_time])){
  197. $return[$crt_time] += $value['num'];
  198. }else{
  199. $return[$crt_time] = $value['num'];
  200. }
  201. }
  202. }
  203. $maxValue = empty($return) ? 0 : max($return);
  204. $today = $return[$date] ?? 0;
  205. $rate2 = $maxValue ? intval($today/$maxValue) : 0;
  206. //包装-----------------------------
  207. $arr = [
  208. [
  209. [
  210. "title"=> "压贴",
  211. "rate"=> $rate
  212. ],
  213. [
  214. "title"=> "包装",
  215. "rate"=> $rate2
  216. ]
  217. ],
  218. ];
  219. return $this->json_return(200,'',['data' => $arr]);
  220. }
  221. //设备信息
  222. public function product_num(Request $request){
  223. //数据模型
  224. $models = [];
  225. foreach (SystemL::$device as $k => $v){
  226. $models[$k] = [
  227. "machine_day_num"=> 0,
  228. "machine_month_num"=> 0,
  229. "machine_week_num"=> 0,
  230. "break_day_num"=> 0,
  231. "break_month_num"=> 0,
  232. "break_week_num"=> 0,
  233. "start_time"=> '',
  234. "day_num"=> 0,
  235. "week_num"=> 0,
  236. "month_num"=> 0,
  237. "rate"=> 0
  238. ];
  239. }
  240. //当天的开始与结束时间戳
  241. $timestamp_today_start = strtotime(date("Y-m-d 00:00:00",time()));
  242. $timestamp_today_end = strtotime(date("Y-m-d 23:59:59",time()));
  243. //查询当日
  244. $today = SystemL::where('time','>=',$timestamp_today_start * 1000)
  245. ->where('time','<=',$timestamp_today_end * 1000)
  246. ->select('device_name','data_point_name','time')
  247. ->whereIn('data_point_name',[SystemL::run,SystemL::work,SystemL::stop,SystemL::standBy])
  248. ->get()->toArray();
  249. //组织当日数据
  250. $this->fillData($today,1,$models);
  251. //上周时间
  252. $previousWeekStartDate = Carbon::now()->subWeek()->startOfWeek();
  253. $previousWeekEndDate = Carbon::now()->subWeek()->endOfWeek();
  254. //上周开始结束日期
  255. $start_week = $previousWeekStartDate->toDateString();
  256. $end_week = $previousWeekEndDate->toDateString();
  257. $last_week_key = $start_week . $end_week;
  258. list($status, $return_last_week) = $this->getRedisData($last_week_key);
  259. if(! $status){
  260. $previousWeekStartTimeStamp = $previousWeekStartDate->timestamp * 1000;
  261. $previousWeekEndTimeStamp = $previousWeekEndDate->timestamp * 1000;
  262. //获取上周数据
  263. $list_week = SystemL::where('time','>=',$previousWeekStartTimeStamp)
  264. ->where('time','<=',$previousWeekEndTimeStamp)
  265. ->select('device_name','data_point_name')
  266. ->whereIn('data_point_name',[SystemL::run,SystemL::work,SystemL::stop,SystemL::standBy])
  267. ->get()->toArray();
  268. //组织上周数据
  269. $this->fillData($list_week,2,$models);
  270. //缓存
  271. Cache::put($last_week_key,json_encode($list_week),86400);
  272. }else{
  273. //有缓存 填充数据 组织上周数据
  274. $this->fillData($return_last_week,2,$models);
  275. }
  276. //上月时间
  277. $previousMonthStartDate = Carbon::now()->subMonth()->startOfMonth();
  278. $previousMonthEndDate = Carbon::now()->subMonth()->endOfMonth();
  279. //上月开始结束日期
  280. $start_month = $previousMonthStartDate->toDateString();
  281. $end_month = $previousMonthEndDate->toDateString();
  282. $last_month_key = $start_month . $end_month;
  283. list($status, $return_last_month) = $this->getRedisData($last_month_key);
  284. if(! $status){
  285. $previousMonthStartTimeStamp = $previousMonthStartDate->timestamp * 1000;
  286. $previousMonthEndTimeStamp = $previousMonthEndDate->timestamp * 1000;
  287. //获取上月数据
  288. $list_month = SystemL::where('time','>=',$previousMonthStartTimeStamp)
  289. ->where('time','<=',$previousMonthEndTimeStamp)
  290. ->select('device_name','data_point_name')
  291. ->whereIn('data_point_name',[SystemL::run,SystemL::work,SystemL::stop,SystemL::standBy])
  292. ->get()->toArray();
  293. //组织上月数据
  294. $this->fillData($list_month,3,$models);
  295. //缓存
  296. Cache::put($last_month_key,json_encode($list_month),86400);
  297. }else{
  298. //有缓存 填充数据 组织上周数据
  299. $this->fillData($return_last_month,3,$models);
  300. }
  301. $return = [];
  302. foreach ($models as $key => $value){
  303. $value['device_name'] = $key;
  304. $return[] = $value;
  305. }
  306. return $this->json_return(200,'',$return);
  307. }
  308. //在制工单
  309. public function work_order(Request $request){
  310. // 获取当前时间戳
  311. $currentTimestamp = time();
  312. $timestamp = strtotime("-3 months", $currentTimestamp);
  313. $startOfDay = strtotime(date('Y-m-d 00:00:00', $timestamp));
  314. $endOfDay = strtotime(date('Y-m-d 23:59:59', $currentTimestamp));
  315. $result = DispatchSub::where('del_time',0)
  316. ->where('crt_time',">=", $startOfDay)
  317. ->where('crt_time',"<=", $endOfDay)
  318. ->whereColumn('dispatch_quantity','>','finished_num')
  319. ->select('dispatch_no as order_no','process_id','product_title','dispatch_quantity as product_num','finished_num as finish_num')
  320. ->get()->toArray();
  321. if(! empty($result)){
  322. $process_id = array_unique(array_column($result,'process_id'));
  323. $processMap = Process::whereIn('id',$process_id)
  324. ->pluck('title','id')
  325. ->toArray();
  326. foreach ($result as $key => $value){
  327. $result[$key]['procedure'] = $processMap[$value['process_id']] ?? '';
  328. }
  329. }
  330. return $this->json_return(200,'',$result);
  331. }
  332. //待加工
  333. public function nu_work_order(Request $request){
  334. $date = date('Ymd',time());
  335. //工序-----------------------------
  336. $model = new OrdersProductProcess(['channel' => $date]);//当前季度的数据
  337. $result = $model->where('del_time',0)
  338. ->where('status',0)
  339. ->select('production_no as order_no','product_title','process_id',DB::raw('count(id) as product_num'))
  340. ->groupBy('order_product_id')
  341. ->get()->toArray();
  342. if(! empty($result)){
  343. $process_id = array_unique(array_column($result,'process_id'));
  344. $processMap = Process::whereIn('id',$process_id)
  345. ->pluck('title','id')
  346. ->toArray();
  347. foreach ($result as $key => $value){
  348. $result[$key]['procedure'] = $processMap[$value['process_id']] ?? '';
  349. }
  350. }
  351. return $this->json_return(200,'',$result);
  352. }
  353. //获取缓存
  354. public function getRedisData($cacheKey){
  355. if(Cache::has($cacheKey)){
  356. return [true,json_decode(Cache::get($cacheKey),true)];
  357. }
  358. return [false, []];
  359. }
  360. //数据填充
  361. public function fillData($list,$type,&$models){
  362. if(empty($list)) return;
  363. $run_time = $process_time = $fault = $start_time = [];
  364. foreach ($list as $value){
  365. if($type == 1 && ! isset($start_time[$value['device_name']])){
  366. $start_time_tmp = date("Y-m-d H:i:s", $value['time'] / 1000);
  367. $start_time[$value['device_name']] = $start_time_tmp;
  368. }
  369. if($value['data_point_name'] == SystemL::run || $value['data_point_name'] == SystemL::standBy){
  370. //运行次数
  371. if(isset($run_time[$value['device_name']])){
  372. $run_time[$value['device_name']] += 1;
  373. }else{
  374. $run_time[$value['device_name']] = 1;
  375. }
  376. }
  377. if($value['data_point_name'] == SystemL::standBy){
  378. //工作次数
  379. if(isset($process_time[$value['device_name']])){
  380. $process_time[$value['device_name']] += 1;
  381. }else{
  382. $process_time[$value['device_name']] = 1;
  383. }
  384. }
  385. if($value['data_point_name'] == SystemL::stop){
  386. //故障次数
  387. if(isset($fault[$value['device_name']])){
  388. $fault[$value['device_name']] += 1;
  389. }else{
  390. $fault[$value['device_name']] = 1;
  391. }
  392. }
  393. }
  394. foreach (SystemL::$device as $key => $value){
  395. //运行次数
  396. $run_num = $run_time[$key] ?? 0;
  397. //工作次数
  398. $process_num = $process_time[$key] ?? 0;
  399. //故障次数
  400. $fault_tmp = $fault[$key] ?? 0;
  401. //运行时间
  402. $run_time_tmp = (new ReportFormsService())->calTimeReturnMin($run_num);
  403. //工作时间
  404. $process_time_tmp = (new ReportFormsService())->calTimeReturnMin($process_num);
  405. //故障时间
  406. $fault_time_tmp = (new ReportFormsService())->calTimeReturnMin($fault_tmp);
  407. //计划运行时间 工作时间 - 计划停机 (没有计划停机)
  408. //实际运行时间 计划运行时间 -故障停机 - 设备调整(没有设备调整
  409. $true_process_time = $process_time_tmp - $fault_time_tmp;
  410. //有效率 实际/计划运行 时间
  411. $efficient = $process_time_tmp > 0 ? number_format($true_process_time / $process_time_tmp,2) : 0;
  412. //表现性 加工数量/实际运行时间
  413. $expressive = $true_process_time > 0 ? number_format($process_num / $true_process_time,2) : 0;
  414. //质量指数 加工数量- 废品数量 / 加工数量
  415. $quality_index = $process_num > 0 ? number_format(($process_num - $fault_tmp) / $process_num,2) : 0;
  416. //OEE
  417. $oee = number_format($efficient * $expressive * $quality_index,2);
  418. if($type == 1){
  419. $models[$key]['machine_day_num'] = $run_num;
  420. $models[$key]['day_num'] = $process_num;
  421. $models[$key]['break_day_num'] = $fault_tmp;
  422. $models[$key]['rate'] = $oee;
  423. $models[$key]['start_time'] = $start_time[$key] ?? '暂未开机';
  424. }elseif($type == 2){
  425. $models[$key]['machine_week_num'] = $run_num;
  426. $models[$key]['week_num'] = $process_num;
  427. $models[$key]['break_week_num'] = $fault_tmp;
  428. }elseif($type == 3){
  429. $models[$key]['machine_month_num'] = $run_num;
  430. $models[$key]['month_num'] = $process_num;
  431. $models[$key]['break_month_num'] = $fault_tmp;
  432. }
  433. }
  434. }
  435. }