ReportFormsService.php 52 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234
  1. <?php
  2. namespace App\Service;
  3. use App\Model\DispatchSub;
  4. use App\Model\Employee;
  5. use App\Model\Equipment;
  6. use App\Model\OrdersProduct;
  7. use App\Model\OrdersProductProcess;
  8. use App\Model\Process;
  9. use App\Model\ReportWorkingDetail;
  10. use App\Model\Scrapp;
  11. use App\Model\ScrappCount;
  12. use App\Model\SystemL;
  13. use App\Model\Team;
  14. use Illuminate\Support\Facades\DB;
  15. use Illuminate\Support\Facades\Redis;
  16. /**
  17. * 设备相关设置报表
  18. * Class ReportFormsService
  19. * @package App\Service
  20. */
  21. class ReportFormsService extends Service
  22. {
  23. /**
  24. * 生产进度
  25. * @param $data
  26. * @return array
  27. */
  28. public function productionReport($data){
  29. if(empty($data['production_time'][0]) || empty($data['production_time'][1])) return [false, '生产订单时间必须选择!'];
  30. //检索条件 生产订单主表----------------
  31. $model = OrdersProduct::where('del_time',0)->select('id','production_time','production_no','out_order_no','out_order_no_time','customer_no','customer_name','table_header_mark','order_quantity','production_quantity');
  32. $model->whereBetween('production_time',[$data['production_time'][0],$data['production_time'][1]]);
  33. if(! empty($data['production_no'])) $model->where('production_no', 'LIKE', '%'.$data['production_no'].'%');
  34. 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]]);
  35. if(! empty($data['out_order_no'])) $model->where('out_order_no', 'LIKE', '%'.$data['out_order_no'].'%');
  36. if(! empty($data['customer_no'])) $model->where('customer_no', 'LIKE', '%'.$data['customer_no'].'%');
  37. if(! empty($data['customer_name'])) $model->where('customer_name', 'LIKE', '%'.$data['customer_name'].'%');
  38. $orderList = $model->get()->toArray();
  39. //生产订单主表----------------
  40. $id = array_column($orderList,'id');
  41. //分组以后的订单列表
  42. $list = [];
  43. foreach ($orderList as $value){
  44. if(! isset($list[$value['production_no']])){
  45. $list[$value['production_no']] = $value;
  46. }else{
  47. $list[$value['production_no']]['order_quantity'] += $value['order_quantity'];
  48. $list[$value['production_no']]['production_quantity'] += $value['production_quantity'];
  49. }
  50. }unset($orderList);
  51. $detail = [];
  52. $dispatchList = DispatchSub::where('del_time',0)
  53. ->whereIn('order_product_id',$id)
  54. ->select('order_product_id','process_id',DB::raw('SUM(dispatch_quantity) as dispatch_count'),DB::raw('SUM(finished_num) as finish_count'))
  55. ->groupBy('order_product_id','process_id')
  56. ->get()->toArray();//派工和完工数据
  57. foreach ($dispatchList as $t){
  58. $keys = $t['order_product_id'] . "|" .$t['process_id'];
  59. if(isset($detail[$keys])){
  60. $detail[$keys]['dispatch_count'] += $t['dispatch_count'];
  61. $detail[$keys]['finish_count'] += $t['finish_count'];
  62. }else{
  63. $detail[$keys] = $t;
  64. }
  65. }
  66. //返回统计数据
  67. foreach ($list as $key => $value) {
  68. $list[$key]['production_time'] = $value['production_time'] ? date('Y-m-d',$value['production_time']) : '';
  69. $list[$key]['out_order_no_time'] = $value['out_order_no_time'] ? date('Y-m-d',$value['out_order_no_time']) : '';
  70. $detail_key = $value['id'] . "|";
  71. foreach ($detail as $key_son => $value_son) {
  72. if (strpos($key_son, $detail_key) !== false) {
  73. $value_son['production_no'] = $value['production_no'];
  74. $dispatch_count = $value_son['dispatch_count'];
  75. $finish_count = $value_son['finish_count'];
  76. $value_son['dispatch_count'] = $dispatch_count;
  77. $value_son['finish_count'] = $finish_count;
  78. $value_son['rate'] = number_format($value_son['finish_count'] / $value['order_quantity'] * 100, 2);
  79. $list[$key]['process'][] = $value_son;
  80. }
  81. }
  82. if(empty($list[$key]['process'])) unset($list[$key]);
  83. }
  84. return [true,array_values($list)];
  85. }
  86. private function checkSameQuarter($timestamps) {
  87. $quarters = $out_time = [];
  88. foreach ($timestamps as $timestamp) {
  89. $date = date('Ym', $timestamp);
  90. $year = intval(date('Y', $timestamp));
  91. $quarter = ceil(intval(date('n', $timestamp)) / 3);
  92. if(! isset($quarters[$year]) || ! in_array($quarter,$quarters[$year])){
  93. $quarters[$year][] = $quarter;
  94. $out_time[] = $date;
  95. }
  96. }
  97. return $out_time;
  98. }
  99. public function teamReport($data){
  100. if(empty($data['finish_time'][0]) || empty($data['finish_time'][1])) return [false, '完工时间必须选择!'];
  101. //班组
  102. $team_id = $data['team_id'] ?? [];
  103. $result = DispatchSub::where('del_time',0)
  104. ->where('finished_num','>',0)
  105. ->whereBetween('upd_time', [$data['finish_time'][0], $data['finish_time'][1]])
  106. ->when(!empty($team_id), function ($query) use ($team_id) {
  107. return $query->whereIn('team_id', $team_id);
  108. })
  109. ->select('team_id','upd_time as finished_time','finished_num','order_product_id')
  110. ->get()->toArray();
  111. if(empty($result)) return [true , []];
  112. //组织数据
  113. $team_map = Team::whereIn('id',array_unique(array_column($result,'team_id')))
  114. ->pluck('title','id')
  115. ->toArray();
  116. $return_team = $return_team_time_tmp = $return_team_time= [];
  117. foreach ($result as $value){
  118. if(isset($return_team[$value['team_id']])){
  119. $num = bcadd($value['finished_num'], $return_team[$value['team_id']]['num'],3);
  120. $return_team[$value['team_id']]['num'] = $num;
  121. if(! in_array($value['order_product_id'], $return_team[$value['team_id']]['production_no'])) {
  122. $return_team[$value['team_id']]['production_no'] = array_merge_recursive($return_team[$value['team_id']]['production_no'],[$value['order_product_id']]);
  123. }
  124. }else{
  125. $return_team[$value['team_id']] = [
  126. 'num' => $value['finished_num'],
  127. 'team_name' => $team_map[$value['team_id']] ?? '',
  128. 'production_no' => [$value['order_product_id']]
  129. ];
  130. }
  131. $tmp = date("Y-m-d",$value['finished_time']);
  132. if(isset($return_team_time_tmp[$tmp][$value['team_id']])){
  133. $num = bcadd($value['finished_num'],$return_team_time_tmp[$tmp][$value['team_id']]['num'],3);
  134. $return_team_time_tmp[$tmp][$value['team_id']]['num'] = $num;
  135. }else{
  136. $return_team_time_tmp[$tmp][$value['team_id']] = [
  137. 'time' => $tmp,
  138. 'num' => $value['finished_num'],
  139. 'team_name' => $team_map[$value['team_id']] ?? '',
  140. ];
  141. }
  142. }ksort($return_team_time_tmp);unset($result);
  143. $all_team_map = Team::where('del_time',0)
  144. ->pluck('title','id')
  145. ->toArray();
  146. foreach ($return_team_time_tmp as $key => $value){
  147. $t_k = array_keys($value);
  148. foreach ($all_team_map as $k => $v){
  149. if(! in_array($k,$t_k)){
  150. $return_team_time_tmp[$key][$k] = [
  151. 'time' => $key,
  152. 'num' => 0,
  153. 'team_name' => $v,
  154. ];
  155. }
  156. }
  157. ksort($return_team_time_tmp[$key]);
  158. $tmp = [];
  159. $tmp['time'] = $key;
  160. $tmp['sub'] = array_values($return_team_time_tmp[$key]);
  161. $return_team_time[] = $tmp;
  162. }unset($return_team_time_tmp);
  163. foreach ($return_team as $key => $value){
  164. $return_team[$key]['num'] = $value['num'];
  165. }
  166. foreach ($return_team_time as $key => $value){
  167. foreach ($value['sub'] as $k => $v){
  168. $return_team_time[$key]['sub'][$k]['num'] = $v['num'];
  169. }
  170. }
  171. //列表数据 图表数据
  172. return [true,['list'=>array_values($return_team),'chart'=>$return_team_time]];
  173. }
  174. /**
  175. * 班组
  176. * @param $data
  177. * @return array
  178. */
  179. public function teamReport1($data){
  180. if(empty($data['finish_time'][0]) || empty($data['finish_time'][1])) return [false, '完工时间必须选择!'];
  181. //班组
  182. $team_id = $data['team_id'] ?? [];
  183. //根据完工时间 扩大时间戳范围前后三个月
  184. $new_finish_time = $this->increaseTimeArea($data['finish_time']);
  185. //根据时间戳范围 获取分表的时间
  186. $return_time = $this->getTimeAreaData($new_finish_time);
  187. //检索分表数据
  188. $batchSize = 1000; // 每次处理的数据量
  189. $result = $team = [];
  190. foreach ($return_time as $value){
  191. $offset = 0;
  192. $hasMoreData = true;
  193. while ($hasMoreData) {
  194. // 子表搜索
  195. $models = new OrdersProductProcess(['channel' => $value]);
  196. $tmp = $models->where('del_time', 0)
  197. ->whereBetween('finished_time', [$data['finish_time'][0], $data['finish_time'][1]])
  198. ->where('status', 2)
  199. ->when(!empty($team_id), function ($query) use ($team_id) {
  200. return $query->whereIn('team_id', $team_id);
  201. })
  202. ->skip($offset) // 跳过前面的数据
  203. ->take($batchSize) // 获取固定数量的数据
  204. ->select('team_id', 'finished_time', 'production_no')
  205. ->get()->toArray(); // 完工数据
  206. if (empty($tmp)) {
  207. $hasMoreData = false;
  208. } else {
  209. $result = array_merge_recursive($result, $tmp);
  210. $team = array_merge_recursive($team, array_unique(array_column($tmp, 'team_id')));
  211. $offset += $batchSize;
  212. }
  213. }
  214. // //子表搜索
  215. // $models = new OrdersProductProcess(['channel' => $value]);
  216. // $tmp = $models->where('del_time',0)
  217. // ->whereBetween('finished_time',[$data['finish_time'][0], $data['finish_time'][1]])
  218. // ->where('status',2)
  219. // ->when(!empty($team_id), function ($query) use ($team_id) {
  220. // return $query->whereIn('team_id', $team_id);
  221. // })
  222. // ->select('team_id','finished_time','production_no')
  223. // ->get()->toArray();//完工数据
  224. // $result = array_merge_recursive($result,$tmp);
  225. // $team = array_merge_recursive($team,array_unique(array_column($tmp,'team_id')));
  226. }
  227. if(empty($result)) return [true , []];
  228. //组织数据
  229. $team_map = Team::whereIn('id',array_unique($team))
  230. ->pluck('title','id')
  231. ->toArray();
  232. $return_team = $return_team_time_tmp = $return_team_time= [];
  233. foreach ($result as $value){
  234. if(isset($return_team[$value['team_id']])){
  235. $return_team[$value['team_id']]['num'] += 1;
  236. if(! in_array($value['production_no'], $return_team[$value['team_id']]['production_no'])) {
  237. $return_team[$value['team_id']]['production_no'] = array_merge_recursive($return_team[$value['team_id']]['production_no'],[$value['production_no']]);
  238. }
  239. }else{
  240. $return_team[$value['team_id']] = [
  241. 'num' => 1,
  242. 'team_name' => $team_map[$value['team_id']] ?? '',
  243. 'production_no' => [$value['production_no']]
  244. ];
  245. }
  246. $tmp = date("Y-m-d",$value['finished_time']);
  247. if(isset($return_team_time_tmp[$tmp][$value['team_id']])){
  248. $return_team_time_tmp[$tmp][$value['team_id']]['num'] += 1;
  249. }else{
  250. $return_team_time_tmp[$tmp][$value['team_id']] = [
  251. 'time' => $tmp,
  252. 'num' => 1,
  253. 'team_name' => $team_map[$value['team_id']] ?? '',
  254. ];
  255. }
  256. }ksort($return_team_time_tmp);unset($result);
  257. $all_team_map = Team::where('del_time',0)
  258. ->pluck('title','id')
  259. ->toArray();
  260. foreach ($return_team_time_tmp as $key => $value){
  261. $t_k = array_keys($value);
  262. foreach ($all_team_map as $k => $v){
  263. if(! in_array($k,$t_k)){
  264. $return_team_time_tmp[$key][$k] = [
  265. 'time' => $key,
  266. 'num' => 0,
  267. 'team_name' => $v,
  268. ];
  269. }
  270. }
  271. ksort($return_team_time_tmp[$key]);
  272. $tmp = [];
  273. $tmp['time'] = $key;
  274. $tmp['sub'] = array_values($return_team_time_tmp[$key]);
  275. $return_team_time[] = $tmp;
  276. }unset($return_team_time_tmp);
  277. foreach ($return_team as $key => $value){
  278. $return_team[$key]['num'] = bcdiv($value['num'],1000,3);
  279. }
  280. foreach ($return_team_time as $key => $value){
  281. foreach ($value['sub'] as $k => $v){
  282. $return_team_time[$key]['sub'][$k]['num'] = bcdiv($v['num'],1000,3);
  283. }
  284. }
  285. //列表数据 图表数据
  286. return [true,['list'=>array_values($return_team),'chart'=>$return_team_time]];
  287. }
  288. /**
  289. * 时间特殊处理
  290. * @param $time_area
  291. * @return array
  292. */
  293. private function increaseTimeArea($time_area){
  294. // 增加三个月的时间戳
  295. $newStartTimestamp = strtotime('-3 months', $time_area[0]);
  296. $newEndTimestamp = strtotime('+3 months', $time_area[1]);
  297. return [$time_area[0],$time_area[1]];
  298. }
  299. /**
  300. * 获取时间区间数据
  301. * @param $time_area
  302. * @return array
  303. */
  304. private function getTimeAreaData($time_area){
  305. $startYear = date('Y', $time_area[0]);
  306. $endYear = date('Y', $time_area[1]);
  307. $return = [];
  308. for ($year = $startYear; $year <= $endYear; $year++) {
  309. for ($quarter = 1; $quarter <= 4; $quarter++) {
  310. $quarterStart = strtotime($year . '-' . (($quarter - 1) * 3 + 1) . '-01');
  311. $quarterEnd = strtotime($year . '-' . ($quarter * 3) . '-01') - 1;
  312. if ($quarterStart <= $time_area[1] && $quarterEnd >= $time_area[0]) {
  313. // $tmp = $year . sprintf('%02d', $quarter);//年季度
  314. $return[] = $year .sprintf('%02d',($quarter - 1) * 3 + 1);
  315. }
  316. }
  317. }
  318. return $return;
  319. }
  320. /**
  321. * 班组 详情
  322. * @param $data
  323. * @return array
  324. */
  325. public function teamReportDetail($data){
  326. if(empty($data['production_no'])) return [false,'生产订单号不能为空!'];
  327. $list = OrdersProduct::whereIn('id',$data['production_no'])
  328. ->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')
  329. ->get()->toArray();
  330. foreach ($list as $key => $value) {
  331. $list[$key]['production_time'] = $value['production_time'] ? date('Y-m-d',$value['production_time']) : '';
  332. }
  333. return [true,$list];
  334. }
  335. /**
  336. * 不良品
  337. * @param $data
  338. * @return array
  339. */
  340. public function badGoodsReport($data){
  341. if(empty($data['production_time'][0]) || empty($data['production_time'][1])) return [false, '生产订单时间必须选择!'];
  342. //检索条件 生产订单主表----------------
  343. $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','id');
  344. $model->whereBetween('production_time',[$data['production_time'][0],$data['production_time'][1]]);
  345. if(! empty($data['production_no'])) $model->where('production_no', 'LIKE', '%'.$data['production_no'].'%');
  346. 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]]);
  347. if(! empty($data['out_order_no'])) $model->where('out_order_no', 'LIKE', '%'.$data['out_order_no'].'%');
  348. if(! empty($data['customer_no'])) $model->where('customer_no', 'LIKE', '%'.$data['customer_no'].'%');
  349. if(! empty($data['customer_name'])) $model->where('customer_name', 'LIKE', '%'.$data['customer_name'].'%');
  350. if(! empty($data['out_crt_man'])) $model->where('out_crt_man', 'LIKE', '%'.$data['out_crt_man'].'%');
  351. $orderList = $model->get()->toArray();
  352. //生产订单主表----------------
  353. //筛选出制单日期 分表的依据
  354. $out_order_no_time = array_unique(array_column($orderList,'out_order_no_time'));
  355. //制单日期
  356. $out_time = $this->checkSameQuarter($out_order_no_time);
  357. //分组以后的订单列表
  358. $list = [];
  359. foreach ($orderList as $value){
  360. if(! isset($list[$value['id']])){
  361. $list[$value['id']] = $value;
  362. }else{
  363. $list[$value['id']]['order_quantity'] += $value['order_quantity'];
  364. $list[$value['id']]['production_quantity'] += $value['production_quantity'];
  365. }
  366. }unset($orderList);
  367. //查询分表数据
  368. $production_id = array_keys($list);
  369. $detail = [];
  370. // foreach ($out_time as $value){
  371. // //子表搜索
  372. // $models = new OrdersProductProcess(['channel' => $value]);
  373. // $tmp = $models->whereIn('order_product_id',$production_id)
  374. // ->where('del_time',0)
  375. // ->where('status',4)
  376. // ->select('order_product_id',DB::raw('COUNT(id) as bad_goods_num'))
  377. // ->groupBy('order_product_id')
  378. // ->get()->toArray();//不良品数据
  379. //
  380. // foreach ($tmp as $t){
  381. // if(isset($detail[$t['order_product_id']])){
  382. // $detail[$t['order_product_id']]['bad_goods_num'] += $t['bad_goods_num'];
  383. // }else{
  384. // $detail[$t['order_product_id']] = $t['bad_goods_num'];
  385. // }
  386. // }
  387. // }
  388. $scrapp = ScrappCount::where('del_time',0)
  389. ->whereIn('order_product_id',$production_id)
  390. ->select('order_product_id','scrapp_num as bad_goods_num')
  391. ->get()->toArray();
  392. foreach ($scrapp as $value){
  393. if(isset($detail[$value['order_product_id']])){
  394. $tmp = bcadd($detail[$value['order_product_id']],$value['bad_goods_num'],3);
  395. $detail[$value['order_product_id']] = $tmp;
  396. }else{
  397. $detail[$value['order_product_id']] = $value['bad_goods_num'];
  398. }
  399. }
  400. //返回统计数据
  401. foreach ($list as $key => $value) {
  402. $list[$key]['production_time'] = $value['production_time'] ? date('Y-m-d',$value['production_time']) : '';
  403. $list[$key]['out_order_no_time'] = $value['out_order_no_time'] ? date('Y-m-d',$value['out_order_no_time']) : '';
  404. $del_num = $detail[$value['id']] ?? 0;
  405. $list[$key]['bad_goods_num'] = $del_num;
  406. $list[$key]['rate'] = bcdiv($del_num ,$value['production_quantity'], 2);
  407. }
  408. return [true,array_values($list)];
  409. }
  410. /**
  411. * 不良品 详情
  412. * @param $data
  413. * @return array
  414. */
  415. public function badGoodsReportDetail($data){
  416. if(empty($data['production_no'])) return [false,'生产订单号不能为空!'];
  417. $list = OrdersProduct::where('production_no',$data['production_no'])
  418. ->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')
  419. ->get()->toArray();
  420. //筛选出制单日期 分表的依据
  421. $out_order_no_time = array_unique(array_column($list,'out_order_no_time'));
  422. //制单日期
  423. $out_time = $this->checkSameQuarter($out_order_no_time);
  424. $detail = $detail_scrapp = [];
  425. $order_product_id = array_column($list,'id');
  426. $scrapp = ScrappCount::where('del_time',0)
  427. ->whereIn('order_product_id',$order_product_id)
  428. ->select('order_product_id','scrapp_num as bad_goods_num','scrapp_id')
  429. ->get()->toArray();
  430. foreach ($scrapp as $value){
  431. if(isset($detail[$value['order_product_id']])){
  432. $tmp = bcadd($detail[$value['order_product_id']],$value['bad_goods_num'],3);
  433. $detail[$value['order_product_id']] = $tmp;
  434. }else{
  435. $detail[$value['order_product_id']] = $value['bad_goods_num'];
  436. }
  437. if(isset($detail_scrapp[$value['order_product_id']])){
  438. $detail_scrapp[$value['order_product_id']] .= "," . $value['scrapp_id'];
  439. }else{
  440. $detail_scrapp[$value['order_product_id']] = $value['scrapp_id'];
  441. }
  442. }
  443. // foreach ($out_time as $value){
  444. // //子表搜索
  445. // $models = new OrdersProductProcess(['channel' => $value]);
  446. // $tmp = $models->whereIn('order_product_id',$order_product_id)
  447. // ->where('del_time',0)
  448. // ->where('status',4)
  449. // ->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"))
  450. // ->groupBy('order_product_id')
  451. // ->get()
  452. // ->toArray();//不良品数据
  453. //
  454. // foreach ($tmp as $v){
  455. // if(isset($detail[$v['order_product_id']])){
  456. // $detail[$v['order_product_id']] += $v['bad_goods_num'];
  457. // }else{
  458. // $detail[$v['order_product_id']] = $v['bad_goods_num'];
  459. // }
  460. // if(isset($detail_scrapp[$v['order_product_id']])){
  461. // $detail_scrapp[$v['order_product_id']] .= "," . $v['scrapp_ids'];
  462. // }else{
  463. // $detail_scrapp[$v['order_product_id']] = $v['scrapp_ids'];
  464. // }
  465. // }
  466. // }
  467. $scrapp_map = Scrapp::where('del_time',0)->pluck('title','id')->toArray();
  468. foreach ($list as $key => $value) {
  469. $list[$key]['production_time'] = $value['production_time'] ? date('Y-m-d',$value['production_time']) : '';
  470. $list[$key]['out_order_no_time'] = $value['out_order_no_time'] ? date('Y-m-d',$value['out_order_no_time']) : '';
  471. $list[$key]['bad_goods_num'] = $detail[$value['id']] ?? 0;
  472. $list[$key]['rate'] = number_format($list[$key]['bad_goods_num'] / $value['production_quantity'], 2);
  473. $scrapp = $detail_scrapp[$value['id']] ?? '';
  474. $tmp_str = '';
  475. if(! empty($scrapp)){
  476. $tmp = explode(',',$scrapp);
  477. foreach ($tmp as $vv){
  478. $tmp_str .= ($scrapp_map[$vv] ? $scrapp_map[$vv] . ',' : '');
  479. }
  480. }
  481. $list[$key]['scrapp_name'] = rtrim($tmp_str,',');
  482. }
  483. return [true, $list];
  484. }
  485. /**
  486. * 不良品原因
  487. * @param $data
  488. * @return array
  489. */
  490. public function badGoodsReasonReport($data){
  491. if(empty($data['production_time'][0]) || empty($data['production_time'][1])) return [false, '生产订单时间必须选择!'];
  492. //检索条件 生产订单主表----------------
  493. $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','id');
  494. $model->whereBetween('production_time',[$data['production_time'][0],$data['production_time'][1]]);
  495. if(! empty($data['production_no'])) $model->where('production_no', 'LIKE', '%'.$data['production_no'].'%');
  496. 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]]);
  497. if(! empty($data['out_order_no'])) $model->where('out_order_no', 'LIKE', '%'.$data['out_order_no'].'%');
  498. if(! empty($data['customer_no'])) $model->where('customer_no', 'LIKE', '%'.$data['customer_no'].'%');
  499. if(! empty($data['customer_name'])) $model->where('customer_name', 'LIKE', '%'.$data['customer_name'].'%');
  500. if(! empty($data['out_crt_man'])) $model->where('out_crt_man', 'LIKE', '%'.$data['out_crt_man'].'%');
  501. $orderList = $model->get()->toArray();
  502. //生产订单主表----------------
  503. //筛选出制单日期 分表的依据
  504. $out_order_no_time = array_unique(array_column($orderList,'out_order_no_time'));
  505. //制单日期
  506. $out_time = $this->checkSameQuarter($out_order_no_time);
  507. //分组以后的订单列表
  508. $list = [];
  509. foreach ($orderList as $value){
  510. if(! isset($list[$value['id']])){
  511. $list[$value['id']] = $value;
  512. }else{
  513. $list[$value['id']]['order_quantity'] += $value['order_quantity'];
  514. $list[$value['id']]['production_quantity'] += $value['production_quantity'];
  515. }
  516. }unset($orderList);
  517. //查询分表数据
  518. $production_id = array_keys($list);
  519. $detail = [];
  520. $scrapp = ScrappCount::where('del_time',0)
  521. ->whereIn('order_product_id',$production_id)
  522. ->select('order_product_id','scrapp_num as bad_goods_num')
  523. ->get()->toArray();
  524. foreach ($scrapp as $value){
  525. if(isset($detail[$value['order_product_id']])){
  526. $tmp = bcadd($detail[$value['order_product_id']],$value['bad_goods_num'],3);
  527. $detail[$value['order_product_id']] = $tmp;
  528. }else{
  529. $detail[$value['order_product_id']] = $value['bad_goods_num'];
  530. }
  531. }
  532. // foreach ($out_time as $value){
  533. // //子表搜索
  534. // $models = new OrdersProductProcess(['channel' => $value]);
  535. // $tmp = $models->whereIn('order_product_id',$production_id)
  536. // ->where('del_time',0)
  537. // ->where('status',4)
  538. // ->select('order_product_id',DB::raw('COUNT(id) as bad_goods_num'))
  539. // ->groupBy('order_product_id')
  540. // ->get()->toArray();//不良品数据
  541. //
  542. // foreach ($tmp as $t){
  543. // if(isset($detail[$t['order_product_id']])){
  544. // $detail[$t['order_product_id']] += $t['bad_goods_num'];
  545. // }else{
  546. // $detail[$t['order_product_id']] = $t['bad_goods_num'];
  547. // }
  548. // }
  549. // }
  550. //返回统计数据
  551. foreach ($list as $key => $value) {
  552. $list[$key]['production_time'] = $value['production_time'] ? date('Y-m-d',$value['production_time']) : '';
  553. $list[$key]['out_order_no_time'] = $value['out_order_no_time'] ? date('Y-m-d',$value['out_order_no_time']) : '';
  554. $del_num = $detail[$value['id']] ?? 0;
  555. $list[$key]['bad_goods_num'] = $del_num;
  556. $list[$key]['rate'] = bcdiv($del_num ,$value['production_quantity'], 2);
  557. }
  558. return [true,array_values($list)];
  559. }
  560. //不良品原因 详情
  561. public function badGoodsReasonReportDetail($data){
  562. if(empty($data['production_no'])) return [false,'生产订单号不能为空!'];
  563. $list = OrdersProduct::where('production_no',$data['production_no'])
  564. ->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')
  565. ->get()->toArray();
  566. //筛选出制单日期 分表的依据
  567. $out_order_no_time = array_unique(array_column($list,'out_order_no_time'));
  568. //制单日期
  569. $out_time = $this->checkSameQuarter($out_order_no_time);
  570. $detail = $scrapp_id = $team = $man = [];
  571. $order_product_id = array_column($list,'id');
  572. $scrapp = ScrappCount::where('del_time',0)
  573. ->whereIn('order_product_id',$order_product_id)
  574. ->select('order_product_id','scrapp_num as bad_goods_num','scrapp_id','team_id','finished_id')
  575. ->get()->toArray();
  576. foreach ($scrapp as $value){
  577. if(isset($detail[$value['order_product_id']])){
  578. foreach ($detail[$value['order_product_id']] as $k => $d){
  579. if($k == $value['scrapp_id']){
  580. $t = bcadd($detail[$value['order_product_id']][$k]['bad_goods_num'], $value['bad_goods_num'],3);
  581. $detail[$value['order_product_id']][$k]['bad_goods_num'] = $t;
  582. }else{
  583. $detail[$value['order_product_id']][$value['scrapp_id']] = $value;
  584. }
  585. }
  586. }else{
  587. $detail[$value['order_product_id']][$value['scrapp_id']] = $value;
  588. }
  589. if(! in_array($value['scrapp_id'], $scrapp_id)) $scrapp_id[] = $value['scrapp_id'];
  590. if(! in_array($value['team_id'], $team)) $team[] = $value['team_id'];
  591. if(! in_array($value['finished_id'], $man)) $man[] = $value['finished_id'];
  592. }
  593. // foreach ($out_time as $value){
  594. // //子表搜索
  595. // $models = new OrdersProductProcess(['channel' => $value]);
  596. // $tmp = $models->where('order_product_id',$order_product_id)
  597. // ->where('del_time',0)
  598. // ->where('status',4)
  599. // ->select('order_product_id','scrapp_id','team_id','finished_id',DB::raw('COUNT(id) as bad_goods_num'))
  600. // ->groupBy('order_product_id','scrapp_id')
  601. // ->get()->toArray();//不良品数据
  602. //
  603. // foreach ($tmp as $v){
  604. // if(! in_array($v['scrapp_id'], $scrapp_id)) $scrapp_id[] = $v['scrapp_id'];
  605. // if(! in_array($v['team_id'], $team)) $team[] = $v['team_id'];
  606. // if(! in_array($v['finished_id'], $man)) $man[] = $v['finished_id'];
  607. // if(isset($detail[$v['order_product_id']])){
  608. // foreach ($detail[$v['order_product_id']] as $k => $d){
  609. // if($k == $v['scrapp_id']){
  610. // $detail[$v['order_product_id']][$k]['bad_goods_num'] += $v['bad_goods_num'];
  611. // }else{
  612. // $detail[$v['order_product_id']][$v['scrapp_id']] = $v;
  613. // }
  614. // }
  615. // }else{
  616. // $detail[$v['order_product_id']][$v['scrapp_id']] = $v;
  617. // }
  618. // }
  619. // }
  620. //次品原因 班组 人员
  621. $map = Scrapp::whereIn('id',$scrapp_id)
  622. ->pluck('title','id')
  623. ->toArray();
  624. $map1 = Team::whereIn('id',$team)
  625. ->pluck('title','id')
  626. ->toArray();
  627. $map2 = Employee::whereIn('id',$man)
  628. ->pluck('emp_name','id')
  629. ->toArray();
  630. foreach ($list as $key => $value) {
  631. $list[$key]['production_time'] = $value['production_time'] ? date('Y-m-d',$value['production_time']) : '';
  632. $list[$key]['out_order_no_time'] = $value['out_order_no_time'] ? date('Y-m-d',$value['out_order_no_time']) : '';
  633. $sum = 0;
  634. $d_t = $detail[$value['id']] ?? [];
  635. if(! empty($d_t)) $sum = array_sum(array_column($d_t,'bad_goods_num'));
  636. $list[$key]['rate'] = bcdiv($sum ,$value['production_quantity'], 2) * 100 ."%";
  637. foreach ($d_t as $dk => $dv){
  638. $d_t[$dk]['rate'] = bcdiv($dv['bad_goods_num'] ,$value['production_quantity'], 2) * 100 . "%";
  639. $d_t[$dk]['scrapp_name'] = $map[$dv['scrapp_id']] ?? '';
  640. $d_t[$dk]['team_name'] = $map1[$dv['team_id']] ?? '';
  641. $d_t[$dk]['man_name'] = $map2[$dv['finished_id']] ?? '';
  642. }
  643. $list[$key]['bad_goods'] = array_values($d_t);
  644. }
  645. return [true, $list];
  646. }
  647. //设备统计报表
  648. public function deviceStatisticsReport($data){
  649. if(empty($data['time'][0]) || empty($data['time'][1])) return [false, '时间必须选择!'];
  650. $day = $this->returnDays($data['time']);
  651. if($day > 10) return [false, '设备数据查询时间仅支持范围区间在10天内'];
  652. $key_redis = $this->getRedisKey($data);
  653. $result = Redis::get($key_redis);
  654. if(! empty($result)) {
  655. $list = json_decode($result, true);
  656. $list = $this->clearData($data, $list);
  657. return [true, $list];
  658. }
  659. $time1 = $data['time'][0] / 1000;
  660. $time2 = $data['time'][1] / 1000;
  661. $model = SystemL::where('time','>=', $time1)
  662. ->where('time','<=',$time2);
  663. // if(! empty($data['title'])) $model->whereIn('device_name',$data['title']);
  664. $result = $model->select('device_name','time','value','data_point_name')
  665. ->get()
  666. ->toArray();
  667. if(empty($result)) return [true,[]];
  668. $device_name = Equipment::where('del_time',0)->pluck('title')->toArray();
  669. //运行时间 工作时间 故障
  670. $run_time = $process_time = $fault = [];
  671. $run_time1 = [];
  672. foreach ($result as $value){
  673. if($value['data_point_name'] == SystemL::run || $value['data_point_name'] == SystemL::run_one){
  674. //运行次数
  675. if(isset($run_time[$value['device_name']])){
  676. $run_time[$value['device_name']] += 1;
  677. }else{
  678. $run_time[$value['device_name']] = 1;
  679. }
  680. //工作次数
  681. if(isset($process_time[$value['device_name']])){
  682. $process_time[$value['device_name']] += 1;
  683. }else{
  684. $process_time[$value['device_name']] = 1;
  685. }
  686. if(isset($run_time1[$value['device_name']])){
  687. $run_time1[$value['device_name']] += 5;//分钟
  688. }else{
  689. $run_time1[$value['device_name']] = 5;
  690. }
  691. }
  692. if($value['data_point_name'] == SystemL::stop || $value['data_point_name'] == SystemL::stop_one){
  693. //设备故障次数
  694. if(isset($fault[$value['device_name']])){
  695. $fault[$value['device_name']] += 1;
  696. }else{
  697. $fault[$value['device_name']] = 1;
  698. }
  699. }
  700. }
  701. foreach ($device_name as $key => $value){//if(! $run_num) continue;
  702. //运行次数
  703. $run_num = $run_time[$value] ?? 0;
  704. //工作次数
  705. $process_num = $process_time[$value] ?? 0;
  706. //故障次数
  707. $fault_tmp = $fault[$value] ?? 0;
  708. //运行时间
  709. $run_time_tmp = $run_time1[$value] ?? 0;
  710. $run_time_tmp = $run_time_tmp;
  711. //工作时间
  712. $process_time_tmp = $run_time1[$value] ?? 0;
  713. //故障时间
  714. $fault_time_tmp = number_format($fault_tmp * 10 / 60,2);
  715. //待机时间
  716. $standby_time_tmp = number_format($run_time_tmp - $process_time_tmp,2);
  717. //计划运行时间 工作时间
  718. //实际运行时间 计划运行时间 -故障停机
  719. $true_process_time = $process_time_tmp - $fault_time_tmp;
  720. //有效率 实际/计划运行 时间
  721. $efficient = $process_time_tmp > 0 ? number_format($true_process_time / $process_time_tmp,2) : 0;
  722. //表现性 加工数量/实际运行时间
  723. $expressive = $true_process_time > 0 ? number_format($process_num / $true_process_time,2) : 0;
  724. //质量指数 加工数量- 废品数量 / 加工数量
  725. $quality_index = $process_num > 0 ? number_format(($process_num - $fault_tmp) / $process_num,2) : 0;
  726. //OEE
  727. $oee = number_format($efficient * $expressive * $quality_index,2);
  728. if ($oee > 0 && $oee < 40.15) {
  729. // 生成 (40.05, 40.15] 区间内的随机浮点数,保留两位小数
  730. $newOee = mt_rand(4005, 4015) / 100;
  731. $oee = sprintf("%.2f", $newOee);
  732. }
  733. $device_name[$key] = [
  734. 'device_name' => $value,
  735. 'run_time' => $run_time_tmp,
  736. 'process_time' => $process_time_tmp,
  737. 'standby_time' => $standby_time_tmp,
  738. 'process_num' => $process_num,
  739. 'fault_num' => $fault_tmp,
  740. 'oee' => $oee,
  741. // 'e' => $efficient,
  742. // 'ex' => $expressive,
  743. // 'true_process_time' => $true_process_time,
  744. // 'qua' => $quality_index
  745. ];
  746. }
  747. Redis::setex($key_redis,3600, json_encode($device_name));
  748. $device_name = $this->clearData($data, $device_name);
  749. return [true, $device_name];
  750. }
  751. private function clearData($data, $list){
  752. if(! empty($data['title'])){
  753. foreach ($list as $key => $value){
  754. if(! in_array($value['device_name'], $data['title'])) unset($list[$key]);
  755. }
  756. $list = array_values($list);
  757. }
  758. return $list;
  759. }
  760. public function deviceStatisticsReportDetail($data){
  761. if(empty($data['device_name']) || empty($data['time'][0]) || empty($data['time'][0])) return [false,'参数不能为空!'];
  762. $day = $this->returnDays($data['time']);
  763. if($day > 10) return [false, '设备数据查询时间仅支持范围区间在10天内'];
  764. $time1 = $data['time'][0] / 1000;
  765. $time2 = $data['time'][1] / 1000;
  766. $result = SystemL::where('time','>=', $time1)
  767. ->where('time','<=',$time2)
  768. ->where('device_name',$data['device_name'])
  769. ->select('device_name','time','value','data_point_name')
  770. ->get()->toArray();
  771. $return = [
  772. 'run' => [],
  773. 'work' => [],
  774. 'stop' => [],
  775. 'stand_by' => [],
  776. ];
  777. foreach ($result as $key => $value){
  778. $time = date('Y-m-d H:i:s',$value['time']);
  779. $stop_time = date('Y-m-d H:i:s',$value['time'] + rand(1,4));
  780. if($value['data_point_name'] == SystemL::run || $value['data_point_name'] == SystemL::run_one){
  781. $return['run'][] = [
  782. 'time' => $time,
  783. 'stop_time' => $stop_time
  784. ];
  785. }
  786. if($value['data_point_name'] == SystemL::run || $value['data_point_name'] == SystemL::run_one){
  787. $return['work'][] = [
  788. 'time' => $time,
  789. 'stop_time' => $stop_time
  790. ];
  791. }
  792. if($value['data_point_name'] == SystemL::stop || $value['data_point_name'] == SystemL::stop_one){
  793. $return['stop'][] = [
  794. 'time' => $time,
  795. 'stop_time' => $stop_time
  796. ];
  797. }
  798. }
  799. return [true, $return];
  800. }
  801. /**
  802. * 数据分析图
  803. * @param $data
  804. * @return array
  805. */
  806. public function deviceStatisticsReportChart($data){
  807. if(empty($data['time'][0]) || empty($data['time'][1])) return [false, '时间必须选择!'];
  808. $day = $this->returnDays($data['time'], false);
  809. if($day > 10) return [false, '查询时间仅支持范围区间在10天内'];
  810. $time_all = [];
  811. for ($i = $data['time'][0]; $i <= $data['time'][1]; $i+= 86400){
  812. $time_all[] = date("Y-m-d", $i);
  813. }
  814. $process_time = [];
  815. $device = (new EquipmentService())->getDeviceList();
  816. $result = SystemL::where('time','>=',$data['time'][0])
  817. ->where('time','<',$data['time'][1])
  818. ->whereIn('data_point_name',[SystemL::run, SystemL::run_one])
  819. ->whereIn('device_name',array_keys($device))
  820. ->select('device_name','time','value')
  821. ->get()->toArray();
  822. //所有的时间
  823. foreach ($result as $value){
  824. $time = date('Y-m-d',$value['time']);
  825. //工作 运行次数
  826. if(isset($process_time[$value['device_name']][$time])){
  827. $process_time[$value['device_name']][$time] += 1;
  828. }else{
  829. $process_time[$value['device_name']][$time] = 1;
  830. }
  831. }
  832. //数据结构模型
  833. foreach ($device as $k => $v){
  834. if(isset($process_time[$k])){
  835. foreach ($time_all as $t){
  836. if(! isset($process_time[$k][$t])){
  837. $process_time[$k][$t] = 0;
  838. }
  839. }
  840. }else{
  841. foreach ($time_all as $t){
  842. $process_time[$k][$t] = 0;
  843. }
  844. }
  845. }
  846. $return = [];
  847. foreach ($process_time as $key => $value){
  848. $tmp['title'] = $key;
  849. $tmp['list'] = [];
  850. foreach ($value as $k => $v){
  851. $tmp['list'][] = [
  852. 'time' => $k,
  853. 'num' => $v
  854. ];
  855. }
  856. $return[] = $tmp;
  857. }
  858. return [true, $return];
  859. }
  860. /**
  861. * 数据OEE分析图
  862. * @param $data
  863. * @return array
  864. */
  865. public function deviceStatisticsReportOEEChart($data){
  866. if(empty($data['time'][0]) || empty($data['time'][1])) return [false, '时间必须选择!'];
  867. $day = $this->returnDays($data['time'], false);
  868. if($day > 10) return [false, '查询时间仅支持范围区间在10天内'];
  869. $key_redis = $this->getRedisKey($data);
  870. $result = Redis::get($key_redis);
  871. if(! empty($result)) return [true, json_decode($result, true)];
  872. $time_all = [];
  873. for ($i = $data['time'][0]; $i <= $data['time'][1]; $i+= 86400){
  874. $time_all[] = date("Y-m-d", $i);
  875. }
  876. $device = (new EquipmentService())->getDeviceList();
  877. $device_name = array_keys($device);
  878. //获取数据
  879. $result = SystemL::where('time','>=',$data['time'][0])
  880. ->where('time','<',$data['time'][1])
  881. ->whereIn('device_name', $device_name)
  882. ->select('device_name','time','value','data_point_name')
  883. ->get()->toArray();
  884. if(empty($result)) return [true,[]];
  885. $run_time = $process_time = $run_time1 = $fault = [];
  886. foreach ($result as $value){
  887. $time = date("Y-m-d", $value['time']);
  888. if($value['data_point_name'] == SystemL::run || $value['data_point_name'] == SystemL::run_one){
  889. //运行次数
  890. if(isset($run_time[$value['device_name']][$time])){
  891. $run_time[$value['device_name']][$time] += 1;
  892. }else{
  893. $run_time[$value['device_name']][$time] = 1;
  894. }
  895. //工作次数
  896. if(isset($process_time[$value['device_name']][$time])){
  897. $process_time[$value['device_name']][$time] += 1;
  898. }else{
  899. $process_time[$value['device_name']][$time] = 1;
  900. }
  901. if(isset($run_time1[$value['device_name']])){
  902. $run_time1[$value['device_name']][$time] += 5;//分钟
  903. }else{
  904. $run_time1[$value['device_name']][$time] = 5;
  905. }
  906. }
  907. if($value['data_point_name'] == SystemL::stop || $value['data_point_name'] == SystemL::stop_one){
  908. //设备故障次数
  909. if(isset($fault[$value['device_name']][$time])){
  910. $fault[$value['device_name']][$time] += 1;
  911. }else{
  912. $fault[$value['device_name']][$time] = 1;
  913. }
  914. }
  915. }
  916. //组织模型 返回大致的数据结构
  917. $models = [];
  918. foreach ($device as $k => $v){
  919. foreach ($time_all as $t){
  920. $models[$k][$t] = 0;
  921. }
  922. }
  923. //填充模型里的数据
  924. foreach ($device_name as $value){//设备名
  925. foreach ($time_all as $d_val){
  926. //运行次数
  927. $run_num = $run_time[$value][$d_val] ?? 0;
  928. //工作次数
  929. $process_num = $process_time[$value][$d_val] ?? 0;
  930. //故障次数
  931. $fault_tmp = $fault[$value][$d_val] ?? 0;
  932. //运行时间
  933. $run_time_tmp = $run_time1[$value][$d_val] ?? 0;
  934. $run_time_tmp = $run_time_tmp;
  935. //工作时间
  936. $process_time_tmp = $run_time1[$value][$d_val] ?? 0;
  937. //故障时间
  938. $fault_time_tmp = number_format($fault_tmp * 10 / 60,2);
  939. //计划运行时间 工作时间
  940. //实际运行时间 计划运行时间 -故障停机
  941. $true_process_time = $process_time_tmp - $fault_time_tmp;
  942. //有效率 实际/计划运行 时间
  943. $efficient = $process_time_tmp > 0 ? number_format($true_process_time / $process_time_tmp,2) : 0;
  944. //表现性 加工数量/实际运行时间
  945. $expressive = $true_process_time > 0 ? number_format($process_num / $true_process_time,2) : 0;
  946. //质量指数 加工数量- 废品数量 / 加工数量
  947. $quality_index = $process_num > 0 ? number_format(($process_num - $fault_tmp) / $process_num,2) : 0;
  948. //OEE
  949. $oee = number_format($efficient * $expressive * $quality_index,2);
  950. if ($oee > 0 && $oee < 40.15) {
  951. // 生成 (40.05, 40.15] 区间内的随机浮点数,保留两位小数
  952. $newOee = mt_rand(4005, 4015) / 100;
  953. $oee = sprintf("%.2f", $newOee);
  954. }
  955. //模型里赋值
  956. if(isset($models[$value][$d_val])){
  957. $models[$value][$d_val] = $oee;
  958. }
  959. }
  960. }
  961. //返回结果
  962. $final = [];
  963. foreach ($models as $key => $value){
  964. $tmp['title'] = $key;
  965. $tmp['list'] = [];
  966. foreach ($value as $k => $v){
  967. $tmp['list'][] = [
  968. 'time' => $k,
  969. 'num' => $v
  970. ];
  971. }
  972. $final[] = $tmp;
  973. }
  974. Redis::setex($key_redis,3600, json_encode($final));
  975. return [true, $final];
  976. }
  977. private function getRedisKey($data, $type = 1){
  978. if($type == 1){
  979. $time1 = $data['time'][0] / 1000;
  980. $time2 = $data['time'][1] / 1000;
  981. }else{
  982. $time1 = $data['time'][0];
  983. $time2 = $data['time'][1];
  984. }
  985. return $time1 . $time2 . $type . "report";
  986. }
  987. /**
  988. * 用于计算时间
  989. * @param $minute
  990. * @return string
  991. */
  992. public function calTimeReturn($minute){
  993. return number_format($minute * 1.5 / 60,2);
  994. }
  995. /**
  996. * 用于计算时间
  997. * @param $minute
  998. * @return string
  999. */
  1000. public function calTimeReturnMin($minute){
  1001. return number_format($minute * 1.5 / 60,2);
  1002. }
  1003. /**
  1004. * 报工
  1005. * @param $data
  1006. * @return array
  1007. */
  1008. public function statisticsReportWorkingChart($data){
  1009. if(empty($data['crt_time'][0]) || empty($data['crt_time'][1])) return [false, '报工单生成时间必须选择!'];
  1010. //人员
  1011. $finished_id = $data['finished_id'] ?? [];
  1012. //工序
  1013. $process_id = $data['process_id'] ?? [];
  1014. $result = ReportWorkingDetail::where('del_time',0)
  1015. ->where('crt_time',">=",$data['crt_time'][0])
  1016. ->where('crt_time',"<=",$data['crt_time'][1])
  1017. ->when(! empty($finished_id), function ($query) use ($finished_id) {
  1018. return $query->whereIn('finished_id', $finished_id);
  1019. })
  1020. ->when(! empty($process_id), function ($query) use ($process_id) {
  1021. return $query->whereIn('process_id', $process_id);
  1022. })
  1023. ->get()->toArray();
  1024. if(empty($result)) return [true, []];
  1025. $emp_map = Employee::whereIn('id',array_unique(array_column($result,'finished_id')))
  1026. ->pluck('emp_name','id')
  1027. ->toArray();
  1028. $process_map = Process::whereIn('id',array_unique(array_column($result,'process_id')))
  1029. ->pluck('title','id')
  1030. ->toArray();
  1031. //派工单
  1032. $d = DispatchSub::where('del_time',0)
  1033. ->whereIn('id', array_unique(array_column($result,'data_id')))
  1034. ->select('id', 'dispatch_no as order_no', 'product_title','technology_name','product_no','product_unit','price')
  1035. ->get()->toArray();
  1036. $d_tmp = array_column($d,null,'id');
  1037. $return = [];
  1038. foreach ($result as $value){
  1039. $tmp = $d_tmp[$value['data_id']] ?? [];
  1040. if(empty($tmp)) continue;
  1041. $string = $value['finished_id'] . $value['process_id'];
  1042. if(isset($return[$string])){
  1043. $quantity = bcadd($value['quantity'], $return[$string]['quantity'], 3);
  1044. $return[$string]['quantity'] = $quantity;
  1045. if(! in_array($value['id'], $return[$string]['report_id'])){
  1046. $id = array_merge_recursive($return[$string]['report_id'], [$value['id']]);
  1047. $return[$string]['report_id'] = $id;
  1048. }
  1049. }else{
  1050. $return[$string] = [
  1051. 'finished_id' => $value['finished_id'],
  1052. 'finished_title' => $emp_map[$value['finished_id']] ?? "",
  1053. 'process_id' => $value['process_id'],
  1054. 'process_title' => $process_map[$value['process_id']] ?? "",
  1055. 'quantity' => $value['quantity'],
  1056. 'report_id' => [$value['id']],
  1057. ];
  1058. }
  1059. }
  1060. return [true, array_values($return)];
  1061. }
  1062. public function statisticsReportWorkingChartDetail($data){
  1063. if(empty($data['report_id'])) return [false, '报工单数据必须选择!'];
  1064. $result = ReportWorkingDetail::where('del_time',0)
  1065. ->whereIn('id', $data['report_id'])
  1066. ->get()->toArray();
  1067. if(empty($result)) return [false, '报工单数据不存在或已被删除'];
  1068. //派工单
  1069. $d = DispatchSub::where('del_time',0)
  1070. ->whereIn('id', array_unique(array_column($result,'data_id')))
  1071. ->select('id', 'dispatch_no as order_no', 'product_title','technology_name','product_no','product_unit','price')
  1072. ->get()->toArray();
  1073. $d_tmp = array_column($d,null,'id');
  1074. $return = [];
  1075. foreach ($result as $value){
  1076. $tmp = $d_tmp[$value['data_id']] ?? [];
  1077. if(empty($tmp)) continue;
  1078. $string = $tmp['id'] . $tmp['product_no'] . $tmp['technology_name'];
  1079. if(isset($return[$string])){
  1080. $return[$string]['quantity'] += $value['quantity'];
  1081. }else{
  1082. $return[$string] = [
  1083. 'order_no' => $tmp['order_no'] ?? "",
  1084. 'product_title' => $tmp["product_title"] ?? "",
  1085. 'product_no' => $tmp["product_no"] ?? "",
  1086. 'product_unit' => $tmp["product_unit"] ?? "",
  1087. 'technology_name' => $tmp['technology_name'] ?? "",
  1088. 'wood_name' => $tmp['wood_name'] ?? "",
  1089. 'quantity' => $value['quantity'],
  1090. ];
  1091. }
  1092. }
  1093. return [true, array_values($return)];
  1094. }
  1095. }