ReportFormsService.php 52 KB

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