ScreenController.php 40 KB

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