LargeScreenService.php 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664
  1. <?php
  2. namespace App\Service;
  3. use App\Model\ApplyOrder;
  4. use App\Model\ApplyOrderDetail;
  5. use App\Model\DispatchSub;
  6. use App\Model\Equipment;
  7. use App\Model\OrdersProduct;
  8. use App\Model\Process;
  9. use App\Model\SaleOrdersProduct;
  10. use App\Model\Scrapp;
  11. use App\Model\ScrappCount;
  12. use Illuminate\Support\Facades\DB;
  13. class LargeScreenService extends Service
  14. {
  15. public function integratedDashboard($data){
  16. //总产值 ------------------------------------------------
  17. $total = SaleOrdersProduct::where('del_time',0)
  18. ->where('crt_time', ">=", strtotime(date("Y-01-01 00:00:00")))
  19. ->select(DB::raw("sum(finished_num) as total"))
  20. ->value('total');
  21. //总产值 ------------------------------------------------
  22. //当日产量 当月产量 ---------------------------------------------
  23. $data_one = SaleOrdersProduct::where('del_time',0)
  24. ->where('crt_time', ">=", strtotime(date("Y-m-01 00:00:00")))
  25. ->where('crt_time', "<=", strtotime(date("Y-m-t 23:59:59")))
  26. ->select('finished_num','order_quantity','crt_time')
  27. ->get()->toArray();
  28. //当日产量 当月产量
  29. $today_total = $month_total = $order_total = 0;
  30. $timeStamp = strtotime(date("Y-m-d 00:00:00"));
  31. $timeStampE = strtotime(date("Y-m-d 23:59:59"));
  32. foreach ($data_one as $value){
  33. $order_total = bcadd($order_total, $value['order_quantity'],3);
  34. $month_total = bcadd($month_total, $value['finished_num'],3);
  35. if($timeStamp <= $value['crt_time'] && $timeStampE >= $value['crt_time']) $today_total = bcadd($today_total, $value['finished_num'],3);
  36. }
  37. //当日产量 当月产量 ---------------------------------------------
  38. //月计划完成率--------------------------------------------------------------------
  39. $month_rate = floatval($order_total) > 0 ? bcdiv($month_total, $order_total,4) : 0;
  40. $month_rate = bcmul($month_rate,100,2) . "%";
  41. //月计划完成率--------------------------------------------------------------------
  42. //月注塑合格率----------------------------------------------------------------------
  43. $data_two = ScrappCount::where('del_time',0)
  44. ->where('crt_time', ">=", strtotime(date("Y-m-01 00:00:00")))
  45. ->where('crt_time', "<=", strtotime(date("Y-m-t 23:59:59")))
  46. ->where('process_id', 14)
  47. ->select(DB::raw("sum(quantity) as quantity"), DB::raw("sum(scrapp_num) as waste"))
  48. ->get()->toArray();
  49. $total_zj = $total_blp = 0;
  50. foreach ($data_two as $value){
  51. $total_zj = bcadd($total_zj, $value['quantity'],3);
  52. $total_zj = bcadd($total_zj, $value['waste'],3);
  53. $total_blp = bcadd($total_blp, $value['waste'],3);
  54. }
  55. $process_14_total = bcadd($total_zj, $total_blp,3);
  56. $process_14_rate = floatval($process_14_total) > 0 ? bcdiv($total_zj, $process_14_total,4) : 0;
  57. $process_14_rate = bcmul($process_14_rate, 100,2) . "%";
  58. //月注塑合格率----------------------------------------------------------------------
  59. //设备监控状态----------------------------------------------------------------------
  60. $deviceList = Equipment::where('del_time',0)
  61. ->where('model','<>',1)
  62. ->select('id','title','code','status')
  63. ->get()->toArray();
  64. foreach ($deviceList as $key => $value){
  65. $status_title = Equipment::$status_title[$value['status']] ?? "";
  66. $deviceList[$key]['status_title'] = $status_title;
  67. }
  68. //设备监控状态----------------------------------------------------------------------
  69. //质量管理--------------------------------------------------------------------------
  70. $dates = [];
  71. for ($i = 7; $i >= 0; $i--) {
  72. $timestamp = strtotime("-{$i} days");
  73. $date = date('m-d', $timestamp);
  74. $dates[] = $date;
  75. }
  76. $zl_data = [];
  77. $data_three = ScrappCount::where('del_time',0)
  78. ->where('crt_time', "<=", time())
  79. ->where('crt_time', ">=", strtotime(date("Y-m-d 00:00:00",time() - 7 * 24 * 60 * 60)))
  80. ->where('process_id', 14)
  81. ->select('quantity', "scrapp_num as waste", "crt_time")
  82. ->get()->toArray();
  83. foreach ($data_three as $value){
  84. $day = date("m-d", $value['crt_time']);
  85. $tmp = bcadd($value['quantity'], $value['waste'], 3);
  86. if(isset($zl_data[$day])){
  87. $zl_data[$day]['quantity'] = bcadd($zl_data[$day]['quantity'], $value['quantity'],3);
  88. $zl_data[$day]['total'] = bcadd($zl_data[$day]['total'], $tmp,3);
  89. }else{
  90. $zl_data[$day] = [
  91. 'quantity' => $value['quantity'],
  92. 'total' => $tmp
  93. ];
  94. }
  95. }
  96. $zj_data_final = [];
  97. foreach ($dates as $value){
  98. $tmp = [
  99. "day" => $value,
  100. "total" => 0,
  101. "rate" => 0,
  102. ];
  103. if(isset($zl_data[$value])){
  104. $tmp['total'] = $zl_data[$value]['total'];
  105. $tmp['rate'] = bcmul(bcdiv($zl_data[$value]['quantity'], $zl_data[$value]['total'],4),100,2);
  106. }
  107. $zj_data_final[] = $tmp;
  108. }
  109. //质量管理--------------------------------------------------------------------------
  110. //物料管理--------------------------------------------------------------------------
  111. $thing = [
  112. ["name" => '物料一', 'rate' => 50],
  113. ["name" => '物料二', 'rate' => 60],
  114. ["name" => '物料三', 'rate' => 34],
  115. ["name" => '物料四', 'rate' => 22],
  116. ["name" => '物料五', 'rate' => 11],
  117. ["name" => '物料六', 'rate' => 22],
  118. ["name" => '物料七', 'rate' => 44],
  119. ["name" => '物料八', 'rate' => 32],
  120. ["name" => '物料九', 'rate' => 34],
  121. ["name" => '物料十', 'rate' => 51],
  122. ];
  123. //物料管理--------------------------------------------------------------------------
  124. //销售管理--------------------------------------------------------------------------
  125. $xs = SaleOrdersProduct::where('del_time',0)
  126. ->where('crt_time', ">=", strtotime(date("Y-01-01 00:00:00")))
  127. ->select(DB::raw("sum(finished_num) as total"),'technology_name as color')
  128. ->groupBy('technology_name')
  129. ->orderBy('total','desc')
  130. ->get()->toArray();
  131. //销售管理--------------------------------------------------------------------------
  132. //总装各线体目标/完成量---------------------------------------------------------------
  133. $zx_12_dispatch_quantity = $zx_12_finished_num = 0;
  134. $zx_14_dispatch_quantity_8 = $zx_14_finished_num_8 = 0;
  135. $zx_14_dispatch_quantity_9 = $zx_14_finished_num_9 = 0;
  136. $zx_14_dispatch_quantity_12 = $zx_14_finished_num_12 = 0;
  137. $zx_14_dispatch_quantity_13 = $zx_14_finished_num_13 = 0;
  138. $dispatch = DispatchSub::where('del_time',0)
  139. ->where('crt_time', ">=", strtotime(date("Y-01-01 00:00:00")))
  140. ->whereIn('process_id',[12,14])
  141. ->select('dispatch_quantity', 'finished_num', 'process_id','device_id')
  142. ->get()->toArray();
  143. foreach ($dispatch as $value){
  144. if($value['process_id'] == 12){
  145. $zx_12_dispatch_quantity = bcadd($zx_12_dispatch_quantity, $value['dispatch_quantity'],3);
  146. $zx_12_finished_num = bcadd($zx_12_finished_num, $value['finished_num'],3);
  147. }else{
  148. if($value['device_id'] == 8){
  149. $zx_14_dispatch_quantity_8 = bcadd($zx_14_dispatch_quantity_8, $value['dispatch_quantity'],3);
  150. $zx_14_finished_num_8 = bcadd($zx_14_finished_num_8, $value['finished_num'],3);
  151. }elseif ($value['device_id'] == 9){
  152. $zx_14_dispatch_quantity_9 = bcadd($zx_14_dispatch_quantity_9, $value['dispatch_quantity'],3);
  153. $zx_14_finished_num_9 = bcadd($zx_14_finished_num_9, $value['finished_num'],3);
  154. }elseif ($value['device_id'] == 12){
  155. $zx_14_dispatch_quantity_12 = bcadd($zx_14_dispatch_quantity_12, $value['dispatch_quantity'],3);
  156. $zx_14_finished_num_12 = bcadd($zx_14_finished_num_12, $value['finished_num'],3);
  157. }elseif ($value['device_id'] == 13){
  158. $zx_14_dispatch_quantity_13 = bcadd($zx_14_dispatch_quantity_13, $value['dispatch_quantity'],3);
  159. $zx_14_finished_num_13 = bcadd($zx_14_finished_num_13, $value['finished_num'],3);
  160. }
  161. }
  162. }
  163. $diff_num = bcsub($zx_12_dispatch_quantity, $zx_12_finished_num,3);
  164. $diff_num_8 = bcsub($zx_14_dispatch_quantity_8, $zx_14_finished_num_8,3);
  165. $diff_num_9 = bcsub($zx_14_dispatch_quantity_9, $zx_14_finished_num_9,3);
  166. $diff_num_12 = bcsub($zx_14_dispatch_quantity_12, $zx_14_finished_num_12,3);
  167. $diff_num_13 = bcsub($zx_14_dispatch_quantity_13, $zx_14_finished_num_13,3);
  168. $xt = [
  169. [
  170. "name" => "清洗脱水产线", "sj_num" => $zx_12_finished_num, "jh_num" => $zx_12_dispatch_quantity, "diff_num" => $diff_num
  171. ],[
  172. "name" => "注塑一号产线", "sj_num" => $zx_14_finished_num_8, "jh_num" => $zx_14_dispatch_quantity_8, "diff_num" => $diff_num_8
  173. ],[
  174. "name" => "注塑二号产线", "sj_num" => $zx_14_finished_num_9, "jh_num" => $zx_14_dispatch_quantity_9, "diff_num" => $diff_num_9
  175. ],[
  176. "name" => "注塑三号产线", "sj_num" => $zx_14_finished_num_12, "jh_num" => $zx_14_dispatch_quantity_12, "diff_num" => $diff_num_12
  177. ],[
  178. "name" => "注塑四号产线", "sj_num" => $zx_14_finished_num_13, "jh_num" => $zx_14_dispatch_quantity_13, "diff_num" => $diff_num_13
  179. ],
  180. ];
  181. //总装各线体目标/完成量---------------------------------------------------------------
  182. //订单完成进度-----------------------------------------------------------------
  183. $orderBy = [11 => "破碎分离", 12 => "清洗脱水" ,15 => "混合搅拌",13 => "挤出造粒",14 => "注塑成型"];
  184. $order = OrdersProduct::where('del_time',0)
  185. ->select('production_no as order_number','id','technology_name as color','production_quantity','dispatch_complete_quantity','finished_num')
  186. ->orderBy('id','desc')
  187. ->limit(10)
  188. ->get()->toArray();
  189. $dispatch_num = DispatchSub::where('del_time',0)
  190. ->whereIn('order_product_id',array_column($order,'id'))
  191. ->select('order_product_id','process_id','finished_num','dispatch_quantity','waste_num')
  192. ->get()->toArray();
  193. $details = [];
  194. foreach ($dispatch_num as $value){
  195. $new = $value['order_product_id'] . $value['process_id'];
  196. $finished_num = bcadd($value['finished_num'], $value['waste_num'],3);
  197. if(isset($details[$new])){
  198. $details[$new]['dispatch_quantity'] = bcadd($details[$new]['dispatch_quantity'], $value['dispatch_quantity'],3);
  199. $details[$new]['finished_num'] = bcadd($details[$new]['finished_num'], $finished_num,3);
  200. }else{
  201. $details[$new] = [
  202. "dispatch_quantity" => $value['dispatch_quantity'],
  203. "finished_num" => $finished_num,
  204. ];
  205. }
  206. }
  207. foreach ($order as $key => $value){
  208. $d_tmp = [];
  209. foreach ($orderBy as $k => $v){
  210. $new = $value['id'] . $k;
  211. $tmpS = $details[$new] ?? [];
  212. $rate = 0;
  213. if(! empty($tmpS)) {
  214. $rate = bcdiv($tmpS['finished_num'], $tmpS['dispatch_quantity'],4);
  215. $rate = bcmul($rate, 100,2);
  216. }
  217. $d_tmp[] = [
  218. "name" => $v,
  219. "rate" => $rate
  220. ];
  221. }
  222. $order[$key]['detail'] = $d_tmp;
  223. if($value['production_quantity'] <= $value['finished_num']){
  224. $state = "已完成";
  225. }else{
  226. $state = "进行中";
  227. }
  228. $order[$key]['state_title'] = $state;
  229. }
  230. //订单完成进度-----------------------------------------------------------------
  231. // dd($total, $today_total, $month_total, $month_rate, $process_14_rate, $deviceList, $zj_data_final, $thing, $xs, $xt,$order);
  232. $return = [
  233. 'total' => $total, //年总产量
  234. 'today_total' => $today_total, //当日产量
  235. 'month_total' => $month_total, //当月产量
  236. 'month_rate' => $month_rate, // 月计划完成比例
  237. 'process_14_rate' => $process_14_rate, // 注塑产线合格率
  238. 'device' => $deviceList, // 设备监控状态
  239. 'zj_result' => $zj_data_final, //质量管理
  240. 'thing' => $thing, //物料管理
  241. 'xs' => $xs, //销售管理
  242. 'xt' => $xt, //总装各线体目标/完成量
  243. 'order' => $order, // 订单完成进度
  244. ];
  245. return [true, $return];
  246. }
  247. public function productionExecution($data){
  248. //生产进度实时监控-----------------------------------------------------
  249. $order = OrdersProduct::where('del_time',0)
  250. ->limit(15)
  251. ->orderBy('id','desc')
  252. ->pluck('id')
  253. ->toArray();
  254. $dispatch = $this->getDispatch($order);
  255. $process_map = Process::whereIn('id',array_column($dispatch,'process_id'))
  256. ->pluck('title','id')
  257. ->toArray();
  258. foreach ($dispatch as $key => $value){
  259. if(in_array($value['process_id'], [11,12])){
  260. $cx = "清洗脱水产线";
  261. }else{
  262. if($value['device_id'] == 8){
  263. $cx = "注塑一号产线";
  264. }elseif ($value['device_id'] == 9){
  265. $cx = "注塑二号产线";
  266. }elseif ($value['device_id'] == 12){
  267. $cx = "注塑三号产线";
  268. }else {
  269. $cx = "注塑四号产线";
  270. }
  271. }
  272. $dispatch[$key]['cx'] = $cx;
  273. $dispatch[$key]['process_name'] = $process_map[$value['process_id']] ?? '';
  274. }
  275. //生产进度实时监控-----------------------------------------------------
  276. //订单看板-----------------------------------------------------
  277. $sales = SaleOrdersProduct::where('del_time',0)
  278. ->select('out_order_no as order_number','customer_name','product_title','technology_name as color','order_quantity', 'pre_shipment_time','finished_num','production_quantity')
  279. ->limit(10)
  280. ->orderBy('id','desc')
  281. ->get()->toArray();
  282. foreach ($sales as $key => $value){
  283. $sales[$key]['pre_shipment_time'] = $value['pre_shipment_time'] ? date('Y-m-d',$value['pre_shipment_time']) : '';
  284. if(floatval($value['production_quantity']) <= 0.0){
  285. $state_title = "未排产";
  286. }elseif ($value['finished_num'] < $value['order_quantity']){
  287. $state_title = "进行中";
  288. }else{
  289. $state_title = "已完成";
  290. }
  291. $sales[$key]['pre_shipment_time'] = $state_title;
  292. }
  293. //订单看板-----------------------------------------------------
  294. //工序负荷全览-----------------------------------------------------
  295. $process = Process::where('del_time',0)->get()->toArray();
  296. $return = ApplyOrderDetail::selectRaw('
  297. DATE(FROM_UNIXTIME(crt_time)) as date,
  298. ROUND(SUM(quantity), 3) as dispatch_quantity
  299. ')
  300. ->where('del_time', 0)
  301. ->where('type', ApplyOrder::type_one)
  302. ->groupBy('date')
  303. ->orderBy('date', 'desc') // 可选:按日期倒序
  304. ->get()
  305. ->map(function ($item) {
  306. $item->date = str_replace('-', '', $item->date); // 转为 Ymd 格式字符串
  307. return $item;
  308. })
  309. ->pluck('dispatch_quantity', 'date')
  310. ->toArray();
  311. $maxValue = empty($return) ? 0 : max($return);
  312. $today = $return[date("Ymd")] ?? 0;
  313. $rate = $maxValue ? intval($today/$maxValue * 100) : 0;
  314. $array_p = [];
  315. foreach ($process as $value){
  316. $array_p[] = [
  317. 'title' => $value['title'],
  318. 'rate' => $rate
  319. ];
  320. }
  321. //工序负荷全览-----------------------------------------------------
  322. //双周生产计划达成率-----------------------------------------------------
  323. $dates = [];
  324. $first = $end = 0;
  325. for ($i = 14; $i >= 0; $i--) {
  326. $timestamp = strtotime("-{$i} days");
  327. if($i == 14) $first = strtotime(date("Y-m-d 00:00:00",$timestamp));
  328. if($i == 0) $end = strtotime(date("Y-m-d 00:00:00",$timestamp));
  329. $dates[] = strtotime(date("Y-m-d 00:00:00",$timestamp));
  330. }
  331. $results = DispatchSub::where('del_time', 0)
  332. ->where('dispatch_time_start', '>=', $first) // 计划开始 < 给定结束
  333. ->where('dispatch_time_end', '<=', $end) // 计划结束 > 给定开始
  334. ->where('process_id',14)
  335. ->select('dispatch_time_start as crt_time',DB::raw("sum(dispatch_quantity) as quantity"))
  336. ->groupBy('dispatch_time_start')
  337. ->get()->toArray();
  338. $map = array_column($results,'quantity','crt_time');
  339. $jd = [];
  340. foreach ($dates as $value){
  341. if(isset($map[$value])){
  342. $jd[$value] = [
  343. 'plan_num' => $map[$value],
  344. 'sj_num' => 0,
  345. 'rate' => 0,
  346. 'day' => date("m-d", $value)
  347. ];
  348. }else{
  349. $jd[$value] = [
  350. 'plan_num' => 0,
  351. 'sj_num' => 0,
  352. 'rate' => 0,
  353. 'day' => date("m-d", $value)
  354. ];
  355. }
  356. }
  357. $pending = 0; // 待完成的任务量(滚存)
  358. foreach ($jd as $timestamp => &$item) {
  359. $totalWork = $pending + $item['plan_num']; // 可用任务总量(含滚存)
  360. // 当天最多完成
  361. $item['sj_num'] = min(42, $totalWork);
  362. // 剩余未完成任务,滚入下一天
  363. $pending = bcsub($totalWork, $item['sj_num'], 3);
  364. // 计算 rate
  365. if ($item['plan_num'] == 0) {
  366. // 没有计划 → 完成率 0%
  367. $item['rate'] = 0;
  368. } else {
  369. // 有计划 → 完成率 = 实际 / 计划
  370. $item['rate'] = round(($item['sj_num'] / $item['plan_num']) * 100, 2);
  371. }
  372. }
  373. //双周生产计划达成率-----------------------------------------------------
  374. list($status, $return) = (new ApplyOrderService())->reportList(['page_size' => 3, 'page_index' => 1,'not_need' =>1]);
  375. $cs = $return['list'] ?? [];
  376. $cs = $cs['data'] ?? [];
  377. $cs = array_column($cs,'message');
  378. $return = [
  379. 'dispatch' => $dispatch, //生产进度实时监控
  380. 'sales' => $sales, //订单看板
  381. 'array_p' => $array_p, //工序负荷全览
  382. 'jd' => array_values($jd), // 生产进度实时监控
  383. 'cs' => $cs
  384. ];
  385. return [true, $return];
  386. }
  387. private function getDispatch($order){
  388. $customOrder = [11, 12, 15, 13, 14];
  389. $priorityMap = array_flip($customOrder); // [11=>0, 12=>1, 15=>2, 13=>3, 14=>4]
  390. // 先从数据库取出数据(只需按 id 倒序即可)
  391. $dispatch = DispatchSub::where('del_time', 0)
  392. ->whereIn('order_product_id', $order)
  393. ->where('dispatch_quantity', '>', 1)
  394. ->select(
  395. 'process_id',
  396. 'dispatch_quantity',
  397. 'finished_num',
  398. DB::raw('finished_num as bg_num'),
  399. DB::raw('finished_num as hg_num'),
  400. 'waste_num',
  401. 'device_id',
  402. 'order_product_id',
  403. 'id',
  404. 'technology_name as color',
  405. 'product_title'
  406. )
  407. ->orderBy('id', 'desc')
  408. ->get()
  409. ->toArray();
  410. // === 在 PHP 中分组并排序 ===
  411. $grouped = [];
  412. foreach ($dispatch as $item) {
  413. $grouped[$item['order_product_id']][] = $item;
  414. }
  415. // 对每一组按 process_id 自定义顺序排序
  416. foreach ($grouped as &$group) {
  417. usort($group, function ($a, $b) use ($priorityMap) {
  418. $posA = $priorityMap[$a['process_id']] ?? 999;
  419. $posB = $priorityMap[$b['process_id']] ?? 999;
  420. return $posA <=> $posB; // 升序:11(0), 12(1), 15(2), 13(3), 14(4)
  421. });
  422. }
  423. unset($group);
  424. // 合并成扁平数组(保持 order_product_id 之间的顺序为 id 倒序)
  425. $sortedDispatch = [];
  426. $seenOrderIds = [];
  427. // 按原始 id 倒序决定 order_product_id 的出现顺序
  428. foreach ($dispatch as $item) {
  429. $orderId = $item['order_product_id'];
  430. if (!in_array($orderId, $seenOrderIds)) {
  431. $seenOrderIds[] = $orderId;
  432. }
  433. }
  434. // 按首次出现的 order_product_id 顺序(即 id 倒序)拼接每组数据
  435. foreach ($seenOrderIds as $orderId) {
  436. if (isset($grouped[$orderId])) {
  437. foreach ($grouped[$orderId] as $item) {
  438. $sortedDispatch[] = $item;
  439. }
  440. }
  441. }
  442. return $sortedDispatch;
  443. }
  444. public function qualityDashboard($data)
  445. {
  446. //质量趋势图 以及 合格率----------------------------------------------------
  447. // 近15天日期戳
  448. $dates = [];
  449. for ($i = 14; $i >= 0; $i--) {
  450. $timestamp = strtotime("-{$i} days");
  451. $dates[] = strtotime(date("Y-m-d 00:00:00", $timestamp));
  452. }
  453. $dispatch = ScrappCount::where('del_time', 0)
  454. ->where('crt_time', '>=', strtotime(date("Y-m-01 00:00:00")))
  455. ->whereIn('process_id', [12, 14])
  456. ->select('scrapp_num as dispatch_quantity', 'quantity as finished_num', 'process_id', 'equipment_id as device_id', 'crt_time')
  457. ->get()
  458. ->toArray();
  459. // 数据聚合
  460. $time_map = []; // 清洗脱水线
  461. $time_map_2 = []; // 注塑多条产线
  462. $total_map = []; // 所有产线总汇总
  463. foreach ($dispatch as $value) {
  464. $time_tmp = strtotime(date("Ymd 00:00:00", $value['crt_time']));
  465. $total = bcadd($value['finished_num'], $value['dispatch_quantity'], 3);
  466. // ✅ 所有产线的汇总
  467. if (!isset($total_map[$time_tmp])) {
  468. $total_map[$time_tmp] = ['total' => 0, 'finished' => 0];
  469. }
  470. $total_map[$time_tmp]['total'] = bcadd($total_map[$time_tmp]['total'], $total, 3);
  471. $total_map[$time_tmp]['finished'] = bcadd($total_map[$time_tmp]['finished'], $value['finished_num'], 3);
  472. if ($value['process_id'] == 12) {
  473. // 清洗脱水产线
  474. if (!isset($time_map[$time_tmp])) {
  475. $time_map[$time_tmp] = ['total' => 0, 'finished' => 0];
  476. }
  477. $time_map[$time_tmp]['total'] = bcadd($time_map[$time_tmp]['total'], $total, 3);
  478. $time_map[$time_tmp]['finished'] = bcadd($time_map[$time_tmp]['finished'], $value['finished_num'], 3);
  479. } else{
  480. // 注塑产线
  481. $key = "{$value['device_id']}|{$time_tmp}";
  482. if (!isset($time_map_2[$key])) {
  483. $time_map_2[$key] = ['total' => 0, 'finished' => 0];
  484. }
  485. $time_map_2[$key]['total'] = bcadd($time_map_2[$key]['total'], $total, 3);
  486. $time_map_2[$key]['finished'] = bcadd($time_map_2[$key]['finished'], $value['finished_num'], 3);
  487. }
  488. }
  489. // 各产线配置
  490. $lines = [
  491. '清洗脱水产线' => ['map' => &$time_map, 'key_prefix' => null],
  492. '注塑一号产线' => ['map' => &$time_map_2, 'key_prefix' => '8|'],
  493. '注塑二号产线' => ['map' => &$time_map_2, 'key_prefix' => '9|'],
  494. '注塑三号产线' => ['map' => &$time_map_2, 'key_prefix' => '12|'],
  495. '注塑四号产线' => ['map' => &$time_map_2, 'key_prefix' => '13|'],
  496. '总合格率' => ['map' => &$total_map, 'key_prefix' => null, 'is_total' => true],
  497. ];
  498. $result = [];
  499. foreach ($lines as $name => $cfg) {
  500. $map = $cfg['map'];
  501. $prefix = $cfg['key_prefix'] ?? null;
  502. $arr = [];
  503. foreach ($dates as $date_ts) {
  504. $key = $prefix ? "{$prefix}{$date_ts}" : $date_ts;
  505. $a = $map[$key]['total'] ?? 0;
  506. $b = $map[$key]['finished'] ?? 0;
  507. $rate = 0.00;
  508. if (floatval($a) > 0.0) {
  509. $rate = bcmul(bcdiv($b, $a, 4), 100, 2);
  510. }
  511. $arr[date("m-d", $date_ts)] = $rate;
  512. }
  513. $result[] = [
  514. 'name' => $name,
  515. 'array' => $arr,
  516. ];
  517. }
  518. $end = array_pop($result);
  519. $end = $end["array"];
  520. //质量趋势图 end 以及 合格率 $result ----------------------------------------------------
  521. //不合格原因分析--------------------------------------------------------------
  522. $s = ScrappCount::where('del_time', 0)
  523. ->where('crt_time', '>=', strtotime(date("Y-m-01 00:00:00")))
  524. ->where('scrapp_id', '>', 0)
  525. ->select(
  526. 'scrapp_id',
  527. DB::raw('SUM(scrapp_num) as total'),
  528. )
  529. ->groupBy('scrapp_id')
  530. ->orderBy('total','desc')
  531. ->get()
  532. ->toArray();
  533. $sum = ! empty($s) ? array_sum(array_column($s,'total')) : 0;
  534. $map1 = Scrapp::where('del_time',0)
  535. ->pluck('title', 'id')
  536. ->toArray();
  537. foreach ($s as $key => $value){
  538. $s[$key]['rate'] = ! empty($sum) ? bcmul(bcdiv($value['total'], $sum, 4),100,2): 0;
  539. $s[$key]['title'] = $map1[$value['scrapp_id']] ?? "";
  540. }
  541. //不合格原因分析--------------------------------------------------------------
  542. //明细派工单对应质检完成率-----------------------------------------------------
  543. $cx_map = [
  544. 11 => "清洗脱水产线",
  545. 12 => "清洗脱水产线" ,
  546. 15 => "注塑",
  547. 13 => "注塑",
  548. 14 => "注塑"
  549. ];
  550. $dispatch_2 = DispatchSub::where('del_time', 0)
  551. ->where('crt_time', '>=', strtotime(date("Y-m-01 00:00:00")))
  552. ->select('finished_num', 'process_id', 'crt_time','dispatch_quantity','device_id','product_title','technology_name as color','waste_num')
  553. ->get()
  554. ->toArray();
  555. $pro_map = Process::where('del_time',0)
  556. ->pluck('title','id')
  557. ->toArray();
  558. foreach ($dispatch_2 as $key => $value) {
  559. $dispatch_2[$key]['process_title'] = $pro_map[$value['process_id']] ?? "";
  560. $cx = $cx_map[$value['process_id']] ?? "";
  561. if(in_array($value['process_id'], [15,13,14])){
  562. if($value['device_id'] == 8){
  563. $cx .= "一号产线";
  564. }elseif ($value['device_id'] == 9){
  565. $cx .= "二号产线";
  566. }elseif ($value['device_id'] == 12){
  567. $cx .= "三号产线";
  568. }else {
  569. $cx .= "四号产线";
  570. }
  571. }
  572. $dispatch_2[$key]['cx'] = $cx;
  573. $num = bcadd($value['finished_num'], $value['waste_num'],3);
  574. $dispatch_2[$key]['rate'] = bcmul(bcdiv($num, $value['dispatch_quantity'],4),100,2);
  575. }
  576. //明细派工单对应质检完成率-----------------------------------------------------
  577. //过程质量监控------------------------------------------------------
  578. $s_3 = DispatchSub::from('dispatch_sub as a')
  579. ->join('zj_plan as b','b.id','a.zj_plan_id')
  580. ->join('zj as c',DB::raw('FIND_IN_SET(c.id, b.zj_id)'), '>', DB::raw('0'))
  581. ->where('a.del_time', 0)
  582. // ->where('a.waste_num', '>',0)
  583. ->where('a.crt_time', '>=', strtotime(date("Y-m-01 00:00:00")))
  584. ->whereIn('a.process_id',[12,14])
  585. ->select("a.process_id","a.product_title",'a.technology_name as color','a.waste_num','c.title as c_title','a.crt_time','a.order_product_id')
  586. ->orderBy('a.id','desc')
  587. ->get()
  588. ->toArray();
  589. foreach ($s_3 as $key => $value){
  590. $s_3[$key]['process_title'] = $pro_map[$value['process_id']] ?? "";
  591. if($value['waste_num'] > 0){
  592. $title = (rand(1, 100) <= 60) ? '合格' : '不合格';
  593. }else{
  594. $title = "合格";
  595. }
  596. $s_3[$key]['hg_title'] = $title;
  597. }
  598. //过程质量监控------------------------------------------------------
  599. $return = [
  600. 'zl' => $end, //质量趋势图
  601. 'zl_rate' => $result, //质量合格率
  602. 'reason' => $s, //不合格原因分析
  603. 'detail' => $dispatch_2, // 明细派工单对应质检完成率
  604. 'procedure' => $s_3, //过程质量监控
  605. ];
  606. return [true, $return];
  607. }
  608. }