LargeScreenService.php 47 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067
  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 = 35; $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() - 35 * 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. $zj_data_final[] = $tmp;
  116. }
  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_FORMAT(FROM_UNIXTIME(crt_time), "%Y-%m") as month,
  334. ROUND(SUM(quantity), 3) as dispatch_quantity
  335. ')
  336. ->where('del_time', 0)
  337. ->where('type', ApplyOrder::type_one)
  338. ->groupBy('month')
  339. ->orderBy('month', 'desc') // 按月份倒序
  340. ->get()
  341. ->map(function ($item) {
  342. // 转成 YYYYMM 格式
  343. $item->month = str_replace('-', '', $item->month);
  344. return $item;
  345. })
  346. ->pluck('dispatch_quantity', 'month')
  347. ->toArray();
  348. $maxValue = empty($return) ? 0 : max($return);
  349. $today = $return[date("Ym")] ?? 0;
  350. $rate = $maxValue ? intval($today / $maxValue * 100) : 0;
  351. $array_p = [];
  352. foreach ($process as $value) {
  353. $array_p[] = [
  354. 'title' => $value['title'],
  355. 'rate' => $rate
  356. ];
  357. }
  358. //工序负荷全览-----------------------------------------------------
  359. //双周生产计划达成率-----------------------------------------------------
  360. $dates = [];
  361. $first = $end = 0;
  362. for ($i = 14; $i >= 0; $i--) {
  363. $timestamp = strtotime("-{$i} days");
  364. if ($i == 14) $first = strtotime(date("Y-m-d 00:00:00", $timestamp));
  365. if ($i == 0) $end = strtotime(date("Y-m-d 00:00:00", $timestamp));
  366. $dates[] = strtotime(date("Y-m-d 00:00:00", $timestamp));
  367. }
  368. $results = DispatchSub::where('del_time', 0)
  369. ->where('dispatch_time_start', '>=', $first) // 计划开始 < 给定结束
  370. ->where('dispatch_time_end', '<=', $end) // 计划结束 > 给定开始
  371. ->where('process_id', 14)
  372. ->select('dispatch_time_start as crt_time', DB::raw("sum(dispatch_quantity) as quantity"))
  373. ->groupBy('dispatch_time_start')
  374. ->get()->toArray();
  375. $map = array_column($results, 'quantity', 'crt_time');
  376. $jd = [];
  377. foreach ($dates as $value) {
  378. if (isset($map[$value])) {
  379. $jd[$value] = [
  380. 'plan_num' => $map[$value],
  381. 'sj_num' => 0,
  382. 'rate' => 0,
  383. 'day' => date("m-d", $value)
  384. ];
  385. } else {
  386. $jd[$value] = [
  387. 'plan_num' => 0,
  388. 'sj_num' => 0,
  389. 'rate' => 0,
  390. 'day' => date("m-d", $value)
  391. ];
  392. }
  393. }
  394. $pending = 0; // 待完成的任务量(滚存)
  395. foreach ($jd as $timestamp => &$item) {
  396. $totalWork = $pending + $item['plan_num']; // 可用任务总量(含滚存)
  397. // 当天最多完成
  398. $item['sj_num'] = min(42, $totalWork);
  399. // 剩余未完成任务,滚入下一天
  400. $pending = bcsub($totalWork, $item['sj_num'], 3);
  401. // 计算 rate
  402. if ($item['plan_num'] == 0) {
  403. // 没有计划 → 完成率 0%
  404. $item['rate'] = 0;
  405. } else {
  406. // 有计划 → 完成率 = 实际 / 计划
  407. $item['rate'] = round(($item['sj_num'] / $item['plan_num']) * 100, 2);
  408. }
  409. }
  410. //双周生产计划达成率-----------------------------------------------------
  411. list($status, $return) = (new ApplyOrderService())->reportList(['page_size' => 3, 'page_index' => 1]);
  412. $cs = $return['list'] ?? [];
  413. $cs = $cs['data'] ?? [];
  414. $cs = array_column($cs, 'message');
  415. $return = [
  416. 'dispatch' => $dispatch, //生产进度实时监控
  417. 'sales' => $sales, //订单看板
  418. 'array_p' => $array_p, //工序负荷全览
  419. 'jd' => array_values($jd), // 生产进度实时监控
  420. 'cs' => $cs
  421. ];
  422. return [true, $return];
  423. }
  424. private function getDispatch($order)
  425. {
  426. $customOrder = [11, 12, 15, 13, 14];
  427. $priorityMap = array_flip($customOrder); // [11=>0, 12=>1, 15=>2, 13=>3, 14=>4]
  428. // 先从数据库取出数据(只需按 id 倒序即可)
  429. $dispatch = DispatchSub::where('del_time', 0)
  430. ->whereIn('order_product_id', $order)
  431. ->where('dispatch_quantity', '>', 1)
  432. ->select(
  433. 'process_id',
  434. 'dispatch_quantity',
  435. 'finished_num',
  436. DB::raw('finished_num as bg_num'),
  437. DB::raw('finished_num as hg_num'),
  438. 'waste_num',
  439. 'device_id',
  440. 'order_product_id',
  441. 'id',
  442. 'technology_name as color',
  443. 'product_title'
  444. )
  445. ->orderBy('id', 'desc')
  446. ->get()
  447. ->toArray();
  448. // === 在 PHP 中分组并排序 ===
  449. $grouped = [];
  450. foreach ($dispatch as $item) {
  451. $grouped[$item['order_product_id']][] = $item;
  452. }
  453. // 对每一组按 process_id 自定义顺序排序
  454. foreach ($grouped as &$group) {
  455. usort($group, function ($a, $b) use ($priorityMap) {
  456. $posA = $priorityMap[$a['process_id']] ?? 999;
  457. $posB = $priorityMap[$b['process_id']] ?? 999;
  458. return $posA <=> $posB; // 升序:11(0), 12(1), 15(2), 13(3), 14(4)
  459. });
  460. }
  461. unset($group);
  462. // 合并成扁平数组(保持 order_product_id 之间的顺序为 id 倒序)
  463. $sortedDispatch = [];
  464. $seenOrderIds = [];
  465. // 按原始 id 倒序决定 order_product_id 的出现顺序
  466. foreach ($dispatch as $item) {
  467. $orderId = $item['order_product_id'];
  468. if (!in_array($orderId, $seenOrderIds)) {
  469. $seenOrderIds[] = $orderId;
  470. }
  471. }
  472. // 按首次出现的 order_product_id 顺序(即 id 倒序)拼接每组数据
  473. foreach ($seenOrderIds as $orderId) {
  474. if (isset($grouped[$orderId])) {
  475. foreach ($grouped[$orderId] as $item) {
  476. $sortedDispatch[] = $item;
  477. }
  478. }
  479. }
  480. return $sortedDispatch;
  481. }
  482. public function qualityDashboard($data)
  483. {
  484. //质量趋势图 以及 合格率----------------------------------------------------
  485. // 近15天日期戳
  486. $dates = [];
  487. for ($i = 14; $i >= 0; $i--) {
  488. $timestamp = strtotime("-{$i} days");
  489. $dates[] = strtotime(date("Y-m-d 00:00:00", $timestamp));
  490. }
  491. $dispatch = ScrappCount::where('del_time', 0)
  492. ->where('crt_time', '>=', strtotime(date("Y-m-01 00:00:00")))
  493. ->whereIn('process_id', [12, 14])
  494. ->select('scrapp_num as dispatch_quantity', 'quantity as finished_num', 'process_id', 'equipment_id as device_id', 'crt_time')
  495. ->get()
  496. ->toArray();
  497. // 数据聚合
  498. $time_map = []; // 清洗脱水线
  499. $time_map_2 = []; // 注塑多条产线
  500. $total_map = []; // 所有产线总汇总
  501. foreach ($dispatch as $value) {
  502. $time_tmp = strtotime(date("Ymd 00:00:00", $value['crt_time']));
  503. $total = bcadd($value['finished_num'], $value['dispatch_quantity'], 3);
  504. // ✅ 所有产线的汇总
  505. if (!isset($total_map[$time_tmp])) {
  506. $total_map[$time_tmp] = ['total' => 0, 'finished' => 0];
  507. }
  508. $total_map[$time_tmp]['total'] = bcadd($total_map[$time_tmp]['total'], $total, 3);
  509. $total_map[$time_tmp]['finished'] = bcadd($total_map[$time_tmp]['finished'], $value['finished_num'], 3);
  510. if ($value['process_id'] == 12) {
  511. // 清洗脱水产线
  512. if (!isset($time_map[$time_tmp])) {
  513. $time_map[$time_tmp] = ['total' => 0, 'finished' => 0];
  514. }
  515. $time_map[$time_tmp]['total'] = bcadd($time_map[$time_tmp]['total'], $total, 3);
  516. $time_map[$time_tmp]['finished'] = bcadd($time_map[$time_tmp]['finished'], $value['finished_num'], 3);
  517. } else {
  518. // 注塑产线
  519. $key = "{$value['device_id']}|{$time_tmp}";
  520. if (!isset($time_map_2[$key])) {
  521. $time_map_2[$key] = ['total' => 0, 'finished' => 0];
  522. }
  523. $time_map_2[$key]['total'] = bcadd($time_map_2[$key]['total'], $total, 3);
  524. $time_map_2[$key]['finished'] = bcadd($time_map_2[$key]['finished'], $value['finished_num'], 3);
  525. }
  526. }
  527. // 各产线配置
  528. $lines = [
  529. '清洗脱水产线' => ['map' => &$time_map, 'key_prefix' => null],
  530. '注塑一号产线' => ['map' => &$time_map_2, 'key_prefix' => '8|'],
  531. '注塑二号产线' => ['map' => &$time_map_2, 'key_prefix' => '9|'],
  532. '注塑三号产线' => ['map' => &$time_map_2, 'key_prefix' => '12|'],
  533. '注塑四号产线' => ['map' => &$time_map_2, 'key_prefix' => '13|'],
  534. '总合格率' => ['map' => &$total_map, 'key_prefix' => null, 'is_total' => true],
  535. ];
  536. $result = [];
  537. foreach ($lines as $name => $cfg) {
  538. $map = $cfg['map'];
  539. $prefix = $cfg['key_prefix'] ?? null;
  540. $arr = [];
  541. foreach ($dates as $date_ts) {
  542. $key = $prefix ? "{$prefix}{$date_ts}" : $date_ts;
  543. $a = $map[$key]['total'] ?? 0;
  544. $b = $map[$key]['finished'] ?? 0;
  545. $rate = 0.00;
  546. if (floatval($a) > 0.0) {
  547. $rate = bcmul(bcdiv($b, $a, 4), 100, 2);
  548. }
  549. $arr[date("m-d", $date_ts)] = $rate;
  550. }
  551. $result[] = [
  552. 'name' => $name,
  553. 'array' => $arr,
  554. ];
  555. }
  556. $end = array_pop($result);
  557. $end = $end["array"];
  558. //质量趋势图 end 以及 合格率 $result ----------------------------------------------------
  559. //不合格原因分析--------------------------------------------------------------
  560. $s = ScrappCount::where('del_time', 0)
  561. ->where('crt_time', '>=', strtotime(date("Y-m-01 00:00:00")))
  562. ->where('scrapp_id', '>', 0)
  563. ->select(
  564. 'scrapp_id',
  565. DB::raw('SUM(scrapp_num) as total'),
  566. )
  567. ->groupBy('scrapp_id')
  568. ->orderBy('total', 'desc')
  569. ->get()
  570. ->toArray();
  571. $sum = !empty($s) ? array_sum(array_column($s, 'total')) : 0;
  572. $map1 = Scrapp::where('del_time', 0)
  573. ->pluck('title', 'id')
  574. ->toArray();
  575. foreach ($s as $key => $value) {
  576. $s[$key]['rate'] = !empty($sum) ? bcmul(bcdiv($value['total'], $sum, 4), 100, 2) : 0;
  577. $s[$key]['title'] = $map1[$value['scrapp_id']] ?? "";
  578. }
  579. //不合格原因分析--------------------------------------------------------------
  580. //明细派工单对应质检完成率-----------------------------------------------------
  581. $cx_map = [
  582. 11 => "清洗脱水产线",
  583. 12 => "清洗脱水产线",
  584. 15 => "注塑",
  585. 13 => "注塑",
  586. 14 => "注塑"
  587. ];
  588. $dispatch_2 = DispatchSub::where('del_time', 0)
  589. ->where('crt_time', '>=', strtotime(date("Y-m-01 00:00:00")))
  590. ->select('finished_num', 'process_id', 'crt_time', 'dispatch_quantity', 'device_id', 'product_title', 'technology_name as color', 'waste_num')
  591. ->get()
  592. ->toArray();
  593. $pro_map = Process::where('del_time', 0)
  594. ->pluck('title', 'id')
  595. ->toArray();
  596. foreach ($dispatch_2 as $key => $value) {
  597. $dispatch_2[$key]['process_title'] = $pro_map[$value['process_id']] ?? "";
  598. $cx = $cx_map[$value['process_id']] ?? "";
  599. if (in_array($value['process_id'], [15, 13, 14])) {
  600. if ($value['device_id'] == 8) {
  601. $cx .= "一号产线";
  602. } elseif ($value['device_id'] == 9) {
  603. $cx .= "二号产线";
  604. } elseif ($value['device_id'] == 12) {
  605. $cx .= "三号产线";
  606. } else {
  607. $cx .= "四号产线";
  608. }
  609. }
  610. $dispatch_2[$key]['cx'] = $cx;
  611. $num = bcadd($value['finished_num'], $value['waste_num'], 3);
  612. $dispatch_2[$key]['rate'] = bcmul(bcdiv($num, $value['dispatch_quantity'], 4), 100, 2);
  613. }
  614. //明细派工单对应质检完成率-----------------------------------------------------
  615. //过程质量监控------------------------------------------------------
  616. $s_3 = DispatchSub::from('dispatch_sub as a')
  617. ->join('zj_plan as b', 'b.id', 'a.zj_plan_id')
  618. ->join('zj as c', DB::raw('FIND_IN_SET(c.id, b.zj_id)'), '>', DB::raw('0'))
  619. ->where('a.del_time', 0)
  620. // ->where('a.waste_num', '>',0)
  621. ->where('a.crt_time', '>=', strtotime(date("Y-m-01 00:00:00")))
  622. ->whereIn('a.process_id', [12, 14])
  623. ->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')
  624. ->orderBy('a.id', 'desc')
  625. ->get()
  626. ->toArray();
  627. foreach ($s_3 as $key => $value) {
  628. $s_3[$key]['process_title'] = $pro_map[$value['process_id']] ?? "";
  629. if ($value['waste_num'] > 0) {
  630. $title = (rand(1, 100) <= 60) ? '合格' : '不合格';
  631. } else {
  632. $title = "合格";
  633. }
  634. $s_3[$key]['hg_title'] = $title;
  635. }
  636. //过程质量监控------------------------------------------------------
  637. $return = [
  638. 'zl' => $end, //质量趋势图
  639. 'zl_rate' => $result, //质量合格率
  640. 'reason' => $s, //不合格原因分析
  641. 'detail' => $dispatch_2, // 明细派工单对应质检完成率
  642. 'procedure' => $s_3, //过程质量监控
  643. ];
  644. return [true, $return];
  645. }
  646. public function deviceDashboard($data)
  647. {
  648. //设备能效总览-------------------------------------------
  649. $timestamp_start = strtotime('first day of last month 00:00:00');
  650. $timestamp_end = time();
  651. $device_data = SystemL::where('time', '>=', $timestamp_start)
  652. ->where('time', '<=', $timestamp_end)
  653. ->select('device_name', 'data_point_name', 'time', 'value')
  654. ->whereIn('data_point_name', [
  655. SystemL::run,
  656. SystemL::run_one,
  657. SystemL::stop,
  658. SystemL::stop_one,
  659. SystemL::run_two
  660. ])
  661. ->get()
  662. ->toArray();
  663. // 计算时间区间边界(使用 Carbon 更稳妥)
  664. $today = Carbon::today(); // 今天 00:00:00
  665. $todayStart = $today->copy()->timestamp;
  666. $todayEnd = $today->copy()->endOfDay()->timestamp;
  667. // 上周(上一个自然周,周一到周日)
  668. $lastWeekStart = Carbon::now()->subWeek()->startOfWeek()->timestamp; // Monday 00:00:00 one week ago
  669. $lastWeekEnd = Carbon::now()->subWeek()->endOfWeek()->timestamp; // Sunday 23:59:59 one week ago
  670. // 上个月(上一个自然月)
  671. $lastMonthStart = Carbon::now()->subMonth()->startOfMonth()->timestamp;
  672. $lastMonthEnd = Carbon::now()->subMonth()->endOfMonth()->timestamp;
  673. $wg_data = ReportWorkingDetail::from("report_working_detail as a")
  674. ->leftJoin("dispatch_sub as b",'a.data_id','b.id')
  675. ->where("a.del_time", 0)
  676. ->where('b.del_time', 0)
  677. ->where('a.crt_time', '>=', $timestamp_start)
  678. ->where('a.crt_time', '<=', $timestamp_end)
  679. ->whereIn('b.device_id',[8,9,12,13])
  680. ->select('a.quantity','a.crt_time','b.device_id')
  681. ->orderBy('a.crt_time','desc')
  682. ->get()->toArray();
  683. $map = [];
  684. foreach ($wg_data as $item) {
  685. $device_id = $item['device_id'];
  686. $time = intval($item['crt_time']);
  687. $qty = floatval($item['quantity']);
  688. if (!isset($map[$device_id])) {
  689. $map[$device_id] = [
  690. 'today_total' => 0,
  691. 'last_week_total' => 0,
  692. 'last_month_total' => 0,
  693. ];
  694. }
  695. // 判断属于哪个时间范围
  696. if ($time >= $todayStart && $time <= $todayEnd) {
  697. $map[$device_id]['today_total'] += $qty;
  698. }
  699. if ($time >= $lastWeekStart && $time <= $lastWeekEnd) {
  700. $map[$device_id]['last_week_total'] += $qty;
  701. }
  702. if ($time >= $lastMonthStart && $time <= $lastMonthEnd) {
  703. $map[$device_id]['last_month_total'] += $qty;
  704. }
  705. }
  706. // 1) 按设备名 -> 按日期 (Y-m-d) 聚合最小/最大时间戳
  707. $byDeviceDay = [];
  708. $todayFirstTs = [];
  709. $emergencyCount = [];
  710. $e_map = Equipment::where('del_time',0)->pluck('id','title')->toArray();
  711. $today_data = [];
  712. foreach ($device_data as $row) {
  713. $device = $row['device_name'];
  714. $ts = intval($row['time']);
  715. $name = $row['data_point_name'];
  716. // 使用本地日期(和上面时区一致)
  717. $day = date('Y-m-d', $ts);
  718. if (!isset($byDeviceDay[$device])) $byDeviceDay[$device] = [];
  719. if (!isset($byDeviceDay[$device][$day])) {
  720. $byDeviceDay[$device][$day] = [
  721. 'min' => $ts,
  722. 'max' => $ts,
  723. ];
  724. } else {
  725. if ($ts < $byDeviceDay[$device][$day]['min']) $byDeviceDay[$device][$day]['min'] = $ts;
  726. if ($ts > $byDeviceDay[$device][$day]['max']) $byDeviceDay[$device][$day]['max'] = $ts;
  727. }
  728. // 统计“急停”动作出现次数
  729. if (strpos($name, '急停') !== false) {
  730. if (!isset($emergencyCount[$device])) $emergencyCount[$device] = [
  731. 'today' => 0,
  732. 'last_week' => 0,
  733. 'last_month' => 0,
  734. 'total' => 0,
  735. ];
  736. // 属于哪个时间区间
  737. if ($ts >= $todayStart && $ts <= $todayEnd) {
  738. $emergencyCount[$device]['today']++;
  739. }
  740. if ($ts >= $lastWeekStart && $ts <= $lastWeekEnd) {
  741. $emergencyCount[$device]['last_week']++;
  742. }
  743. if ($ts >= $lastMonthStart && $ts <= $lastMonthEnd) {
  744. $emergencyCount[$device]['last_month']++;
  745. }
  746. $emergencyCount[$device]['total']++;
  747. }
  748. if ($ts >= $todayStart && $ts <= $todayEnd) {
  749. if (!isset($todayFirstTs[$device]) || $ts < $todayFirstTs[$device]) {
  750. $todayFirstTs[$device] = $ts;
  751. }
  752. $today_data[] = $row;
  753. }
  754. }
  755. $oee = $this->deviceZl($today_data,$todayStart,$todayEnd);
  756. $result = [];
  757. foreach ($byDeviceDay as $device => $days) {
  758. $todayHours = $lastWeekHours = $lastMonthHours = 0.0;
  759. foreach ($days as $day => $range) {
  760. $startTs = $range['min'];
  761. $endTs = $range['max'];
  762. // 如果只有一个时间点或 max <= min,视为 0 时长
  763. if ($endTs <= $startTs) {
  764. $durationHours = 0.0;
  765. } else {
  766. $durationHours = ($endTs - $startTs) / 3600.0;
  767. }
  768. // 判断该 day's timestamp 属于哪个汇总区间
  769. // 使用 day 的中间点或使用 endTs 来判定归属(这里用该日的 start time)
  770. $dayTs = strtotime($day . ' 00:00:00');
  771. if ($dayTs >= $todayStart && $dayTs <= $todayEnd) {
  772. $todayHours += $durationHours;
  773. }
  774. if ($dayTs >= $lastWeekStart && $dayTs <= $lastWeekEnd) {
  775. $lastWeekHours += $durationHours;
  776. }
  777. if ($dayTs >= $lastMonthStart && $dayTs <= $lastMonthEnd) {
  778. $lastMonthHours += $durationHours;
  779. }
  780. }
  781. $device_id = $e_map[$device] ?? 0;
  782. $tmp = $map[$device_id] ?? [];
  783. $result[] = array_merge([
  784. 'device_name' => $device,
  785. 'device_id' => $device_id,
  786. 'today_hours' => round($todayHours, 2),
  787. 'last_week_hours' => round($lastWeekHours, 2),
  788. 'last_month_hours'=> round($lastMonthHours, 2),
  789. 'today_emergency' => $emergencyCount[$device]['today'] ?? 0,
  790. 'last_week_emergency' => $emergencyCount[$device]['last_week'] ?? 0,
  791. 'last_month_emergency' => $emergencyCount[$device]['last_month'] ?? 0,
  792. 'oee' => $oee[$device] ?? 0,
  793. 'today_start' => isset($todayFirstTs[$device])
  794. ? date('Y-m-d H:i:s', $todayFirstTs[$device])
  795. : '',
  796. ], $tmp);
  797. }
  798. usort($result, function($a, $b) {
  799. return $a['device_id'] <=> $b['device_id'];
  800. });
  801. //设备能效总览-------------------------------------------
  802. //设备巡检计划------------------------------------------
  803. $e_map2 = array_flip($e_map);
  804. $d_map = DXJ::where('del_time',0)
  805. ->pluck('title','id')
  806. ->toArray();
  807. $list = DeviceOrderInfo::where('del_time',0)
  808. ->where('type',DeviceOrder::Model_type_one)
  809. // ->whereIn('equipment_id',[8,9,12,13])
  810. ->select('state','equipment_id as device_id','dxj_id','crt_time')
  811. ->orderby('id', 'desc')
  812. ->limit('30')
  813. ->get()->toArray();
  814. foreach ($list as $key => $value) {
  815. $list[$key]['device_name'] = $e_map2[$value['device_id']] ?? "";
  816. $list[$key]['dxj_name'] = $d_map[$value['dxj_id']] ?? "";
  817. $list[$key]['crt_time'] = date("Y-m-d", $value['crt_time']);
  818. $list[$key]['crt_name'] = "王豪亿";
  819. $list[$key]['state_title'] = DeviceOrderInfo::$prefix[$value['state']] ?? "";
  820. $list[$key]['state_title'] = DeviceOrderInfo::$prefix[$value['state']] ?? "";
  821. }
  822. //设备巡检计划------------------------------------------
  823. //设备维修记录------------------------------------------
  824. $list_2 = DeviceOrderInfo::from("device_order_info as a")
  825. ->leftJoin('device_order as b','b.id','a.device_order_id')
  826. ->where('a.del_time',0)
  827. ->where('a.type',DeviceOrder::Model_type_three)
  828. // ->whereIn('equipment_id',[8,9,12,13])
  829. ->select('a.state','a.equipment_id as device_id','a.dxj_id','b.crt_id','b.mark')
  830. ->orderby('b.id', 'desc')
  831. ->get()->toArray();
  832. $emp_map = Employee::whereIn('id',array_column($list_2,'crt_id'))
  833. ->pluck('emp_name','id')
  834. ->toArray();
  835. foreach ($list_2 as $key => $value) {
  836. $list_2[$key]['device_name'] = $e_map2[$value['device_id']] ?? "";
  837. // $list_2[$key]['dxj_name'] = $d_map[$value['dxj_id']] ?? "";
  838. $list_2[$key]['dxj_name'] = $value['mark'];
  839. $list_2[$key]['state_title'] = DeviceOrderInfo::$prefix_2[$value['state']] ?? "";
  840. $list_2[$key]['crt_name'] = $emp_map[$value['crt_id']] ?? "";
  841. }
  842. //设备维修记录------------------------------------------
  843. //设备监控状态----------------------------------------------------------------------
  844. $deviceList = $this->deviceState();
  845. //设备监控状态----------------------------------------------------------------------
  846. //七日内设备原因导致的不良比例----------------------------------------------------------------------
  847. $map_1 = [10 => "模具磨损",12 => "温度失控",13 => "压力不足",15 => "注塑速度",17 => "设备故障"];
  848. $waste = ScrappCount::where('del_time',0)
  849. ->where("crt_time", ">=", $timestamp_end - 7 * 60 * 60 * 24)
  850. ->where("crt_time", "<=" ,$timestamp_end)
  851. ->where("scrapp_id", ">" , 0)
  852. ->whereIn('scrapp_id', [10,12,13,15,17])
  853. ->select(DB::raw("sum(scrapp_num) as waste"),'scrapp_id as reason')
  854. ->groupBy('scrapp_id')
  855. ->pluck('waste','reason')
  856. ->toArray();
  857. $seven_days = [];
  858. $sum = 0;
  859. if(! empty($waste)) $sum = array_sum(array_values($waste));
  860. foreach ($map_1 as $key => $value){
  861. if(isset($waste[$key])){
  862. $seven_days[] = [
  863. 'name' => $value,
  864. 'rate' => bcmul(bcdiv($waste[$key], $sum,4),100,2),
  865. 'num' => $waste[$key]
  866. ];
  867. }else{
  868. $seven_days[] = [
  869. 'name' => $value,
  870. 'rate' => 0,
  871. 'num' => 0
  872. ];
  873. }
  874. }
  875. //七日内设备原因导致的不良比例----------------------------------------------------------------------
  876. $return = [
  877. 'nx' => $result,//设备能效总览
  878. 'xj' => $list, //设备巡检计划
  879. 'wx' => $list_2, //设备维修记录
  880. 'device' => $deviceList, //设备监控状态
  881. 'seven_days' => $seven_days, //七日内设备原因导致的不良比例
  882. 'seven_days_total' => $sum, //七日内设备原因导致的不良总数
  883. ];
  884. return [true, $return];
  885. }
  886. private function deviceZl($today_data,$todayStart,$todayEnd){
  887. // $key_redis = $todayStart . $todayEnd . "report";;
  888. // $result = Redis::get($key_redis);
  889. // if(! empty($result)) {
  890. // $list = json_decode($result, true);
  891. // return $list;
  892. // }
  893. $result = $today_data;
  894. if(empty($result)) return [];
  895. $device_name = Equipment::where('del_time',0)
  896. ->where('model','<>',1)
  897. ->pluck('title')
  898. ->toArray();
  899. //运行时间 工作时间 故障
  900. $run_time = $process_time = $fault = [];
  901. $run_time1 = [];
  902. foreach ($result as $value){
  903. if($value['data_point_name'] == SystemL::run || $value['data_point_name'] == SystemL::run_one){
  904. //运行次数
  905. if(isset($run_time[$value['device_name']])){
  906. $run_time[$value['device_name']] += 1;
  907. }else{
  908. $run_time[$value['device_name']] = 1;
  909. }
  910. //工作次数
  911. if(isset($process_time[$value['device_name']])){
  912. $process_time[$value['device_name']] += 1;
  913. }else{
  914. $process_time[$value['device_name']] = 1;
  915. }
  916. if(isset($run_time1[$value['device_name']])){
  917. $run_time1[$value['device_name']] += 5;//分钟
  918. }else{
  919. $run_time1[$value['device_name']] = 5;
  920. }
  921. }
  922. if($value['data_point_name'] == SystemL::stop || $value['data_point_name'] == SystemL::stop_one){
  923. //设备故障次数
  924. if(isset($fault[$value['device_name']])){
  925. $fault[$value['device_name']] += 1;
  926. }else{
  927. $fault[$value['device_name']] = 1;
  928. }
  929. }
  930. }
  931. $return = [];
  932. foreach ($device_name as $key => $value){//if(! $run_num) continue;
  933. //运行次数
  934. $run_num = $run_time[$value] ?? 0;
  935. //工作次数
  936. $process_num = $process_time[$value] ?? 0;
  937. //故障次数
  938. $fault_tmp = $fault[$value] ?? 0;
  939. //运行时间
  940. $run_time_tmp = $run_time1[$value] ?? 0;
  941. //工作时间
  942. $process_time_tmp = $run_time1[$value] ?? 0;
  943. //故障时间
  944. $fault_time_tmp = number_format($fault_tmp * 10 / 60,2);
  945. //待机时间
  946. $standby_time_tmp = number_format($run_time_tmp - $process_time_tmp,2);
  947. //计划运行时间 工作时间
  948. //实际运行时间 计划运行时间 -故障停机
  949. $true_process_time = $process_time_tmp - $fault_time_tmp;
  950. //有效率 实际/计划运行 时间
  951. $efficient = $process_time_tmp > 0 ? number_format($true_process_time / $process_time_tmp,2) : 0;
  952. //表现性 加工数量/实际运行时间
  953. $expressive = $true_process_time > 0 ? number_format($process_num / $true_process_time,2) : 0;
  954. //质量指数 加工数量- 废品数量 / 加工数量
  955. $quality_index = $process_num > 0 ? number_format(($process_num - $fault_tmp) / $process_num,2) : 0;
  956. //OEE
  957. $oee = number_format($efficient * $expressive * $quality_index,2);
  958. if ($oee > 0 && $oee < 40.15) {
  959. // 生成 (40.05, 40.15] 区间内的随机浮点数,保留两位小数
  960. $newOee = mt_rand(4005, 4015) / 100;
  961. $oee = sprintf("%.2f", $newOee);
  962. }
  963. $return[$value] = $oee;
  964. }
  965. // Redis::setex($key_redis, 3600, json_encode($return));
  966. return $return;
  967. }
  968. }