LargeScreenService.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. <?php
  2. namespace App\Service;
  3. use App\Model\DispatchSub;
  4. use App\Model\Equipment;
  5. use App\Model\OrdersProduct;
  6. use App\Model\SaleOrdersProduct;
  7. use App\Model\ScrappCount;
  8. use Illuminate\Support\Facades\DB;
  9. class LargeScreenService extends Service
  10. {
  11. public function integratedDashboard($data){
  12. //总产值 ------------------------------------------------
  13. $total = SaleOrdersProduct::where('del_time',0)
  14. ->where('crt_time', ">=", strtotime(date("Y-01-01 00:00:00")))
  15. ->select(DB::raw("sum(finished_num) as total"))
  16. ->value('total');
  17. //总产值 ------------------------------------------------
  18. //当日产量 当月产量 ---------------------------------------------
  19. $data_one = SaleOrdersProduct::where('del_time',0)
  20. ->where('crt_time', ">=", strtotime(date("Y-m-01 00:00:00")))
  21. ->where('crt_time', "<=", strtotime(date("Y-m-t 23:59:59")))
  22. ->select('finished_num','order_quantity','crt_time')
  23. ->get()->toArray();
  24. //当日产量 当月产量
  25. $today_total = $month_total = $order_total = 0;
  26. $timeStamp = strtotime(date("Y-m-d 00:00:00"));
  27. $timeStampE = strtotime(date("Y-m-d 23:59:59"));
  28. foreach ($data_one as $value){
  29. $order_total = bcadd($order_total, $value['order_quantity'],3);
  30. $month_total = bcadd($month_total, $value['finished_num'],3);
  31. if($timeStamp <= $value['crt_time'] && $timeStampE >= $value['crt_time']) $today_total = bcadd($today_total, $value['finished_num'],3);
  32. }
  33. //当日产量 当月产量 ---------------------------------------------
  34. //月计划完成率--------------------------------------------------------------------
  35. $month_rate = floatval($order_total) > 0 ? bcdiv($month_total, $order_total,4) : 0;
  36. $month_rate = bcmul($month_rate,100,2) . "%";
  37. //月计划完成率--------------------------------------------------------------------
  38. //月注塑合格率----------------------------------------------------------------------
  39. $data_two = ScrappCount::where('del_time',0)
  40. ->where('crt_time', ">=", strtotime(date("Y-m-01 00:00:00")))
  41. ->where('crt_time', "<=", strtotime(date("Y-m-t 23:59:59")))
  42. ->where('process_id', 14)
  43. ->select(DB::raw("sum(quantity) as quantity"), DB::raw("sum(scrapp_num) as waste"))
  44. ->get()->toArray();
  45. $total_zj = $total_blp = 0;
  46. foreach ($data_two as $value){
  47. $total_zj = bcadd($total_zj, $value['quantity'],3);
  48. $total_zj = bcadd($total_zj, $value['waste'],3);
  49. $total_blp = bcadd($total_blp, $value['waste'],3);
  50. }
  51. $process_14_total = bcadd($total_zj, $total_blp,3);
  52. $process_14_rate = floatval($process_14_total) > 0 ? bcdiv($total_zj, $process_14_total,4) : 0;
  53. $process_14_rate = bcmul($process_14_rate, 100,2) . "%";
  54. //月注塑合格率----------------------------------------------------------------------
  55. //设备监控状态----------------------------------------------------------------------
  56. $deviceList = Equipment::where('del_time',0)
  57. ->where('model','<>',1)
  58. ->select('id','title','code','status')
  59. ->get()->toArray();
  60. foreach ($deviceList as $key => $value){
  61. $status_title = Equipment::$status_title[$value['status']] ?? "";
  62. $deviceList[$key]['status_title'] = $status_title;
  63. }
  64. //设备监控状态----------------------------------------------------------------------
  65. //质量管理--------------------------------------------------------------------------
  66. $dates = [];
  67. for ($i = 7; $i >= 0; $i--) {
  68. $timestamp = strtotime("-{$i} days");
  69. $date = date('m-d', $timestamp);
  70. $dates[] = $date;
  71. }
  72. $zl_data = [];
  73. $data_three = ScrappCount::where('del_time',0)
  74. ->where('crt_time', "<=", time())
  75. ->where('crt_time', ">=", strtotime(date("Y-m-d 00:00:00",time() - 7 * 24 * 60 * 60)))
  76. ->where('process_id', 14)
  77. ->select('quantity', "scrapp_num as waste", "crt_time")
  78. ->get()->toArray();
  79. foreach ($data_three as $value){
  80. $day = date("m-d", $value['crt_time']);
  81. $tmp = bcadd($value['quantity'], $value['waste'], 3);
  82. if(isset($zl_data[$day])){
  83. $zl_data[$day]['quantity'] = bcadd($zl_data[$day]['quantity'], $value['quantity'],3);
  84. $zl_data[$day]['total'] = bcadd($zl_data[$day]['total'], $tmp,3);
  85. }else{
  86. $zl_data[$day] = [
  87. 'quantity' => $value['quantity'],
  88. 'total' => $tmp
  89. ];
  90. }
  91. }
  92. $zj_data_final = [];
  93. foreach ($dates as $value){
  94. $tmp = [
  95. "day" => $value,
  96. "total" => 0,
  97. "rate" => 0,
  98. ];
  99. if(isset($zl_data[$value])){
  100. $tmp['total'] = $zl_data[$value]['total'];
  101. $tmp['rate'] = bcmul(bcdiv($zl_data[$value]['quantity'], $zl_data[$value]['total'],4),100,2);
  102. }
  103. $zj_data_final[] = $tmp;
  104. }
  105. //质量管理--------------------------------------------------------------------------
  106. //物料管理--------------------------------------------------------------------------
  107. $thing = [
  108. ["name" => '物料一', 'rate' => 50],
  109. ["name" => '物料二', 'rate' => 60],
  110. ["name" => '物料三', 'rate' => 34],
  111. ["name" => '物料四', 'rate' => 22],
  112. ["name" => '物料五', 'rate' => 11],
  113. ["name" => '物料六', 'rate' => 22],
  114. ["name" => '物料七', 'rate' => 44],
  115. ["name" => '物料八', 'rate' => 32],
  116. ["name" => '物料九', 'rate' => 34],
  117. ["name" => '物料十', 'rate' => 51],
  118. ];
  119. //物料管理--------------------------------------------------------------------------
  120. //销售管理--------------------------------------------------------------------------
  121. $xs = SaleOrdersProduct::where('del_time',0)
  122. ->where('crt_time', ">=", strtotime(date("Y-01-01 00:00:00")))
  123. ->select(DB::raw("sum(finished_num) as total"),'technology_name as color')
  124. ->groupBy('technology_name')
  125. ->orderBy('total','desc')
  126. ->get()->toArray();
  127. //销售管理--------------------------------------------------------------------------
  128. //总装各线体目标/完成量---------------------------------------------------------------
  129. $zx_12_dispatch_quantity = $zx_12_finished_num = 0;
  130. $zx_14_dispatch_quantity_8 = $zx_14_finished_num_8 = 0;
  131. $zx_14_dispatch_quantity_9 = $zx_14_finished_num_9 = 0;
  132. $zx_14_dispatch_quantity_12 = $zx_14_finished_num_12 = 0;
  133. $zx_14_dispatch_quantity_13 = $zx_14_finished_num_13 = 0;
  134. $dispatch = DispatchSub::where('del_time',0)
  135. ->where('crt_time', ">=", strtotime(date("Y-01-01 00:00:00")))
  136. ->whereIn('process_id',[12,14])
  137. ->select('dispatch_quantity', 'finished_num', 'process_id','device_id')
  138. ->get()->toArray();
  139. foreach ($dispatch as $value){
  140. if($value['process_id'] == 12){
  141. $zx_12_dispatch_quantity = bcadd($zx_12_dispatch_quantity, $value['dispatch_quantity'],3);
  142. $zx_12_finished_num = bcadd($zx_12_finished_num, $value['finished_num'],3);
  143. }else{
  144. if($value['device_id'] == 8){
  145. $zx_14_dispatch_quantity_8 = bcadd($zx_14_dispatch_quantity_8, $value['dispatch_quantity'],3);
  146. $zx_14_finished_num_8 = bcadd($zx_14_finished_num_8, $value['finished_num'],3);
  147. }elseif ($value['device_id'] == 9){
  148. $zx_14_dispatch_quantity_9 = bcadd($zx_14_dispatch_quantity_9, $value['dispatch_quantity'],3);
  149. $zx_14_finished_num_9 = bcadd($zx_14_finished_num_9, $value['finished_num'],3);
  150. }elseif ($value['device_id'] == 12){
  151. $zx_14_dispatch_quantity_12 = bcadd($zx_14_dispatch_quantity_12, $value['dispatch_quantity'],3);
  152. $zx_14_finished_num_12 = bcadd($zx_14_finished_num_12, $value['finished_num'],3);
  153. }elseif ($value['device_id'] == 13){
  154. $zx_14_dispatch_quantity_13 = bcadd($zx_14_dispatch_quantity_13, $value['dispatch_quantity'],3);
  155. $zx_14_finished_num_13 = bcadd($zx_14_finished_num_13, $value['finished_num'],3);
  156. }
  157. }
  158. }
  159. $diff_num = bcsub($zx_12_dispatch_quantity, $zx_12_finished_num,3);
  160. $diff_num_8 = bcsub($zx_14_dispatch_quantity_8, $zx_14_finished_num_8,3);
  161. $diff_num_9 = bcsub($zx_14_dispatch_quantity_9, $zx_14_finished_num_9,3);
  162. $diff_num_12 = bcsub($zx_14_dispatch_quantity_12, $zx_14_finished_num_12,3);
  163. $diff_num_13 = bcsub($zx_14_dispatch_quantity_13, $zx_14_finished_num_13,3);
  164. $xt = [
  165. [
  166. "name" => "清洗脱水产线", "sj_num" => $zx_12_finished_num, "jh_num" => $zx_12_dispatch_quantity, "diff_num" => $diff_num
  167. ],[
  168. "name" => "注塑一号产线", "sj_num" => $zx_14_finished_num_8, "jh_num" => $zx_14_dispatch_quantity_8, "diff_num" => $diff_num_8
  169. ],[
  170. "name" => "注塑二号产线", "sj_num" => $zx_14_finished_num_9, "jh_num" => $zx_14_dispatch_quantity_9, "diff_num" => $diff_num_9
  171. ],[
  172. "name" => "注塑三号产线", "sj_num" => $zx_14_finished_num_12, "jh_num" => $zx_14_dispatch_quantity_12, "diff_num" => $diff_num_12
  173. ],[
  174. "name" => "注塑四号产线", "sj_num" => $zx_14_finished_num_13, "jh_num" => $zx_14_dispatch_quantity_13, "diff_num" => $diff_num_13
  175. ],
  176. ];
  177. //总装各线体目标/完成量---------------------------------------------------------------
  178. //订单完成进度-----------------------------------------------------------------
  179. $orderBy = [11 => "破碎分离", 12 => "清洗脱水" ,15 => "混合搅拌",13 => "挤出造粒",14 => "注塑成型"];
  180. $order = OrdersProduct::where('del_time',0)
  181. ->select('production_no as order_number','id','technology_name as color','production_quantity','dispatch_complete_quantity','finished_num')
  182. ->orderBy('id','desc')
  183. ->limit(10)
  184. ->get()->toArray();
  185. $dispatch_num = DispatchSub::where('del_time',0)
  186. ->whereIn('order_product_id',array_column($order,'id'))
  187. ->select('order_product_id','process_id','finished_num','dispatch_quantity','waste_num')
  188. ->get()->toArray();
  189. $details = [];
  190. foreach ($dispatch_num as $value){
  191. $new = $value['order_product_id'] . $value['process_id'];
  192. $finished_num = bcadd($value['finished_num'], $value['waste_num'],3);
  193. if(isset($details[$new])){
  194. $details[$new]['dispatch_quantity'] = bcadd($details[$new]['dispatch_quantity'], $value['dispatch_quantity'],3);
  195. $details[$new]['finished_num'] = bcadd($details[$new]['finished_num'], $finished_num,3);
  196. }else{
  197. $details[$new] = [
  198. "dispatch_quantity" => $value['dispatch_quantity'],
  199. "finished_num" => $finished_num,
  200. ];
  201. }
  202. }
  203. foreach ($order as $key => $value){
  204. $d_tmp = [];
  205. foreach ($orderBy as $k => $v){
  206. $new = $value['id'] . $k;
  207. $tmpS = $details[$new] ?? [];
  208. $rate = 0;
  209. if(! empty($tmpS)) {
  210. $rate = bcdiv($tmpS['finished_num'], $tmpS['dispatch_quantity'],4);
  211. $rate = bcmul($rate, 100,2);
  212. }
  213. $d_tmp[] = [
  214. "name" => $v,
  215. "rate" => $rate
  216. ];
  217. }
  218. $order[$key]['detail'] = $d_tmp;
  219. if($value['production_quantity'] <= $value['finished_num']){
  220. $state = "已完成";
  221. }else{
  222. $state = "进行中";
  223. }
  224. $order[$key]['state_title'] = $state;
  225. }
  226. //订单完成进度-----------------------------------------------------------------
  227. // dd($total, $today_total, $month_total, $month_rate, $process_14_rate, $deviceList, $zj_data_final, $thing, $xs, $xt,$order);
  228. $return = [
  229. 'total' => $total, //年总产量
  230. 'today_total' => $today_total, //当日产量
  231. 'month_total' => $month_total, //当月产量
  232. 'month_rate' => $month_rate, // 月计划完成比例
  233. 'process_14_rate' => $process_14_rate, // 注塑产线合格率
  234. 'device' => $deviceList, // 设备监控状态
  235. 'zj_result' => $zj_data_final, //质量管理
  236. 'thing' => $thing, //物料管理
  237. 'xs' => $xs, //销售管理
  238. 'xt' => $xt, //总装各线体目标/完成量
  239. 'order' => $order, // 订单完成进度
  240. ];
  241. return [true, $return];
  242. }
  243. }