ReportFormsService.php 54 KB

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