LargeScreenService.php 51 KB

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