LargeScreenService.php 46 KB

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