ScreenController.php 40 KB

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