ScreenController.php 18 KB

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