ReportFormsService.php 62 KB

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