ScreenController.php 17 KB

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