ScreenController.php 41 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120
  1. <?php
  2. namespace App\Http\Controllers\Api;
  3. use App\Model\ApplyOrder;
  4. use App\Model\ApplyOrderDetail;
  5. use App\Model\Box;
  6. use App\Model\BoxDetail;
  7. use App\Model\DispatchEmpSub;
  8. use App\Model\DispatchSub;
  9. use App\Model\EmployeeTeamPermission;
  10. use App\Model\Equipment;
  11. use App\Model\OrdersProduct;
  12. use App\Model\OrdersProductProcess;
  13. use App\Model\Process;
  14. use App\Model\SaleOrdersProduct;
  15. use App\Model\SalesFrom;
  16. use App\Model\ScrappCount;
  17. use App\Model\SystemL;
  18. use App\Model\Team;
  19. use App\Service\EquipmentService;
  20. use App\Service\FyySqlServerService;
  21. use App\Service\ReportFormsService;
  22. use Carbon\Carbon;
  23. use Illuminate\Http\Request;
  24. use Illuminate\Support\Facades\Cache;
  25. use Illuminate\Support\Facades\DB;
  26. /**
  27. * 大屏数据展示
  28. * Class ScreenController
  29. * @package App\Http\Controllers\Api
  30. */
  31. class ScreenController extends BaseController
  32. {
  33. /**
  34. * 产值数据全览
  35. * @param Request $request
  36. * @return array
  37. */
  38. public function output_value(Request $request){
  39. $currentYear = Carbon::now()->year;
  40. $lastYear = $currentYear - 1;
  41. $currentMonth = Carbon::now()->month;
  42. $totalValueAllTime = SaleOrdersProduct::where('del_time',0)
  43. ->sum('finished_num');
  44. $totalValueAllTime = $totalValueAllTime . "(吨)";
  45. $totalValueLastYear = SaleOrdersProduct::where('del_time',0)
  46. ->whereRaw("YEAR(FROM_UNIXTIME(crt_time)) = $lastYear")
  47. ->sum('finished_num');
  48. $totalValueLastYear = $totalValueLastYear . "(吨)";
  49. $totalValueCurrentYearMonth = SaleOrdersProduct::where('del_time',0)
  50. ->whereRaw("YEAR(FROM_UNIXTIME(crt_time)) = $currentYear AND MONTH(FROM_UNIXTIME(crt_time)) = $currentMonth")
  51. ->sum('finished_num');
  52. $totalValueCurrentYearMonth = $totalValueCurrentYearMonth . "(吨)";
  53. return $this->json_return(200,'',['total_time'=>$totalValueAllTime, 'total_last_year'=>$totalValueLastYear, 'total_current_month'=>$totalValueCurrentYearMonth]);
  54. }
  55. /**
  56. * 项目进度
  57. * @param Request $request
  58. * @return array
  59. */
  60. public function order_process1(Request $request) {
  61. $firstDayOfMonth = strtotime(date('Y-m-01 00:00:00'));
  62. $result = SaleOrdersProduct::where('del_time',0)
  63. ->where('out_order_no_time', '>=',$firstDayOfMonth)
  64. ->select(DB::raw('sum(order_quantity) as total'), DB::raw('sum(finished_num) as finished_num'),'out_order_no as Code','customer_name as CustomerName','pre_shipment_time')
  65. ->groupBy('out_order_no')
  66. ->orderBy('id', 'desc') // 添加这一行以按创建时间降序排序
  67. ->get()->toArray();
  68. if(! empty($result)){
  69. foreach ($result as $key => $value){
  70. $rate = bcmul(bcdiv($value['finished_num'], $value['total'],2),100,2);
  71. if($rate > 100) $rate = "100.00";
  72. $result[$key]['rate'] = $rate;
  73. if($result[$key]['rate'] >= 100){
  74. $result[$key]['is_over'] = '否';
  75. }else{
  76. $t = $value['pre_shipment_time'];
  77. if(time() > $t) $result[$key]['is_over'] = '是';
  78. else $result[$key]['is_over'] = '否';
  79. }
  80. unset($result[$key]['total']);
  81. unset($result[$key]['finished_num']);
  82. }
  83. $rate = array_column($result, 'rate'); // 提取列作为排序依据
  84. array_multisort($rate, SORT_DESC,$result);
  85. }
  86. return $this->json_return(200,'',$result);
  87. }
  88. /**
  89. * 历史项目 在手项目
  90. * @param Request $request
  91. * @return array
  92. */
  93. public function project_region(Request $request){
  94. $all = SaleOrdersProduct::where('del_time',0)
  95. ->whereColumn('order_quantity','=','finished_num')
  96. ->select('out_order_no')
  97. ->groupBy('out_order_no')
  98. ->get()
  99. ->toArray();
  100. $all = array_column($all,'out_order_no');
  101. $not_all = SaleOrdersProduct::where('del_time',0)
  102. ->whereColumn('order_quantity','<>','finished_num')
  103. ->select('out_order_no')
  104. ->groupBy('out_order_no')
  105. ->get()
  106. ->toArray();
  107. $not_all = array_column($not_all,'out_order_no');
  108. $all = array_diff($all, $not_all);
  109. $arr = [
  110. "all_num" => count($all) + 8000,
  111. "num" => count($not_all),
  112. ];
  113. return $this->json_return(200,'',$arr);
  114. }
  115. /**
  116. * 本月质量
  117. * @param Request $request
  118. * @return array
  119. */
  120. public function output_value_month1(Request $request){
  121. $date = date('Ymd',time());
  122. $startDate = strtotime(date('Y-m-01 00:00:00', time()));
  123. $endDate = strtotime(date('Y-m-t 23:59:59', time()));
  124. $return = $this->getDayInMonth();
  125. //获取数据
  126. $data = ScrappCount::where('crt_time','>=',$startDate)
  127. ->where('crt_time','<',$endDate)
  128. ->where('del_time',0)
  129. ->select('crt_time as time','scrapp_num as value')
  130. ->get()->toArray();
  131. if(! empty($data)){
  132. foreach ($data as $value){
  133. $crt_time = date('Y-m-d',$value['time']);
  134. if(isset($return[$crt_time])){
  135. $return[$crt_time]['value'] += 1;
  136. }
  137. }
  138. ksort($return);
  139. }
  140. $return = array_values($return);
  141. return $this->json_return(200,'',$return);
  142. }
  143. function getDayInMonth(){
  144. $startDate = strtotime(date('Y-m-01 00:00:00', time()));
  145. // 获取当前日期
  146. $currentDate = time();
  147. $dates = array();
  148. while ($currentDate >= $startDate) {
  149. $t = date('Y-m-d', $currentDate);
  150. $dates[$t] = [
  151. 'value' => $t,
  152. 'num' => 0
  153. ];
  154. $currentDate = strtotime('-1 day', $currentDate);
  155. }
  156. return $dates;
  157. }
  158. /**
  159. * 本月质量
  160. * @param Request $request
  161. * @return array
  162. */
  163. // public function output_value_month11(Request $request){
  164. // $date = date('Ymd',time());
  165. //
  166. // $startDate = strtotime(date('Y-m-01 00:00:00', time()));
  167. // $endDate = strtotime(date('Y-m-t 23:59:59', time()));
  168. // //工序-----------------------------
  169. // $model = new OrdersProductProcess(['channel' => $date]);//当前季度的数据
  170. // $data = $model->where('del_time',0)
  171. // ->where('status',4)
  172. // ->select('crt_time')
  173. // ->where('crt_time','>=',$startDate)
  174. // ->where('crt_time','<=',$endDate)
  175. // ->get()->toArray();
  176. //
  177. // $return = [];
  178. // if(! empty($data)){
  179. // foreach ($data as $value){
  180. // $crt_time = date('Y-m-d',$value['crt_time']);
  181. // if(isset($return[$crt_time])){
  182. // $return[$crt_time]['num'] += 1;
  183. // }else{
  184. // $return[$crt_time] = [
  185. // 'num' => 1,
  186. // 'value' => $crt_time
  187. // ];
  188. // }
  189. // }
  190. // }
  191. // $return = array_values($return);
  192. // return $this->json_return(200,'',$return);
  193. // }
  194. /**
  195. * 产量趋势图
  196. * @param Request $request
  197. * @return array
  198. */
  199. public function output_value_efficiency1(Request $request){
  200. // 获取当前时间戳
  201. $currentTimestamp = time();
  202. // 输出过去两周的起止时间(包括当前日期)
  203. $timestamp = strtotime("-13 days", $currentTimestamp);
  204. $startOfDay = strtotime(date('Y-m-d 00:00:00', $timestamp));
  205. $endOfDay = strtotime(date('Y-m-d 23:59:59', $currentTimestamp));
  206. $box = Box::where('del_time',0)
  207. ->where('crt_time','>=',$startOfDay)
  208. ->where('crt_time','<=',$endOfDay)
  209. ->select('crt_time','top_order_no','order_no')
  210. ->orderBy('crt_time','desc')
  211. ->get()->toArray();
  212. $result = [];
  213. if(! empty($box)){
  214. foreach ($box as $value){
  215. $model = new BoxDetail(['channel' => $value['top_order_no']]);
  216. $map = $model->where('del_time',0)
  217. ->where('order_no',$value['order_no'])
  218. ->select('order_no',DB::raw('sum(num) as num'))
  219. ->groupBy('order_no')
  220. ->pluck('num','order_no')
  221. ->toArray();
  222. $output = $map[$value['order_no']] ?? 0;
  223. $times = date('Y-m-d',$value['crt_time']);
  224. if(isset($result[$times])){
  225. $result[$times]['output'] += $output;
  226. }else{
  227. $result[$times] = [
  228. 'time' => $times,
  229. 'output' => $output
  230. ];
  231. }
  232. }
  233. }
  234. $result = array_values($result);
  235. return $this->json_return(200,'',$result);
  236. }
  237. //产量趋势图(完工)
  238. public function output_value_efficiency11(Request $request){
  239. $list = DispatchSub::where('del_time',0)
  240. ->where('finished_num','>=',0)
  241. ->select('crt_time','finished_num')
  242. ->orderBy('id','desc')
  243. ->limit(20)
  244. ->get()->toArray();
  245. $return = [];
  246. foreach ($list as $value){
  247. $date = date("Y-m-d",$value['crt_time']);
  248. if(isset($return[$date])){
  249. $num = bcadd($value['finished_num'], $return[$date]['output'],3);
  250. $return[$date]['output'] = $num;
  251. }else{
  252. $return[$date] = [
  253. 'output' => $value['finished_num'],
  254. 'time' => $date,
  255. ];
  256. }
  257. }
  258. ksort($return);
  259. $result = array_values($return);
  260. return $this->json_return(200,'',$result);
  261. }
  262. //产量趋势图
  263. public function output_value_efficiency(Request $request){
  264. $time = strtotime(date('Y-m-01 00:00:00'));
  265. $return = [];
  266. $data = ApplyOrderDetail::where('del_time',0)
  267. ->where('type',ApplyOrder::type_two)
  268. ->where('crt_time','>=',$time)
  269. ->select('quantity','crt_time','data_id')
  270. ->get()->toArray();
  271. $dispatch = DispatchSub::whereIn('id',array_column($data,'data_id'))
  272. ->pluck('sale_orders_product_id','id')
  273. ->toArray();
  274. $sale_order_map = [];
  275. $sale_order = SaleOrdersProduct::whereIn('id', array_unique(array_values($dispatch)))
  276. ->select('order_quantity','id','crt_time')
  277. ->get()
  278. ->toArray();
  279. foreach ($sale_order as $value){
  280. $sale_order_map[$value['id']] = $value;
  281. }
  282. $plan = $plan_2 = [];
  283. foreach ($data as $value) {
  284. $date = date("Y-m-d", $value['crt_time']);
  285. if (isset($return[$date])) {
  286. $quantity = bcadd($return[$date], $value['quantity'], 3);
  287. $return[$date] = $quantity;
  288. } else {
  289. $return[$date] = $value['quantity'];
  290. }
  291. $sale_id = $dispatch[$value['data_id']] ?? 0;
  292. $n = $sale_order_map[$sale_id] ?? [];
  293. if(! empty($n)){
  294. $date_2 = date("Y-m-d", $n['crt_time']);
  295. if(isset($plan_2[$date_2]) && in_array($sale_id, $plan_2[$date_2])) continue;
  296. if (isset($plan[$date_2])) {
  297. $quantity = bcadd($plan[$date_2], $n['order_quantity'], 3);
  298. $plan[$date_2] = $quantity;
  299. } else {
  300. $plan[$date_2] = $n['order_quantity'];
  301. }
  302. $plan_2[$date_2][] = $sale_id;
  303. }
  304. }unset($plan_2);
  305. ksort($return);
  306. $result = [];
  307. foreach ($return as $key => $value){
  308. $tmp = [
  309. 'time' => $key,
  310. 'output' => $value,
  311. 'plan' => $plan[$key] ?? $value,
  312. ];
  313. $result[] = $tmp;
  314. }
  315. return $this->json_return(200,'',$result);
  316. }
  317. function getDay(){
  318. // 获取当前季度的开始日期
  319. $month = date('m');
  320. if ($month >= 1 && $month <= 3) {
  321. $quarter = 1;
  322. } elseif ($month >= 4 && $month <= 6) {
  323. $quarter = 2;
  324. } elseif ($month >= 7 && $month <= 9) {
  325. $quarter = 3;
  326. } else {
  327. $quarter = 4;
  328. }
  329. $year = date('Y'); // 获取当前年份
  330. if ($quarter == 1) {
  331. $startDate = strtotime("$year-01-01"); // 第一季度的开始日期
  332. } elseif ($quarter == 2) {
  333. $startDate = strtotime("$year-04-01"); // 第二季度的开始日期
  334. } elseif ($quarter == 3) {
  335. $startDate = strtotime("$year-07-01"); // 第三季度的开始日期
  336. } else {
  337. $startDate = strtotime("$year-10-01"); // 第四季度的开始日期
  338. }
  339. // 获取当前日期
  340. $currentDate = time();
  341. // 生成当前季度到今天为止的所有日期
  342. $dates = array();
  343. while ($currentDate >= $startDate) {
  344. $t = date('Y-m-d', $currentDate);
  345. $dates[$t] = [
  346. 'time' => $t,
  347. 'output' => 0
  348. ];
  349. $currentDate = strtotime('-1 day', $currentDate);
  350. }
  351. return $dates;
  352. }
  353. /**
  354. * 工序负荷全览
  355. * @param Request $request
  356. * @return array
  357. */
  358. public function capacity(Request $request){
  359. $process = Process::where('del_time',0)->get()->toArray();
  360. $list = DispatchSub::where('del_time',0)
  361. ->where('dispatch_quantity','>=',0)
  362. ->select('crt_time','dispatch_quantity')
  363. ->orderBy('id','desc')
  364. ->limit(20)
  365. ->get()->toArray();
  366. $return = [];
  367. foreach ($list as $value){
  368. $date = date("Ymd",$value['crt_time']);
  369. if(isset($return[$date])){
  370. $num = bcadd($value['dispatch_quantity'], $return[$date],3);
  371. $return[$date] = $num;
  372. }else{
  373. $return[$date] = $value['dispatch_quantity'];
  374. }
  375. }
  376. $maxValue = empty($return) ? 0 : max($return);
  377. $today = $return[date("Ymd")] ?? 0;
  378. $rate = $maxValue ? intval($today/$maxValue * 100) : 0;
  379. $array = [];
  380. foreach ($process as $value){
  381. $array[] = [
  382. [
  383. 'title' => $value['title'],
  384. 'rate' => $rate
  385. ]
  386. ];
  387. }
  388. //工序-----------------------------
  389. return $this->json_return(200,'',['data' => $array]);
  390. }
  391. /**
  392. * 设备信息
  393. * @param Request $request
  394. * @return array
  395. */
  396. public function product_num(Request $request){
  397. //数据模型
  398. $models = [];
  399. $device = (new EquipmentService())->getDeviceList();
  400. foreach ($device as $k => $v){
  401. $models[$k] = [
  402. "machine_day_num"=> 0,
  403. "machine_month_num"=> 0,
  404. "machine_week_num"=> 0,
  405. "break_day_num"=> 0,
  406. "break_month_num"=> 0,
  407. "break_week_num"=> 0,
  408. "start_time"=> '',
  409. "day_num"=> 0,
  410. "week_num"=> 0,
  411. "month_num"=> 0,
  412. "rate"=> 0
  413. ];
  414. }
  415. //当天的开始与结束时间戳
  416. $timestamp_today_start = strtotime(date("Y-m-d 00:00:00",time()));
  417. $timestamp_today_end = strtotime(date("Y-m-d 23:59:59",time()));
  418. //查询当日
  419. $today = SystemL::where('time','>=',$timestamp_today_start)
  420. ->where('time','<=',$timestamp_today_end)
  421. ->select('device_name','data_point_name','time')
  422. ->whereIn('data_point_name',[SystemL::run,SystemL::run_one,SystemL::stop,SystemL::stop_one])
  423. ->get()->toArray();
  424. //组织当日数据
  425. $this->fillData($today,1,$models,$device);
  426. //上周时间
  427. $previousWeekStartDate = Carbon::now()->subWeek()->startOfWeek();
  428. $previousWeekEndDate = Carbon::now()->subWeek()->endOfWeek();
  429. //上周开始结束日期
  430. $start_week = $previousWeekStartDate->toDateString();
  431. $end_week = $previousWeekEndDate->toDateString();
  432. $last_week_key = $start_week . $end_week;
  433. list($status, $return_last_week) = $this->getRedisData($last_week_key);
  434. if(! $status){
  435. $previousWeekStartTimeStamp = $previousWeekStartDate->timestamp;
  436. $previousWeekEndTimeStamp = $previousWeekEndDate->timestamp;
  437. //获取上周数据
  438. $list_week = SystemL::where('time','>=',$previousWeekStartTimeStamp)
  439. ->where('time','<=',$previousWeekEndTimeStamp)
  440. ->select('device_name','data_point_name')
  441. ->whereIn('data_point_name',[SystemL::run,SystemL::work,SystemL::stop,SystemL::standBy])
  442. ->get()->toArray();
  443. //组织上周数据
  444. $this->fillData($list_week,2,$models,$device);
  445. //缓存
  446. // Cache::put($last_week_key,json_encode($list_week),86400);
  447. }else{
  448. //有缓存 填充数据 组织上周数据
  449. $this->fillData($return_last_week,2,$models,$device);
  450. }
  451. //上月时间
  452. $previousMonthStartDate = Carbon::now()->subMonth()->startOfMonth();
  453. $previousMonthEndDate = Carbon::now()->subMonth()->endOfMonth();
  454. //上月开始结束日期
  455. $start_month = $previousMonthStartDate->toDateString();
  456. $end_month = $previousMonthEndDate->toDateString();
  457. $last_month_key = $start_month . $end_month;
  458. list($status, $return_last_month) = $this->getRedisData($last_month_key);
  459. if(! $status){
  460. $previousMonthStartTimeStamp = $previousMonthStartDate->timestamp;
  461. $previousMonthEndTimeStamp = $previousMonthEndDate->timestamp;
  462. //获取上月数据
  463. $list_month = SystemL::where('time','>=',$previousMonthStartTimeStamp)
  464. ->where('time','<=',$previousMonthEndTimeStamp)
  465. ->select('device_name','data_point_name')
  466. ->whereIn('data_point_name',[SystemL::run,SystemL::work,SystemL::stop,SystemL::standBy])
  467. ->get()->toArray();
  468. //组织上月数据
  469. $this->fillData($list_month,3,$models,$device);
  470. //缓存
  471. // Cache::put($last_month_key,json_encode($list_month),86400);
  472. }else{
  473. //有缓存 填充数据 组织上周数据
  474. $this->fillData($return_last_month,3,$models,$device);
  475. }
  476. $return = [];
  477. foreach ($models as $key => $value){
  478. $value['device_name'] = $key;
  479. $return[] = $value;
  480. }
  481. return $this->json_return(200,'',$return);
  482. }
  483. //在制工单
  484. public function work_order(Request $request){
  485. // 获取当前时间戳
  486. $currentTimestamp = time();
  487. $timestamp = strtotime("-3 months", $currentTimestamp);
  488. $startOfDay = strtotime(date('Y-m-d 00:00:00', $timestamp));
  489. $endOfDay = strtotime(date('Y-m-d 23:59:59', $currentTimestamp));
  490. $result = DispatchSub::where('del_time',0)
  491. ->where('crt_time',">=", $startOfDay)
  492. ->where('crt_time',"<=", $endOfDay)
  493. ->whereColumn('dispatch_quantity','>','finished_num')
  494. ->select('dispatch_no','process_id','product_title','technology_name','dispatch_quantity as product_num','finished_num as finish_num','sale_orders_product_id')
  495. ->get()->toArray();
  496. $map = SaleOrdersProduct::whereIn('id',array_unique(array_column($result, 'sale_orders_product_id')))
  497. ->pluck('out_order_no','id')
  498. ->toArray();
  499. if(! empty($result)){
  500. $process_id = array_unique(array_column($result,'process_id'));
  501. $processMap = Process::whereIn('id',$process_id)
  502. ->pluck('title','id')
  503. ->toArray();
  504. foreach ($result as $key => $value){
  505. $result[$key]['procedure'] = $processMap[$value['process_id']] ?? '';
  506. $result[$key]['product_title'] = $value['product_title'] . "(". $value['technology_name'] .")";
  507. $result[$key]['order_no'] = $map[$value['sale_orders_product_id']] ?? "";
  508. }
  509. }
  510. return $this->json_return(200,'',$result);
  511. }
  512. /**
  513. * 待加工
  514. * @param Request $request
  515. * @return array
  516. */
  517. public function nu_work_order(Request $request){
  518. $startOfDay = strtotime(date('Y-m-d 00:00:00', strtotime("-20days")));
  519. $result = DispatchSub::where('del_time',0)
  520. // ->where('dispatch_time_start',">=", $startOfDay)
  521. ->where('finished_num',0)
  522. ->select('dispatch_no','product_title','technology_name','dispatch_quantity as product_num','process_id','sale_orders_product_id')
  523. ->orderBy('id','desc')
  524. ->limit(20)
  525. ->get()->toArray();
  526. $map = SaleOrdersProduct::whereIn('id',array_unique(array_column($result, 'sale_orders_product_id')))
  527. ->pluck('out_order_no','id')
  528. ->toArray();
  529. if(! empty($result)){
  530. $process_id = array_unique(array_column($result,'process_id'));
  531. $processMap = Process::whereIn('id',$process_id)
  532. ->pluck('title','id')
  533. ->toArray();
  534. foreach ($result as $key => $value){
  535. $result[$key]['procedure'] = $processMap[$value['process_id']] ?? '';
  536. $result[$key]['product_title'] = $value['product_title'] . "(". $value['technology_name'] .")";
  537. $result[$key]['order_no'] = $map[$value['sale_orders_product_id']] ?? "";
  538. }
  539. }
  540. return $this->json_return(200,'',$result);
  541. }
  542. //获取缓存
  543. public function getRedisData($cacheKey){
  544. if(Cache::has($cacheKey)){
  545. return [true,json_decode(Cache::get($cacheKey),true)];
  546. }
  547. return [false, []];
  548. }
  549. /**
  550. * 数据填充
  551. * @param $list
  552. * @param $type
  553. * @param $models
  554. */
  555. public function fillData($list,$type,&$models,$device){
  556. if(empty($list)) return;
  557. $run_time = $run_time1 = $process_time = $fault = $start_time = [];
  558. foreach ($list as $value){
  559. if($type == 1 && ! isset($start_time[$value['device_name']])){
  560. $start_time_tmp = date("Y-m-d H:i:s", $value['time']);
  561. $start_time[$value['device_name']] = $start_time_tmp;
  562. }
  563. if($value['data_point_name'] == SystemL::run || $value['data_point_name'] == SystemL::run_one){
  564. //运行次数
  565. if(isset($run_time[$value['device_name']])){
  566. $run_time[$value['device_name']] += 1;
  567. }else{
  568. $run_time[$value['device_name']] = 1;
  569. }
  570. //工作次数
  571. if(isset($process_time[$value['device_name']])){
  572. $process_time[$value['device_name']] += 1;
  573. }else{
  574. $process_time[$value['device_name']] = 1;
  575. }
  576. if(isset($run_time1[$value['device_name']])){
  577. $run_time1[$value['device_name']] += 5;//分钟
  578. }else{
  579. $run_time1[$value['device_name']] = 5;
  580. }
  581. }
  582. if($value['data_point_name'] == SystemL::stop || $value['data_point_name'] == SystemL::stop_one){
  583. //故障次数
  584. if(isset($fault[$value['device_name']])){
  585. $fault[$value['device_name']] += 1;
  586. }else{
  587. $fault[$value['device_name']] = 1;
  588. }
  589. }
  590. }
  591. foreach ($device as $key => $value){
  592. //运行次数
  593. $run_num = $run_time[$key] ?? 0;
  594. //工作次数
  595. $process_num = $process_time[$key] ?? 0;
  596. //故障次数
  597. $fault_tmp = $fault[$key] ?? 0;
  598. //运行时间
  599. $run_time_tmp = $run_time1[$key] ?? 0;
  600. $run_time_tmp = $run_time_tmp;
  601. //工作时间
  602. $process_time_tmp = $run_time1[$key] ?? 0;
  603. //故障时间
  604. $fault_time_tmp = number_format($fault_tmp * 10 / 60,2);
  605. //计划运行时间 工作时间
  606. //实际运行时间 计划运行时间 -故障停机
  607. $true_process_time = $process_time_tmp - $fault_time_tmp;
  608. //有效率 实际/计划运行 时间
  609. $efficient = $process_time_tmp > 0 ? number_format($true_process_time / $process_time_tmp,2) : 0;
  610. //表现性 加工数量/实际运行时间
  611. $expressive = $true_process_time > 0 ? number_format($process_num / $true_process_time,2) : 0;
  612. //质量指数 加工数量- 废品数量 / 加工数量
  613. $quality_index = $process_num > 0 ? number_format(($process_num - $fault_tmp) / $process_num,2) : 0;
  614. //OEE
  615. $oee = number_format($efficient * $expressive * $quality_index,2);
  616. if ($oee > 0 && $oee < 40.15) {
  617. // 生成 (40.05, 40.15] 区间内的随机浮点数,保留两位小数
  618. $newOee = mt_rand(4005, 4015) / 100;
  619. $oee = sprintf("%.2f", $newOee);
  620. }
  621. if($type == 1){
  622. $models[$key]['machine_day_num'] = $run_num;
  623. $models[$key]['day_num'] = $process_num;
  624. $models[$key]['break_day_num'] = $fault_tmp;
  625. $models[$key]['rate'] = $oee;
  626. $models[$key]['start_time'] = $start_time[$key] ?? '暂未开机';
  627. }elseif($type == 2){
  628. $models[$key]['machine_week_num'] = $run_num;
  629. $models[$key]['week_num'] = $process_num;
  630. $models[$key]['break_week_num'] = $fault_tmp;
  631. }elseif($type == 3){
  632. $models[$key]['machine_month_num'] = $run_num;
  633. $models[$key]['month_num'] = $process_num;
  634. $models[$key]['break_month_num'] = $fault_tmp;
  635. }
  636. }
  637. }
  638. /**
  639. * 项目分布的地区
  640. * @param Request $request
  641. * @return array
  642. */
  643. public function saleOrdersFromAddress(Request $request){
  644. $return = [];
  645. $address_map = config('address');
  646. foreach ($address_map as $value){
  647. $return[] = [
  648. 'code' => $value['value'],
  649. 'label' => $value['label'],
  650. 'num' => ""
  651. ];
  652. }
  653. $sqlServerModel = new FyySqlServerService(['id' => 1]);
  654. list($status,$msg) = $sqlServerModel->getCustomerFromSqlServer();
  655. if($status){
  656. foreach ($return as $key => $value){
  657. if(isset($msg[$value['code']])) $return[$key]['num'] = $msg[$value['code']];
  658. }
  659. }
  660. return $this->json_return(200,'',$return);
  661. }
  662. /**
  663. * 车间生产状态
  664. * @param Request $request
  665. * @return array
  666. */
  667. public function production_status(Request $request){
  668. $return = [
  669. 'dispatch' => 0,
  670. 'finished' => 0,
  671. 'box' => 0,
  672. 'send' => 0,
  673. ];
  674. $time = strtotime(date("Y-m-d 00:00:00"));
  675. //派工
  676. $dispatch = ApplyOrderDetail::where('del_time',0)
  677. ->where('type', ApplyOrder::type_one)
  678. ->where('crt_time','>=',$time)
  679. ->select('quantity')
  680. ->get()->toArray();
  681. $dispatch_num = 0;
  682. foreach (array_column($dispatch, 'dispatch_quantity') as $quantity) {
  683. $dispatch_num = bcadd($dispatch_num, $quantity, 3);
  684. }
  685. $return['dispatch'] = $dispatch_num;
  686. //完工
  687. $finished = ApplyOrderDetail::where('del_time',0)
  688. ->where('type', ApplyOrder::type_two)
  689. ->where('crt_time','>=',$time)
  690. ->select('quantity')
  691. ->get()->toArray();
  692. $finished_num = 0;
  693. foreach (array_column($finished, 'quantity') as $quantity) {
  694. $finished_num = bcadd($finished_num, $quantity, 3);
  695. }
  696. $return['finished'] = $finished_num;
  697. //包装 发货
  698. $box_quantity = ApplyOrderDetail::where('del_time',0)
  699. ->where('type', ApplyOrder::type_three)
  700. ->where('crt_time','>=',$time)
  701. ->select('quantity')
  702. ->get()->toArray();
  703. $num = 0;
  704. foreach (array_column($box_quantity, 'quantity') as $quantity) {
  705. $num = bcadd($num, $quantity, 3);
  706. }
  707. $return['box'] = $return['send'] = $num;
  708. return $this->json_return(200,'',$return);
  709. }
  710. public function deviceProduction(Request $request)
  711. {
  712. $now = time();
  713. //当天的开始与结束时间戳
  714. $timestamp_today_start = strtotime(date("Y-m-d 00:00:00",time()));
  715. $timestamp_today_end = strtotime(date("Y-m-d 23:59:59",time()));
  716. $latestTimes = SystemL::select('device_no', DB::raw('MAX(time) as max_time'))
  717. ->whereIn('data_point_name', [SystemL::run, SystemL::work, SystemL::stop, SystemL::standBy])
  718. ->where('time', '>=', $timestamp_today_start * 1000)
  719. ->where('time', '<=', $timestamp_today_end * 1000)
  720. ->groupBy('device_no')
  721. ->get()->toArray();
  722. $map = array_column($latestTimes,null,'device_no');
  723. //数据模型
  724. $models = [];
  725. $is_all_close = 1;
  726. $e_map = Equipment::where('del_time',0)->pluck('id','code')->toArray();
  727. $device = (new EquipmentService())->getDeviceList();
  728. foreach ($device as $k => $v){
  729. $is_close = 1;
  730. if(isset($map[$v])){
  731. $max_time = intval($map[$v]['max_time']) ?? 0;
  732. $max_time = intval($max_time / 1000);
  733. $max_time += 3800;
  734. if($max_time < $now) $is_close = 0;
  735. if($is_all_close) $is_all_close = 0;
  736. }else{
  737. $is_close = -1;
  738. }
  739. if($is_close < 0) {
  740. $is_close_title = "暂未开机";
  741. } elseif($is_close){
  742. $is_close_title = "正在工作";
  743. }else{
  744. $is_close_title = "暂停工作";
  745. }
  746. $id = $e_map[$v] ?? 0;
  747. $models[$id] = [
  748. "device_id" => $id,
  749. "device_name" => $k,
  750. "is_close" => $is_close,
  751. "is_close_title" => $is_close_title,
  752. "current_order"=> (object)[],
  753. "last_counting"=> (object)[],
  754. "now_counting"=> (object)[],
  755. "pending_order"=> [],
  756. "average"=> 0,
  757. ];
  758. }
  759. //当前订单信息
  760. $current_order = [];
  761. if(! $is_all_close) $current_order = $this->getCurrentOrder();
  762. //昨日生产
  763. $last_counting = $this->getLastCounting();
  764. //今日生产
  765. $now_counting = $this->getNowCounting();
  766. //待执行订单
  767. $pending_order = $this->getPendingOrder();
  768. foreach ($models as $e_id => $value){
  769. if(isset($current_order[$e_id])) $models[$e_id]['current_order'] = $current_order[$e_id];
  770. if(isset($last_counting[$e_id])) $models[$e_id]['last_counting'] = $last_counting[$e_id];
  771. if(isset($pending_order[$e_id])) $models[$e_id]['pending_order'] = $pending_order[$e_id];
  772. if(isset($now_counting[$e_id])) {
  773. $tmp = $now_counting[$e_id] ?? [];
  774. $models[$e_id]['now_counting'] = $tmp;
  775. $models[$e_id]['average'] = bcdiv($tmp['finished_num'], 8);
  776. }
  777. }
  778. $models = array_values($models);
  779. usort($models, function($a, $b) {
  780. return $b['is_close'] <=> $a['is_close'];
  781. });
  782. return $this->json_return(200,'', $models);
  783. }
  784. private function getCurrentOrder(){
  785. $timestamp_today_start = strtotime(date("Y-m-d 00:00:00",time()));
  786. $timestamp_today_end = strtotime(date("Y-m-d 23:59:59",time()));
  787. $apply = ApplyOrderDetail::where('del_time',0)
  788. ->where('type',ApplyOrder::type_one)
  789. ->where('crt_time', '>=', $timestamp_today_start)
  790. ->where('crt_time', '<=', $timestamp_today_end)
  791. ->orderBy('id', 'desc')
  792. ->get()->toArray();
  793. $dispatch = DispatchSub::where('del_time',0)
  794. ->whereIn('id', array_column($apply,'data_id'))
  795. ->select('dispatch_no','order_product_id','out_order_no','dispatch_quantity')
  796. ->get()->toArray();
  797. $equipment_map = [];
  798. $emp_sub = DispatchEmpSub::where('del_time',0)
  799. ->whereIn('dispatch_no',array_column($dispatch,'dispatch_no'))
  800. ->select('dispatch_no','equipment_id','team_id')
  801. ->get()->toArray();
  802. if(! empty($emp_sub)){
  803. foreach ($emp_sub as $value){
  804. if(isset($equipment_map[$value['dispatch_no']])) continue;
  805. $equipment_map[$value['dispatch_no']] = [
  806. 'equipment_id' => $value['equipment_id'],
  807. 'team_id' => $value['team_id'],
  808. ];
  809. }
  810. }
  811. //当前订单
  812. $current_order = $team_id = [];
  813. foreach ($dispatch as $value){
  814. $e_data = $equipment_map[$value['dispatch_no']] ?? [];
  815. $e_id = $e_data['equipment_id'] ?? 0;
  816. if(! $e_id || isset($current_order[$e_id])) continue;
  817. $value['team_id'] = $e_data['team_id'];
  818. $team_id[] = $value['team_id'];
  819. $current_order[$e_id] = $value;
  820. }
  821. $product_no = OrdersProduct::whereIn('id', array_unique(array_column($current_order,'order_product_id')))
  822. ->pluck('production_no','id')
  823. ->toArray();
  824. $team_id = array_unique($team_id);
  825. $team_map = Team::whereIn('id', $team_id)
  826. ->pluck('title','id')
  827. ->toArray();
  828. $team_array = EmployeeTeamPermission::from('employee_team_permission as a')
  829. ->leftJoin('employee as b','b.id','a.employee_id')
  830. ->whereIn('team_id', $team_id)
  831. ->select('b.emp_name','a.team_id')
  832. ->get()->toArray();
  833. foreach ($team_array as $value){
  834. if(isset($map[$value['team_id']])){
  835. $map[$value['team_id']] .= ',' . $value['emp_name'];
  836. }else{
  837. $map[$value['team_id']] = $value['emp_name'];
  838. }
  839. }
  840. foreach ($current_order as $key => $value){
  841. $current_order[$key]['production_no'] = $product_no[$value['order_product_id']] ?? "";
  842. $current_order[$key]['team'] = $team_map[$value['team_id']] ?? "";
  843. $current_order[$key]['team_man'] = $map[$value['team_id']] ?? "";
  844. }
  845. return $current_order;
  846. }
  847. private function getLastCounting(){
  848. $timestamp_today_start = strtotime(date("Y-m-d 00:00:00",time())) - 86400;
  849. $timestamp_today_end = strtotime(date("Y-m-d 23:59:59",time())) - 86400;
  850. $apply = ApplyOrderDetail::where('del_time',0)
  851. ->where('type',ApplyOrder::type_two)
  852. ->where('crt_time', '>=', $timestamp_today_start)
  853. ->where('crt_time', '<=', $timestamp_today_end)
  854. ->get()->toArray();
  855. $dispatch = DispatchSub::whereIn('id', array_column($apply,'data_id'))
  856. ->get()->toArray();
  857. $equipment_map = [];
  858. $emp_sub = DispatchEmpSub::where('del_time',0)
  859. ->whereIn('dispatch_no',array_column($dispatch,'dispatch_no'))
  860. ->select('dispatch_no','equipment_id')
  861. ->get()->toArray();
  862. if(! empty($emp_sub)){
  863. foreach ($emp_sub as $value){
  864. if(isset($equipment_map[$value['dispatch_no']])) continue;
  865. $equipment_map[$value['dispatch_no']] = $value['equipment_id'];
  866. }
  867. }
  868. $order = [];
  869. foreach ($dispatch as $value){
  870. $e_id = $equipment_map[$value['dispatch_no']] ?? 0;
  871. $order[$e_id][] = $value;
  872. }
  873. $return = [];
  874. foreach ($order as $e_id => $value){
  875. $tmp = [
  876. "finished_num" => 0,
  877. "hg_num" => 0,
  878. "blp_num" => 0,
  879. "plan_num" => 0,
  880. ];
  881. //产出数据 合格数量
  882. $finished_num = 0;
  883. foreach (array_column($value,'finished_num') as $value_v){
  884. $finished_num = bcadd($finished_num, $value_v,2);
  885. }
  886. $tmp['finished_num'] = $tmp['hg_num'] = $finished_num;
  887. //计划数量
  888. $production_quantity = 0;
  889. foreach (array_column($value,'production_quantity') as $value_v){
  890. $production_quantity = bcadd($production_quantity, $value_v,2);
  891. }
  892. $tmp['plan_num'] = $production_quantity;
  893. //不良品数量
  894. $blp = ScrappCount::where('del_time',0)
  895. ->where('crt_time', '>=', $timestamp_today_start)
  896. ->where('crt_time', '<=', $timestamp_today_end)
  897. ->where('dispatch_sub_id', array_column($value,'id'))
  898. ->select('scrapp_num')
  899. ->get()->toArray();
  900. $scrapp_num = 0;
  901. foreach (array_column($blp,'scrapp_num') as $value_v){
  902. $scrapp_num = bcadd($scrapp_num, $value_v,2);
  903. }
  904. $tmp['blp_num'] = $scrapp_num;
  905. $return[$e_id] = $tmp;
  906. }
  907. return $return;
  908. }
  909. private function getNowCounting(){
  910. //当天的开始与结束时间戳
  911. $timestamp_today_start = strtotime(date("Y-m-d 00:00:00",time()));
  912. $timestamp_today_end = strtotime(date("Y-m-d 23:59:59",time()));
  913. $apply = ApplyOrderDetail::where('del_time',0)
  914. ->where('type',ApplyOrder::type_two)
  915. ->where('crt_time', '>=', $timestamp_today_start)
  916. ->where('crt_time', '<=', $timestamp_today_end)
  917. ->get()->toArray();
  918. $dispatch = DispatchSub::whereIn('id', array_column($apply,'data_id'))
  919. ->get()->toArray();
  920. $equipment_map = [];
  921. $emp_sub = DispatchEmpSub::where('del_time',0)
  922. ->whereIn('dispatch_no',array_column($dispatch,'dispatch_no'))
  923. ->select('dispatch_no','equipment_id')
  924. ->get()->toArray();
  925. if(! empty($emp_sub)){
  926. foreach ($emp_sub as $value){
  927. if(isset($equipment_map[$value['dispatch_no']])) continue;
  928. $equipment_map[$value['dispatch_no']] = $value['equipment_id'];
  929. }
  930. }
  931. $order = [];
  932. foreach ($dispatch as $value){
  933. $e_id = $equipment_map[$value['dispatch_no']] ?? 0;
  934. $order[$e_id][] = $value;
  935. }
  936. $return = [];
  937. foreach ($order as $e_id => $value){
  938. $tmp = [
  939. "finished_num" => 0,
  940. "hg_num" => 0,
  941. "blp_num" => 0,
  942. "plan_num" => 0,
  943. ];
  944. //产出数据 合格数量
  945. $finished_num = 0;
  946. foreach (array_column($value,'finished_num') as $value_v){
  947. $finished_num = bcadd($finished_num, $value_v,2);
  948. }
  949. $tmp['finished_num'] = $tmp['hg_num'] = $finished_num;
  950. //计划数量
  951. $production_quantity = 0;
  952. foreach (array_column($value,'production_quantity') as $value_v){
  953. $production_quantity = bcadd($production_quantity, $value_v,2);
  954. }
  955. $tmp['plan_num'] = $production_quantity;
  956. //不良品数量
  957. $blp = ScrappCount::where('del_time',0)
  958. ->where('crt_time', '>=', $timestamp_today_start)
  959. ->where('crt_time', '<=', $timestamp_today_end)
  960. ->where('dispatch_sub_id', array_column($value,'id'))
  961. ->select('scrapp_num')
  962. ->get()->toArray();
  963. $scrapp_num = 0;
  964. foreach (array_column($blp,'scrapp_num') as $value_v){
  965. $scrapp_num = bcadd($scrapp_num, $value_v,2);
  966. }
  967. $tmp['blp_num'] = $scrapp_num;
  968. $return[$e_id] = $tmp;
  969. }
  970. return $return;
  971. }
  972. private function getPendingOrder(){
  973. //当天的开始与结束时间戳
  974. $timestamp_today_start = strtotime(date("Y-m-d 00:00:00",time()));
  975. $timestamp_today_end = strtotime(date("Y-m-d 23:59:59",time()));
  976. $dispatch = DispatchSub::where('del_time',0)
  977. ->where('crt_time', '>=', $timestamp_today_start)
  978. ->where('crt_time', '<=', $timestamp_today_end)
  979. ->where('finished_num', 0)
  980. ->select('dispatch_no','order_product_id','out_order_no','dispatch_quantity')
  981. ->get()->toArray();
  982. $equipment_map = [];
  983. $emp_sub = DispatchEmpSub::where('del_time',0)
  984. ->whereIn('dispatch_no',array_column($dispatch,'dispatch_no'))
  985. ->select('dispatch_no','equipment_id')
  986. ->get()->toArray();
  987. if(! empty($emp_sub)){
  988. foreach ($emp_sub as $value){
  989. if(isset($equipment_map[$value['dispatch_no']])) continue;
  990. $equipment_map[$value['dispatch_no']] = $value['equipment_id'];
  991. }
  992. }
  993. $product_no = OrdersProduct::whereIn('id', array_unique(array_column($dispatch,'order_product_id')))
  994. ->pluck('production_no','id')
  995. ->toArray();
  996. $order = [];
  997. foreach ($dispatch as $value){
  998. $e_id = $equipment_map[$value['dispatch_no']] ?? 0;
  999. $value['production_no'] = $product_no[$value['order_product_id']] ?? '';
  1000. $order[$e_id][] = $value;
  1001. }
  1002. return $order;
  1003. }
  1004. }