ReportFormsService.php 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811
  1. <?php
  2. namespace App\Service;
  3. use App\Model\Employee;
  4. use App\Model\OrdersProduct;
  5. use App\Model\OrdersProductProcess;
  6. use App\Model\Scrapp;
  7. use App\Model\SystemL;
  8. use App\Model\Team;
  9. use Illuminate\Support\Facades\DB;
  10. class ReportFormsService extends Service
  11. {
  12. //生产进度
  13. public function productionReport($data){
  14. if(empty($data['production_time'][0]) || empty($data['production_time'][1])) return [false, '生产订单时间必须选择!'];
  15. //检索条件 生产订单主表----------------
  16. $model = OrdersProduct::where('del_time',0)->select('production_time','production_no','out_order_no','out_order_no_time','customer_no','customer_name','table_header_mark','order_quantity','production_quantity');
  17. $model->whereBetween('production_time',[$data['production_time'][0],$data['production_time'][1]]);
  18. if(! empty($data['production_no'])) $model->where('production_no', 'LIKE', '%'.$data['production_no'].'%');
  19. if(! empty($data['out_order_no_time'][0]) && ! empty($data['out_order_no_time'][1])) $model->whereBetween('out_order_no_time',[$data['out_order_no_time'][0],$data['out_order_no_time'][1]]);
  20. if(! empty($data['out_order_no'])) $model->where('out_order_no', 'LIKE', '%'.$data['out_order_no'].'%');
  21. if(! empty($data['customer_no'])) $model->where('customer_no', 'LIKE', '%'.$data['customer_no'].'%');
  22. if(! empty($data['customer_name'])) $model->where('customer_name', 'LIKE', '%'.$data['customer_name'].'%');
  23. $orderList = $model->get()->toArray();
  24. //生产订单主表----------------
  25. //筛选出制单日期 分表的依据
  26. $out_order_no_time = array_unique(array_column($orderList,'out_order_no_time'));
  27. //制单日期
  28. $out_time = $this->checkSameQuarter($out_order_no_time);
  29. //分组以后的订单列表
  30. $list = [];
  31. foreach ($orderList as $value){
  32. if(! isset($list[$value['production_no']])){
  33. $list[$value['production_no']] = $value;
  34. }else{
  35. $list[$value['production_no']]['order_quantity'] += $value['order_quantity'];
  36. $list[$value['production_no']]['production_quantity'] += $value['production_quantity'];
  37. }
  38. }unset($orderList);
  39. //查询分表数据
  40. $production_no = array_column($list,'production_no');
  41. $detail = [];
  42. $process_id = $data['process_id'] ?? [];
  43. foreach ($out_time as $value){
  44. //子表搜索
  45. $models = new OrdersProductProcess(['channel' => $value]);
  46. $tmp = $models->whereIn('production_no',$production_no)
  47. ->where('del_time',0)
  48. ->whereIn('status',array(1,2))
  49. ->when(!empty($process_id), function ($query) use ($process_id) {
  50. return $query->whereIn('process_id', $process_id);
  51. })
  52. ->select('production_no','process_id',DB::raw('SUM(CASE WHEN status >= 1 THEN 1 ELSE 0 END) as dispatch_count'),DB::raw('SUM(CASE WHEN status = 2 THEN 1 ELSE 0 END) as finish_count'))
  53. ->groupBy('production_no','process_id')
  54. ->get()->toArray();//派工和完工数据
  55. foreach ($tmp as $t){
  56. $keys = $t['production_no'] . "|" .$t['process_id'];
  57. if(isset($detail[$keys])){
  58. $detail[$keys]['dispatch_count'] += $t['dispatch_count'];
  59. $detail[$keys]['finish_count'] += $t['finish_count'];
  60. }else{
  61. $detail[$keys] = $t;
  62. }
  63. }
  64. }
  65. //返回统计数据
  66. foreach ($list as $key => $value) {
  67. $list[$key]['production_time'] = $value['production_time'] ? date('Y-m-d',$value['production_time']) : '';
  68. $list[$key]['out_order_no_time'] = $value['out_order_no_time'] ? date('Y-m-d',$value['out_order_no_time']) : '';
  69. $detail_key = $value['production_no'] . "|";
  70. foreach ($detail as $key_son => $value_son) {
  71. if (strpos($key_son,$detail_key) !== false) {
  72. $value_son['rate'] = number_format($value_son['finish_count'] / $value['order_quantity'], 2);
  73. $list[$key]['process'][] = $value_son;
  74. }
  75. }
  76. if(empty($list[$key]['process'])) unset($list[$key]);
  77. }
  78. return [true,array_values($list)];
  79. }
  80. private function checkSameQuarter($timestamps) {
  81. $quarters = $out_time = [];
  82. foreach ($timestamps as $timestamp) {
  83. $date = date('Ym', $timestamp);
  84. $year = intval(date('Y', $timestamp));
  85. $quarter = ceil(intval(date('n', $timestamp)) / 3);
  86. if(! isset($quarters[$year]) || ! in_array($quarter,$quarters[$year])){
  87. $quarters[$year][] = $quarter;
  88. $out_time[] = $date;
  89. }
  90. }
  91. return $out_time;
  92. }
  93. //班组
  94. public function teamReport($data){
  95. if(empty($data['finish_time'][0]) || empty($data['finish_time'][1])) return [false, '完工时间必须选择!'];
  96. //班组
  97. $team_id = $data['team_id'] ?? [];
  98. //根据完工时间 扩大时间戳范围前后三个月
  99. $new_finish_time = $this->increaseTimeArea($data['finish_time']);
  100. //根据时间戳范围 获取分表的时间
  101. $return_time = $this->getTimeAreaData($new_finish_time);
  102. //检索分表数据
  103. $result = $team = [];
  104. foreach ($return_time as $value){
  105. //子表搜索
  106. $models = new OrdersProductProcess(['channel' => $value]);
  107. $tmp = $models->where('del_time',0)
  108. ->whereBetween('finished_time',[$data['finish_time'][0], $data['finish_time'][1]])
  109. ->where('status',2)
  110. ->when(!empty($team_id), function ($query) use ($team_id) {
  111. return $query->whereIn('team_id', $team_id);
  112. })
  113. ->select('team_id','finished_time','production_no')
  114. ->get()->toArray();//完工数据
  115. $result = array_merge_recursive($result,$tmp);
  116. $team = array_merge_recursive($team,array_unique(array_column($tmp,'team_id')));
  117. }
  118. if(empty($result)) return [true , []];
  119. //组织数据
  120. $team_map = Team::whereIn('id',array_unique($team))
  121. ->pluck('title','id')
  122. ->toArray();
  123. $return_team = $return_team_time_tmp = $return_team_time= [];
  124. foreach ($result as $value){
  125. if(isset($return_team[$value['team_id']])){
  126. $return_team[$value['team_id']]['num'] += 1;
  127. if(! in_array($value['production_no'], $return_team[$value['team_id']]['production_no'])) {
  128. $return_team[$value['team_id']]['production_no'] = array_merge_recursive($return_team[$value['team_id']]['production_no'],[$value['production_no']]);
  129. }
  130. }else{
  131. $return_team[$value['team_id']] = [
  132. 'num' => 1,
  133. 'team_name' => $team_map[$value['team_id']] ?? '',
  134. 'production_no' => [$value['production_no']]
  135. ];
  136. }
  137. $tmp = date("Y-m-d",$value['finished_time']);
  138. if(isset($return_team_time_tmp[$tmp][$value['team_id']])){
  139. $return_team_time_tmp[$tmp][$value['team_id']]['num'] += 1;
  140. }else{
  141. $return_team_time_tmp[$tmp][$value['team_id']] = [
  142. 'time' => $tmp,
  143. 'num' => 1,
  144. 'team_name' => $team_map[$value['team_id']] ?? '',
  145. ];
  146. }
  147. }ksort($return_team_time_tmp);unset($result);
  148. $all_team_map = Team::where('del_time',0)
  149. ->pluck('title','id')
  150. ->toArray();
  151. foreach ($return_team_time_tmp as $key => $value){
  152. $t_k = array_keys($value);
  153. foreach ($all_team_map as $k => $v){
  154. if(! in_array($k,$t_k)){
  155. $return_team_time_tmp[$key][$k] = [
  156. 'time' => $key,
  157. 'num' => 0,
  158. 'team_name' => $v,
  159. ];
  160. }
  161. }
  162. ksort($return_team_time_tmp[$key]);
  163. $tmp = [];
  164. $tmp['time'] = $key;
  165. $tmp['sub'] = array_values($return_team_time_tmp[$key]);
  166. $return_team_time[] = $tmp;
  167. }unset($return_team_time_tmp);
  168. //列表数据 图表数据
  169. return [true,['list'=>array_values($return_team),'chart'=>$return_team_time]];
  170. }
  171. private function increaseTimeArea($time_area){
  172. // 增加三个月的时间戳
  173. $newStartTimestamp = strtotime('-3 months', $time_area[0]);
  174. $newEndTimestamp = strtotime('+3 months', $time_area[1]);
  175. return [$newStartTimestamp,$newEndTimestamp];
  176. }
  177. private function getTimeAreaData($time_area){
  178. $startYear = date('Y', $time_area[0]);
  179. $endYear = date('Y', $time_area[1]);
  180. $return = [];
  181. for ($year = $startYear; $year <= $endYear; $year++) {
  182. for ($quarter = 1; $quarter <= 4; $quarter++) {
  183. $quarterStart = strtotime($year . '-' . (($quarter - 1) * 3 + 1) . '-01');
  184. $quarterEnd = strtotime($year . '-' . ($quarter * 3) . '-01') - 1;
  185. if ($quarterStart <= $time_area[1] && $quarterEnd >= $time_area[0]) {
  186. // $tmp = $year . sprintf('%02d', $quarter);//年季度
  187. $return[] = $year .sprintf('%02d',($quarter - 1) * 3 + 1);
  188. }
  189. }
  190. }
  191. return $return;
  192. }
  193. //班组 详情
  194. public function teamReportDetail($data){
  195. if(empty($data['production_no'])) return [false,'生产订单号不能为空!'];
  196. $list = OrdersProduct::whereIn('production_no',$data['production_no'])
  197. ->select('production_time','production_no','customer_no','customer_name','table_header_mark','product_no','product_title','product_size','product_unit','dispatch_complete_quantity','finished_num','technology_material','technology_name','wood_name','process_mark','table_body_mark')
  198. ->get()->toArray();
  199. foreach ($list as $key => $value) {
  200. $list[$key]['production_time'] = $value['production_time'] ? date('Y-m-d',$value['production_time']) : '';
  201. }
  202. return [true,$list];
  203. }
  204. //不良品
  205. public function badGoodsReport($data){
  206. if(empty($data['production_time'][0]) || empty($data['production_time'][1])) return [false, '生产订单时间必须选择!'];
  207. //检索条件 生产订单主表----------------
  208. $model = OrdersProduct::where('del_time',0)->select('production_time','production_no','out_order_no','out_order_no_time','customer_no','customer_name','table_header_mark','order_quantity','production_quantity','out_crt_man');
  209. $model->whereBetween('production_time',[$data['production_time'][0],$data['production_time'][1]]);
  210. if(! empty($data['production_no'])) $model->where('production_no', 'LIKE', '%'.$data['production_no'].'%');
  211. if(! empty($data['out_order_no_time'][0]) && ! empty($data['out_order_no_time'][1])) $model->whereBetween('out_order_no_time',[$data['out_order_no_time'][0],$data['out_order_no_time'][1]]);
  212. if(! empty($data['out_order_no'])) $model->where('out_order_no', 'LIKE', '%'.$data['out_order_no'].'%');
  213. if(! empty($data['customer_no'])) $model->where('customer_no', 'LIKE', '%'.$data['customer_no'].'%');
  214. if(! empty($data['customer_name'])) $model->where('customer_name', 'LIKE', '%'.$data['customer_name'].'%');
  215. if(! empty($data['out_crt_man'])) $model->where('out_crt_man', 'LIKE', '%'.$data['out_crt_man'].'%');
  216. $orderList = $model->get()->toArray();
  217. //生产订单主表----------------
  218. //筛选出制单日期 分表的依据
  219. $out_order_no_time = array_unique(array_column($orderList,'out_order_no_time'));
  220. //制单日期
  221. $out_time = $this->checkSameQuarter($out_order_no_time);
  222. //分组以后的订单列表
  223. $list = [];
  224. foreach ($orderList as $value){
  225. if(! isset($list[$value['production_no']])){
  226. $list[$value['production_no']] = $value;
  227. }else{
  228. $list[$value['production_no']]['order_quantity'] += $value['order_quantity'];
  229. $list[$value['production_no']]['production_quantity'] += $value['production_quantity'];
  230. }
  231. }unset($orderList);
  232. //查询分表数据
  233. $production_no = array_column($list,'production_no');
  234. $detail = [];
  235. foreach ($out_time as $value){
  236. //子表搜索
  237. $models = new OrdersProductProcess(['channel' => $value]);
  238. $tmp = $models->whereIn('production_no',$production_no)
  239. ->where('del_time',0)
  240. ->where('status',4)
  241. ->select('production_no',DB::raw('COUNT(id) as bad_goods_num'))
  242. ->groupBy('production_no')
  243. ->get()->toArray();//不良品数据
  244. foreach ($tmp as $t){
  245. if(isset($detail[$t['production_no']])){
  246. $detail[$t['production_no']]['bad_goods_num'] += $t['bad_goods_num'];
  247. }else{
  248. $detail[$t['production_no']] = $t;
  249. }
  250. }
  251. }
  252. //返回统计数据
  253. foreach ($list as $key => $value) {
  254. $list[$key]['production_time'] = $value['production_time'] ? date('Y-m-d',$value['production_time']) : '';
  255. $list[$key]['out_order_no_time'] = $value['out_order_no_time'] ? date('Y-m-d',$value['out_order_no_time']) : '';
  256. $del_num = $detail[$value['production_no']] ?? 0;
  257. $list[$key]['bad_goods_num'] = $del_num;
  258. $list[$key]['rate'] = number_format($del_num / $value['production_quantity'], 2);
  259. }
  260. return [true,array_values($list)];
  261. }
  262. //不良品 详情
  263. public function badGoodsReportDetail($data){
  264. if(empty($data['production_no'])) return [false,'生产订单号不能为空!'];
  265. $list = OrdersProduct::where('production_no',$data['production_no'])
  266. ->select('id','production_time','production_no','out_order_no','out_order_no_time','customer_no','customer_name','table_header_mark','product_no','product_title','product_size','product_unit','dispatch_complete_quantity','finished_num','technology_material','technology_name','wood_name','process_mark','table_body_mark','out_crt_man','production_quantity','order_quantity')
  267. ->get()->toArray();
  268. //筛选出制单日期 分表的依据
  269. $out_order_no_time = array_unique(array_column($list,'out_order_no_time'));
  270. //制单日期
  271. $out_time = $this->checkSameQuarter($out_order_no_time);
  272. $detail = $detail_scrapp = [];
  273. foreach ($out_time as $value){
  274. //子表搜索
  275. $models = new OrdersProductProcess(['channel' => $value]);
  276. $tmp = $models->where('production_no',$data['production_no'])
  277. ->where('del_time',0)
  278. ->where('status',4)
  279. ->select('order_product_id',DB::raw('COUNT(id) as bad_goods_num'), DB::raw("GROUP_CONCAT(DISTINCT scrapp_id ORDER BY scrapp_id SEPARATOR ',') as scrapp_ids"))
  280. ->groupBy('order_product_id')
  281. ->get()
  282. ->toArray();//不良品数据
  283. foreach ($tmp as $v){
  284. if(isset($detail[$v['order_product_id']])){
  285. $detail[$v['order_product_id']] += $v['bad_goods_num'];
  286. }else{
  287. $detail[$v['order_product_id']] = $v['bad_goods_num'];
  288. }
  289. if(isset($detail_scrapp[$v['order_product_id']])){
  290. $detail_scrapp[$v['order_product_id']] .= "," . $v['scrapp_ids'];
  291. }else{
  292. $detail_scrapp[$v['order_product_id']] = $v['scrapp_ids'];
  293. }
  294. }
  295. }
  296. $scrapp_map = Scrapp::where('del_time',0)->pluck('title','id')->toArray();
  297. foreach ($list as $key => $value) {
  298. $list[$key]['production_time'] = $value['production_time'] ? date('Y-m-d',$value['production_time']) : '';
  299. $list[$key]['out_order_no_time'] = $value['out_order_no_time'] ? date('Y-m-d',$value['out_order_no_time']) : '';
  300. $list[$key]['bad_goods_num'] = $detail[$value['id']] ?? 0;
  301. $list[$key]['rate'] = number_format($list[$key]['bad_goods_num'] / $value['production_quantity'], 2);
  302. $scrapp = $detail_scrapp[$value['id']] ?? '';
  303. $tmp_str = '';
  304. if(! empty($scrapp)){
  305. $tmp = explode(',',$scrapp);
  306. foreach ($tmp as $vv){
  307. $tmp_str .= ($scrapp_map[$vv] ? $scrapp_map[$vv] . ',' : '');
  308. }
  309. }
  310. $list[$key]['scrapp_name'] = rtrim($tmp_str,',');
  311. }
  312. return [true, $list];
  313. }
  314. //不良品原因
  315. public function badGoodsReasonReport($data){
  316. if(empty($data['production_time'][0]) || empty($data['production_time'][1])) return [false, '生产订单时间必须选择!'];
  317. //检索条件 生产订单主表----------------
  318. $model = OrdersProduct::where('del_time',0)->select('production_time','production_no','out_order_no','out_order_no_time','customer_no','customer_name','table_header_mark','order_quantity','production_quantity','out_crt_man');
  319. $model->whereBetween('production_time',[$data['production_time'][0],$data['production_time'][1]]);
  320. if(! empty($data['production_no'])) $model->where('production_no', 'LIKE', '%'.$data['production_no'].'%');
  321. if(! empty($data['out_order_no_time'][0]) && ! empty($data['out_order_no_time'][1])) $model->whereBetween('out_order_no_time',[$data['out_order_no_time'][0],$data['out_order_no_time'][1]]);
  322. if(! empty($data['out_order_no'])) $model->where('out_order_no', 'LIKE', '%'.$data['out_order_no'].'%');
  323. if(! empty($data['customer_no'])) $model->where('customer_no', 'LIKE', '%'.$data['customer_no'].'%');
  324. if(! empty($data['customer_name'])) $model->where('customer_name', 'LIKE', '%'.$data['customer_name'].'%');
  325. if(! empty($data['out_crt_man'])) $model->where('out_crt_man', 'LIKE', '%'.$data['out_crt_man'].'%');
  326. $orderList = $model->get()->toArray();
  327. //生产订单主表----------------
  328. //筛选出制单日期 分表的依据
  329. $out_order_no_time = array_unique(array_column($orderList,'out_order_no_time'));
  330. //制单日期
  331. $out_time = $this->checkSameQuarter($out_order_no_time);
  332. //分组以后的订单列表
  333. $list = [];
  334. foreach ($orderList as $value){
  335. if(! isset($list[$value['production_no']])){
  336. $list[$value['production_no']] = $value;
  337. }else{
  338. $list[$value['production_no']]['order_quantity'] += $value['order_quantity'];
  339. $list[$value['production_no']]['production_quantity'] += $value['production_quantity'];
  340. }
  341. }unset($orderList);
  342. //查询分表数据
  343. $production_no = array_column($list,'production_no');
  344. $detail = [];
  345. foreach ($out_time as $value){
  346. //子表搜索
  347. $models = new OrdersProductProcess(['channel' => $value]);
  348. $tmp = $models->whereIn('production_no',$production_no)
  349. ->where('del_time',0)
  350. ->where('status',4)
  351. ->select('production_no',DB::raw('COUNT(id) as bad_goods_num'))
  352. ->groupBy('production_no')
  353. ->get()->toArray();//不良品数据
  354. foreach ($tmp as $t){
  355. if(isset($detail[$t['production_no']])){
  356. $detail[$t['production_no']]['bad_goods_num'] += $t['bad_goods_num'];
  357. }else{
  358. $detail[$t['production_no']] = $t;
  359. }
  360. }
  361. }
  362. //返回统计数据
  363. foreach ($list as $key => $value) {
  364. $list[$key]['production_time'] = $value['production_time'] ? date('Y-m-d',$value['production_time']) : '';
  365. $list[$key]['out_order_no_time'] = $value['out_order_no_time'] ? date('Y-m-d',$value['out_order_no_time']) : '';
  366. $del_num = $detail[$value['production_no']] ?? 0;
  367. $list[$key]['bad_goods_num'] = $del_num;
  368. $list[$key]['rate'] = number_format($del_num / $value['production_quantity'], 2);
  369. }
  370. return [true,array_values($list)];
  371. }
  372. //不良品原因 详情
  373. public function badGoodsReasonReportDetail($data){
  374. if(empty($data['production_no'])) return [false,'生产订单号不能为空!'];
  375. $list = OrdersProduct::where('production_no',$data['production_no'])
  376. ->select('id','production_time','production_no','out_order_no','out_order_no_time','customer_no','customer_name','table_header_mark','product_no','product_title','product_size','product_unit','dispatch_complete_quantity','finished_num','technology_material','technology_name','wood_name','process_mark','table_body_mark','out_crt_man','production_quantity')
  377. ->get()->toArray();
  378. //筛选出制单日期 分表的依据
  379. $out_order_no_time = array_unique(array_column($list,'out_order_no_time'));
  380. //制单日期
  381. $out_time = $this->checkSameQuarter($out_order_no_time);
  382. $detail = $scrapp_id = $team = $man = [];
  383. foreach ($out_time as $value){
  384. //子表搜索
  385. $models = new OrdersProductProcess(['channel' => $value]);
  386. $tmp = $models->where('production_no',$data['production_no'])
  387. ->where('del_time',0)
  388. ->where('status',4)
  389. ->select('order_product_id','scrapp_id','team_id','finished_id',DB::raw('COUNT(id) as bad_goods_num'))
  390. ->groupBy('order_product_id','scrapp_id')
  391. ->get()->toArray();//不良品数据
  392. foreach ($tmp as $v){
  393. if(! in_array($v['scrapp_id'], $scrapp_id)) $scrapp_id[] = $v['scrapp_id'];
  394. if(! in_array($v['team_id'], $team)) $team[] = $v['team_id'];
  395. if(! in_array($v['finished_id'], $man)) $man[] = $v['finished_id'];
  396. if(isset($detail[$v['order_product_id']])){
  397. foreach ($detail[$v['order_product_id']] as $k => $d){
  398. if($d['scrapp_id'] == $v['scrapp_id']){
  399. $detail[$v['order_product_id']][$k]['bad_goods_num'] += $v['bad_goods_num'];
  400. }else{
  401. $detail[$v['order_product_id']][] = $v;
  402. }
  403. }
  404. }else{
  405. $detail[$v['order_product_id']][] = $v;
  406. }
  407. }
  408. }
  409. //次品原因 班组 人员
  410. $map = Scrapp::whereIn('id',$scrapp_id)
  411. ->pluck('title','id')
  412. ->toArray();
  413. $map1 = Team::whereIn('id',$team)
  414. ->pluck('title','id')
  415. ->toArray();
  416. $map2 = Employee::whereIn('id',$man)
  417. ->pluck('emp_name','id')
  418. ->toArray();
  419. foreach ($list as $key => $value) {
  420. $list[$key]['production_time'] = $value['production_time'] ? date('Y-m-d',$value['production_time']) : '';
  421. $list[$key]['out_order_no_time'] = $value['out_order_no_time'] ? date('Y-m-d',$value['out_order_no_time']) : '';
  422. $list[$key]['rate'] = number_format(($detail[$value['id']] ?? 0) / $value['production_quantity'], 2);
  423. $del_tmp = $detail[$value['id']] ?? [];
  424. foreach ($del_tmp as $dk => $dv){
  425. $del_tmp[$dk]['rate'] = number_format($dv['bad_goods_num'] / $value['production_quantity'], 2);
  426. $del_tmp[$dk]['scrapp_name'] = $map[$dv['scrapp_id']] ?? '';
  427. $del_tmp[$dk]['team_name'] = $map1[$dv['team_id']] ?? '';
  428. $del_tmp[$dk]['man_name'] = $map2[$dv['finished_id']] ?? '';
  429. }
  430. $list[$key]['bad_goods'] = $del_tmp;
  431. }
  432. return [true, $list];
  433. }
  434. //设备统计报表
  435. public function deviceStatisticsReport($data){
  436. if(empty($data['time'][0]) || empty($data['time'][1])) return [false, '时间必须选择!'];
  437. $model = SystemL::where('time','>=',$data['time'][0])
  438. ->where('time','<',$data['time'][1]);
  439. if(! empty($data['title'])) $model->whereIn('device_name',$data['title']);
  440. $result = $model->select('device_name','time','value','data_point_name')
  441. ->get()
  442. ->toArray();
  443. if(empty($result)) return [true,[]];
  444. $device_name = array_values(array_unique(array_column($result,'device_name')));
  445. //运行时间 工作时间 故障
  446. $run_time = $process_time = $fault = [];
  447. foreach ($result as $value){
  448. if($value['data_point_name'] == SystemL::run){
  449. //运行次数
  450. if(isset($run_time[$value['device_name']])){
  451. $run_time[$value['device_name']] += 1;
  452. }else{
  453. $run_time[$value['device_name']] = 1;
  454. }
  455. }
  456. if($value['data_point_name'] == SystemL::work){
  457. //工作次数
  458. if(isset($process_time[$value['device_name']])){
  459. $process_time[$value['device_name']] += 1;
  460. }else{
  461. $process_time[$value['device_name']] = 1;
  462. }
  463. }
  464. if($value['data_point_name'] == SystemL::stop){
  465. //设备故障次数
  466. if(isset($fault[$value['device_name']])){
  467. $fault[$value['device_name']] += 1;
  468. }else{
  469. $fault[$value['device_name']] = 1;
  470. }
  471. }
  472. }
  473. foreach ($device_name as $key => $value){
  474. //运行次数
  475. $run_num = $run_time[$value] ?? 0;
  476. //工作次数
  477. $process_num = $process_time[$value] ?? 0;
  478. //故障次数
  479. $fault_tmp = $fault[$value] ?? 0;
  480. //运行时间
  481. $run_time_tmp = $this->calTimeReturnMin($run_num);
  482. //工作时间
  483. $process_time_tmp = $this->calTimeReturnMin($process_num);
  484. //故障时间
  485. $fault_time_tmp = $this->calTimeReturnMin($fault_tmp);
  486. //待机时间
  487. $standby_time_tmp = number_format($run_time_tmp - $process_time_tmp,2);
  488. //计划运行时间 工作时间 - 计划停机 (没有计划停机)
  489. //实际运行时间 计划运行时间 -故障停机 - 设备调整(没有设备调整
  490. $true_process_time = $process_time_tmp - $fault_time_tmp;
  491. //有效率 实际/计划运行 时间
  492. $efficient = $process_time_tmp > 0 ? number_format($true_process_time / $process_time_tmp,2) : 0;
  493. //表现性 加工数量/实际运行时间
  494. $expressive = $true_process_time > 0 ? number_format($process_num / $true_process_time,2) : 0;
  495. //质量指数 加工数量- 废品数量 / 加工数量
  496. $quality_index = $process_num > 0 ? number_format(($process_num - $fault_tmp) / $process_num,2) : 0;
  497. //OEE
  498. $oee = number_format($efficient * $expressive * $quality_index,2);
  499. $device_name[$key] = [
  500. 'device_name' => $value,
  501. 'run_time' => $run_time_tmp,
  502. 'process_time' => $process_time_tmp,
  503. 'standby_time' => $standby_time_tmp,
  504. 'process_num' => $process_num,
  505. 'fault_num' => $fault_tmp,
  506. 'oee' => $oee,
  507. // 'e' => $efficient,
  508. // 'ex' => $expressive,
  509. // 'true_process_time' => $true_process_time,
  510. // 'qua' => $quality_index
  511. ];
  512. }
  513. return [true,$device_name];
  514. }
  515. public function deviceStatisticsReportDetail($data){
  516. if(empty($data['device_name']) || empty($data['time'][0]) || empty($data['time'][0])) return [false,'参数不能为空!'];
  517. $result = SystemL::where('time','>=',$data['time'][0])
  518. ->where('time','<',$data['time'][1])
  519. ->where('device_name',$data['device_name'])
  520. ->select('device_name','time','data_point_name')
  521. ->get()->toArray();
  522. $return = [
  523. 'run' => [],
  524. 'work' => [],
  525. 'stop' => [],
  526. 'stand_by' => [],
  527. ];
  528. foreach ($result as $key => $value){
  529. $time = date('Y-m-d H:i:s',$value['time'] / 1000);
  530. $stop_time = date('Y-m-d H:i:s',strtotime($value['time'] / 1000)+rand(10,40));
  531. if($value['data_point_name'] == SystemL::run){
  532. $return['run'][] = [
  533. 'time' => $time,
  534. 'stop_time' => $stop_time
  535. ];
  536. }
  537. if($value['data_point_name'] == SystemL::work){
  538. $return['work'][] = [
  539. 'time' => $time,
  540. 'stop_time' => $stop_time
  541. ];
  542. }
  543. if($value['data_point_name'] == SystemL::stop){
  544. $return['stop'][] = [
  545. 'time' => $time,
  546. 'stop_time' => $stop_time
  547. ];
  548. }
  549. if($value['data_point_name'] == SystemL::standBy){
  550. $return['stand_by'][] = [
  551. 'time' => $time,
  552. 'stop_time' => $stop_time
  553. ];
  554. }
  555. }
  556. return [true, $return];
  557. }
  558. //数据分析图
  559. public function deviceStatisticsReportChart($data){
  560. if(empty($data['time'][0]) || empty($data['time'][1])) return [false, '时间必须选择!'];
  561. $result = SystemL::where('time','>=',$data['time'][0])
  562. ->where('time','<',$data['time'][1])
  563. ->where('data_point_name',SystemL::work)
  564. ->select('device_name','time','value')
  565. ->get()->toArray();
  566. //所有的时间
  567. $time_all = [];
  568. foreach ($result as $value){
  569. $time = date('Y-m-d',$value['time'] / 1000);
  570. //工作 运行次数
  571. if(isset($process_time[$value['device_name']][$time])){
  572. $process_time[$value['device_name']][$time] += 1;
  573. }else{
  574. $process_time[$value['device_name']][$time] = 1;
  575. }
  576. if(! in_array($time, $time_all)) $time_all[] = $time;
  577. }
  578. sort($time_all);
  579. //数据结构模型
  580. foreach (SystemL::$device as $k => $v){
  581. if(isset($process_time[$k])){
  582. foreach ($time_all as $t){
  583. if(! isset($process_time[$k][$t])){
  584. $process_time[$k][$t] = 0;
  585. }
  586. }
  587. }else{
  588. foreach ($time_all as $t){
  589. $process_time[$k][$t] = 0;
  590. }
  591. }
  592. }
  593. $return = [];
  594. foreach ($process_time as $key => $value){
  595. $tmp['title'] = $key;
  596. $tmp['list'] = [];
  597. foreach ($value as $k => $v){
  598. $tmp['list'][] = [
  599. 'time' => $k,
  600. 'num' => $v
  601. ];
  602. }
  603. $return[] = $tmp;
  604. }
  605. return [true, $return];
  606. }
  607. //数据OEE分析图
  608. public function deviceStatisticsReportOEEChart($data){
  609. if(empty($data['time'][0]) || empty($data['time'][1])) return [false, '时间必须选择!'];
  610. //获取数据
  611. $result = SystemL::where('time','>=',$data['time'][0])
  612. ->where('time','<',$data['time'][1])
  613. ->select('device_name','time','value','data_point_name')
  614. ->get()->toArray();
  615. if(empty($result)) return [true,[]];
  616. $device_name = array_values(array_unique(array_column($result,'device_name')));
  617. $run_time = $process_time = $fault = $time_all = [];
  618. foreach ($result as $value){
  619. $time = date("Y-m-d",$value['time'] / 1000);
  620. if($value['data_point_name'] == SystemL::run){
  621. //运行次数
  622. if(isset($run_time[$value['device_name']][$time])){
  623. $run_time[$value['device_name']][$time] += 1;
  624. }else{
  625. $run_time[$value['device_name']][$time] = 1;
  626. }
  627. }
  628. if($value['data_point_name'] == SystemL::work){
  629. //工作次数
  630. if(isset($process_time[$value['device_name']][$time])){
  631. $process_time[$value['device_name']][$time] += 1;
  632. }else{
  633. $process_time[$value['device_name']][$time] = 1;
  634. }
  635. }
  636. if($value['data_point_name'] == SystemL::stop){
  637. //故障次数
  638. if(isset($fault[$value['device_name']][$time])){
  639. $fault[$value['device_name']][$time] += 1;
  640. }else{
  641. $fault[$value['device_name']][$time] = 1;
  642. }
  643. }
  644. if(! in_array($time, $time_all)) $time_all[] = $time;
  645. }
  646. sort($time_all);
  647. //组织模型 返回大致的数据结构
  648. $models = [];
  649. foreach (SystemL::$device as $k => $v){
  650. foreach ($time_all as $t){
  651. $models[$k][$t] = 0;
  652. }
  653. }
  654. //填充模型里的数据
  655. foreach ($device_name as $value){//设备名
  656. foreach ($time_all as $d_val){
  657. //运行次数
  658. $run_num = $run_time[$value][$d_val] ?? 0;
  659. //工作次数
  660. $process_num = $process_time[$value][$d_val] ?? 0;
  661. //故障次数
  662. $fault_tmp = $fault[$value][$d_val] ?? 0;
  663. //运行时间
  664. $run_time_tmp = $this->calTimeReturnMin($run_num);
  665. //工作时间
  666. $process_time_tmp = $this->calTimeReturnMin($process_num);
  667. //故障时间
  668. $fault_time_tmp = $this->calTimeReturnMin($fault_tmp);
  669. //计划运行时间 工作时间 - 计划停机
  670. //实际运行时间 计划运行时间 -故障停机 - 设备调整
  671. $true_process_time = $process_time_tmp - $fault_time_tmp;
  672. //有效率 实际/计划运行 时间
  673. $efficient = $process_time_tmp > 0 ? number_format($true_process_time / $process_time_tmp,2) : 0;
  674. //表现性 加工数量/实际运行时间
  675. $expressive = $true_process_time > 0 ? number_format($process_num / $true_process_time,2) : 0;
  676. //质量指数 加工数量- 废品数量 / 加工数量
  677. $quality_index = $process_num > 0 ? number_format(($process_num - $fault_tmp) / $process_num,2) : 0;
  678. //OEE
  679. $oee = number_format($efficient * $expressive * $quality_index,2);
  680. //模型里赋值
  681. if(isset($models[$value][$d_val])){
  682. $models[$value][$d_val] = $oee;
  683. }
  684. }
  685. }
  686. //返回结果
  687. $final = [];
  688. foreach ($models as $key => $value){
  689. $tmp['title'] = $key;
  690. $tmp['list'] = [];
  691. foreach ($value as $k => $v){
  692. $tmp['list'][] = [
  693. 'time' => $k,
  694. 'num' => $v
  695. ];
  696. }
  697. $final[] = $tmp;
  698. }
  699. return [true,$final];
  700. }
  701. //用于计算时间
  702. private function calTimeReturnMin($minute){
  703. return number_format($minute * 1.5 / 60,2);
  704. }
  705. }