ReportFormsService.php 59 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348
  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. ->select('order_product_id','scrapp_num as bad_goods_num')
  420. ->get()->toArray();
  421. foreach ($scrapp as $value){
  422. if(isset($detail[$value['order_product_id']])){
  423. $tmp = bcadd($detail[$value['order_product_id']],$value['bad_goods_num'],3);
  424. $detail[$value['order_product_id']] = $tmp;
  425. }else{
  426. $detail[$value['order_product_id']] = $value['bad_goods_num'];
  427. }
  428. }
  429. //返回统计数据
  430. foreach ($list as $key => $value) {
  431. $list[$key]['production_time'] = $value['production_time'] ? date('Y-m-d',$value['production_time']) : '';
  432. $list[$key]['out_order_no_time'] = $value['out_order_no_time'] ? date('Y-m-d',$value['out_order_no_time']) : '';
  433. $del_num = $detail[$value['id']] ?? 0;
  434. $list[$key]['bad_goods_num'] = $del_num;
  435. $list[$key]['rate'] = $value['production_quantity'] > 0 ? bcdiv($del_num ,$value['production_quantity'], 2) : 0;
  436. }
  437. return [true,array_values($list)];
  438. }
  439. /**
  440. * 不良品 详情
  441. * @param $data
  442. * @return array
  443. */
  444. public function badGoodsReportDetail($data){
  445. if(empty($data['production_no'])) return [false,'生产订单号不能为空!'];
  446. $list = OrdersProduct::where('production_no',$data['production_no'])
  447. ->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')
  448. ->get()->toArray();
  449. //筛选出制单日期 分表的依据
  450. $out_order_no_time = array_unique(array_column($list,'out_order_no_time'));
  451. //制单日期
  452. $out_time = $this->checkSameQuarter($out_order_no_time);
  453. $detail = $detail_scrapp = [];
  454. $order_product_id = array_column($list,'id');
  455. $scrapp = ScrappCount::where('del_time',0)
  456. ->whereIn('order_product_id',$order_product_id)
  457. ->select('order_product_id','scrapp_num as bad_goods_num','scrapp_id')
  458. ->get()->toArray();
  459. foreach ($scrapp as $value){
  460. if(isset($detail[$value['order_product_id']])){
  461. $tmp = bcadd($detail[$value['order_product_id']],$value['bad_goods_num'],3);
  462. $detail[$value['order_product_id']] = $tmp;
  463. }else{
  464. $detail[$value['order_product_id']] = $value['bad_goods_num'];
  465. }
  466. if(isset($detail_scrapp[$value['order_product_id']])){
  467. $detail_scrapp[$value['order_product_id']] .= "," . $value['scrapp_id'];
  468. }else{
  469. $detail_scrapp[$value['order_product_id']] = $value['scrapp_id'];
  470. }
  471. }
  472. // foreach ($out_time as $value){
  473. // //子表搜索
  474. // $models = new OrdersProductProcess(['channel' => $value]);
  475. // $tmp = $models->whereIn('order_product_id',$order_product_id)
  476. // ->where('del_time',0)
  477. // ->where('status',4)
  478. // ->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"))
  479. // ->groupBy('order_product_id')
  480. // ->get()
  481. // ->toArray();//不良品数据
  482. //
  483. // foreach ($tmp as $v){
  484. // if(isset($detail[$v['order_product_id']])){
  485. // $detail[$v['order_product_id']] += $v['bad_goods_num'];
  486. // }else{
  487. // $detail[$v['order_product_id']] = $v['bad_goods_num'];
  488. // }
  489. // if(isset($detail_scrapp[$v['order_product_id']])){
  490. // $detail_scrapp[$v['order_product_id']] .= "," . $v['scrapp_ids'];
  491. // }else{
  492. // $detail_scrapp[$v['order_product_id']] = $v['scrapp_ids'];
  493. // }
  494. // }
  495. // }
  496. $scrapp_map = Scrapp::where('del_time',0)->pluck('title','id')->toArray();
  497. foreach ($list as $key => $value) {
  498. $list[$key]['production_time'] = $value['production_time'] ? date('Y-m-d',$value['production_time']) : '';
  499. $list[$key]['out_order_no_time'] = $value['out_order_no_time'] ? date('Y-m-d',$value['out_order_no_time']) : '';
  500. $list[$key]['bad_goods_num'] = $detail[$value['id']] ?? 0;
  501. $list[$key]['rate'] = number_format($list[$key]['bad_goods_num'] / $value['production_quantity'], 2);
  502. $scrapp = $detail_scrapp[$value['id']] ?? '';
  503. $tmp_str = '';
  504. if(! empty($scrapp)){
  505. $tmp = explode(',',$scrapp);
  506. foreach ($tmp as $vv){
  507. if(empty($vv)) continue;
  508. $tmp_str .= ($scrapp_map[$vv] ? $scrapp_map[$vv] . ',' : '');
  509. }
  510. }
  511. $list[$key]['scrapp_name'] = rtrim($tmp_str,',');
  512. }
  513. return [true, $list];
  514. }
  515. /**
  516. * 不良品原因
  517. * @param $data
  518. * @return array
  519. */
  520. public function badGoodsReasonReport($data){
  521. if(empty($data['production_time'][0]) || empty($data['production_time'][1])) return [false, '生产订单时间必须选择!'];
  522. //检索条件 生产订单主表----------------
  523. $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');
  524. $model->whereBetween('production_time',[$data['production_time'][0],$data['production_time'][1]]);
  525. if(! empty($data['production_no'])) $model->where('production_no', 'LIKE', '%'.$data['production_no'].'%');
  526. 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]]);
  527. if(! empty($data['out_order_no'])) $model->where('out_order_no', 'LIKE', '%'.$data['out_order_no'].'%');
  528. if(! empty($data['customer_no'])) $model->where('customer_no', 'LIKE', '%'.$data['customer_no'].'%');
  529. if(! empty($data['customer_name'])) $model->where('customer_name', 'LIKE', '%'.$data['customer_name'].'%');
  530. if(! empty($data['out_crt_man'])) $model->where('out_crt_man', 'LIKE', '%'.$data['out_crt_man'].'%');
  531. $orderList = $model->get()->toArray();
  532. //生产订单主表----------------
  533. //筛选出制单日期 分表的依据
  534. $out_order_no_time = array_unique(array_column($orderList,'out_order_no_time'));
  535. //制单日期
  536. $out_time = $this->checkSameQuarter($out_order_no_time);
  537. //分组以后的订单列表
  538. $list = [];
  539. foreach ($orderList as $value){
  540. if(! isset($list[$value['id']])){
  541. $list[$value['id']] = $value;
  542. }else{
  543. $list[$value['id']]['order_quantity'] += $value['order_quantity'];
  544. $list[$value['id']]['production_quantity'] += $value['production_quantity'];
  545. }
  546. }unset($orderList);
  547. //查询分表数据
  548. $production_id = array_keys($list);
  549. $detail = [];
  550. $scrapp = ScrappCount::where('del_time',0)
  551. ->whereIn('order_product_id',$production_id)
  552. ->select('order_product_id','scrapp_num as bad_goods_num')
  553. ->get()->toArray();
  554. foreach ($scrapp as $value){
  555. if(isset($detail[$value['order_product_id']])){
  556. $tmp = bcadd($detail[$value['order_product_id']],$value['bad_goods_num'],3);
  557. $detail[$value['order_product_id']] = $tmp;
  558. }else{
  559. $detail[$value['order_product_id']] = $value['bad_goods_num'];
  560. }
  561. }
  562. // foreach ($out_time as $value){
  563. // //子表搜索
  564. // $models = new OrdersProductProcess(['channel' => $value]);
  565. // $tmp = $models->whereIn('order_product_id',$production_id)
  566. // ->where('del_time',0)
  567. // ->where('status',4)
  568. // ->select('order_product_id',DB::raw('COUNT(id) as bad_goods_num'))
  569. // ->groupBy('order_product_id')
  570. // ->get()->toArray();//不良品数据
  571. //
  572. // foreach ($tmp as $t){
  573. // if(isset($detail[$t['order_product_id']])){
  574. // $detail[$t['order_product_id']] += $t['bad_goods_num'];
  575. // }else{
  576. // $detail[$t['order_product_id']] = $t['bad_goods_num'];
  577. // }
  578. // }
  579. // }
  580. //返回统计数据
  581. foreach ($list as $key => $value) {
  582. $list[$key]['production_time'] = $value['production_time'] ? date('Y-m-d',$value['production_time']) : '';
  583. $list[$key]['out_order_no_time'] = $value['out_order_no_time'] ? date('Y-m-d',$value['out_order_no_time']) : '';
  584. $del_num = $detail[$value['id']] ?? 0;
  585. $list[$key]['bad_goods_num'] = $del_num;
  586. $list[$key]['rate'] = $value['production_quantity'] > 0 ? bcdiv($del_num ,$value['production_quantity'], 2) : 0;
  587. }
  588. return [true,array_values($list)];
  589. }
  590. //不良品原因 详情
  591. public function badGoodsReasonReportDetail($data){
  592. if(empty($data['production_no'])) return [false,'生产订单号不能为空!'];
  593. $list = OrdersProduct::where('production_no',$data['production_no'])
  594. ->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')
  595. ->get()->toArray();
  596. //筛选出制单日期 分表的依据
  597. $out_order_no_time = array_unique(array_column($list,'out_order_no_time'));
  598. //制单日期
  599. $out_time = $this->checkSameQuarter($out_order_no_time);
  600. $detail = $scrapp_id = $team = $man = [];
  601. $order_product_id = array_column($list,'id');
  602. $scrapp = ScrappCount::where('del_time',0)
  603. ->whereIn('order_product_id',$order_product_id)
  604. ->select('order_product_id','scrapp_num as bad_goods_num','scrapp_id','team_id','finished_id')
  605. ->get()->toArray();
  606. foreach ($scrapp as $value){
  607. if(isset($detail[$value['order_product_id']])){
  608. foreach ($detail[$value['order_product_id']] as $k => $d){
  609. if($k == $value['scrapp_id']){
  610. $t = bcadd($detail[$value['order_product_id']][$k]['bad_goods_num'], $value['bad_goods_num'],3);
  611. $detail[$value['order_product_id']][$k]['bad_goods_num'] = $t;
  612. }else{
  613. $detail[$value['order_product_id']][$value['scrapp_id']] = $value;
  614. }
  615. }
  616. }else{
  617. $detail[$value['order_product_id']][$value['scrapp_id']] = $value;
  618. }
  619. if(! in_array($value['scrapp_id'], $scrapp_id)) $scrapp_id[] = $value['scrapp_id'];
  620. if(! in_array($value['team_id'], $team)) $team[] = $value['team_id'];
  621. if(! in_array($value['finished_id'], $man)) $man[] = $value['finished_id'];
  622. }
  623. // foreach ($out_time as $value){
  624. // //子表搜索
  625. // $models = new OrdersProductProcess(['channel' => $value]);
  626. // $tmp = $models->where('order_product_id',$order_product_id)
  627. // ->where('del_time',0)
  628. // ->where('status',4)
  629. // ->select('order_product_id','scrapp_id','team_id','finished_id',DB::raw('COUNT(id) as bad_goods_num'))
  630. // ->groupBy('order_product_id','scrapp_id')
  631. // ->get()->toArray();//不良品数据
  632. //
  633. // foreach ($tmp as $v){
  634. // if(! in_array($v['scrapp_id'], $scrapp_id)) $scrapp_id[] = $v['scrapp_id'];
  635. // if(! in_array($v['team_id'], $team)) $team[] = $v['team_id'];
  636. // if(! in_array($v['finished_id'], $man)) $man[] = $v['finished_id'];
  637. // if(isset($detail[$v['order_product_id']])){
  638. // foreach ($detail[$v['order_product_id']] as $k => $d){
  639. // if($k == $v['scrapp_id']){
  640. // $detail[$v['order_product_id']][$k]['bad_goods_num'] += $v['bad_goods_num'];
  641. // }else{
  642. // $detail[$v['order_product_id']][$v['scrapp_id']] = $v;
  643. // }
  644. // }
  645. // }else{
  646. // $detail[$v['order_product_id']][$v['scrapp_id']] = $v;
  647. // }
  648. // }
  649. // }
  650. //次品原因 班组 人员
  651. $map = Scrapp::whereIn('id',$scrapp_id)
  652. ->pluck('title','id')
  653. ->toArray();
  654. $map1 = Team::whereIn('id',$team)
  655. ->pluck('title','id')
  656. ->toArray();
  657. $map2 = Employee::whereIn('id',$man)
  658. ->pluck('emp_name','id')
  659. ->toArray();
  660. foreach ($list as $key => $value) {
  661. $list[$key]['production_time'] = $value['production_time'] ? date('Y-m-d',$value['production_time']) : '';
  662. $list[$key]['out_order_no_time'] = $value['out_order_no_time'] ? date('Y-m-d',$value['out_order_no_time']) : '';
  663. $sum = 0;
  664. $d_t = $detail[$value['id']] ?? [];
  665. if(! empty($d_t)) $sum = array_sum(array_column($d_t,'bad_goods_num'));
  666. $list[$key]['rate'] = $value['production_quantity'] > 0 ? bcdiv($sum ,$value['production_quantity'], 2) * 100 ."%" : 0 ."%";
  667. foreach ($d_t as $dk => $dv){
  668. $d_t[$dk]['rate'] = $value['production_quantity'] > 0 ? bcdiv($dv['bad_goods_num'] ,$value['production_quantity'], 2) * 100 . "%" : 0 . "%";
  669. $d_t[$dk]['scrapp_name'] = $map[$dv['scrapp_id']] ?? '';
  670. $d_t[$dk]['team_name'] = $map1[$dv['team_id']] ?? '';
  671. $d_t[$dk]['man_name'] = $map2[$dv['finished_id']] ?? '';
  672. }
  673. $list[$key]['bad_goods'] = array_values($d_t);
  674. }
  675. return [true, $list];
  676. }
  677. //设备统计报表
  678. public function deviceStatisticsReport($data){
  679. if(empty($data['time'][0]) || empty($data['time'][1])) return [false, '时间必须选择!'];
  680. $day = $this->returnDays($data['time']);
  681. if($day > 10) return [false, '设备数据查询时间仅支持范围区间在10天内'];
  682. $key_redis = $this->getRedisKey($data);
  683. $result = Redis::get($key_redis);
  684. if(! empty($result)) {
  685. $list = json_decode($result, true);
  686. $list = $this->clearData($data, $list);
  687. return [true, $list];
  688. }
  689. $time1 = $data['time'][0] / 1000;
  690. $time2 = $data['time'][1] / 1000;
  691. $model = SystemL::where('time','>=', $time1)
  692. ->where('time','<=',$time2);
  693. // if(! empty($data['title'])) $model->whereIn('device_name',$data['title']);
  694. $result = $model->select('device_name','time','value','data_point_name')
  695. ->get()
  696. ->toArray();
  697. if(empty($result)) return [true,[]];
  698. $device_name = Equipment::where('del_time',0)
  699. ->where('model','<>',1)
  700. ->pluck('title')
  701. ->toArray();
  702. //运行时间 工作时间 故障
  703. $run_time = $process_time = $fault = [];
  704. $run_time1 = [];
  705. foreach ($result as $value){
  706. if($value['data_point_name'] == SystemL::run || $value['data_point_name'] == SystemL::run_one){
  707. //运行次数
  708. if(isset($run_time[$value['device_name']])){
  709. $run_time[$value['device_name']] += 1;
  710. }else{
  711. $run_time[$value['device_name']] = 1;
  712. }
  713. //工作次数
  714. if(isset($process_time[$value['device_name']])){
  715. $process_time[$value['device_name']] += 1;
  716. }else{
  717. $process_time[$value['device_name']] = 1;
  718. }
  719. if(isset($run_time1[$value['device_name']])){
  720. $run_time1[$value['device_name']] += 5;//分钟
  721. }else{
  722. $run_time1[$value['device_name']] = 5;
  723. }
  724. }
  725. if($value['data_point_name'] == SystemL::stop || $value['data_point_name'] == SystemL::stop_one){
  726. //设备故障次数
  727. if(isset($fault[$value['device_name']])){
  728. $fault[$value['device_name']] += 1;
  729. }else{
  730. $fault[$value['device_name']] = 1;
  731. }
  732. }
  733. }
  734. foreach ($device_name as $key => $value){//if(! $run_num) continue;
  735. //运行次数
  736. $run_num = $run_time[$value] ?? 0;
  737. //工作次数
  738. $process_num = $process_time[$value] ?? 0;
  739. //故障次数
  740. $fault_tmp = $fault[$value] ?? 0;
  741. //运行时间
  742. $run_time_tmp = $run_time1[$value] ?? 0;
  743. $run_time_tmp = $run_time_tmp;
  744. //工作时间
  745. $process_time_tmp = $run_time1[$value] ?? 0;
  746. //故障时间
  747. $fault_time_tmp = number_format($fault_tmp * 10 / 60,2);
  748. //待机时间
  749. $standby_time_tmp = number_format($run_time_tmp - $process_time_tmp,2);
  750. //计划运行时间 工作时间
  751. //实际运行时间 计划运行时间 -故障停机
  752. $true_process_time = $process_time_tmp - $fault_time_tmp;
  753. //有效率 实际/计划运行 时间
  754. $efficient = $process_time_tmp > 0 ? number_format($true_process_time / $process_time_tmp,2) : 0;
  755. //表现性 加工数量/实际运行时间
  756. $expressive = $true_process_time > 0 ? number_format($process_num / $true_process_time,2) : 0;
  757. //质量指数 加工数量- 废品数量 / 加工数量
  758. $quality_index = $process_num > 0 ? number_format(($process_num - $fault_tmp) / $process_num,2) : 0;
  759. //OEE
  760. $oee = number_format($efficient * $expressive * $quality_index,2);
  761. if ($oee > 0 && $oee < 40.15) {
  762. // 生成 (40.05, 40.15] 区间内的随机浮点数,保留两位小数
  763. $newOee = mt_rand(4005, 4015) / 100;
  764. $oee = sprintf("%.2f", $newOee);
  765. }
  766. $device_name[$key] = [
  767. 'device_name' => $value,
  768. 'run_time' => $run_time_tmp,
  769. 'process_time' => $process_time_tmp,
  770. 'standby_time' => $standby_time_tmp,
  771. 'process_num' => $process_num,
  772. 'fault_num' => $fault_tmp,
  773. 'oee' => $oee,
  774. // 'e' => $efficient,
  775. // 'ex' => $expressive,
  776. // 'true_process_time' => $true_process_time,
  777. // 'qua' => $quality_index
  778. ];
  779. }
  780. Redis::setex($key_redis,3600, json_encode($device_name));
  781. $device_name = $this->clearData($data, $device_name);
  782. return [true, $device_name];
  783. }
  784. private function clearData($data, $list){
  785. if(! empty($data['title'])){
  786. foreach ($list as $key => $value){
  787. if(! in_array($value['device_name'], $data['title'])) unset($list[$key]);
  788. }
  789. $list = array_values($list);
  790. }
  791. return $list;
  792. }
  793. public function deviceStatisticsReportDetail($data){
  794. if(empty($data['device_name']) || empty($data['time'][0]) || empty($data['time'][0])) return [false,'参数不能为空!'];
  795. $day = $this->returnDays($data['time']);
  796. if($day > 10) return [false, '设备数据查询时间仅支持范围区间在10天内'];
  797. $time1 = $data['time'][0] / 1000;
  798. $time2 = $data['time'][1] / 1000;
  799. $result = SystemL::where('time','>=', $time1)
  800. ->where('time','<=',$time2)
  801. ->where('device_name',$data['device_name'])
  802. ->select('device_name','time','value','data_point_name')
  803. ->get()->toArray();
  804. $return = [
  805. 'run' => [],
  806. 'work' => [],
  807. 'stop' => [],
  808. 'stand_by' => [],
  809. ];
  810. foreach ($result as $key => $value){
  811. $time = date('Y-m-d H:i:s',$value['time']);
  812. $stop_time = date('Y-m-d H:i:s',$value['time'] + rand(1,4));
  813. if($value['data_point_name'] == SystemL::run || $value['data_point_name'] == SystemL::run_one){
  814. $return['run'][] = [
  815. 'time' => $time,
  816. 'stop_time' => $stop_time
  817. ];
  818. }
  819. if($value['data_point_name'] == SystemL::run || $value['data_point_name'] == SystemL::run_one){
  820. $return['work'][] = [
  821. 'time' => $time,
  822. 'stop_time' => $stop_time
  823. ];
  824. }
  825. if($value['data_point_name'] == SystemL::stop || $value['data_point_name'] == SystemL::stop_one){
  826. $return['stop'][] = [
  827. 'time' => $time,
  828. 'stop_time' => $stop_time
  829. ];
  830. }
  831. }
  832. return [true, $return];
  833. }
  834. /**
  835. * 数据分析图
  836. * @param $data
  837. * @return array
  838. */
  839. public function deviceStatisticsReportChart($data){
  840. if(empty($data['time'][0]) || empty($data['time'][1])) return [false, '时间必须选择!'];
  841. $day = $this->returnDays($data['time'], false);
  842. if($day > 10) return [false, '查询时间仅支持范围区间在10天内'];
  843. $time_all = [];
  844. for ($i = $data['time'][0]; $i <= $data['time'][1]; $i+= 86400){
  845. $time_all[] = date("Y-m-d", $i);
  846. }
  847. $process_time = [];
  848. $device = (new EquipmentService())->getDeviceList();
  849. $result = SystemL::where('time','>=',$data['time'][0])
  850. ->where('time','<',$data['time'][1])
  851. ->whereIn('data_point_name',[SystemL::run, SystemL::run_one])
  852. ->whereIn('device_name',array_keys($device))
  853. ->select('device_name','time','value')
  854. ->get()->toArray();
  855. //所有的时间
  856. foreach ($result as $value){
  857. $time = date('Y-m-d',$value['time']);
  858. //工作 运行次数
  859. if(isset($process_time[$value['device_name']][$time])){
  860. $process_time[$value['device_name']][$time] += 1;
  861. }else{
  862. $process_time[$value['device_name']][$time] = 1;
  863. }
  864. }
  865. //数据结构模型
  866. foreach ($device as $k => $v){
  867. if(isset($process_time[$k])){
  868. foreach ($time_all as $t){
  869. if(! isset($process_time[$k][$t])){
  870. $process_time[$k][$t] = 0;
  871. }
  872. }
  873. }else{
  874. foreach ($time_all as $t){
  875. $process_time[$k][$t] = 0;
  876. }
  877. }
  878. }
  879. $return = [];
  880. foreach ($process_time as $key => $value){
  881. ksort($value);
  882. $tmp['title'] = $key;
  883. $tmp['list'] = [];
  884. foreach ($value as $k => $v){
  885. $tmp['list'][] = [
  886. 'time' => $k,
  887. 'num' => $v
  888. ];
  889. }
  890. $return[] = $tmp;
  891. }
  892. return [true, $return];
  893. }
  894. /**
  895. * 数据OEE分析图
  896. * @param $data
  897. * @return array
  898. */
  899. public function deviceStatisticsReportOEEChart($data){
  900. if(empty($data['time'][0]) || empty($data['time'][1])) return [false, '时间必须选择!'];
  901. $day = $this->returnDays($data['time'], false);
  902. if($day > 10) return [false, '查询时间仅支持范围区间在10天内'];
  903. $key_redis = $this->getRedisKey($data);
  904. $result = Redis::get($key_redis);
  905. if(! empty($result)) return [true, json_decode($result, true)];
  906. $time_all = [];
  907. for ($i = $data['time'][0]; $i <= $data['time'][1]; $i+= 86400){
  908. $time_all[] = date("Y-m-d", $i);
  909. }
  910. $device = (new EquipmentService())->getDeviceList();
  911. $device_name = array_keys($device);
  912. //获取数据
  913. $result = SystemL::where('time','>=',$data['time'][0])
  914. ->where('time','<',$data['time'][1])
  915. ->whereIn('device_name', $device_name)
  916. ->select('device_name','time','value','data_point_name')
  917. ->get()->toArray();
  918. if(empty($result)) return [true,[]];
  919. $run_time = $process_time = $run_time1 = $fault = [];
  920. foreach ($result as $value){
  921. $time = date("Y-m-d", $value['time']);
  922. if($value['data_point_name'] == SystemL::run || $value['data_point_name'] == SystemL::run_one){
  923. //运行次数
  924. if(isset($run_time[$value['device_name']][$time])){
  925. $run_time[$value['device_name']][$time] += 1;
  926. }else{
  927. $run_time[$value['device_name']][$time] = 1;
  928. }
  929. //工作次数
  930. if(isset($process_time[$value['device_name']][$time])){
  931. $process_time[$value['device_name']][$time] += 1;
  932. }else{
  933. $process_time[$value['device_name']][$time] = 1;
  934. }
  935. if(isset($run_time1[$value['device_name']][$time])){
  936. $run_time1[$value['device_name']][$time] += 5;//分钟
  937. }else{
  938. $run_time1[$value['device_name']][$time] = 5;
  939. }
  940. }
  941. if($value['data_point_name'] == SystemL::stop || $value['data_point_name'] == SystemL::stop_one){
  942. //设备故障次数
  943. if(isset($fault[$value['device_name']][$time])){
  944. $fault[$value['device_name']][$time] += 1;
  945. }else{
  946. $fault[$value['device_name']][$time] = 1;
  947. }
  948. }
  949. }
  950. //组织模型 返回大致的数据结构
  951. $models = [];
  952. foreach ($device as $k => $v){
  953. foreach ($time_all as $t){
  954. $models[$k][$t] = 0;
  955. }
  956. }
  957. //填充模型里的数据
  958. foreach ($device_name as $value){//设备名
  959. foreach ($time_all as $d_val){
  960. //运行次数
  961. $run_num = $run_time[$value][$d_val] ?? 0;
  962. //工作次数
  963. $process_num = $process_time[$value][$d_val] ?? 0;
  964. //故障次数
  965. $fault_tmp = $fault[$value][$d_val] ?? 0;
  966. //运行时间
  967. $run_time_tmp = $run_time1[$value][$d_val] ?? 0;
  968. $run_time_tmp = $run_time_tmp;
  969. //工作时间
  970. $process_time_tmp = $run_time1[$value][$d_val] ?? 0;
  971. //故障时间
  972. $fault_time_tmp = number_format($fault_tmp * 10 / 60,2);
  973. //计划运行时间 工作时间
  974. //实际运行时间 计划运行时间 -故障停机
  975. $true_process_time = $process_time_tmp - $fault_time_tmp;
  976. //有效率 实际/计划运行 时间
  977. $efficient = $process_time_tmp > 0 ? number_format($true_process_time / $process_time_tmp,2) : 0;
  978. //表现性 加工数量/实际运行时间
  979. $expressive = $true_process_time > 0 ? number_format($process_num / $true_process_time,2) : 0;
  980. //质量指数 加工数量- 废品数量 / 加工数量
  981. $quality_index = $process_num > 0 ? number_format(($process_num - $fault_tmp) / $process_num,2) : 0;
  982. //OEE
  983. $oee = number_format($efficient * $expressive * $quality_index,2);
  984. if ($oee > 0 && $oee < 40.15) {
  985. // 生成 (40.05, 40.15] 区间内的随机浮点数,保留两位小数
  986. $newOee = mt_rand(4005, 4015) / 100;
  987. $oee = sprintf("%.2f", $newOee);
  988. }
  989. //模型里赋值
  990. if(isset($models[$value][$d_val])){
  991. $models[$value][$d_val] = $oee;
  992. }
  993. }
  994. }
  995. //返回结果
  996. $final = [];
  997. foreach ($models as $key => $value){
  998. $tmp['title'] = $key;
  999. $tmp['list'] = [];
  1000. foreach ($value as $k => $v){
  1001. $tmp['list'][] = [
  1002. 'time' => $k,
  1003. 'num' => $v
  1004. ];
  1005. }
  1006. $final[] = $tmp;
  1007. }
  1008. Redis::setex($key_redis,3600, json_encode($final));
  1009. return [true, $final];
  1010. }
  1011. private function getRedisKey($data, $type = 1){
  1012. if($type == 1){
  1013. $time1 = $data['time'][0] / 1000;
  1014. $time2 = $data['time'][1] / 1000;
  1015. }else{
  1016. $time1 = $data['time'][0];
  1017. $time2 = $data['time'][1];
  1018. }
  1019. return $time1 . $time2 . $type . "report";
  1020. }
  1021. /**
  1022. * 用于计算时间
  1023. * @param $minute
  1024. * @return string
  1025. */
  1026. public function calTimeReturn($minute){
  1027. return number_format($minute * 1.5 / 60,2);
  1028. }
  1029. /**
  1030. * 用于计算时间
  1031. * @param $minute
  1032. * @return string
  1033. */
  1034. public function calTimeReturnMin($minute){
  1035. return number_format($minute * 1.5 / 60,2);
  1036. }
  1037. /**
  1038. * 报工
  1039. * @param $data
  1040. * @return array
  1041. */
  1042. public function statisticsReportWorkingChart($data){
  1043. if(empty($data['crt_time'][0]) || empty($data['crt_time'][1])) return [false, '报工单生成时间必须选择!'];
  1044. //人员
  1045. $finished_id = $data['finished_id'] ?? [];
  1046. //工序
  1047. $process_id = $data['process_id'] ?? [];
  1048. $result = ReportWorkingDetail::where('del_time',0)
  1049. ->where('crt_time',">=",$data['crt_time'][0])
  1050. ->where('crt_time',"<=",$data['crt_time'][1])
  1051. ->when(! empty($finished_id), function ($query) use ($finished_id) {
  1052. return $query->whereIn('finished_id', $finished_id);
  1053. })
  1054. ->when(! empty($process_id), function ($query) use ($process_id) {
  1055. return $query->whereIn('process_id', $process_id);
  1056. })
  1057. ->get()->toArray();
  1058. if(empty($result)) return [true, []];
  1059. $emp_map = Employee::whereIn('id',array_unique(array_column($result,'finished_id')))
  1060. ->pluck('emp_name','id')
  1061. ->toArray();
  1062. $process_map = Process::whereIn('id',array_unique(array_column($result,'process_id')))
  1063. ->pluck('title','id')
  1064. ->toArray();
  1065. //派工单
  1066. $d = DispatchSub::where('del_time',0)
  1067. ->whereIn('id', array_unique(array_column($result,'data_id')))
  1068. ->select('id', 'dispatch_no as order_no', 'product_title','technology_name','product_no','product_unit','price')
  1069. ->get()->toArray();
  1070. $d_tmp = array_column($d,null,'id');
  1071. $return = [];
  1072. foreach ($result as $value){
  1073. $tmp = $d_tmp[$value['data_id']] ?? [];
  1074. if(empty($tmp)) continue;
  1075. $string = $value['finished_id'] . $value['process_id'];
  1076. if(isset($return[$string])){
  1077. $quantity = bcadd($value['quantity'], $return[$string]['quantity'], 3);
  1078. $return[$string]['quantity'] = $quantity;
  1079. if(! in_array($value['id'], $return[$string]['report_id'])){
  1080. $id = array_merge_recursive($return[$string]['report_id'], [$value['id']]);
  1081. $return[$string]['report_id'] = $id;
  1082. }
  1083. }else{
  1084. $return[$string] = [
  1085. 'finished_id' => $value['finished_id'],
  1086. 'finished_title' => $emp_map[$value['finished_id']] ?? "",
  1087. 'process_id' => $value['process_id'],
  1088. 'process_title' => $process_map[$value['process_id']] ?? "",
  1089. 'quantity' => $value['quantity'],
  1090. 'report_id' => [$value['id']],
  1091. ];
  1092. }
  1093. }
  1094. return [true, array_values($return)];
  1095. }
  1096. public function statisticsReportWorkingChartDetail($data){
  1097. if(empty($data['report_id'])) return [false, '报工单数据必须选择!'];
  1098. $result = ReportWorkingDetail::where('del_time',0)
  1099. ->whereIn('id', $data['report_id'])
  1100. ->get()->toArray();
  1101. if(empty($result)) return [false, '报工单数据不存在或已被删除'];
  1102. //派工单
  1103. $d = DispatchSub::where('del_time',0)
  1104. ->whereIn('id', array_unique(array_column($result,'data_id')))
  1105. ->select('id', 'dispatch_no as order_no', 'product_title','technology_name','product_no','product_unit','price')
  1106. ->get()->toArray();
  1107. $d_tmp = array_column($d,null,'id');
  1108. $return = [];
  1109. foreach ($result as $value){
  1110. $tmp = $d_tmp[$value['data_id']] ?? [];
  1111. if(empty($tmp)) continue;
  1112. $string = $tmp['id'] . $tmp['product_no'] . $tmp['technology_name'];
  1113. if(isset($return[$string])){
  1114. $return[$string]['quantity'] += $value['quantity'];
  1115. }else{
  1116. $return[$string] = [
  1117. 'order_no' => $tmp['order_no'] ?? "",
  1118. 'product_title' => $tmp["product_title"] ?? "",
  1119. 'product_no' => $tmp["product_no"] ?? "",
  1120. 'product_unit' => $tmp["product_unit"] ?? "",
  1121. 'technology_name' => $tmp['technology_name'] ?? "",
  1122. 'wood_name' => $tmp['wood_name'] ?? "",
  1123. 'quantity' => $value['quantity'],
  1124. ];
  1125. }
  1126. }
  1127. return [true, array_values($return)];
  1128. }
  1129. public function xsReport($data){
  1130. $model = SaleOrdersProduct::where('del_time',0)
  1131. ->select('id','out_order_no','customer_name','product_title','order_quantity','technology_name','production_quantity','box_num','finished_num','box_num as shipment_num')
  1132. ->orderBy('crt_time','desc');
  1133. if(! empty($data['out_order_no'])) {
  1134. if(! is_array($data['out_order_no'])) {
  1135. $model->where('out_order_no', 'LIKE', '%'.$data['out_order_no'].'%');
  1136. }else{
  1137. $model->whereIn('out_order_no', $data['out_order_no']);
  1138. }
  1139. }
  1140. if(! empty($data['customer_name'])) $model->where('customer_name', 'LIKE', '%'.$data['customer_name'].'%');
  1141. if(! empty($data['product_no'])) $model->where('product_no', 'LIKE', '%'.$data['product_no'].'%');
  1142. if(! empty($data['product_title'])) $model->where('product_title', 'LIKE', '%'.$data['product_title'].'%');
  1143. if(! empty($data['product_size'])) $model->where('product_size', 'LIKE', '%'.$data['product_size'].'%');
  1144. if(! empty($data['technology_material'])) $model->where('technology_material', 'LIKE', '%'.$data['technology_material'].'%');
  1145. if(! empty($data['technology_name'])) $model->where('technology_name', 'LIKE', '%'.$data['technology_name'].'%');
  1146. if(! empty($data['process_mark'])) $model->where('process_mark', 'LIKE', '%'.$data['process_mark'].'%');
  1147. if(! empty($data['table_header_mark'])) $model->where('table_header_mark', 'LIKE', '%'.$data['table_header_mark'].'%');
  1148. if(! empty($data['table_body_mark'])) $model->where('table_body_mark', 'LIKE', '%'.$data['table_body_mark'].'%');
  1149. if(! empty($data['out_checker_man'])) $model->where('out_checker_man', 'LIKE', '%'.$data['out_checker_man'].'%');
  1150. if(! empty($data['out_crt_man'])) $model->where('out_crt_man', 'LIKE', '%'.$data['out_crt_man'].'%');
  1151. 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]]);
  1152. 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]]);
  1153. 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]]);
  1154. if(isset($data['status'])) $model->where('status',$data['status']);
  1155. $list = $this->limit($model,'',$data);
  1156. $list = $this->fillXsData($list);
  1157. return [true, $list];
  1158. }
  1159. public function fillXsData($list){
  1160. if(empty($list['data'])) return $list;
  1161. $sale_orders_product_id = array_column($list['data'],'id');
  1162. //派工单数据
  1163. $dispatch_data = DispatchSub::where('del_time',0)
  1164. ->whereIn('sale_orders_product_id',$sale_orders_product_id)
  1165. ->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')
  1166. ->groupBy('sale_orders_product_id', 'process_id')
  1167. ->orderBy('sale_orders_product_id','desc')
  1168. ->get()->toArray();
  1169. $dispatch_data_map = [];
  1170. foreach ($dispatch_data as $value){
  1171. $dispatch_data_map[$value['sale_orders_product_id']][] = $value;
  1172. }
  1173. $num = ScrappCount::where('del_time',0)
  1174. ->whereIn('sale_orders_product_id',$sale_orders_product_id)
  1175. ->select(DB::raw('sum(quantity) as quantity'),DB::raw('sum(scrapp_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. $scrapp_map = [];
  1180. foreach ($num as $value){
  1181. $tmp = bcadd($value['quantity'], $value['waste_num'],3);
  1182. $scrapp_map[$value['sale_orders_product_id'] . $value['process_id']] = $tmp;
  1183. }
  1184. $process = Process::where('del_time',0)->pluck('title','id')->toArray();
  1185. foreach ($list['data'] as $key => $value){
  1186. $tmp = $dispatch_data_map[$value['id']] ?? [];
  1187. foreach ($tmp as $k => $v){
  1188. $num = '0.000';
  1189. if(isset($scrapp_map[$v['sale_orders_product_id'] . $v['process_id']])) $num = $scrapp_map[$v['sale_orders_product_id'] . $v['process_id']];
  1190. $tmp[$k]['process_title'] = $process[$v['process_id']] ?? "";
  1191. $tmp[$k]['bg_num'] = $num;
  1192. $tmp[$k]['zj_num'] = $num;
  1193. }
  1194. $list['data'][$key]['mul_data'] = $tmp;
  1195. }
  1196. return $list;
  1197. }
  1198. }