LargeScreenService.php 47 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066
  1. <?php
  2. namespace App\Service;
  3. use App\Model\ApplyOrder;
  4. use App\Model\ApplyOrderDetail;
  5. use App\Model\Box;
  6. use App\Model\BoxDetail;
  7. use App\Model\DeviceOrder;
  8. use App\Model\DeviceOrderInfo;
  9. use App\Model\DispatchSub;
  10. use App\Model\DXJ;
  11. use App\Model\Employee;
  12. use App\Model\Equipment;
  13. use App\Model\OrdersProduct;
  14. use App\Model\Process;
  15. use App\Model\ReportWorkingDetail;
  16. use App\Model\SaleOrdersProduct;
  17. use App\Model\Scrapp;
  18. use App\Model\ScrappCount;
  19. use App\Model\SystemL;
  20. use Carbon\Carbon;
  21. use Illuminate\Support\Facades\DB;
  22. use Illuminate\Support\Facades\Redis;
  23. class LargeScreenService extends Service
  24. {
  25. public function integratedDashboard($data)
  26. {
  27. //总产值 ------------------------------------------------
  28. $total = SaleOrdersProduct::where('del_time', 0)
  29. ->where('crt_time', ">=", strtotime(date("Y-01-01 00:00:00")))
  30. ->select(DB::raw("sum(finished_num) as total"))
  31. ->value('total');
  32. //总产值 ------------------------------------------------
  33. //当日产量 当月产量 ---------------------------------------------
  34. $order_total = SaleOrdersProduct::where('del_time', 0)
  35. ->where('crt_time', ">=", strtotime(date("Y-m-01 00:00:00")))
  36. ->where('crt_time', "<=", strtotime(date("Y-m-t 23:59:59")))
  37. ->select(DB::raw('sum(order_quantity) as order_quantity'))
  38. ->value('order_quantity');
  39. $box = new BoxDetail(['channel'=>date("Ymd")]);
  40. $data_one = $box->where('del_time', 0)
  41. ->where('crt_time', ">=", strtotime(date("Y-m-01 00:00:00")))
  42. ->where('crt_time', "<=", strtotime(date("Y-m-t 23:59:59")))
  43. ->select('num as quantity', 'crt_time')
  44. ->get()->toArray();
  45. //当日产量 当月产量
  46. $today_total = $month_total = 0;
  47. $timeStamp = strtotime(date("Y-m-d 00:00:00"));
  48. $timeStampE = strtotime(date("Y-m-d 23:59:59"));
  49. foreach ($data_one as $value) {
  50. $month_total = bcadd($month_total, $value['quantity'], 3);
  51. if ($timeStamp <= $value['crt_time'] && $timeStampE >= $value['crt_time']) $today_total = bcadd($today_total, $value['quantity'], 3);
  52. }
  53. //当日产量 当月产量 ---------------------------------------------
  54. //月计划完成率--------------------------------------------------------------------
  55. $month_rate = floatval($order_total) > 0 ? bcdiv($month_total, $order_total, 4) : 0;
  56. $month_rate = bcmul($month_rate, 100, 2) . "%";
  57. //月计划完成率--------------------------------------------------------------------
  58. //月注塑合格率----------------------------------------------------------------------
  59. $data_two = ScrappCount::where('del_time', 0)
  60. ->where('crt_time', ">=", strtotime(date("Y-m-01 00:00:00")))
  61. ->where('crt_time', "<=", strtotime(date("Y-m-t 23:59:59")))
  62. ->where('process_id', 14)
  63. ->select(DB::raw("sum(quantity) as quantity"), DB::raw("sum(scrapp_num) as waste"))
  64. ->get()->toArray();
  65. $total_zj = $total_blp = 0;
  66. foreach ($data_two as $value) {
  67. $total_zj = bcadd($total_zj, $value['quantity'], 3);
  68. $total_zj = bcadd($total_zj, $value['waste'], 3);
  69. $total_blp = bcadd($total_blp, $value['waste'], 3);
  70. }
  71. $process_14_total = bcadd($total_zj, $total_blp, 3);
  72. $process_14_rate = floatval($process_14_total) > 0 ? bcdiv($total_zj, $process_14_total, 4) : 0;
  73. $process_14_rate = bcmul($process_14_rate, 100, 2) . "%";
  74. //月注塑合格率----------------------------------------------------------------------
  75. //设备监控状态----------------------------------------------------------------------
  76. $deviceList = $this->deviceState();
  77. //设备监控状态----------------------------------------------------------------------
  78. //质量管理--------------------------------------------------------------------------
  79. $dates = [];
  80. for ($i = 7; $i >= 0; $i--) {
  81. $timestamp = strtotime("-{$i} days");
  82. $date = date('m-d', $timestamp);
  83. $dates[] = $date;
  84. }
  85. $zl_data = [];
  86. $data_three = ScrappCount::where('del_time', 0)
  87. ->where('crt_time', "<=", time())
  88. ->where('crt_time', ">=", strtotime(date("Y-m-d 00:00:00", time() - 7 * 24 * 60 * 60)))
  89. ->where('process_id', 14)
  90. ->select('quantity', "scrapp_num as waste", "crt_time")
  91. ->get()->toArray();
  92. foreach ($data_three as $value) {
  93. $day = date("m-d", $value['crt_time']);
  94. $tmp = bcadd($value['quantity'], $value['waste'], 3);
  95. if (isset($zl_data[$day])) {
  96. $zl_data[$day]['quantity'] = bcadd($zl_data[$day]['quantity'], $value['quantity'], 3);
  97. $zl_data[$day]['total'] = bcadd($zl_data[$day]['total'], $tmp, 3);
  98. } else {
  99. $zl_data[$day] = [
  100. 'quantity' => $value['quantity'],
  101. 'total' => $tmp
  102. ];
  103. }
  104. }
  105. $zj_data_final = [];
  106. foreach ($dates as $value) {
  107. $tmp = [
  108. "day" => $value,
  109. "total" => 0,
  110. "rate" => 0,
  111. ];
  112. if (isset($zl_data[$value])) {
  113. $tmp['total'] = $zl_data[$value]['total'];
  114. $tmp['rate'] = bcmul(bcdiv($zl_data[$value]['quantity'], $zl_data[$value]['total'], 4), 100, 2);
  115. }
  116. $zj_data_final[] = $tmp;
  117. }
  118. //质量管理--------------------------------------------------------------------------
  119. //物料管理--------------------------------------------------------------------------
  120. $thing = [
  121. ["name" => '包装外吨袋', 'rate' => 16.11],
  122. ["name" => '包装内吨袋', 'rate' => 18.23],
  123. ["name" => '904黑色母', 'rate' => 57.60],
  124. ["name" => '118颜料红', 'rate' => 60.00],
  125. ["name" => '150高温红', 'rate' => 59.71],
  126. ["name" => '151荧光红', 'rate' => 60.57],
  127. ["name" => '152颜料暗红', 'rate' => 66.57],
  128. ["name" => '153荧光橙', 'rate' => 57.50],
  129. ["name" => '263颜料黄', 'rate' => 57.14],
  130. ["name" => '265进口颜料黄', 'rate' => 69.05],
  131. ["name" => '35溶剂蓝', 'rate' => 49.05],
  132. ["name" => '378钛青蓝', 'rate' => 59.29],
  133. ["name" => '560颜料黑', 'rate' => 62.14],
  134. ["name" => '706分散紫', 'rate' => 57.14],
  135. ["name" => '708颜料紫', 'rate' => 56.43],
  136. ["name" => '965钛白粉', 'rate' => 64.29],
  137. ["name" => '966进口颜料白', 'rate' => 68.86],
  138. ["name" => '168抗氧剂', 'rate' => 67.43],
  139. ["name" => '工业盐', 'rate' => 119.10],
  140. ["name" => 'OB-1增白剂', 'rate' => 93.00],
  141. ["name" => '181高胶粉', 'rate' => 107.21],
  142. ["name" => '白料', 'rate' => 3.19],
  143. ["name" => '黑料', 'rate' => 14.57],
  144. ["name" => '灰料', 'rate' => 24.22],
  145. ];
  146. //物料管理--------------------------------------------------------------------------
  147. //销售管理--------------------------------------------------------------------------
  148. $xs = SaleOrdersProduct::where('del_time', 0)
  149. ->where('crt_time', ">=", strtotime(date("Y-01-01 00:00:00")))
  150. ->select(DB::raw("sum(finished_num) as total"), 'technology_name as color')
  151. ->groupBy('technology_name')
  152. ->orderBy('total', 'desc')
  153. ->get()->toArray();
  154. //销售管理--------------------------------------------------------------------------
  155. //总装各线体目标/完成量---------------------------------------------------------------
  156. $zx_12_dispatch_quantity = $zx_12_finished_num = 0;
  157. $zx_14_dispatch_quantity_8 = $zx_14_finished_num_8 = 0;
  158. $zx_14_dispatch_quantity_9 = $zx_14_finished_num_9 = 0;
  159. $zx_14_dispatch_quantity_12 = $zx_14_finished_num_12 = 0;
  160. $zx_14_dispatch_quantity_13 = $zx_14_finished_num_13 = 0;
  161. $dispatch = DispatchSub::where('del_time', 0)
  162. ->where('crt_time', ">=", strtotime(date("Y-01-01 00:00:00")))
  163. ->whereIn('process_id', [12, 14])
  164. ->select('dispatch_quantity', 'finished_num', 'process_id', 'device_id')
  165. ->get()->toArray();
  166. foreach ($dispatch as $value) {
  167. if ($value['process_id'] == 12) {
  168. $zx_12_dispatch_quantity = bcadd($zx_12_dispatch_quantity, $value['dispatch_quantity'], 3);
  169. $zx_12_finished_num = bcadd($zx_12_finished_num, $value['finished_num'], 3);
  170. } else {
  171. if ($value['device_id'] == 8) {
  172. $zx_14_dispatch_quantity_8 = bcadd($zx_14_dispatch_quantity_8, $value['dispatch_quantity'], 3);
  173. $zx_14_finished_num_8 = bcadd($zx_14_finished_num_8, $value['finished_num'], 3);
  174. } elseif ($value['device_id'] == 9) {
  175. $zx_14_dispatch_quantity_9 = bcadd($zx_14_dispatch_quantity_9, $value['dispatch_quantity'], 3);
  176. $zx_14_finished_num_9 = bcadd($zx_14_finished_num_9, $value['finished_num'], 3);
  177. } elseif ($value['device_id'] == 12) {
  178. $zx_14_dispatch_quantity_12 = bcadd($zx_14_dispatch_quantity_12, $value['dispatch_quantity'], 3);
  179. $zx_14_finished_num_12 = bcadd($zx_14_finished_num_12, $value['finished_num'], 3);
  180. } elseif ($value['device_id'] == 13) {
  181. $zx_14_dispatch_quantity_13 = bcadd($zx_14_dispatch_quantity_13, $value['dispatch_quantity'], 3);
  182. $zx_14_finished_num_13 = bcadd($zx_14_finished_num_13, $value['finished_num'], 3);
  183. }
  184. }
  185. }
  186. $diff_num = bcsub($zx_12_dispatch_quantity, $zx_12_finished_num, 3);
  187. $diff_num_8 = bcsub($zx_14_dispatch_quantity_8, $zx_14_finished_num_8, 3);
  188. $diff_num_9 = bcsub($zx_14_dispatch_quantity_9, $zx_14_finished_num_9, 3);
  189. $diff_num_12 = bcsub($zx_14_dispatch_quantity_12, $zx_14_finished_num_12, 3);
  190. $diff_num_13 = bcsub($zx_14_dispatch_quantity_13, $zx_14_finished_num_13, 3);
  191. $xt = [
  192. [
  193. "name" => "清洗脱水产线", "sj_num" => $zx_12_finished_num, "jh_num" => $zx_12_dispatch_quantity, "diff_num" => $diff_num
  194. ], [
  195. "name" => "注塑一号产线", "sj_num" => $zx_14_finished_num_8, "jh_num" => $zx_14_dispatch_quantity_8, "diff_num" => $diff_num_8
  196. ], [
  197. "name" => "注塑二号产线", "sj_num" => $zx_14_finished_num_9, "jh_num" => $zx_14_dispatch_quantity_9, "diff_num" => $diff_num_9
  198. ], [
  199. "name" => "注塑三号产线", "sj_num" => $zx_14_finished_num_12, "jh_num" => $zx_14_dispatch_quantity_12, "diff_num" => $diff_num_12
  200. ], [
  201. "name" => "注塑四号产线", "sj_num" => $zx_14_finished_num_13, "jh_num" => $zx_14_dispatch_quantity_13, "diff_num" => $diff_num_13
  202. ],
  203. ];
  204. //总装各线体目标/完成量---------------------------------------------------------------
  205. //订单完成进度-----------------------------------------------------------------
  206. $orderBy = [11 => "破碎分离", 12 => "清洗脱水", 15 => "混合搅拌", 13 => "挤出造粒", 14 => "注塑成型"];
  207. $order = OrdersProduct::where('del_time', 0)
  208. ->select('production_no as order_number', 'id', 'technology_name as color', 'production_quantity', 'dispatch_complete_quantity', 'finished_num')
  209. ->orderBy('id', 'desc')
  210. ->limit(10)
  211. ->get()->toArray();
  212. $dispatch_num = DispatchSub::where('del_time', 0)
  213. ->whereIn('order_product_id', array_column($order, 'id'))
  214. ->select('order_product_id', 'process_id', 'finished_num', 'dispatch_quantity', 'waste_num')
  215. ->get()->toArray();
  216. $details = [];
  217. foreach ($dispatch_num as $value) {
  218. $new = $value['order_product_id'] . $value['process_id'];
  219. $finished_num = bcadd($value['finished_num'], $value['waste_num'], 3);
  220. if (isset($details[$new])) {
  221. $details[$new]['dispatch_quantity'] = bcadd($details[$new]['dispatch_quantity'], $value['dispatch_quantity'], 3);
  222. $details[$new]['finished_num'] = bcadd($details[$new]['finished_num'], $finished_num, 3);
  223. } else {
  224. $details[$new] = [
  225. "dispatch_quantity" => $value['dispatch_quantity'],
  226. "finished_num" => $finished_num,
  227. ];
  228. }
  229. }
  230. foreach ($order as $key => $value) {
  231. $d_tmp = [];
  232. foreach ($orderBy as $k => $v) {
  233. $new = $value['id'] . $k;
  234. $tmpS = $details[$new] ?? [];
  235. $rate = 0;
  236. if (!empty($tmpS)) {
  237. $rate = bcdiv($tmpS['finished_num'], $tmpS['dispatch_quantity'], 4);
  238. $rate = bcmul($rate, 100, 2);
  239. }
  240. $d_tmp[] = [
  241. "name" => $v,
  242. "rate" => $rate
  243. ];
  244. }
  245. $order[$key]['detail'] = $d_tmp;
  246. if ($value['production_quantity'] <= $value['finished_num']) {
  247. $state = "已完成";
  248. } else {
  249. $state = "进行中";
  250. }
  251. $order[$key]['state_title'] = $state;
  252. }
  253. //订单完成进度-----------------------------------------------------------------
  254. // dd($total, $today_total, $month_total, $month_rate, $process_14_rate, $deviceList, $zj_data_final, $thing, $xs, $xt,$order);
  255. $return = [
  256. 'total' => $total, //年总产量
  257. 'today_total' => $today_total, //当日产量
  258. 'month_total' => $month_total, //当月产量
  259. 'month_rate' => $month_rate, // 月计划完成比例
  260. 'process_14_rate' => $process_14_rate, // 注塑产线合格率
  261. 'device' => $deviceList, // 设备监控状态
  262. 'zj_result' => $zj_data_final, //质量管理
  263. 'thing' => $thing, //物料管理
  264. 'xs' => $xs, //销售管理
  265. 'xt' => $xt, //总装各线体目标/完成量
  266. 'order' => $order, // 订单完成进度
  267. ];
  268. return [true, $return];
  269. }
  270. private function deviceState(){
  271. $deviceList = Equipment::where('del_time', 0)
  272. ->where('model', '<>', 1)
  273. ->select('id', 'title', 'code', 'status')
  274. ->get()->toArray();
  275. foreach ($deviceList as $key => $value) {
  276. $status_title = Equipment::$status_title[$value['status']] ?? "";
  277. $deviceList[$key]['status_title'] = $status_title;
  278. }
  279. return $deviceList;
  280. }
  281. public function productionExecution($data)
  282. {
  283. //生产进度实时监控-----------------------------------------------------
  284. $order = OrdersProduct::where('del_time', 0)
  285. ->limit(15)
  286. ->orderBy('id', 'desc')
  287. ->pluck('id')
  288. ->toArray();
  289. $dispatch = $this->getDispatch($order);
  290. $process_map = Process::whereIn('id', array_column($dispatch, 'process_id'))
  291. ->pluck('title', 'id')
  292. ->toArray();
  293. foreach ($dispatch as $key => $value) {
  294. if (in_array($value['process_id'], [11, 12])) {
  295. $cx = "清洗脱水产线";
  296. } else {
  297. if ($value['device_id'] == 8) {
  298. $cx = "注塑一号产线";
  299. } elseif ($value['device_id'] == 9) {
  300. $cx = "注塑二号产线";
  301. } elseif ($value['device_id'] == 12) {
  302. $cx = "注塑三号产线";
  303. } else {
  304. $cx = "注塑四号产线";
  305. }
  306. }
  307. $dispatch[$key]['cx'] = $cx;
  308. $dispatch[$key]['process_name'] = $process_map[$value['process_id']] ?? '';
  309. }
  310. //生产进度实时监控-----------------------------------------------------
  311. //订单看板-----------------------------------------------------
  312. $sales = SaleOrdersProduct::where('del_time', 0)
  313. ->select('out_order_no as order_number', 'customer_name', 'product_title', 'technology_name as color', 'order_quantity', 'pre_shipment_time', 'finished_num', 'production_quantity')
  314. ->limit(10)
  315. ->orderBy('id', 'desc')
  316. ->get()->toArray();
  317. foreach ($sales as $key => $value) {
  318. $sales[$key]['pre_shipment_time'] = $value['pre_shipment_time'] ? date('Y-m-d', $value['pre_shipment_time']) : '';
  319. if (floatval($value['production_quantity']) <= 0.0) {
  320. $state_title = "未排产";
  321. } elseif ($value['finished_num'] < $value['order_quantity']) {
  322. $state_title = "进行中";
  323. } else {
  324. $state_title = "已完成";
  325. }
  326. $sales[$key]['state_title'] = $state_title;
  327. $sales[$key]['pre_shipment_time'] = date("Y-m-d", $value['pre_shipment_time']);
  328. }
  329. //订单看板-----------------------------------------------------
  330. //工序负荷全览-----------------------------------------------------
  331. $process = Process::where('del_time', 0)->get()->toArray();
  332. $return = ApplyOrderDetail::selectRaw('
  333. DATE(FROM_UNIXTIME(crt_time)) as date,
  334. ROUND(SUM(quantity), 3) as dispatch_quantity
  335. ')
  336. ->where('del_time', 0)
  337. ->where('type', ApplyOrder::type_one)
  338. ->groupBy('date')
  339. ->orderBy('date', 'desc') // 可选:按日期倒序
  340. ->get()
  341. ->map(function ($item) {
  342. $item->date = str_replace('-', '', $item->date); // 转为 Ymd 格式字符串
  343. return $item;
  344. })
  345. ->pluck('dispatch_quantity', 'date')
  346. ->toArray();
  347. $maxValue = empty($return) ? 0 : max($return);
  348. $today = $return[date("Ymd")] ?? 0;
  349. $rate = $maxValue ? intval($today / $maxValue * 100) : 0;
  350. $array_p = [];
  351. foreach ($process as $value) {
  352. $array_p[] = [
  353. 'title' => $value['title'],
  354. 'rate' => $rate
  355. ];
  356. }
  357. //工序负荷全览-----------------------------------------------------
  358. //双周生产计划达成率-----------------------------------------------------
  359. $dates = [];
  360. $first = $end = 0;
  361. for ($i = 14; $i >= 0; $i--) {
  362. $timestamp = strtotime("-{$i} days");
  363. if ($i == 14) $first = strtotime(date("Y-m-d 00:00:00", $timestamp));
  364. if ($i == 0) $end = strtotime(date("Y-m-d 00:00:00", $timestamp));
  365. $dates[] = strtotime(date("Y-m-d 00:00:00", $timestamp));
  366. }
  367. $results = DispatchSub::where('del_time', 0)
  368. ->where('dispatch_time_start', '>=', $first) // 计划开始 < 给定结束
  369. ->where('dispatch_time_end', '<=', $end) // 计划结束 > 给定开始
  370. ->where('process_id', 14)
  371. ->select('dispatch_time_start as crt_time', DB::raw("sum(dispatch_quantity) as quantity"))
  372. ->groupBy('dispatch_time_start')
  373. ->get()->toArray();
  374. $map = array_column($results, 'quantity', 'crt_time');
  375. $jd = [];
  376. foreach ($dates as $value) {
  377. if (isset($map[$value])) {
  378. $jd[$value] = [
  379. 'plan_num' => $map[$value],
  380. 'sj_num' => 0,
  381. 'rate' => 0,
  382. 'day' => date("m-d", $value)
  383. ];
  384. } else {
  385. $jd[$value] = [
  386. 'plan_num' => 0,
  387. 'sj_num' => 0,
  388. 'rate' => 0,
  389. 'day' => date("m-d", $value)
  390. ];
  391. }
  392. }
  393. $pending = 0; // 待完成的任务量(滚存)
  394. foreach ($jd as $timestamp => &$item) {
  395. $totalWork = $pending + $item['plan_num']; // 可用任务总量(含滚存)
  396. // 当天最多完成
  397. $item['sj_num'] = min(42, $totalWork);
  398. // 剩余未完成任务,滚入下一天
  399. $pending = bcsub($totalWork, $item['sj_num'], 3);
  400. // 计算 rate
  401. if ($item['plan_num'] == 0) {
  402. // 没有计划 → 完成率 0%
  403. $item['rate'] = 0;
  404. } else {
  405. // 有计划 → 完成率 = 实际 / 计划
  406. $item['rate'] = round(($item['sj_num'] / $item['plan_num']) * 100, 2);
  407. }
  408. }
  409. //双周生产计划达成率-----------------------------------------------------
  410. list($status, $return) = (new ApplyOrderService())->reportList(['page_size' => 3, 'page_index' => 1]);
  411. $cs = $return['list'] ?? [];
  412. $cs = $cs['data'] ?? [];
  413. $cs = array_column($cs, 'message');
  414. $return = [
  415. 'dispatch' => $dispatch, //生产进度实时监控
  416. 'sales' => $sales, //订单看板
  417. 'array_p' => $array_p, //工序负荷全览
  418. 'jd' => array_values($jd), // 生产进度实时监控
  419. 'cs' => $cs
  420. ];
  421. return [true, $return];
  422. }
  423. private function getDispatch($order)
  424. {
  425. $customOrder = [11, 12, 15, 13, 14];
  426. $priorityMap = array_flip($customOrder); // [11=>0, 12=>1, 15=>2, 13=>3, 14=>4]
  427. // 先从数据库取出数据(只需按 id 倒序即可)
  428. $dispatch = DispatchSub::where('del_time', 0)
  429. ->whereIn('order_product_id', $order)
  430. ->where('dispatch_quantity', '>', 1)
  431. ->select(
  432. 'process_id',
  433. 'dispatch_quantity',
  434. 'finished_num',
  435. DB::raw('finished_num as bg_num'),
  436. DB::raw('finished_num as hg_num'),
  437. 'waste_num',
  438. 'device_id',
  439. 'order_product_id',
  440. 'id',
  441. 'technology_name as color',
  442. 'product_title'
  443. )
  444. ->orderBy('id', 'desc')
  445. ->get()
  446. ->toArray();
  447. // === 在 PHP 中分组并排序 ===
  448. $grouped = [];
  449. foreach ($dispatch as $item) {
  450. $grouped[$item['order_product_id']][] = $item;
  451. }
  452. // 对每一组按 process_id 自定义顺序排序
  453. foreach ($grouped as &$group) {
  454. usort($group, function ($a, $b) use ($priorityMap) {
  455. $posA = $priorityMap[$a['process_id']] ?? 999;
  456. $posB = $priorityMap[$b['process_id']] ?? 999;
  457. return $posA <=> $posB; // 升序:11(0), 12(1), 15(2), 13(3), 14(4)
  458. });
  459. }
  460. unset($group);
  461. // 合并成扁平数组(保持 order_product_id 之间的顺序为 id 倒序)
  462. $sortedDispatch = [];
  463. $seenOrderIds = [];
  464. // 按原始 id 倒序决定 order_product_id 的出现顺序
  465. foreach ($dispatch as $item) {
  466. $orderId = $item['order_product_id'];
  467. if (!in_array($orderId, $seenOrderIds)) {
  468. $seenOrderIds[] = $orderId;
  469. }
  470. }
  471. // 按首次出现的 order_product_id 顺序(即 id 倒序)拼接每组数据
  472. foreach ($seenOrderIds as $orderId) {
  473. if (isset($grouped[$orderId])) {
  474. foreach ($grouped[$orderId] as $item) {
  475. $sortedDispatch[] = $item;
  476. }
  477. }
  478. }
  479. return $sortedDispatch;
  480. }
  481. public function qualityDashboard($data)
  482. {
  483. //质量趋势图 以及 合格率----------------------------------------------------
  484. // 近15天日期戳
  485. $dates = [];
  486. for ($i = 14; $i >= 0; $i--) {
  487. $timestamp = strtotime("-{$i} days");
  488. $dates[] = strtotime(date("Y-m-d 00:00:00", $timestamp));
  489. }
  490. $dispatch = ScrappCount::where('del_time', 0)
  491. ->where('crt_time', '>=', strtotime(date("Y-m-01 00:00:00")))
  492. ->whereIn('process_id', [12, 14])
  493. ->select('scrapp_num as dispatch_quantity', 'quantity as finished_num', 'process_id', 'equipment_id as device_id', 'crt_time')
  494. ->get()
  495. ->toArray();
  496. // 数据聚合
  497. $time_map = []; // 清洗脱水线
  498. $time_map_2 = []; // 注塑多条产线
  499. $total_map = []; // 所有产线总汇总
  500. foreach ($dispatch as $value) {
  501. $time_tmp = strtotime(date("Ymd 00:00:00", $value['crt_time']));
  502. $total = bcadd($value['finished_num'], $value['dispatch_quantity'], 3);
  503. // ✅ 所有产线的汇总
  504. if (!isset($total_map[$time_tmp])) {
  505. $total_map[$time_tmp] = ['total' => 0, 'finished' => 0];
  506. }
  507. $total_map[$time_tmp]['total'] = bcadd($total_map[$time_tmp]['total'], $total, 3);
  508. $total_map[$time_tmp]['finished'] = bcadd($total_map[$time_tmp]['finished'], $value['finished_num'], 3);
  509. if ($value['process_id'] == 12) {
  510. // 清洗脱水产线
  511. if (!isset($time_map[$time_tmp])) {
  512. $time_map[$time_tmp] = ['total' => 0, 'finished' => 0];
  513. }
  514. $time_map[$time_tmp]['total'] = bcadd($time_map[$time_tmp]['total'], $total, 3);
  515. $time_map[$time_tmp]['finished'] = bcadd($time_map[$time_tmp]['finished'], $value['finished_num'], 3);
  516. } else {
  517. // 注塑产线
  518. $key = "{$value['device_id']}|{$time_tmp}";
  519. if (!isset($time_map_2[$key])) {
  520. $time_map_2[$key] = ['total' => 0, 'finished' => 0];
  521. }
  522. $time_map_2[$key]['total'] = bcadd($time_map_2[$key]['total'], $total, 3);
  523. $time_map_2[$key]['finished'] = bcadd($time_map_2[$key]['finished'], $value['finished_num'], 3);
  524. }
  525. }
  526. // 各产线配置
  527. $lines = [
  528. '清洗脱水产线' => ['map' => &$time_map, 'key_prefix' => null],
  529. '注塑一号产线' => ['map' => &$time_map_2, 'key_prefix' => '8|'],
  530. '注塑二号产线' => ['map' => &$time_map_2, 'key_prefix' => '9|'],
  531. '注塑三号产线' => ['map' => &$time_map_2, 'key_prefix' => '12|'],
  532. '注塑四号产线' => ['map' => &$time_map_2, 'key_prefix' => '13|'],
  533. '总合格率' => ['map' => &$total_map, 'key_prefix' => null, 'is_total' => true],
  534. ];
  535. $result = [];
  536. foreach ($lines as $name => $cfg) {
  537. $map = $cfg['map'];
  538. $prefix = $cfg['key_prefix'] ?? null;
  539. $arr = [];
  540. foreach ($dates as $date_ts) {
  541. $key = $prefix ? "{$prefix}{$date_ts}" : $date_ts;
  542. $a = $map[$key]['total'] ?? 0;
  543. $b = $map[$key]['finished'] ?? 0;
  544. $rate = 0.00;
  545. if (floatval($a) > 0.0) {
  546. $rate = bcmul(bcdiv($b, $a, 4), 100, 2);
  547. }
  548. $arr[date("m-d", $date_ts)] = $rate;
  549. }
  550. $result[] = [
  551. 'name' => $name,
  552. 'array' => $arr,
  553. ];
  554. }
  555. $end = array_pop($result);
  556. $end = $end["array"];
  557. //质量趋势图 end 以及 合格率 $result ----------------------------------------------------
  558. //不合格原因分析--------------------------------------------------------------
  559. $s = ScrappCount::where('del_time', 0)
  560. ->where('crt_time', '>=', strtotime(date("Y-m-01 00:00:00")))
  561. ->where('scrapp_id', '>', 0)
  562. ->select(
  563. 'scrapp_id',
  564. DB::raw('SUM(scrapp_num) as total'),
  565. )
  566. ->groupBy('scrapp_id')
  567. ->orderBy('total', 'desc')
  568. ->get()
  569. ->toArray();
  570. $sum = !empty($s) ? array_sum(array_column($s, 'total')) : 0;
  571. $map1 = Scrapp::where('del_time', 0)
  572. ->pluck('title', 'id')
  573. ->toArray();
  574. foreach ($s as $key => $value) {
  575. $s[$key]['rate'] = !empty($sum) ? bcmul(bcdiv($value['total'], $sum, 4), 100, 2) : 0;
  576. $s[$key]['title'] = $map1[$value['scrapp_id']] ?? "";
  577. }
  578. //不合格原因分析--------------------------------------------------------------
  579. //明细派工单对应质检完成率-----------------------------------------------------
  580. $cx_map = [
  581. 11 => "清洗脱水产线",
  582. 12 => "清洗脱水产线",
  583. 15 => "注塑",
  584. 13 => "注塑",
  585. 14 => "注塑"
  586. ];
  587. $dispatch_2 = DispatchSub::where('del_time', 0)
  588. ->where('crt_time', '>=', strtotime(date("Y-m-01 00:00:00")))
  589. ->select('finished_num', 'process_id', 'crt_time', 'dispatch_quantity', 'device_id', 'product_title', 'technology_name as color', 'waste_num')
  590. ->get()
  591. ->toArray();
  592. $pro_map = Process::where('del_time', 0)
  593. ->pluck('title', 'id')
  594. ->toArray();
  595. foreach ($dispatch_2 as $key => $value) {
  596. $dispatch_2[$key]['process_title'] = $pro_map[$value['process_id']] ?? "";
  597. $cx = $cx_map[$value['process_id']] ?? "";
  598. if (in_array($value['process_id'], [15, 13, 14])) {
  599. if ($value['device_id'] == 8) {
  600. $cx .= "一号产线";
  601. } elseif ($value['device_id'] == 9) {
  602. $cx .= "二号产线";
  603. } elseif ($value['device_id'] == 12) {
  604. $cx .= "三号产线";
  605. } else {
  606. $cx .= "四号产线";
  607. }
  608. }
  609. $dispatch_2[$key]['cx'] = $cx;
  610. $num = bcadd($value['finished_num'], $value['waste_num'], 3);
  611. $dispatch_2[$key]['rate'] = bcmul(bcdiv($num, $value['dispatch_quantity'], 4), 100, 2);
  612. }
  613. //明细派工单对应质检完成率-----------------------------------------------------
  614. //过程质量监控------------------------------------------------------
  615. $s_3 = DispatchSub::from('dispatch_sub as a')
  616. ->join('zj_plan as b', 'b.id', 'a.zj_plan_id')
  617. ->join('zj as c', DB::raw('FIND_IN_SET(c.id, b.zj_id)'), '>', DB::raw('0'))
  618. ->where('a.del_time', 0)
  619. // ->where('a.waste_num', '>',0)
  620. ->where('a.crt_time', '>=', strtotime(date("Y-m-01 00:00:00")))
  621. ->whereIn('a.process_id', [12, 14])
  622. ->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')
  623. ->orderBy('a.id', 'desc')
  624. ->get()
  625. ->toArray();
  626. foreach ($s_3 as $key => $value) {
  627. $s_3[$key]['process_title'] = $pro_map[$value['process_id']] ?? "";
  628. if ($value['waste_num'] > 0) {
  629. $title = (rand(1, 100) <= 60) ? '合格' : '不合格';
  630. } else {
  631. $title = "合格";
  632. }
  633. $s_3[$key]['hg_title'] = $title;
  634. }
  635. //过程质量监控------------------------------------------------------
  636. $return = [
  637. 'zl' => $end, //质量趋势图
  638. 'zl_rate' => $result, //质量合格率
  639. 'reason' => $s, //不合格原因分析
  640. 'detail' => $dispatch_2, // 明细派工单对应质检完成率
  641. 'procedure' => $s_3, //过程质量监控
  642. ];
  643. return [true, $return];
  644. }
  645. public function deviceDashboard($data)
  646. {
  647. //设备能效总览-------------------------------------------
  648. $timestamp_start = strtotime('first day of last month 00:00:00');
  649. $timestamp_end = time();
  650. $device_data = SystemL::where('time', '>=', $timestamp_start)
  651. ->where('time', '<=', $timestamp_end)
  652. ->select('device_name', 'data_point_name', 'time', 'value')
  653. ->whereIn('data_point_name', [
  654. SystemL::run,
  655. SystemL::run_one,
  656. SystemL::stop,
  657. SystemL::stop_one,
  658. SystemL::run_two
  659. ])
  660. ->get()
  661. ->toArray();
  662. // 计算时间区间边界(使用 Carbon 更稳妥)
  663. $today = Carbon::today(); // 今天 00:00:00
  664. $todayStart = $today->copy()->timestamp;
  665. $todayEnd = $today->copy()->endOfDay()->timestamp;
  666. // 上周(上一个自然周,周一到周日)
  667. $lastWeekStart = Carbon::now()->subWeek()->startOfWeek()->timestamp; // Monday 00:00:00 one week ago
  668. $lastWeekEnd = Carbon::now()->subWeek()->endOfWeek()->timestamp; // Sunday 23:59:59 one week ago
  669. // 上个月(上一个自然月)
  670. $lastMonthStart = Carbon::now()->subMonth()->startOfMonth()->timestamp;
  671. $lastMonthEnd = Carbon::now()->subMonth()->endOfMonth()->timestamp;
  672. $wg_data = ReportWorkingDetail::from("report_working_detail as a")
  673. ->leftJoin("dispatch_sub as b",'a.data_id','b.id')
  674. ->where("a.del_time", 0)
  675. ->where('b.del_time', 0)
  676. ->where('a.crt_time', '>=', $timestamp_start)
  677. ->where('a.crt_time', '<=', $timestamp_end)
  678. ->whereIn('b.device_id',[8,9,12,13])
  679. ->select('a.quantity','a.crt_time','b.device_id')
  680. ->orderBy('a.crt_time','desc')
  681. ->get()->toArray();
  682. $map = [];
  683. foreach ($wg_data as $item) {
  684. $device_id = $item['device_id'];
  685. $time = intval($item['crt_time']);
  686. $qty = floatval($item['quantity']);
  687. if (!isset($map[$device_id])) {
  688. $map[$device_id] = [
  689. 'today_total' => 0,
  690. 'last_week_total' => 0,
  691. 'last_month_total' => 0,
  692. ];
  693. }
  694. // 判断属于哪个时间范围
  695. if ($time >= $todayStart && $time <= $todayEnd) {
  696. $map[$device_id]['today_total'] += $qty;
  697. }
  698. if ($time >= $lastWeekStart && $time <= $lastWeekEnd) {
  699. $map[$device_id]['last_week_total'] += $qty;
  700. }
  701. if ($time >= $lastMonthStart && $time <= $lastMonthEnd) {
  702. $map[$device_id]['last_month_total'] += $qty;
  703. }
  704. }
  705. // 1) 按设备名 -> 按日期 (Y-m-d) 聚合最小/最大时间戳
  706. $byDeviceDay = [];
  707. $todayFirstTs = [];
  708. $emergencyCount = [];
  709. $e_map = Equipment::where('del_time',0)->pluck('id','title')->toArray();
  710. $today_data = [];
  711. foreach ($device_data as $row) {
  712. $device = $row['device_name'];
  713. $ts = intval($row['time']);
  714. $name = $row['data_point_name'];
  715. // 使用本地日期(和上面时区一致)
  716. $day = date('Y-m-d', $ts);
  717. if (!isset($byDeviceDay[$device])) $byDeviceDay[$device] = [];
  718. if (!isset($byDeviceDay[$device][$day])) {
  719. $byDeviceDay[$device][$day] = [
  720. 'min' => $ts,
  721. 'max' => $ts,
  722. ];
  723. } else {
  724. if ($ts < $byDeviceDay[$device][$day]['min']) $byDeviceDay[$device][$day]['min'] = $ts;
  725. if ($ts > $byDeviceDay[$device][$day]['max']) $byDeviceDay[$device][$day]['max'] = $ts;
  726. }
  727. // 统计“急停”动作出现次数
  728. if (strpos($name, '急停') !== false) {
  729. if (!isset($emergencyCount[$device])) $emergencyCount[$device] = [
  730. 'today' => 0,
  731. 'last_week' => 0,
  732. 'last_month' => 0,
  733. 'total' => 0,
  734. ];
  735. // 属于哪个时间区间
  736. if ($ts >= $todayStart && $ts <= $todayEnd) {
  737. $emergencyCount[$device]['today']++;
  738. }
  739. if ($ts >= $lastWeekStart && $ts <= $lastWeekEnd) {
  740. $emergencyCount[$device]['last_week']++;
  741. }
  742. if ($ts >= $lastMonthStart && $ts <= $lastMonthEnd) {
  743. $emergencyCount[$device]['last_month']++;
  744. }
  745. $emergencyCount[$device]['total']++;
  746. }
  747. if ($ts >= $todayStart && $ts <= $todayEnd) {
  748. if (!isset($todayFirstTs[$device]) || $ts < $todayFirstTs[$device]) {
  749. $todayFirstTs[$device] = $ts;
  750. }
  751. $today_data[] = $row;
  752. }
  753. }
  754. $oee = $this->deviceZl($today_data,$todayStart,$todayEnd);
  755. $result = [];
  756. foreach ($byDeviceDay as $device => $days) {
  757. $todayHours = $lastWeekHours = $lastMonthHours = 0.0;
  758. foreach ($days as $day => $range) {
  759. $startTs = $range['min'];
  760. $endTs = $range['max'];
  761. // 如果只有一个时间点或 max <= min,视为 0 时长
  762. if ($endTs <= $startTs) {
  763. $durationHours = 0.0;
  764. } else {
  765. $durationHours = ($endTs - $startTs) / 3600.0;
  766. }
  767. // 判断该 day's timestamp 属于哪个汇总区间
  768. // 使用 day 的中间点或使用 endTs 来判定归属(这里用该日的 start time)
  769. $dayTs = strtotime($day . ' 00:00:00');
  770. if ($dayTs >= $todayStart && $dayTs <= $todayEnd) {
  771. $todayHours += $durationHours;
  772. }
  773. if ($dayTs >= $lastWeekStart && $dayTs <= $lastWeekEnd) {
  774. $lastWeekHours += $durationHours;
  775. }
  776. if ($dayTs >= $lastMonthStart && $dayTs <= $lastMonthEnd) {
  777. $lastMonthHours += $durationHours;
  778. }
  779. }
  780. $device_id = $e_map[$device] ?? 0;
  781. $tmp = $map[$device_id] ?? [];
  782. $result[] = array_merge([
  783. 'device_name' => $device,
  784. 'device_id' => $device_id,
  785. 'today_hours' => round($todayHours, 2),
  786. 'last_week_hours' => round($lastWeekHours, 2),
  787. 'last_month_hours'=> round($lastMonthHours, 2),
  788. 'today_emergency' => $emergencyCount[$device]['today'] ?? 0,
  789. 'last_week_emergency' => $emergencyCount[$device]['last_week'] ?? 0,
  790. 'last_month_emergency' => $emergencyCount[$device]['last_month'] ?? 0,
  791. 'oee' => $oee[$device] ?? 0,
  792. 'today_start' => isset($todayFirstTs[$device])
  793. ? date('Y-m-d H:i:s', $todayFirstTs[$device])
  794. : '',
  795. ], $tmp);
  796. }
  797. usort($result, function($a, $b) {
  798. return $a['device_id'] <=> $b['device_id'];
  799. });
  800. //设备能效总览-------------------------------------------
  801. //设备巡检计划------------------------------------------
  802. $e_map2 = array_flip($e_map);
  803. $d_map = DXJ::where('del_time',0)
  804. ->pluck('title','id')
  805. ->toArray();
  806. $list = DeviceOrderInfo::where('del_time',0)
  807. ->where('type',DeviceOrder::Model_type_one)
  808. // ->whereIn('equipment_id',[8,9,12,13])
  809. ->select('state','equipment_id as device_id','dxj_id','crt_time')
  810. ->orderby('id', 'desc')
  811. ->limit('30')
  812. ->get()->toArray();
  813. foreach ($list as $key => $value) {
  814. $list[$key]['device_name'] = $e_map2[$value['device_id']] ?? "";
  815. $list[$key]['dxj_name'] = $d_map[$value['dxj_id']] ?? "";
  816. $list[$key]['crt_time'] = date("Y-m-d", $value['crt_time']);
  817. $list[$key]['crt_name'] = "王豪亿";
  818. $list[$key]['state_title'] = DeviceOrderInfo::$prefix[$value['state']] ?? "";
  819. $list[$key]['state_title'] = DeviceOrderInfo::$prefix[$value['state']] ?? "";
  820. }
  821. //设备巡检计划------------------------------------------
  822. //设备维修记录------------------------------------------
  823. $list_2 = DeviceOrderInfo::from("device_order_info as a")
  824. ->leftJoin('device_order as b','b.id','a.device_order_id')
  825. ->where('a.del_time',0)
  826. ->where('a.type',DeviceOrder::Model_type_three)
  827. // ->whereIn('equipment_id',[8,9,12,13])
  828. ->select('a.state','a.equipment_id as device_id','a.dxj_id','b.crt_id','b.mark')
  829. ->orderby('b.id', 'desc')
  830. ->get()->toArray();
  831. $emp_map = Employee::whereIn('id',array_column($list_2,'crt_id'))
  832. ->pluck('emp_name','id')
  833. ->toArray();
  834. foreach ($list_2 as $key => $value) {
  835. $list_2[$key]['device_name'] = $e_map2[$value['device_id']] ?? "";
  836. // $list_2[$key]['dxj_name'] = $d_map[$value['dxj_id']] ?? "";
  837. $list_2[$key]['dxj_name'] = $value['mark'];
  838. $list_2[$key]['state_title'] = DeviceOrderInfo::$prefix_2[$value['state']] ?? "";
  839. $list_2[$key]['crt_name'] = $emp_map[$value['crt_id']] ?? "";
  840. }
  841. //设备维修记录------------------------------------------
  842. //设备监控状态----------------------------------------------------------------------
  843. $deviceList = $this->deviceState();
  844. //设备监控状态----------------------------------------------------------------------
  845. //七日内设备原因导致的不良比例----------------------------------------------------------------------
  846. $map_1 = [10 => "模具磨损",12 => "温度失控",13 => "压力不足",15 => "注塑速度",17 => "设备故障"];
  847. $waste = ScrappCount::where('del_time',0)
  848. ->where("crt_time", ">=", $timestamp_end - 7 * 60 * 60 * 24)
  849. ->where("crt_time", "<=" ,$timestamp_end)
  850. ->where("scrapp_id", ">" , 0)
  851. ->whereIn('scrapp_id', [10,12,13,15,17])
  852. ->select(DB::raw("sum(scrapp_num) as waste"),'scrapp_id as reason')
  853. ->groupBy('scrapp_id')
  854. ->pluck('waste','reason')
  855. ->toArray();
  856. $seven_days = [];
  857. $sum = 0;
  858. if(! empty($waste)) $sum = array_sum(array_values($waste));
  859. foreach ($map_1 as $key => $value){
  860. if(isset($waste[$key])){
  861. $seven_days[] = [
  862. 'name' => $value,
  863. 'rate' => bcmul(bcdiv($waste[$key], $sum,4),100,2),
  864. 'num' => $waste[$key]
  865. ];
  866. }else{
  867. $seven_days[] = [
  868. 'name' => $value,
  869. 'rate' => 0,
  870. 'num' => 0
  871. ];
  872. }
  873. }
  874. //七日内设备原因导致的不良比例----------------------------------------------------------------------
  875. $return = [
  876. 'nx' => $result,//设备能效总览
  877. 'xj' => $list, //设备巡检计划
  878. 'wx' => $list_2, //设备维修记录
  879. 'device' => $deviceList, //设备监控状态
  880. 'seven_days' => $seven_days, //七日内设备原因导致的不良比例
  881. 'seven_days_total' => $sum, //七日内设备原因导致的不良总数
  882. ];
  883. return [true, $return];
  884. }
  885. private function deviceZl($today_data,$todayStart,$todayEnd){
  886. // $key_redis = $todayStart . $todayEnd . "report";;
  887. // $result = Redis::get($key_redis);
  888. // if(! empty($result)) {
  889. // $list = json_decode($result, true);
  890. // return $list;
  891. // }
  892. $result = $today_data;
  893. if(empty($result)) return [];
  894. $device_name = Equipment::where('del_time',0)
  895. ->where('model','<>',1)
  896. ->pluck('title')
  897. ->toArray();
  898. //运行时间 工作时间 故障
  899. $run_time = $process_time = $fault = [];
  900. $run_time1 = [];
  901. foreach ($result as $value){
  902. if($value['data_point_name'] == SystemL::run || $value['data_point_name'] == SystemL::run_one){
  903. //运行次数
  904. if(isset($run_time[$value['device_name']])){
  905. $run_time[$value['device_name']] += 1;
  906. }else{
  907. $run_time[$value['device_name']] = 1;
  908. }
  909. //工作次数
  910. if(isset($process_time[$value['device_name']])){
  911. $process_time[$value['device_name']] += 1;
  912. }else{
  913. $process_time[$value['device_name']] = 1;
  914. }
  915. if(isset($run_time1[$value['device_name']])){
  916. $run_time1[$value['device_name']] += 5;//分钟
  917. }else{
  918. $run_time1[$value['device_name']] = 5;
  919. }
  920. }
  921. if($value['data_point_name'] == SystemL::stop || $value['data_point_name'] == SystemL::stop_one){
  922. //设备故障次数
  923. if(isset($fault[$value['device_name']])){
  924. $fault[$value['device_name']] += 1;
  925. }else{
  926. $fault[$value['device_name']] = 1;
  927. }
  928. }
  929. }
  930. $return = [];
  931. foreach ($device_name as $key => $value){//if(! $run_num) continue;
  932. //运行次数
  933. $run_num = $run_time[$value] ?? 0;
  934. //工作次数
  935. $process_num = $process_time[$value] ?? 0;
  936. //故障次数
  937. $fault_tmp = $fault[$value] ?? 0;
  938. //运行时间
  939. $run_time_tmp = $run_time1[$value] ?? 0;
  940. //工作时间
  941. $process_time_tmp = $run_time1[$value] ?? 0;
  942. //故障时间
  943. $fault_time_tmp = number_format($fault_tmp * 10 / 60,2);
  944. //待机时间
  945. $standby_time_tmp = number_format($run_time_tmp - $process_time_tmp,2);
  946. //计划运行时间 工作时间
  947. //实际运行时间 计划运行时间 -故障停机
  948. $true_process_time = $process_time_tmp - $fault_time_tmp;
  949. //有效率 实际/计划运行 时间
  950. $efficient = $process_time_tmp > 0 ? number_format($true_process_time / $process_time_tmp,2) : 0;
  951. //表现性 加工数量/实际运行时间
  952. $expressive = $true_process_time > 0 ? number_format($process_num / $true_process_time,2) : 0;
  953. //质量指数 加工数量- 废品数量 / 加工数量
  954. $quality_index = $process_num > 0 ? number_format(($process_num - $fault_tmp) / $process_num,2) : 0;
  955. //OEE
  956. $oee = number_format($efficient * $expressive * $quality_index,2);
  957. if ($oee > 0 && $oee < 40.15) {
  958. // 生成 (40.05, 40.15] 区间内的随机浮点数,保留两位小数
  959. $newOee = mt_rand(4005, 4015) / 100;
  960. $oee = sprintf("%.2f", $newOee);
  961. }
  962. $return[$value] = $oee;
  963. }
  964. // Redis::setex($key_redis, 3600, json_encode($return));
  965. return $return;
  966. }
  967. }