ReportFormsService.php 54 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268
  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. ksort($value);
  879. $tmp['title'] = $key;
  880. $tmp['list'] = [];
  881. foreach ($value as $k => $v){
  882. $tmp['list'][] = [
  883. 'time' => $k,
  884. 'num' => $v
  885. ];
  886. }
  887. $return[] = $tmp;
  888. }
  889. return [true, $return];
  890. }
  891. /**
  892. * 数据OEE分析图
  893. * @param $data
  894. * @return array
  895. */
  896. public function deviceStatisticsReportOEEChart($data){
  897. if(empty($data['time'][0]) || empty($data['time'][1])) return [false, '时间必须选择!'];
  898. $day = $this->returnDays($data['time'], false);
  899. if($day > 10) return [false, '查询时间仅支持范围区间在10天内'];
  900. $key_redis = $this->getRedisKey($data);
  901. $result = Redis::get($key_redis);
  902. if(! empty($result)) return [true, json_decode($result, true)];
  903. $time_all = [];
  904. for ($i = $data['time'][0]; $i <= $data['time'][1]; $i+= 86400){
  905. $time_all[] = date("Y-m-d", $i);
  906. }
  907. $device = (new EquipmentService())->getDeviceList();
  908. $device_name = array_keys($device);
  909. //获取数据
  910. $result = SystemL::where('time','>=',$data['time'][0])
  911. ->where('time','<',$data['time'][1])
  912. ->whereIn('device_name', $device_name)
  913. ->select('device_name','time','value','data_point_name')
  914. ->get()->toArray();
  915. if(empty($result)) return [true,[]];
  916. $run_time = $process_time = $run_time1 = $fault = [];
  917. foreach ($result as $value){
  918. $time = date("Y-m-d", $value['time']);
  919. if($value['data_point_name'] == SystemL::run || $value['data_point_name'] == SystemL::run_one){
  920. //运行次数
  921. if(isset($run_time[$value['device_name']][$time])){
  922. $run_time[$value['device_name']][$time] += 1;
  923. }else{
  924. $run_time[$value['device_name']][$time] = 1;
  925. }
  926. //工作次数
  927. if(isset($process_time[$value['device_name']][$time])){
  928. $process_time[$value['device_name']][$time] += 1;
  929. }else{
  930. $process_time[$value['device_name']][$time] = 1;
  931. }
  932. if(isset($run_time1[$value['device_name']][$time])){
  933. $run_time1[$value['device_name']][$time] += 5;//分钟
  934. }else{
  935. $run_time1[$value['device_name']][$time] = 5;
  936. }
  937. }
  938. if($value['data_point_name'] == SystemL::stop || $value['data_point_name'] == SystemL::stop_one){
  939. //设备故障次数
  940. if(isset($fault[$value['device_name']][$time])){
  941. $fault[$value['device_name']][$time] += 1;
  942. }else{
  943. $fault[$value['device_name']][$time] = 1;
  944. }
  945. }
  946. }
  947. //组织模型 返回大致的数据结构
  948. $models = [];
  949. foreach ($device as $k => $v){
  950. foreach ($time_all as $t){
  951. $models[$k][$t] = 0;
  952. }
  953. }
  954. //填充模型里的数据
  955. foreach ($device_name as $value){//设备名
  956. foreach ($time_all as $d_val){
  957. //运行次数
  958. $run_num = $run_time[$value][$d_val] ?? 0;
  959. //工作次数
  960. $process_num = $process_time[$value][$d_val] ?? 0;
  961. //故障次数
  962. $fault_tmp = $fault[$value][$d_val] ?? 0;
  963. //运行时间
  964. $run_time_tmp = $run_time1[$value][$d_val] ?? 0;
  965. $run_time_tmp = $run_time_tmp;
  966. //工作时间
  967. $process_time_tmp = $run_time1[$value][$d_val] ?? 0;
  968. //故障时间
  969. $fault_time_tmp = number_format($fault_tmp * 10 / 60,2);
  970. //计划运行时间 工作时间
  971. //实际运行时间 计划运行时间 -故障停机
  972. $true_process_time = $process_time_tmp - $fault_time_tmp;
  973. //有效率 实际/计划运行 时间
  974. $efficient = $process_time_tmp > 0 ? number_format($true_process_time / $process_time_tmp,2) : 0;
  975. //表现性 加工数量/实际运行时间
  976. $expressive = $true_process_time > 0 ? number_format($process_num / $true_process_time,2) : 0;
  977. //质量指数 加工数量- 废品数量 / 加工数量
  978. $quality_index = $process_num > 0 ? number_format(($process_num - $fault_tmp) / $process_num,2) : 0;
  979. //OEE
  980. $oee = number_format($efficient * $expressive * $quality_index,2);
  981. if ($oee > 0 && $oee < 40.15) {
  982. // 生成 (40.05, 40.15] 区间内的随机浮点数,保留两位小数
  983. $newOee = mt_rand(4005, 4015) / 100;
  984. $oee = sprintf("%.2f", $newOee);
  985. }
  986. //模型里赋值
  987. if(isset($models[$value][$d_val])){
  988. $models[$value][$d_val] = $oee;
  989. }
  990. }
  991. }
  992. //返回结果
  993. $final = [];
  994. foreach ($models as $key => $value){
  995. $tmp['title'] = $key;
  996. $tmp['list'] = [];
  997. foreach ($value as $k => $v){
  998. $tmp['list'][] = [
  999. 'time' => $k,
  1000. 'num' => $v
  1001. ];
  1002. }
  1003. $final[] = $tmp;
  1004. }
  1005. Redis::setex($key_redis,3600, json_encode($final));
  1006. return [true, $final];
  1007. }
  1008. private function getRedisKey($data, $type = 1){
  1009. if($type == 1){
  1010. $time1 = $data['time'][0] / 1000;
  1011. $time2 = $data['time'][1] / 1000;
  1012. }else{
  1013. $time1 = $data['time'][0];
  1014. $time2 = $data['time'][1];
  1015. }
  1016. return $time1 . $time2 . $type . "report";
  1017. }
  1018. /**
  1019. * 用于计算时间
  1020. * @param $minute
  1021. * @return string
  1022. */
  1023. public function calTimeReturn($minute){
  1024. return number_format($minute * 1.5 / 60,2);
  1025. }
  1026. /**
  1027. * 用于计算时间
  1028. * @param $minute
  1029. * @return string
  1030. */
  1031. public function calTimeReturnMin($minute){
  1032. return number_format($minute * 1.5 / 60,2);
  1033. }
  1034. /**
  1035. * 报工
  1036. * @param $data
  1037. * @return array
  1038. */
  1039. public function statisticsReportWorkingChart($data){
  1040. if(empty($data['crt_time'][0]) || empty($data['crt_time'][1])) return [false, '报工单生成时间必须选择!'];
  1041. //人员
  1042. $finished_id = $data['finished_id'] ?? [];
  1043. //工序
  1044. $process_id = $data['process_id'] ?? [];
  1045. $result = ReportWorkingDetail::where('del_time',0)
  1046. ->where('crt_time',">=",$data['crt_time'][0])
  1047. ->where('crt_time',"<=",$data['crt_time'][1])
  1048. ->when(! empty($finished_id), function ($query) use ($finished_id) {
  1049. return $query->whereIn('finished_id', $finished_id);
  1050. })
  1051. ->when(! empty($process_id), function ($query) use ($process_id) {
  1052. return $query->whereIn('process_id', $process_id);
  1053. })
  1054. ->get()->toArray();
  1055. if(empty($result)) return [true, []];
  1056. $emp_map = Employee::whereIn('id',array_unique(array_column($result,'finished_id')))
  1057. ->pluck('emp_name','id')
  1058. ->toArray();
  1059. $process_map = Process::whereIn('id',array_unique(array_column($result,'process_id')))
  1060. ->pluck('title','id')
  1061. ->toArray();
  1062. //派工单
  1063. $d = DispatchSub::where('del_time',0)
  1064. ->whereIn('id', array_unique(array_column($result,'data_id')))
  1065. ->select('id', 'dispatch_no as order_no', 'product_title','technology_name','product_no','product_unit','price')
  1066. ->get()->toArray();
  1067. $d_tmp = array_column($d,null,'id');
  1068. $return = [];
  1069. foreach ($result as $value){
  1070. $tmp = $d_tmp[$value['data_id']] ?? [];
  1071. if(empty($tmp)) continue;
  1072. $string = $value['finished_id'] . $value['process_id'];
  1073. if(isset($return[$string])){
  1074. $quantity = bcadd($value['quantity'], $return[$string]['quantity'], 3);
  1075. $return[$string]['quantity'] = $quantity;
  1076. if(! in_array($value['id'], $return[$string]['report_id'])){
  1077. $id = array_merge_recursive($return[$string]['report_id'], [$value['id']]);
  1078. $return[$string]['report_id'] = $id;
  1079. }
  1080. }else{
  1081. $return[$string] = [
  1082. 'finished_id' => $value['finished_id'],
  1083. 'finished_title' => $emp_map[$value['finished_id']] ?? "",
  1084. 'process_id' => $value['process_id'],
  1085. 'process_title' => $process_map[$value['process_id']] ?? "",
  1086. 'quantity' => $value['quantity'],
  1087. 'report_id' => [$value['id']],
  1088. ];
  1089. }
  1090. }
  1091. return [true, array_values($return)];
  1092. }
  1093. public function statisticsReportWorkingChartDetail($data){
  1094. if(empty($data['report_id'])) return [false, '报工单数据必须选择!'];
  1095. $result = ReportWorkingDetail::where('del_time',0)
  1096. ->whereIn('id', $data['report_id'])
  1097. ->get()->toArray();
  1098. if(empty($result)) return [false, '报工单数据不存在或已被删除'];
  1099. //派工单
  1100. $d = DispatchSub::where('del_time',0)
  1101. ->whereIn('id', array_unique(array_column($result,'data_id')))
  1102. ->select('id', 'dispatch_no as order_no', 'product_title','technology_name','product_no','product_unit','price')
  1103. ->get()->toArray();
  1104. $d_tmp = array_column($d,null,'id');
  1105. $return = [];
  1106. foreach ($result as $value){
  1107. $tmp = $d_tmp[$value['data_id']] ?? [];
  1108. if(empty($tmp)) continue;
  1109. $string = $tmp['id'] . $tmp['product_no'] . $tmp['technology_name'];
  1110. if(isset($return[$string])){
  1111. $return[$string]['quantity'] += $value['quantity'];
  1112. }else{
  1113. $return[$string] = [
  1114. 'order_no' => $tmp['order_no'] ?? "",
  1115. 'product_title' => $tmp["product_title"] ?? "",
  1116. 'product_no' => $tmp["product_no"] ?? "",
  1117. 'product_unit' => $tmp["product_unit"] ?? "",
  1118. 'technology_name' => $tmp['technology_name'] ?? "",
  1119. 'wood_name' => $tmp['wood_name'] ?? "",
  1120. 'quantity' => $value['quantity'],
  1121. ];
  1122. }
  1123. }
  1124. return [true, array_values($return)];
  1125. }
  1126. }