ReportFormsService.php 59 KB

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