LargeScreenService.php 59 KB

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