ScreenController.php 41 KB

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