ReportFormsService.php 58 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332
  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. /**
  365. * 不良品
  366. * @param $data
  367. * @return array
  368. */
  369. public function badGoodsReport($data){
  370. if(empty($data['production_time'][0]) || empty($data['production_time'][1])) return [false, '生产订单时间必须选择!'];
  371. //检索条件 生产订单主表----------------
  372. $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');
  373. $model->whereBetween('production_time',[$data['production_time'][0],$data['production_time'][1]]);
  374. if(! empty($data['production_no'])) $model->where('production_no', 'LIKE', '%'.$data['production_no'].'%');
  375. 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]]);
  376. if(! empty($data['out_order_no'])) $model->where('out_order_no', 'LIKE', '%'.$data['out_order_no'].'%');
  377. if(! empty($data['customer_no'])) $model->where('customer_no', 'LIKE', '%'.$data['customer_no'].'%');
  378. if(! empty($data['customer_name'])) $model->where('customer_name', 'LIKE', '%'.$data['customer_name'].'%');
  379. if(! empty($data['out_crt_man'])) $model->where('out_crt_man', 'LIKE', '%'.$data['out_crt_man'].'%');
  380. $orderList = $model->get()->toArray();
  381. //生产订单主表----------------
  382. //筛选出制单日期 分表的依据
  383. $out_order_no_time = array_unique(array_column($orderList,'out_order_no_time'));
  384. //制单日期
  385. $out_time = $this->checkSameQuarter($out_order_no_time);
  386. //分组以后的订单列表
  387. $list = [];
  388. foreach ($orderList as $value){
  389. if(! isset($list[$value['id']])){
  390. $list[$value['id']] = $value;
  391. }else{
  392. $list[$value['id']]['order_quantity'] += $value['order_quantity'];
  393. $list[$value['id']]['production_quantity'] += $value['production_quantity'];
  394. }
  395. }unset($orderList);
  396. //查询分表数据
  397. $production_id = array_keys($list);
  398. $detail = [];
  399. // foreach ($out_time as $value){
  400. // //子表搜索
  401. // $models = new OrdersProductProcess(['channel' => $value]);
  402. // $tmp = $models->whereIn('order_product_id',$production_id)
  403. // ->where('del_time',0)
  404. // ->where('status',4)
  405. // ->select('order_product_id',DB::raw('COUNT(id) as bad_goods_num'))
  406. // ->groupBy('order_product_id')
  407. // ->get()->toArray();//不良品数据
  408. //
  409. // foreach ($tmp as $t){
  410. // if(isset($detail[$t['order_product_id']])){
  411. // $detail[$t['order_product_id']]['bad_goods_num'] += $t['bad_goods_num'];
  412. // }else{
  413. // $detail[$t['order_product_id']] = $t['bad_goods_num'];
  414. // }
  415. // }
  416. // }
  417. $scrapp = ScrappCount::where('del_time',0)
  418. ->whereIn('order_product_id',$production_id)
  419. ->select('order_product_id','scrapp_num as bad_goods_num')
  420. ->get()->toArray();
  421. foreach ($scrapp as $value){
  422. if(isset($detail[$value['order_product_id']])){
  423. $tmp = bcadd($detail[$value['order_product_id']],$value['bad_goods_num'],3);
  424. $detail[$value['order_product_id']] = $tmp;
  425. }else{
  426. $detail[$value['order_product_id']] = $value['bad_goods_num'];
  427. }
  428. }
  429. //返回统计数据
  430. foreach ($list as $key => $value) {
  431. $list[$key]['production_time'] = $value['production_time'] ? date('Y-m-d',$value['production_time']) : '';
  432. $list[$key]['out_order_no_time'] = $value['out_order_no_time'] ? date('Y-m-d',$value['out_order_no_time']) : '';
  433. $del_num = $detail[$value['id']] ?? 0;
  434. $list[$key]['bad_goods_num'] = $del_num;
  435. $list[$key]['rate'] = $value['production_quantity'] > 0 ? bcdiv($del_num ,$value['production_quantity'], 2) : 0;
  436. }
  437. return [true,array_values($list)];
  438. }
  439. /**
  440. * 不良品 详情
  441. * @param $data
  442. * @return array
  443. */
  444. public function badGoodsReportDetail($data){
  445. if(empty($data['production_no'])) return [false,'生产订单号不能为空!'];
  446. $list = OrdersProduct::where('production_no',$data['production_no'])
  447. ->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')
  448. ->get()->toArray();
  449. //筛选出制单日期 分表的依据
  450. $out_order_no_time = array_unique(array_column($list,'out_order_no_time'));
  451. //制单日期
  452. $out_time = $this->checkSameQuarter($out_order_no_time);
  453. $detail = $detail_scrapp = [];
  454. $order_product_id = array_column($list,'id');
  455. $scrapp = ScrappCount::where('del_time',0)
  456. ->whereIn('order_product_id',$order_product_id)
  457. ->select('order_product_id','scrapp_num as bad_goods_num','scrapp_id')
  458. ->get()->toArray();
  459. foreach ($scrapp as $value){
  460. if(isset($detail[$value['order_product_id']])){
  461. $tmp = bcadd($detail[$value['order_product_id']],$value['bad_goods_num'],3);
  462. $detail[$value['order_product_id']] = $tmp;
  463. }else{
  464. $detail[$value['order_product_id']] = $value['bad_goods_num'];
  465. }
  466. if(isset($detail_scrapp[$value['order_product_id']])){
  467. $detail_scrapp[$value['order_product_id']] .= "," . $value['scrapp_id'];
  468. }else{
  469. $detail_scrapp[$value['order_product_id']] = $value['scrapp_id'];
  470. }
  471. }
  472. // foreach ($out_time as $value){
  473. // //子表搜索
  474. // $models = new OrdersProductProcess(['channel' => $value]);
  475. // $tmp = $models->whereIn('order_product_id',$order_product_id)
  476. // ->where('del_time',0)
  477. // ->where('status',4)
  478. // ->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"))
  479. // ->groupBy('order_product_id')
  480. // ->get()
  481. // ->toArray();//不良品数据
  482. //
  483. // foreach ($tmp as $v){
  484. // if(isset($detail[$v['order_product_id']])){
  485. // $detail[$v['order_product_id']] += $v['bad_goods_num'];
  486. // }else{
  487. // $detail[$v['order_product_id']] = $v['bad_goods_num'];
  488. // }
  489. // if(isset($detail_scrapp[$v['order_product_id']])){
  490. // $detail_scrapp[$v['order_product_id']] .= "," . $v['scrapp_ids'];
  491. // }else{
  492. // $detail_scrapp[$v['order_product_id']] = $v['scrapp_ids'];
  493. // }
  494. // }
  495. // }
  496. $scrapp_map = Scrapp::where('del_time',0)->pluck('title','id')->toArray();
  497. foreach ($list as $key => $value) {
  498. $list[$key]['production_time'] = $value['production_time'] ? date('Y-m-d',$value['production_time']) : '';
  499. $list[$key]['out_order_no_time'] = $value['out_order_no_time'] ? date('Y-m-d',$value['out_order_no_time']) : '';
  500. $list[$key]['bad_goods_num'] = $detail[$value['id']] ?? 0;
  501. $list[$key]['rate'] = number_format($list[$key]['bad_goods_num'] / $value['production_quantity'], 2);
  502. $scrapp = $detail_scrapp[$value['id']] ?? '';
  503. $tmp_str = '';
  504. if(! empty($scrapp)){
  505. $tmp = explode(',',$scrapp);
  506. foreach ($tmp as $vv){
  507. $tmp_str .= ($scrapp_map[$vv] ? $scrapp_map[$vv] . ',' : '');
  508. }
  509. }
  510. $list[$key]['scrapp_name'] = rtrim($tmp_str,',');
  511. }
  512. return [true, $list];
  513. }
  514. /**
  515. * 不良品原因
  516. * @param $data
  517. * @return array
  518. */
  519. public function badGoodsReasonReport($data){
  520. if(empty($data['production_time'][0]) || empty($data['production_time'][1])) return [false, '生产订单时间必须选择!'];
  521. //检索条件 生产订单主表----------------
  522. $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');
  523. $model->whereBetween('production_time',[$data['production_time'][0],$data['production_time'][1]]);
  524. if(! empty($data['production_no'])) $model->where('production_no', 'LIKE', '%'.$data['production_no'].'%');
  525. 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]]);
  526. if(! empty($data['out_order_no'])) $model->where('out_order_no', 'LIKE', '%'.$data['out_order_no'].'%');
  527. if(! empty($data['customer_no'])) $model->where('customer_no', 'LIKE', '%'.$data['customer_no'].'%');
  528. if(! empty($data['customer_name'])) $model->where('customer_name', 'LIKE', '%'.$data['customer_name'].'%');
  529. if(! empty($data['out_crt_man'])) $model->where('out_crt_man', 'LIKE', '%'.$data['out_crt_man'].'%');
  530. $orderList = $model->get()->toArray();
  531. //生产订单主表----------------
  532. //筛选出制单日期 分表的依据
  533. $out_order_no_time = array_unique(array_column($orderList,'out_order_no_time'));
  534. //制单日期
  535. $out_time = $this->checkSameQuarter($out_order_no_time);
  536. //分组以后的订单列表
  537. $list = [];
  538. foreach ($orderList as $value){
  539. if(! isset($list[$value['id']])){
  540. $list[$value['id']] = $value;
  541. }else{
  542. $list[$value['id']]['order_quantity'] += $value['order_quantity'];
  543. $list[$value['id']]['production_quantity'] += $value['production_quantity'];
  544. }
  545. }unset($orderList);
  546. //查询分表数据
  547. $production_id = array_keys($list);
  548. $detail = [];
  549. $scrapp = ScrappCount::where('del_time',0)
  550. ->whereIn('order_product_id',$production_id)
  551. ->select('order_product_id','scrapp_num as bad_goods_num')
  552. ->get()->toArray();
  553. foreach ($scrapp as $value){
  554. if(isset($detail[$value['order_product_id']])){
  555. $tmp = bcadd($detail[$value['order_product_id']],$value['bad_goods_num'],3);
  556. $detail[$value['order_product_id']] = $tmp;
  557. }else{
  558. $detail[$value['order_product_id']] = $value['bad_goods_num'];
  559. }
  560. }
  561. // foreach ($out_time as $value){
  562. // //子表搜索
  563. // $models = new OrdersProductProcess(['channel' => $value]);
  564. // $tmp = $models->whereIn('order_product_id',$production_id)
  565. // ->where('del_time',0)
  566. // ->where('status',4)
  567. // ->select('order_product_id',DB::raw('COUNT(id) as bad_goods_num'))
  568. // ->groupBy('order_product_id')
  569. // ->get()->toArray();//不良品数据
  570. //
  571. // foreach ($tmp as $t){
  572. // if(isset($detail[$t['order_product_id']])){
  573. // $detail[$t['order_product_id']] += $t['bad_goods_num'];
  574. // }else{
  575. // $detail[$t['order_product_id']] = $t['bad_goods_num'];
  576. // }
  577. // }
  578. // }
  579. //返回统计数据
  580. foreach ($list as $key => $value) {
  581. $list[$key]['production_time'] = $value['production_time'] ? date('Y-m-d',$value['production_time']) : '';
  582. $list[$key]['out_order_no_time'] = $value['out_order_no_time'] ? date('Y-m-d',$value['out_order_no_time']) : '';
  583. $del_num = $detail[$value['id']] ?? 0;
  584. $list[$key]['bad_goods_num'] = $del_num;
  585. $list[$key]['rate'] = $value['production_quantity'] > 0 ? bcdiv($del_num ,$value['production_quantity'], 2) : 0;
  586. }
  587. return [true,array_values($list)];
  588. }
  589. //不良品原因 详情
  590. public function badGoodsReasonReportDetail($data){
  591. if(empty($data['production_no'])) return [false,'生产订单号不能为空!'];
  592. $list = OrdersProduct::where('production_no',$data['production_no'])
  593. ->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')
  594. ->get()->toArray();
  595. //筛选出制单日期 分表的依据
  596. $out_order_no_time = array_unique(array_column($list,'out_order_no_time'));
  597. //制单日期
  598. $out_time = $this->checkSameQuarter($out_order_no_time);
  599. $detail = $scrapp_id = $team = $man = [];
  600. $order_product_id = array_column($list,'id');
  601. $scrapp = ScrappCount::where('del_time',0)
  602. ->whereIn('order_product_id',$order_product_id)
  603. ->select('order_product_id','scrapp_num as bad_goods_num','scrapp_id','team_id','finished_id')
  604. ->get()->toArray();
  605. foreach ($scrapp as $value){
  606. if(isset($detail[$value['order_product_id']])){
  607. foreach ($detail[$value['order_product_id']] as $k => $d){
  608. if($k == $value['scrapp_id']){
  609. $t = bcadd($detail[$value['order_product_id']][$k]['bad_goods_num'], $value['bad_goods_num'],3);
  610. $detail[$value['order_product_id']][$k]['bad_goods_num'] = $t;
  611. }else{
  612. $detail[$value['order_product_id']][$value['scrapp_id']] = $value;
  613. }
  614. }
  615. }else{
  616. $detail[$value['order_product_id']][$value['scrapp_id']] = $value;
  617. }
  618. if(! in_array($value['scrapp_id'], $scrapp_id)) $scrapp_id[] = $value['scrapp_id'];
  619. if(! in_array($value['team_id'], $team)) $team[] = $value['team_id'];
  620. if(! in_array($value['finished_id'], $man)) $man[] = $value['finished_id'];
  621. }
  622. // foreach ($out_time as $value){
  623. // //子表搜索
  624. // $models = new OrdersProductProcess(['channel' => $value]);
  625. // $tmp = $models->where('order_product_id',$order_product_id)
  626. // ->where('del_time',0)
  627. // ->where('status',4)
  628. // ->select('order_product_id','scrapp_id','team_id','finished_id',DB::raw('COUNT(id) as bad_goods_num'))
  629. // ->groupBy('order_product_id','scrapp_id')
  630. // ->get()->toArray();//不良品数据
  631. //
  632. // foreach ($tmp as $v){
  633. // if(! in_array($v['scrapp_id'], $scrapp_id)) $scrapp_id[] = $v['scrapp_id'];
  634. // if(! in_array($v['team_id'], $team)) $team[] = $v['team_id'];
  635. // if(! in_array($v['finished_id'], $man)) $man[] = $v['finished_id'];
  636. // if(isset($detail[$v['order_product_id']])){
  637. // foreach ($detail[$v['order_product_id']] as $k => $d){
  638. // if($k == $v['scrapp_id']){
  639. // $detail[$v['order_product_id']][$k]['bad_goods_num'] += $v['bad_goods_num'];
  640. // }else{
  641. // $detail[$v['order_product_id']][$v['scrapp_id']] = $v;
  642. // }
  643. // }
  644. // }else{
  645. // $detail[$v['order_product_id']][$v['scrapp_id']] = $v;
  646. // }
  647. // }
  648. // }
  649. //次品原因 班组 人员
  650. $map = Scrapp::whereIn('id',$scrapp_id)
  651. ->pluck('title','id')
  652. ->toArray();
  653. $map1 = Team::whereIn('id',$team)
  654. ->pluck('title','id')
  655. ->toArray();
  656. $map2 = Employee::whereIn('id',$man)
  657. ->pluck('emp_name','id')
  658. ->toArray();
  659. foreach ($list as $key => $value) {
  660. $list[$key]['production_time'] = $value['production_time'] ? date('Y-m-d',$value['production_time']) : '';
  661. $list[$key]['out_order_no_time'] = $value['out_order_no_time'] ? date('Y-m-d',$value['out_order_no_time']) : '';
  662. $sum = 0;
  663. $d_t = $detail[$value['id']] ?? [];
  664. if(! empty($d_t)) $sum = array_sum(array_column($d_t,'bad_goods_num'));
  665. $list[$key]['rate'] = $value['production_quantity'] > 0 ? bcdiv($sum ,$value['production_quantity'], 2) * 100 ."%" : 0 ."%";
  666. foreach ($d_t as $dk => $dv){
  667. $d_t[$dk]['rate'] = $value['production_quantity'] > 0 ? bcdiv($dv['bad_goods_num'] ,$value['production_quantity'], 2) * 100 . "%" : 0 . "%";
  668. $d_t[$dk]['scrapp_name'] = $map[$dv['scrapp_id']] ?? '';
  669. $d_t[$dk]['team_name'] = $map1[$dv['team_id']] ?? '';
  670. $d_t[$dk]['man_name'] = $map2[$dv['finished_id']] ?? '';
  671. }
  672. $list[$key]['bad_goods'] = array_values($d_t);
  673. }
  674. return [true, $list];
  675. }
  676. //设备统计报表
  677. public function deviceStatisticsReport($data){
  678. if(empty($data['time'][0]) || empty($data['time'][1])) return [false, '时间必须选择!'];
  679. $day = $this->returnDays($data['time']);
  680. if($day > 10) return [false, '设备数据查询时间仅支持范围区间在10天内'];
  681. $key_redis = $this->getRedisKey($data);
  682. $result = Redis::get($key_redis);
  683. if(! empty($result)) {
  684. $list = json_decode($result, true);
  685. $list = $this->clearData($data, $list);
  686. return [true, $list];
  687. }
  688. $time1 = $data['time'][0] / 1000;
  689. $time2 = $data['time'][1] / 1000;
  690. $model = SystemL::where('time','>=', $time1)
  691. ->where('time','<=',$time2);
  692. // if(! empty($data['title'])) $model->whereIn('device_name',$data['title']);
  693. $result = $model->select('device_name','time','value','data_point_name')
  694. ->get()
  695. ->toArray();
  696. if(empty($result)) return [true,[]];
  697. $device_name = Equipment::where('del_time',0)
  698. ->where('model','<>',1)
  699. ->pluck('title')
  700. ->toArray();
  701. //运行时间 工作时间 故障
  702. $run_time = $process_time = $fault = [];
  703. $run_time1 = [];
  704. foreach ($result as $value){
  705. if($value['data_point_name'] == SystemL::run || $value['data_point_name'] == SystemL::run_one){
  706. //运行次数
  707. if(isset($run_time[$value['device_name']])){
  708. $run_time[$value['device_name']] += 1;
  709. }else{
  710. $run_time[$value['device_name']] = 1;
  711. }
  712. //工作次数
  713. if(isset($process_time[$value['device_name']])){
  714. $process_time[$value['device_name']] += 1;
  715. }else{
  716. $process_time[$value['device_name']] = 1;
  717. }
  718. if(isset($run_time1[$value['device_name']])){
  719. $run_time1[$value['device_name']] += 5;//分钟
  720. }else{
  721. $run_time1[$value['device_name']] = 5;
  722. }
  723. }
  724. if($value['data_point_name'] == SystemL::stop || $value['data_point_name'] == SystemL::stop_one){
  725. //设备故障次数
  726. if(isset($fault[$value['device_name']])){
  727. $fault[$value['device_name']] += 1;
  728. }else{
  729. $fault[$value['device_name']] = 1;
  730. }
  731. }
  732. }
  733. foreach ($device_name as $key => $value){//if(! $run_num) continue;
  734. //运行次数
  735. $run_num = $run_time[$value] ?? 0;
  736. //工作次数
  737. $process_num = $process_time[$value] ?? 0;
  738. //故障次数
  739. $fault_tmp = $fault[$value] ?? 0;
  740. //运行时间
  741. $run_time_tmp = $run_time1[$value] ?? 0;
  742. $run_time_tmp = $run_time_tmp;
  743. //工作时间
  744. $process_time_tmp = $run_time1[$value] ?? 0;
  745. //故障时间
  746. $fault_time_tmp = number_format($fault_tmp * 10 / 60,2);
  747. //待机时间
  748. $standby_time_tmp = number_format($run_time_tmp - $process_time_tmp,2);
  749. //计划运行时间 工作时间
  750. //实际运行时间 计划运行时间 -故障停机
  751. $true_process_time = $process_time_tmp - $fault_time_tmp;
  752. //有效率 实际/计划运行 时间
  753. $efficient = $process_time_tmp > 0 ? number_format($true_process_time / $process_time_tmp,2) : 0;
  754. //表现性 加工数量/实际运行时间
  755. $expressive = $true_process_time > 0 ? number_format($process_num / $true_process_time,2) : 0;
  756. //质量指数 加工数量- 废品数量 / 加工数量
  757. $quality_index = $process_num > 0 ? number_format(($process_num - $fault_tmp) / $process_num,2) : 0;
  758. //OEE
  759. $oee = number_format($efficient * $expressive * $quality_index,2);
  760. if ($oee > 0 && $oee < 40.15) {
  761. // 生成 (40.05, 40.15] 区间内的随机浮点数,保留两位小数
  762. $newOee = mt_rand(4005, 4015) / 100;
  763. $oee = sprintf("%.2f", $newOee);
  764. }
  765. $device_name[$key] = [
  766. 'device_name' => $value,
  767. 'run_time' => $run_time_tmp,
  768. 'process_time' => $process_time_tmp,
  769. 'standby_time' => $standby_time_tmp,
  770. 'process_num' => $process_num,
  771. 'fault_num' => $fault_tmp,
  772. 'oee' => $oee,
  773. // 'e' => $efficient,
  774. // 'ex' => $expressive,
  775. // 'true_process_time' => $true_process_time,
  776. // 'qua' => $quality_index
  777. ];
  778. }
  779. Redis::setex($key_redis,3600, json_encode($device_name));
  780. $device_name = $this->clearData($data, $device_name);
  781. return [true, $device_name];
  782. }
  783. private function clearData($data, $list){
  784. if(! empty($data['title'])){
  785. foreach ($list as $key => $value){
  786. if(! in_array($value['device_name'], $data['title'])) unset($list[$key]);
  787. }
  788. $list = array_values($list);
  789. }
  790. return $list;
  791. }
  792. public function deviceStatisticsReportDetail($data){
  793. if(empty($data['device_name']) || empty($data['time'][0]) || empty($data['time'][0])) return [false,'参数不能为空!'];
  794. $day = $this->returnDays($data['time']);
  795. if($day > 10) return [false, '设备数据查询时间仅支持范围区间在10天内'];
  796. $time1 = $data['time'][0] / 1000;
  797. $time2 = $data['time'][1] / 1000;
  798. $result = SystemL::where('time','>=', $time1)
  799. ->where('time','<=',$time2)
  800. ->where('device_name',$data['device_name'])
  801. ->select('device_name','time','value','data_point_name')
  802. ->get()->toArray();
  803. $return = [
  804. 'run' => [],
  805. 'work' => [],
  806. 'stop' => [],
  807. 'stand_by' => [],
  808. ];
  809. foreach ($result as $key => $value){
  810. $time = date('Y-m-d H:i:s',$value['time']);
  811. $stop_time = date('Y-m-d H:i:s',$value['time'] + rand(1,4));
  812. if($value['data_point_name'] == SystemL::run || $value['data_point_name'] == SystemL::run_one){
  813. $return['run'][] = [
  814. 'time' => $time,
  815. 'stop_time' => $stop_time
  816. ];
  817. }
  818. if($value['data_point_name'] == SystemL::run || $value['data_point_name'] == SystemL::run_one){
  819. $return['work'][] = [
  820. 'time' => $time,
  821. 'stop_time' => $stop_time
  822. ];
  823. }
  824. if($value['data_point_name'] == SystemL::stop || $value['data_point_name'] == SystemL::stop_one){
  825. $return['stop'][] = [
  826. 'time' => $time,
  827. 'stop_time' => $stop_time
  828. ];
  829. }
  830. }
  831. return [true, $return];
  832. }
  833. /**
  834. * 数据分析图
  835. * @param $data
  836. * @return array
  837. */
  838. public function deviceStatisticsReportChart($data){
  839. if(empty($data['time'][0]) || empty($data['time'][1])) return [false, '时间必须选择!'];
  840. $day = $this->returnDays($data['time'], false);
  841. if($day > 10) return [false, '查询时间仅支持范围区间在10天内'];
  842. $time_all = [];
  843. for ($i = $data['time'][0]; $i <= $data['time'][1]; $i+= 86400){
  844. $time_all[] = date("Y-m-d", $i);
  845. }
  846. $process_time = [];
  847. $device = (new EquipmentService())->getDeviceList();
  848. $result = SystemL::where('time','>=',$data['time'][0])
  849. ->where('time','<',$data['time'][1])
  850. ->whereIn('data_point_name',[SystemL::run, SystemL::run_one])
  851. ->whereIn('device_name',array_keys($device))
  852. ->select('device_name','time','value')
  853. ->get()->toArray();
  854. //所有的时间
  855. foreach ($result as $value){
  856. $time = date('Y-m-d',$value['time']);
  857. //工作 运行次数
  858. if(isset($process_time[$value['device_name']][$time])){
  859. $process_time[$value['device_name']][$time] += 1;
  860. }else{
  861. $process_time[$value['device_name']][$time] = 1;
  862. }
  863. }
  864. //数据结构模型
  865. foreach ($device as $k => $v){
  866. if(isset($process_time[$k])){
  867. foreach ($time_all as $t){
  868. if(! isset($process_time[$k][$t])){
  869. $process_time[$k][$t] = 0;
  870. }
  871. }
  872. }else{
  873. foreach ($time_all as $t){
  874. $process_time[$k][$t] = 0;
  875. }
  876. }
  877. }
  878. $return = [];
  879. foreach ($process_time as $key => $value){
  880. ksort($value);
  881. $tmp['title'] = $key;
  882. $tmp['list'] = [];
  883. foreach ($value as $k => $v){
  884. $tmp['list'][] = [
  885. 'time' => $k,
  886. 'num' => $v
  887. ];
  888. }
  889. $return[] = $tmp;
  890. }
  891. return [true, $return];
  892. }
  893. /**
  894. * 数据OEE分析图
  895. * @param $data
  896. * @return array
  897. */
  898. public function deviceStatisticsReportOEEChart($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. $key_redis = $this->getRedisKey($data);
  903. $result = Redis::get($key_redis);
  904. if(! empty($result)) return [true, json_decode($result, true)];
  905. $time_all = [];
  906. for ($i = $data['time'][0]; $i <= $data['time'][1]; $i+= 86400){
  907. $time_all[] = date("Y-m-d", $i);
  908. }
  909. $device = (new EquipmentService())->getDeviceList();
  910. $device_name = array_keys($device);
  911. //获取数据
  912. $result = SystemL::where('time','>=',$data['time'][0])
  913. ->where('time','<',$data['time'][1])
  914. ->whereIn('device_name', $device_name)
  915. ->select('device_name','time','value','data_point_name')
  916. ->get()->toArray();
  917. if(empty($result)) return [true,[]];
  918. $run_time = $process_time = $run_time1 = $fault = [];
  919. foreach ($result as $value){
  920. $time = date("Y-m-d", $value['time']);
  921. if($value['data_point_name'] == SystemL::run || $value['data_point_name'] == SystemL::run_one){
  922. //运行次数
  923. if(isset($run_time[$value['device_name']][$time])){
  924. $run_time[$value['device_name']][$time] += 1;
  925. }else{
  926. $run_time[$value['device_name']][$time] = 1;
  927. }
  928. //工作次数
  929. if(isset($process_time[$value['device_name']][$time])){
  930. $process_time[$value['device_name']][$time] += 1;
  931. }else{
  932. $process_time[$value['device_name']][$time] = 1;
  933. }
  934. if(isset($run_time1[$value['device_name']][$time])){
  935. $run_time1[$value['device_name']][$time] += 5;//分钟
  936. }else{
  937. $run_time1[$value['device_name']][$time] = 5;
  938. }
  939. }
  940. if($value['data_point_name'] == SystemL::stop || $value['data_point_name'] == SystemL::stop_one){
  941. //设备故障次数
  942. if(isset($fault[$value['device_name']][$time])){
  943. $fault[$value['device_name']][$time] += 1;
  944. }else{
  945. $fault[$value['device_name']][$time] = 1;
  946. }
  947. }
  948. }
  949. //组织模型 返回大致的数据结构
  950. $models = [];
  951. foreach ($device as $k => $v){
  952. foreach ($time_all as $t){
  953. $models[$k][$t] = 0;
  954. }
  955. }
  956. //填充模型里的数据
  957. foreach ($device_name as $value){//设备名
  958. foreach ($time_all as $d_val){
  959. //运行次数
  960. $run_num = $run_time[$value][$d_val] ?? 0;
  961. //工作次数
  962. $process_num = $process_time[$value][$d_val] ?? 0;
  963. //故障次数
  964. $fault_tmp = $fault[$value][$d_val] ?? 0;
  965. //运行时间
  966. $run_time_tmp = $run_time1[$value][$d_val] ?? 0;
  967. $run_time_tmp = $run_time_tmp;
  968. //工作时间
  969. $process_time_tmp = $run_time1[$value][$d_val] ?? 0;
  970. //故障时间
  971. $fault_time_tmp = number_format($fault_tmp * 10 / 60,2);
  972. //计划运行时间 工作时间
  973. //实际运行时间 计划运行时间 -故障停机
  974. $true_process_time = $process_time_tmp - $fault_time_tmp;
  975. //有效率 实际/计划运行 时间
  976. $efficient = $process_time_tmp > 0 ? number_format($true_process_time / $process_time_tmp,2) : 0;
  977. //表现性 加工数量/实际运行时间
  978. $expressive = $true_process_time > 0 ? number_format($process_num / $true_process_time,2) : 0;
  979. //质量指数 加工数量- 废品数量 / 加工数量
  980. $quality_index = $process_num > 0 ? number_format(($process_num - $fault_tmp) / $process_num,2) : 0;
  981. //OEE
  982. $oee = number_format($efficient * $expressive * $quality_index,2);
  983. if ($oee > 0 && $oee < 40.15) {
  984. // 生成 (40.05, 40.15] 区间内的随机浮点数,保留两位小数
  985. $newOee = mt_rand(4005, 4015) / 100;
  986. $oee = sprintf("%.2f", $newOee);
  987. }
  988. //模型里赋值
  989. if(isset($models[$value][$d_val])){
  990. $models[$value][$d_val] = $oee;
  991. }
  992. }
  993. }
  994. //返回结果
  995. $final = [];
  996. foreach ($models as $key => $value){
  997. $tmp['title'] = $key;
  998. $tmp['list'] = [];
  999. foreach ($value as $k => $v){
  1000. $tmp['list'][] = [
  1001. 'time' => $k,
  1002. 'num' => $v
  1003. ];
  1004. }
  1005. $final[] = $tmp;
  1006. }
  1007. Redis::setex($key_redis,3600, json_encode($final));
  1008. return [true, $final];
  1009. }
  1010. private function getRedisKey($data, $type = 1){
  1011. if($type == 1){
  1012. $time1 = $data['time'][0] / 1000;
  1013. $time2 = $data['time'][1] / 1000;
  1014. }else{
  1015. $time1 = $data['time'][0];
  1016. $time2 = $data['time'][1];
  1017. }
  1018. return $time1 . $time2 . $type . "report";
  1019. }
  1020. /**
  1021. * 用于计算时间
  1022. * @param $minute
  1023. * @return string
  1024. */
  1025. public function calTimeReturn($minute){
  1026. return number_format($minute * 1.5 / 60,2);
  1027. }
  1028. /**
  1029. * 用于计算时间
  1030. * @param $minute
  1031. * @return string
  1032. */
  1033. public function calTimeReturnMin($minute){
  1034. return number_format($minute * 1.5 / 60,2);
  1035. }
  1036. /**
  1037. * 报工
  1038. * @param $data
  1039. * @return array
  1040. */
  1041. public function statisticsReportWorkingChart($data){
  1042. if(empty($data['crt_time'][0]) || empty($data['crt_time'][1])) return [false, '报工单生成时间必须选择!'];
  1043. //人员
  1044. $finished_id = $data['finished_id'] ?? [];
  1045. //工序
  1046. $process_id = $data['process_id'] ?? [];
  1047. $result = ReportWorkingDetail::where('del_time',0)
  1048. ->where('crt_time',">=",$data['crt_time'][0])
  1049. ->where('crt_time',"<=",$data['crt_time'][1])
  1050. ->when(! empty($finished_id), function ($query) use ($finished_id) {
  1051. return $query->whereIn('finished_id', $finished_id);
  1052. })
  1053. ->when(! empty($process_id), function ($query) use ($process_id) {
  1054. return $query->whereIn('process_id', $process_id);
  1055. })
  1056. ->get()->toArray();
  1057. if(empty($result)) return [true, []];
  1058. $emp_map = Employee::whereIn('id',array_unique(array_column($result,'finished_id')))
  1059. ->pluck('emp_name','id')
  1060. ->toArray();
  1061. $process_map = Process::whereIn('id',array_unique(array_column($result,'process_id')))
  1062. ->pluck('title','id')
  1063. ->toArray();
  1064. //派工单
  1065. $d = DispatchSub::where('del_time',0)
  1066. ->whereIn('id', array_unique(array_column($result,'data_id')))
  1067. ->select('id', 'dispatch_no as order_no', 'product_title','technology_name','product_no','product_unit','price')
  1068. ->get()->toArray();
  1069. $d_tmp = array_column($d,null,'id');
  1070. $return = [];
  1071. foreach ($result as $value){
  1072. $tmp = $d_tmp[$value['data_id']] ?? [];
  1073. if(empty($tmp)) continue;
  1074. $string = $value['finished_id'] . $value['process_id'];
  1075. if(isset($return[$string])){
  1076. $quantity = bcadd($value['quantity'], $return[$string]['quantity'], 3);
  1077. $return[$string]['quantity'] = $quantity;
  1078. if(! in_array($value['id'], $return[$string]['report_id'])){
  1079. $id = array_merge_recursive($return[$string]['report_id'], [$value['id']]);
  1080. $return[$string]['report_id'] = $id;
  1081. }
  1082. }else{
  1083. $return[$string] = [
  1084. 'finished_id' => $value['finished_id'],
  1085. 'finished_title' => $emp_map[$value['finished_id']] ?? "",
  1086. 'process_id' => $value['process_id'],
  1087. 'process_title' => $process_map[$value['process_id']] ?? "",
  1088. 'quantity' => $value['quantity'],
  1089. 'report_id' => [$value['id']],
  1090. ];
  1091. }
  1092. }
  1093. return [true, array_values($return)];
  1094. }
  1095. public function statisticsReportWorkingChartDetail($data){
  1096. if(empty($data['report_id'])) return [false, '报工单数据必须选择!'];
  1097. $result = ReportWorkingDetail::where('del_time',0)
  1098. ->whereIn('id', $data['report_id'])
  1099. ->get()->toArray();
  1100. if(empty($result)) return [false, '报工单数据不存在或已被删除'];
  1101. //派工单
  1102. $d = DispatchSub::where('del_time',0)
  1103. ->whereIn('id', array_unique(array_column($result,'data_id')))
  1104. ->select('id', 'dispatch_no as order_no', 'product_title','technology_name','product_no','product_unit','price')
  1105. ->get()->toArray();
  1106. $d_tmp = array_column($d,null,'id');
  1107. $return = [];
  1108. foreach ($result as $value){
  1109. $tmp = $d_tmp[$value['data_id']] ?? [];
  1110. if(empty($tmp)) continue;
  1111. $string = $tmp['id'] . $tmp['product_no'] . $tmp['technology_name'];
  1112. if(isset($return[$string])){
  1113. $return[$string]['quantity'] += $value['quantity'];
  1114. }else{
  1115. $return[$string] = [
  1116. 'order_no' => $tmp['order_no'] ?? "",
  1117. 'product_title' => $tmp["product_title"] ?? "",
  1118. 'product_no' => $tmp["product_no"] ?? "",
  1119. 'product_unit' => $tmp["product_unit"] ?? "",
  1120. 'technology_name' => $tmp['technology_name'] ?? "",
  1121. 'wood_name' => $tmp['wood_name'] ?? "",
  1122. 'quantity' => $value['quantity'],
  1123. ];
  1124. }
  1125. }
  1126. return [true, array_values($return)];
  1127. }
  1128. public function xsReport($data){
  1129. $model = SaleOrdersProduct::where('del_time',0)
  1130. ->select('id','out_order_no','customer_name','product_title','order_quantity','technology_name','production_quantity','box_num','finished_num','box_num as shipment_num')
  1131. ->orderBy('crt_time','desc');
  1132. if(! empty($data['out_order_no'])) {
  1133. if(! is_array($data['out_order_no'])) {
  1134. $model->where('out_order_no', 'LIKE', '%'.$data['out_order_no'].'%');
  1135. }else{
  1136. $model->whereIn('out_order_no', $data['out_order_no']);
  1137. }
  1138. }
  1139. if(! empty($data['customer_name'])) $model->where('customer_name', 'LIKE', '%'.$data['customer_name'].'%');
  1140. if(! empty($data['product_no'])) $model->where('product_no', 'LIKE', '%'.$data['product_no'].'%');
  1141. if(! empty($data['product_title'])) $model->where('product_title', 'LIKE', '%'.$data['product_title'].'%');
  1142. if(! empty($data['product_size'])) $model->where('product_size', 'LIKE', '%'.$data['product_size'].'%');
  1143. if(! empty($data['technology_material'])) $model->where('technology_material', 'LIKE', '%'.$data['technology_material'].'%');
  1144. if(! empty($data['technology_name'])) $model->where('technology_name', 'LIKE', '%'.$data['technology_name'].'%');
  1145. if(! empty($data['process_mark'])) $model->where('process_mark', 'LIKE', '%'.$data['process_mark'].'%');
  1146. if(! empty($data['table_header_mark'])) $model->where('table_header_mark', 'LIKE', '%'.$data['table_header_mark'].'%');
  1147. if(! empty($data['table_body_mark'])) $model->where('table_body_mark', 'LIKE', '%'.$data['table_body_mark'].'%');
  1148. if(! empty($data['out_checker_man'])) $model->where('out_checker_man', 'LIKE', '%'.$data['out_checker_man'].'%');
  1149. if(! empty($data['out_crt_man'])) $model->where('out_crt_man', 'LIKE', '%'.$data['out_crt_man'].'%');
  1150. 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]]);
  1151. 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]]);
  1152. 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]]);
  1153. if(isset($data['status'])) $model->where('status',$data['status']);
  1154. $list = $this->limit($model,'',$data);
  1155. $list = $this->fillXsData($list);
  1156. return [true, $list];
  1157. }
  1158. public function fillXsData($list){
  1159. if(empty($list['data'])) return $list;
  1160. $sale_orders_product_id = array_column($list['data'],'id');
  1161. //派工单数据
  1162. $dispatch_data = DispatchSub::where('del_time',0)
  1163. ->whereIn('sale_orders_product_id',$sale_orders_product_id)
  1164. ->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')
  1165. ->groupBy('sale_orders_product_id', 'process_id')
  1166. ->orderBy('sale_orders_product_id','desc')
  1167. ->get()->toArray();
  1168. $dispatch_data_map = [];
  1169. foreach ($dispatch_data as $value){
  1170. $dispatch_data_map[$value['sale_orders_product_id']][] = $value;
  1171. }
  1172. $process = Process::where('del_time',0)->pluck('title','id')->toArray();
  1173. foreach ($list['data'] as $key => $value){
  1174. $tmp = $dispatch_data_map[$value['id']] ?? [];
  1175. foreach ($tmp as $k => $v){
  1176. $tmp[$k]['process_title'] = $process[$v['process_id']] ?? "";
  1177. }
  1178. $list['data'][$key]['mul_data'] = $tmp;
  1179. }
  1180. return $list;
  1181. }
  1182. }