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