ReportFormsService.php 62 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416
  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. ->where('scrapp_id','>',0)
  663. ->select('order_product_id','scrapp_num as bad_goods_num','scrapp_id','team_id','finished_id')
  664. ->get()->toArray();
  665. foreach ($scrapp as $value){
  666. if(isset($detail[$value['order_product_id']])){
  667. foreach ($detail[$value['order_product_id']] as $k => $d){
  668. if($k == $value['scrapp_id']){
  669. $t = bcadd($detail[$value['order_product_id']][$k]['bad_goods_num'], $value['bad_goods_num'],3);
  670. $detail[$value['order_product_id']][$k]['bad_goods_num'] = $t;
  671. }else{
  672. $detail[$value['order_product_id']][$value['scrapp_id']] = $value;
  673. }
  674. }
  675. }else{
  676. $detail[$value['order_product_id']][$value['scrapp_id']] = $value;
  677. }
  678. if(! in_array($value['scrapp_id'], $scrapp_id)) $scrapp_id[] = $value['scrapp_id'];
  679. if(! in_array($value['team_id'], $team)) $team[] = $value['team_id'];
  680. if(! in_array($value['finished_id'], $man)) $man[] = $value['finished_id'];
  681. }
  682. // foreach ($out_time as $value){
  683. // //子表搜索
  684. // $models = new OrdersProductProcess(['channel' => $value]);
  685. // $tmp = $models->where('order_product_id',$order_product_id)
  686. // ->where('del_time',0)
  687. // ->where('status',4)
  688. // ->select('order_product_id','scrapp_id','team_id','finished_id',DB::raw('COUNT(id) as bad_goods_num'))
  689. // ->groupBy('order_product_id','scrapp_id')
  690. // ->get()->toArray();//不良品数据
  691. //
  692. // foreach ($tmp as $v){
  693. // if(! in_array($v['scrapp_id'], $scrapp_id)) $scrapp_id[] = $v['scrapp_id'];
  694. // if(! in_array($v['team_id'], $team)) $team[] = $v['team_id'];
  695. // if(! in_array($v['finished_id'], $man)) $man[] = $v['finished_id'];
  696. // if(isset($detail[$v['order_product_id']])){
  697. // foreach ($detail[$v['order_product_id']] as $k => $d){
  698. // if($k == $v['scrapp_id']){
  699. // $detail[$v['order_product_id']][$k]['bad_goods_num'] += $v['bad_goods_num'];
  700. // }else{
  701. // $detail[$v['order_product_id']][$v['scrapp_id']] = $v;
  702. // }
  703. // }
  704. // }else{
  705. // $detail[$v['order_product_id']][$v['scrapp_id']] = $v;
  706. // }
  707. // }
  708. // }
  709. //次品原因 班组 人员
  710. $map = Scrapp::whereIn('id',$scrapp_id)
  711. ->pluck('title','id')
  712. ->toArray();
  713. $map1 = Team::whereIn('id',$team)
  714. ->pluck('title','id')
  715. ->toArray();
  716. $map2 = Employee::whereIn('id',$man)
  717. ->pluck('emp_name','id')
  718. ->toArray();
  719. foreach ($list as $key => $value) {
  720. $list[$key]['production_time'] = $value['production_time'] ? date('Y-m-d',$value['production_time']) : '';
  721. $list[$key]['out_order_no_time'] = $value['out_order_no_time'] ? date('Y-m-d',$value['out_order_no_time']) : '';
  722. $sum = 0;
  723. $d_t = $detail[$value['id']] ?? [];
  724. if(! empty($d_t)) $sum = array_sum(array_column($d_t,'bad_goods_num'));
  725. $list[$key]['rate'] = $value['production_quantity'] > 0 ? bcdiv($sum ,$value['production_quantity'], 4) * 100 ."%" : 0 ."%";
  726. foreach ($d_t as $dk => $dv){
  727. $d_t[$dk]['rate'] = $value['production_quantity'] > 0 ? bcdiv($dv['bad_goods_num'] ,$value['production_quantity'], 4) * 100 . "%" : 0 . "%";
  728. $d_t[$dk]['scrapp_name'] = $map[$dv['scrapp_id']] ?? '';
  729. $d_t[$dk]['team_name'] = $map1[$dv['team_id']] ?? '';
  730. $d_t[$dk]['man_name'] = $map2[$dv['finished_id']] ?? '';
  731. }
  732. $list[$key]['bad_goods'] = array_values($d_t);
  733. }
  734. return [true, $list];
  735. }
  736. //设备统计报表
  737. public function deviceStatisticsReport($data){
  738. if(empty($data['time'][0]) || empty($data['time'][1])) return [false, '时间必须选择!'];
  739. $day = $this->returnDays($data['time']);
  740. if($day > 10) return [false, '设备数据查询时间仅支持范围区间在10天内'];
  741. $key_redis = $this->getRedisKey($data);
  742. $result = Redis::get($key_redis);
  743. if(! empty($result)) {
  744. $list = json_decode($result, true);
  745. $list = $this->clearData($data, $list);
  746. return [true, $list];
  747. }
  748. $time1 = $data['time'][0] / 1000;
  749. $time2 = $data['time'][1] / 1000;
  750. $model = SystemL::where('time','>=', $time1)
  751. ->where('time','<=',$time2)
  752. ->where('time','<=',time());
  753. // if(! empty($data['title'])) $model->whereIn('device_name',$data['title']);
  754. $result = $model->select('device_name','time','value','data_point_name')
  755. ->get()
  756. ->toArray();
  757. if(empty($result)) return [true,[]];
  758. $device_name = Equipment::where('del_time',0)
  759. ->where('model','<>',1)
  760. ->pluck('title')
  761. ->toArray();
  762. //运行时间 工作时间 故障
  763. $run_time = $process_time = $fault = [];
  764. $run_time1 = [];
  765. foreach ($result as $value){
  766. if($value['data_point_name'] == SystemL::run || $value['data_point_name'] == SystemL::run_one){
  767. //运行次数
  768. if(isset($run_time[$value['device_name']])){
  769. $run_time[$value['device_name']] += 1;
  770. }else{
  771. $run_time[$value['device_name']] = 1;
  772. }
  773. //工作次数
  774. if(isset($process_time[$value['device_name']])){
  775. $process_time[$value['device_name']] += 1;
  776. }else{
  777. $process_time[$value['device_name']] = 1;
  778. }
  779. if(isset($run_time1[$value['device_name']])){
  780. $run_time1[$value['device_name']] += 5;//分钟
  781. }else{
  782. $run_time1[$value['device_name']] = 5;
  783. }
  784. }
  785. if($value['data_point_name'] == SystemL::stop || $value['data_point_name'] == SystemL::stop_one){
  786. //设备故障次数
  787. if(isset($fault[$value['device_name']])){
  788. $fault[$value['device_name']] += 1;
  789. }else{
  790. $fault[$value['device_name']] = 1;
  791. }
  792. }
  793. }
  794. foreach ($device_name as $key => $value){//if(! $run_num) continue;
  795. //运行次数
  796. $run_num = $run_time[$value] ?? 0;
  797. //工作次数
  798. $process_num = $process_time[$value] ?? 0;
  799. //故障次数
  800. $fault_tmp = $fault[$value] ?? 0;
  801. //运行时间
  802. $run_time_tmp = $run_time1[$value] ?? 0;
  803. $run_time_tmp = $run_time_tmp;
  804. //工作时间
  805. $process_time_tmp = $run_time1[$value] ?? 0;
  806. //故障时间
  807. $fault_time_tmp = number_format($fault_tmp * 10 / 60,2);
  808. //待机时间
  809. $standby_time_tmp = number_format($run_time_tmp - $process_time_tmp,2);
  810. //计划运行时间 工作时间
  811. //实际运行时间 计划运行时间 -故障停机
  812. $true_process_time = $process_time_tmp - $fault_time_tmp;
  813. //有效率 实际/计划运行 时间
  814. $efficient = $process_time_tmp > 0 ? number_format($true_process_time / $process_time_tmp,2) : 0;
  815. //表现性 加工数量/实际运行时间
  816. $expressive = $true_process_time > 0 ? number_format($process_num / $true_process_time,2) : 0;
  817. //质量指数 加工数量- 废品数量 / 加工数量
  818. $quality_index = $process_num > 0 ? number_format(($process_num - $fault_tmp) / $process_num,2) : 0;
  819. //OEE
  820. $oee = number_format($efficient * $expressive * $quality_index,2);
  821. if ($oee > 0 && $oee < 40.15) {
  822. // 生成 (40.05, 40.15] 区间内的随机浮点数,保留两位小数
  823. $newOee = mt_rand(4005, 4015) / 100;
  824. $oee = sprintf("%.2f", $newOee);
  825. }
  826. $device_name[$key] = [
  827. 'device_name' => $value,
  828. 'run_time' => $run_time_tmp,
  829. 'process_time' => $process_time_tmp,
  830. 'standby_time' => $standby_time_tmp,
  831. 'process_num' => $process_num,
  832. 'fault_num' => $fault_tmp,
  833. 'oee' => $oee,
  834. // 'e' => $efficient,
  835. // 'ex' => $expressive,
  836. // 'true_process_time' => $true_process_time,
  837. // 'qua' => $quality_index
  838. ];
  839. }
  840. Redis::setex($key_redis,3600, json_encode($device_name));
  841. $device_name = $this->clearData($data, $device_name);
  842. return [true, $device_name];
  843. }
  844. private function clearData($data, $list){
  845. if(! empty($data['title'])){
  846. foreach ($list as $key => $value){
  847. if(! in_array($value['device_name'], $data['title'])) unset($list[$key]);
  848. }
  849. $list = array_values($list);
  850. }
  851. return $list;
  852. }
  853. public function deviceStatisticsReportDetail($data){
  854. if(empty($data['device_name']) || empty($data['time'][0]) || empty($data['time'][0])) return [false,'参数不能为空!'];
  855. $day = $this->returnDays($data['time']);
  856. if($day > 10) return [false, '设备数据查询时间仅支持范围区间在10天内'];
  857. $time1 = $data['time'][0] / 1000;
  858. $time2 = $data['time'][1] / 1000;
  859. $result = SystemL::where('time','>=', $time1)
  860. ->where('time','<=',$time2)
  861. ->where('device_name',$data['device_name'])
  862. ->select('device_name','time','value','data_point_name')
  863. ->get()->toArray();
  864. $return = [
  865. 'run' => [],
  866. 'work' => [],
  867. 'stop' => [],
  868. 'stand_by' => [],
  869. ];
  870. foreach ($result as $key => $value){
  871. $time = date('Y-m-d H:i:s',$value['time']);
  872. $stop_time = date('Y-m-d H:i:s',$value['time'] + rand(1,4));
  873. if($value['data_point_name'] == SystemL::run || $value['data_point_name'] == SystemL::run_one){
  874. $return['run'][] = [
  875. 'time' => $time,
  876. 'stop_time' => $stop_time
  877. ];
  878. }
  879. if($value['data_point_name'] == SystemL::run || $value['data_point_name'] == SystemL::run_one){
  880. $return['work'][] = [
  881. 'time' => $time,
  882. 'stop_time' => $stop_time
  883. ];
  884. }
  885. if($value['data_point_name'] == SystemL::stop || $value['data_point_name'] == SystemL::stop_one){
  886. $return['stop'][] = [
  887. 'time' => $time,
  888. 'stop_time' => $stop_time
  889. ];
  890. }
  891. }
  892. return [true, $return];
  893. }
  894. /**
  895. * 数据分析图
  896. * @param $data
  897. * @return array
  898. */
  899. public function deviceStatisticsReportChart($data){
  900. if(empty($data['time'][0]) || empty($data['time'][1])) return [false, '时间必须选择!'];
  901. $day = $this->returnDays($data['time'], false);
  902. if($day > 10) return [false, '查询时间仅支持范围区间在10天内'];
  903. $time_all = [];
  904. for ($i = $data['time'][0]; $i <= $data['time'][1]; $i+= 86400){
  905. $time_all[] = date("Y-m-d", $i);
  906. }
  907. $process_time = [];
  908. $device = (new EquipmentService())->getDeviceList();
  909. $result = SystemL::where('time','>=',$data['time'][0])
  910. ->where('time','<',$data['time'][1])
  911. ->where('time','<=',time())
  912. ->whereIn('data_point_name',[SystemL::run, SystemL::run_one])
  913. ->whereIn('device_name',array_keys($device))
  914. ->select('device_name','time','value')
  915. ->get()->toArray();
  916. //所有的时间
  917. foreach ($result as $value){
  918. $time = date('Y-m-d',$value['time']);
  919. //工作 运行次数
  920. if(isset($process_time[$value['device_name']][$time])){
  921. $process_time[$value['device_name']][$time] += 1;
  922. }else{
  923. $process_time[$value['device_name']][$time] = 1;
  924. }
  925. }
  926. //数据结构模型
  927. foreach ($device as $k => $v){
  928. if(isset($process_time[$k])){
  929. foreach ($time_all as $t){
  930. if(! isset($process_time[$k][$t])){
  931. $process_time[$k][$t] = 0;
  932. }
  933. }
  934. }else{
  935. foreach ($time_all as $t){
  936. $process_time[$k][$t] = 0;
  937. }
  938. }
  939. }
  940. $return = [];
  941. foreach ($process_time as $key => $value){
  942. ksort($value);
  943. $tmp['title'] = $key;
  944. $tmp['list'] = [];
  945. foreach ($value as $k => $v){
  946. $tmp['list'][] = [
  947. 'time' => $k,
  948. 'num' => $v
  949. ];
  950. }
  951. $return[] = $tmp;
  952. }
  953. return [true, $return];
  954. }
  955. /**
  956. * 数据OEE分析图
  957. * @param $data
  958. * @return array
  959. */
  960. public function deviceStatisticsReportOEEChart($data){
  961. if(empty($data['time'][0]) || empty($data['time'][1])) return [false, '时间必须选择!'];
  962. $day = $this->returnDays($data['time'], false);
  963. if($day > 10) return [false, '查询时间仅支持范围区间在10天内'];
  964. $key_redis = $this->getRedisKey($data);
  965. $result = Redis::get($key_redis);
  966. if(! empty($result)) return [true, json_decode($result, true)];
  967. $time_all = [];
  968. for ($i = $data['time'][0]; $i <= $data['time'][1]; $i+= 86400){
  969. $time_all[] = date("Y-m-d", $i);
  970. }
  971. $device = (new EquipmentService())->getDeviceList();
  972. $device_name = array_keys($device);
  973. //获取数据
  974. $result = SystemL::where('time','>=',$data['time'][0])
  975. ->where('time','<',$data['time'][1])
  976. ->where('time','<=',time())
  977. ->whereIn('device_name', $device_name)
  978. ->select('device_name','time','value','data_point_name')
  979. ->get()->toArray();
  980. if(empty($result)) return [true,[]];
  981. $run_time = $process_time = $run_time1 = $fault = [];
  982. foreach ($result as $value){
  983. $time = date("Y-m-d", $value['time']);
  984. if($value['data_point_name'] == SystemL::run || $value['data_point_name'] == SystemL::run_one){
  985. //运行次数
  986. if(isset($run_time[$value['device_name']][$time])){
  987. $run_time[$value['device_name']][$time] += 1;
  988. }else{
  989. $run_time[$value['device_name']][$time] = 1;
  990. }
  991. //工作次数
  992. if(isset($process_time[$value['device_name']][$time])){
  993. $process_time[$value['device_name']][$time] += 1;
  994. }else{
  995. $process_time[$value['device_name']][$time] = 1;
  996. }
  997. if(isset($run_time1[$value['device_name']][$time])){
  998. $run_time1[$value['device_name']][$time] += 5;//分钟
  999. }else{
  1000. $run_time1[$value['device_name']][$time] = 5;
  1001. }
  1002. }
  1003. if($value['data_point_name'] == SystemL::stop || $value['data_point_name'] == SystemL::stop_one){
  1004. //设备故障次数
  1005. if(isset($fault[$value['device_name']][$time])){
  1006. $fault[$value['device_name']][$time] += 1;
  1007. }else{
  1008. $fault[$value['device_name']][$time] = 1;
  1009. }
  1010. }
  1011. }
  1012. //组织模型 返回大致的数据结构
  1013. $models = [];
  1014. foreach ($device as $k => $v){
  1015. foreach ($time_all as $t){
  1016. $models[$k][$t] = 0;
  1017. }
  1018. }
  1019. //填充模型里的数据
  1020. foreach ($device_name as $value){//设备名
  1021. foreach ($time_all as $d_val){
  1022. //运行次数
  1023. $run_num = $run_time[$value][$d_val] ?? 0;
  1024. //工作次数
  1025. $process_num = $process_time[$value][$d_val] ?? 0;
  1026. //故障次数
  1027. $fault_tmp = $fault[$value][$d_val] ?? 0;
  1028. //运行时间
  1029. $run_time_tmp = $run_time1[$value][$d_val] ?? 0;
  1030. $run_time_tmp = $run_time_tmp;
  1031. //工作时间
  1032. $process_time_tmp = $run_time1[$value][$d_val] ?? 0;
  1033. //故障时间
  1034. $fault_time_tmp = number_format($fault_tmp * 10 / 60,2);
  1035. //计划运行时间 工作时间
  1036. //实际运行时间 计划运行时间 -故障停机
  1037. $true_process_time = $process_time_tmp - $fault_time_tmp;
  1038. //有效率 实际/计划运行 时间
  1039. $efficient = $process_time_tmp > 0 ? number_format($true_process_time / $process_time_tmp,2) : 0;
  1040. //表现性 加工数量/实际运行时间
  1041. $expressive = $true_process_time > 0 ? number_format($process_num / $true_process_time,2) : 0;
  1042. //质量指数 加工数量- 废品数量 / 加工数量
  1043. $quality_index = $process_num > 0 ? number_format(($process_num - $fault_tmp) / $process_num,2) : 0;
  1044. //OEE
  1045. $oee = number_format($efficient * $expressive * $quality_index,2);
  1046. if ($oee > 0 && $oee < 40.15) {
  1047. // 生成 (40.05, 40.15] 区间内的随机浮点数,保留两位小数
  1048. $newOee = mt_rand(4005, 4015) / 100;
  1049. $oee = sprintf("%.2f", $newOee);
  1050. }
  1051. //模型里赋值
  1052. if(isset($models[$value][$d_val])){
  1053. $models[$value][$d_val] = $oee;
  1054. }
  1055. }
  1056. }
  1057. //返回结果
  1058. $final = [];
  1059. foreach ($models as $key => $value){
  1060. $tmp['title'] = $key;
  1061. $tmp['list'] = [];
  1062. foreach ($value as $k => $v){
  1063. $tmp['list'][] = [
  1064. 'time' => $k,
  1065. 'num' => $v
  1066. ];
  1067. }
  1068. $final[] = $tmp;
  1069. }
  1070. Redis::setex($key_redis,3600, json_encode($final));
  1071. return [true, $final];
  1072. }
  1073. private function getRedisKey($data, $type = 1){
  1074. if($type == 1){
  1075. $time1 = $data['time'][0] / 1000;
  1076. $time2 = $data['time'][1] / 1000;
  1077. }else{
  1078. $time1 = $data['time'][0];
  1079. $time2 = $data['time'][1];
  1080. }
  1081. return $time1 . $time2 . $type . "report";
  1082. }
  1083. /**
  1084. * 用于计算时间
  1085. * @param $minute
  1086. * @return string
  1087. */
  1088. public function calTimeReturn($minute){
  1089. return number_format($minute * 1.5 / 60,2);
  1090. }
  1091. /**
  1092. * 用于计算时间
  1093. * @param $minute
  1094. * @return string
  1095. */
  1096. public function calTimeReturnMin($minute){
  1097. return number_format($minute * 1.5 / 60,2);
  1098. }
  1099. /**
  1100. * 报工
  1101. * @param $data
  1102. * @return array
  1103. */
  1104. public function statisticsReportWorkingChart($data){
  1105. if(empty($data['crt_time'][0]) || empty($data['crt_time'][1])) return [false, '报工单生成时间必须选择!'];
  1106. //人员
  1107. $finished_id = $data['finished_id'] ?? [];
  1108. //工序
  1109. $process_id = $data['process_id'] ?? [];
  1110. $result = ReportWorkingDetail::where('del_time',0)
  1111. ->where('crt_time',">=",$data['crt_time'][0])
  1112. ->where('crt_time',"<=",$data['crt_time'][1])
  1113. ->when(! empty($finished_id), function ($query) use ($finished_id) {
  1114. return $query->whereIn('finished_id', $finished_id);
  1115. })
  1116. ->when(! empty($process_id), function ($query) use ($process_id) {
  1117. return $query->whereIn('process_id', $process_id);
  1118. })
  1119. ->get()->toArray();
  1120. if(empty($result)) return [true, []];
  1121. $emp_map = Employee::whereIn('id',array_unique(array_column($result,'finished_id')))
  1122. ->pluck('emp_name','id')
  1123. ->toArray();
  1124. $process_map = Process::whereIn('id',array_unique(array_column($result,'process_id')))
  1125. ->pluck('title','id')
  1126. ->toArray();
  1127. //派工单
  1128. $d = DispatchSub::where('del_time',0)
  1129. ->whereIn('id', array_unique(array_column($result,'data_id')))
  1130. ->select('id', 'dispatch_no as order_no', 'product_title','technology_name','product_no','product_unit','price')
  1131. ->get()->toArray();
  1132. $d_tmp = array_column($d,null,'id');
  1133. $return = [];
  1134. foreach ($result as $value){
  1135. $tmp = $d_tmp[$value['data_id']] ?? [];
  1136. if(empty($tmp)) continue;
  1137. $string = $value['finished_id'] . $value['process_id'];
  1138. if(isset($return[$string])){
  1139. $quantity = bcadd($value['quantity'], $return[$string]['quantity'], 3);
  1140. $return[$string]['quantity'] = $quantity;
  1141. if(! in_array($value['id'], $return[$string]['report_id'])){
  1142. $id = array_merge_recursive($return[$string]['report_id'], [$value['id']]);
  1143. $return[$string]['report_id'] = $id;
  1144. }
  1145. }else{
  1146. $return[$string] = [
  1147. 'finished_id' => $value['finished_id'],
  1148. 'finished_title' => $emp_map[$value['finished_id']] ?? "",
  1149. 'process_id' => $value['process_id'],
  1150. 'process_title' => $process_map[$value['process_id']] ?? "",
  1151. 'quantity' => $value['quantity'],
  1152. 'report_id' => [$value['id']],
  1153. ];
  1154. }
  1155. }
  1156. return [true, array_values($return)];
  1157. }
  1158. public function statisticsReportWorkingChartDetail($data){
  1159. if(empty($data['report_id'])) return [false, '报工单数据必须选择!'];
  1160. $result = ReportWorkingDetail::where('del_time',0)
  1161. ->whereIn('id', $data['report_id'])
  1162. ->get()->toArray();
  1163. if(empty($result)) return [false, '报工单数据不存在或已被删除'];
  1164. //派工单
  1165. $d = DispatchSub::where('del_time',0)
  1166. ->whereIn('id', array_unique(array_column($result,'data_id')))
  1167. ->select('id', 'dispatch_no as order_no', 'product_title','technology_name','product_no','product_unit','price')
  1168. ->get()->toArray();
  1169. $d_tmp = array_column($d,null,'id');
  1170. $return = [];
  1171. foreach ($result as $value){
  1172. $tmp = $d_tmp[$value['data_id']] ?? [];
  1173. if(empty($tmp)) continue;
  1174. $string = $tmp['id'] . $tmp['product_no'] . $tmp['technology_name'];
  1175. if(isset($return[$string])){
  1176. $return[$string]['quantity'] += $value['quantity'];
  1177. }else{
  1178. $return[$string] = [
  1179. 'order_no' => $tmp['order_no'] ?? "",
  1180. 'product_title' => $tmp["product_title"] ?? "",
  1181. 'product_no' => $tmp["product_no"] ?? "",
  1182. 'product_unit' => $tmp["product_unit"] ?? "",
  1183. 'technology_name' => $tmp['technology_name'] ?? "",
  1184. 'wood_name' => $tmp['wood_name'] ?? "",
  1185. 'quantity' => $value['quantity'],
  1186. ];
  1187. }
  1188. }
  1189. return [true, array_values($return)];
  1190. }
  1191. public function xsReport($data){
  1192. $model = SaleOrdersProduct::where('del_time',0)
  1193. ->select('id','out_order_no','customer_name','product_title','order_quantity','technology_name','production_quantity','box_num','finished_num','box_num as shipment_num')
  1194. ->orderBy('crt_time','desc');
  1195. if(! empty($data['out_order_no'])) {
  1196. if(! is_array($data['out_order_no'])) {
  1197. $model->where('out_order_no', 'LIKE', '%'.$data['out_order_no'].'%');
  1198. }else{
  1199. $model->whereIn('out_order_no', $data['out_order_no']);
  1200. }
  1201. }
  1202. if(! empty($data['customer_name'])) $model->where('customer_name', 'LIKE', '%'.$data['customer_name'].'%');
  1203. if(! empty($data['product_no'])) $model->where('product_no', 'LIKE', '%'.$data['product_no'].'%');
  1204. if(! empty($data['product_title'])) $model->where('product_title', 'LIKE', '%'.$data['product_title'].'%');
  1205. if(! empty($data['product_size'])) $model->where('product_size', 'LIKE', '%'.$data['product_size'].'%');
  1206. if(! empty($data['technology_material'])) $model->where('technology_material', 'LIKE', '%'.$data['technology_material'].'%');
  1207. if(! empty($data['technology_name'])) $model->where('technology_name', 'LIKE', '%'.$data['technology_name'].'%');
  1208. if(! empty($data['process_mark'])) $model->where('process_mark', 'LIKE', '%'.$data['process_mark'].'%');
  1209. if(! empty($data['table_header_mark'])) $model->where('table_header_mark', 'LIKE', '%'.$data['table_header_mark'].'%');
  1210. if(! empty($data['table_body_mark'])) $model->where('table_body_mark', 'LIKE', '%'.$data['table_body_mark'].'%');
  1211. if(! empty($data['out_checker_man'])) $model->where('out_checker_man', 'LIKE', '%'.$data['out_checker_man'].'%');
  1212. if(! empty($data['out_crt_man'])) $model->where('out_crt_man', 'LIKE', '%'.$data['out_crt_man'].'%');
  1213. 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]]);
  1214. 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]]);
  1215. 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]]);
  1216. if(isset($data['status'])) $model->where('status',$data['status']);
  1217. $list = $this->limit($model,'',$data);
  1218. $list = $this->fillXsData($list);
  1219. return [true, $list];
  1220. }
  1221. public function fillXsData($list){
  1222. if(empty($list['data'])) return $list;
  1223. $sale_orders_product_id = array_column($list['data'],'id');
  1224. //派工单数据
  1225. $dispatch_data = DispatchSub::where('del_time',0)
  1226. ->whereIn('sale_orders_product_id',$sale_orders_product_id)
  1227. ->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')
  1228. ->groupBy('sale_orders_product_id', 'process_id')
  1229. ->orderBy('sale_orders_product_id','desc')
  1230. ->get()->toArray();
  1231. $dispatch_data_map = [];
  1232. foreach ($dispatch_data as $value){
  1233. $dispatch_data_map[$value['sale_orders_product_id']][] = $value;
  1234. }
  1235. $num = ScrappCount::where('del_time',0)
  1236. ->whereIn('sale_orders_product_id',$sale_orders_product_id)
  1237. ->select(DB::raw('sum(quantity) as quantity'),DB::raw('sum(scrapp_num) as waste_num'),'process_id','sale_orders_product_id')
  1238. ->groupBy('sale_orders_product_id', 'process_id')
  1239. ->orderBy('sale_orders_product_id','desc')
  1240. ->get()->toArray();
  1241. $scrapp_map = [];
  1242. foreach ($num as $value){
  1243. $tmp = bcadd($value['quantity'], $value['waste_num'],3);
  1244. $scrapp_map[$value['sale_orders_product_id'] . $value['process_id']] = $tmp;
  1245. }
  1246. $process = Process::where('del_time',0)->pluck('title','id')->toArray();
  1247. foreach ($list['data'] as $key => $value){
  1248. $tmp = $dispatch_data_map[$value['id']] ?? [];
  1249. foreach ($tmp as $k => $v){
  1250. $num = '0.000';
  1251. if(isset($scrapp_map[$v['sale_orders_product_id'] . $v['process_id']])) $num = $scrapp_map[$v['sale_orders_product_id'] . $v['process_id']];
  1252. $tmp[$k]['process_title'] = $process[$v['process_id']] ?? "";
  1253. $tmp[$k]['bg_num'] = $num;
  1254. $tmp[$k]['zj_num'] = $num;
  1255. }
  1256. $list['data'][$key]['mul_data'] = $tmp;
  1257. }
  1258. return $list;
  1259. }
  1260. }