ReportFormsService.php 59 KB

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