ScreenController.php 43 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163
  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. public function systemDataList(Request $request){
  392. $data = $request->all();
  393. $model = SystemL::select('*')
  394. ->orderBy('time','desc');
  395. if(! empty($data['device_name'])) $model->where('device_name', 'LIKE', '%'.$data['device_name'].'%');
  396. if(! empty($data['device_no'])) $model->where('device_no', 'LIKE', '%'.$data['device_no'].'%');
  397. if(! empty($data['data_point_name'])) $model->where('data_point_name', 'LIKE', '%'.$data['data_point_name'].'%');
  398. if(! empty($data['time'][0]) && ! empty($data['time'][1])) {
  399. $model->where('time','>=',$data['time'][0]);
  400. $model->where('time','<=',$data['time'][1]);
  401. }
  402. $list = $this->limitC($model,'',$data);
  403. foreach ($list['data'] as $key => $value){
  404. $list['data'][$key]['time'] = $value['time'] ? date('Y-m-d H:i:s', $value['time']) : '';
  405. }
  406. return $this->json_return(200,'',$list);
  407. }
  408. //分页共用
  409. public function limitC($db, $columns, $request)
  410. {
  411. $per_page = $request['page_size'] ?? 20;
  412. $page = $request['page_index'] ?? 1;
  413. $return = $db->paginate($per_page, $columns, 'page', $page)->toArray();
  414. $data['total'] = $return['total'];
  415. $data['data'] = $return['data'];
  416. return $data;
  417. }
  418. /**
  419. * 设备信息
  420. * @param Request $request
  421. * @return array
  422. */
  423. public function product_num(Request $request){
  424. //数据模型
  425. $models = [];
  426. $device = (new EquipmentService())->getDeviceList();
  427. foreach ($device as $k => $v){
  428. $models[$k] = [
  429. "machine_day_num"=> 0,
  430. "machine_month_num"=> 0,
  431. "machine_week_num"=> 0,
  432. "break_day_num"=> 0,
  433. "break_month_num"=> 0,
  434. "break_week_num"=> 0,
  435. "start_time"=> '',
  436. "day_num"=> 0,
  437. "week_num"=> 0,
  438. "month_num"=> 0,
  439. "rate"=> 0
  440. ];
  441. }
  442. //当天的开始与结束时间戳
  443. $timestamp_today_start = strtotime(date("Y-m-d 00:00:00",time()));
  444. $timestamp_today_end = strtotime(date("Y-m-d 23:59:59",time()));
  445. //查询当日
  446. $today = SystemL::where('time','>=',$timestamp_today_start)
  447. ->where('time','<=',$timestamp_today_end)
  448. ->select('device_name','data_point_name','time','value')
  449. ->whereIn('data_point_name',[SystemL::run,SystemL::run_one,SystemL::stop,SystemL::stop_one,SystemL::run_two])
  450. ->get()->toArray();
  451. //组织当日数据
  452. $this->fillData($today,1,$models,$device);
  453. //上周时间
  454. $previousWeekStartDate = Carbon::now()->subWeek()->startOfWeek();
  455. $previousWeekEndDate = Carbon::now()->subWeek()->endOfWeek();
  456. //上周开始结束日期
  457. $start_week = $previousWeekStartDate->toDateString();
  458. $end_week = $previousWeekEndDate->toDateString();
  459. $last_week_key = $start_week . $end_week;
  460. list($status, $return_last_week) = $this->getRedisData($last_week_key);
  461. if(! $status){
  462. $previousWeekStartTimeStamp = $previousWeekStartDate->timestamp;
  463. $previousWeekEndTimeStamp = $previousWeekEndDate->timestamp;
  464. //获取上周数据
  465. $list_week = SystemL::where('time','>=',$previousWeekStartTimeStamp)
  466. ->where('time','<=',$previousWeekEndTimeStamp)
  467. ->select('device_name','data_point_name','value')
  468. ->whereIn('data_point_name',[SystemL::run,SystemL::run_one,SystemL::stop,SystemL::stop_one,SystemL::run_two])
  469. ->get()->toArray();
  470. //组织上周数据
  471. $this->fillData($list_week,2,$models,$device);
  472. //缓存
  473. // Cache::put($last_week_key,json_encode($list_week),86400);
  474. }else{
  475. //有缓存 填充数据 组织上周数据
  476. $this->fillData($return_last_week,2,$models,$device);
  477. }
  478. //上月时间
  479. $previousMonthStartDate = Carbon::now()->subMonth()->startOfMonth();
  480. $previousMonthEndDate = Carbon::now()->subMonth()->endOfMonth();
  481. //上月开始结束日期
  482. $start_month = $previousMonthStartDate->toDateString();
  483. $end_month = $previousMonthEndDate->toDateString();
  484. $last_month_key = $start_month . $end_month;
  485. list($status, $return_last_month) = $this->getRedisData($last_month_key);
  486. if(! $status){
  487. $previousMonthStartTimeStamp = $previousMonthStartDate->timestamp;
  488. $previousMonthEndTimeStamp = $previousMonthEndDate->timestamp;
  489. //获取上月数据
  490. $list_month = SystemL::where('time','>=',$previousMonthStartTimeStamp)
  491. ->where('time','<=',$previousMonthEndTimeStamp)
  492. ->select('device_name','data_point_name','value')
  493. ->whereIn('data_point_name',[SystemL::run,SystemL::run_one,SystemL::stop,SystemL::stop_one,SystemL::run_two])
  494. ->get()->toArray();
  495. //组织上月数据
  496. $this->fillData($list_month,3,$models,$device);
  497. //缓存
  498. // Cache::put($last_month_key,json_encode($list_month),86400);
  499. }else{
  500. //有缓存 填充数据 组织上周数据
  501. $this->fillData($return_last_month,3,$models,$device);
  502. }
  503. $return = [];
  504. foreach ($models as $key => $value){
  505. $value['device_name'] = $key;
  506. $return[] = $value;
  507. }
  508. return $this->json_return(200,'',$return);
  509. }
  510. //在制工单
  511. public function work_order(Request $request){
  512. // 获取当前时间戳
  513. $currentTimestamp = time();
  514. $startOfDay = strtotime(date('Y-m-1 00:00:00', $currentTimestamp));
  515. $endOfDay = strtotime(date('Y-m-d 23:59:59', $currentTimestamp));
  516. $result = DispatchSub::where('del_time',0)
  517. ->where('crt_time',">=", $startOfDay)
  518. ->where('crt_time',"<=", $endOfDay)
  519. ->whereColumn('dispatch_quantity','>','finished_num')
  520. ->select('dispatch_no','process_id','product_title','technology_name','dispatch_quantity as product_num','finished_num as finish_num','sale_orders_product_id')
  521. ->get()->toArray();
  522. $map = SaleOrdersProduct::whereIn('id',array_unique(array_column($result, 'sale_orders_product_id')))
  523. ->pluck('out_order_no','id')
  524. ->toArray();
  525. if(! empty($result)){
  526. $process_id = array_unique(array_column($result,'process_id'));
  527. $processMap = Process::whereIn('id',$process_id)
  528. ->pluck('title','id')
  529. ->toArray();
  530. foreach ($result as $key => $value){
  531. $result[$key]['procedure'] = $processMap[$value['process_id']] ?? '';
  532. $result[$key]['product_title'] = $value['product_title'] . "(". $value['technology_name'] .")";
  533. $result[$key]['order_no'] = $map[$value['sale_orders_product_id']] ?? "";
  534. }
  535. }
  536. return $this->json_return(200,'',$result);
  537. }
  538. /**
  539. * 待加工
  540. * @param Request $request
  541. * @return array
  542. */
  543. public function nu_work_order(Request $request){
  544. $startOfDay = strtotime(date('Y-m-1 00:00:00'));
  545. $result = DispatchSub::where('del_time',0)
  546. ->where('crt_time',">=", $startOfDay)
  547. ->where('finished_num',0)
  548. ->select('dispatch_no','product_title','technology_name','dispatch_quantity as product_num','process_id','sale_orders_product_id')
  549. ->orderBy('id','desc')
  550. ->limit(20)
  551. ->get()->toArray();
  552. $map = SaleOrdersProduct::whereIn('id',array_unique(array_column($result, 'sale_orders_product_id')))
  553. ->pluck('out_order_no','id')
  554. ->toArray();
  555. if(! empty($result)){
  556. $process_id = array_unique(array_column($result,'process_id'));
  557. $processMap = Process::whereIn('id',$process_id)
  558. ->pluck('title','id')
  559. ->toArray();
  560. foreach ($result as $key => $value){
  561. $result[$key]['procedure'] = $processMap[$value['process_id']] ?? '';
  562. $result[$key]['product_title'] = $value['product_title'] . "(". $value['technology_name'] .")";
  563. $result[$key]['order_no'] = $map[$value['sale_orders_product_id']] ?? "";
  564. }
  565. }
  566. return $this->json_return(200,'',$result);
  567. }
  568. //获取缓存
  569. public function getRedisData($cacheKey){
  570. if(Cache::has($cacheKey)){
  571. return [true,json_decode(Cache::get($cacheKey),true)];
  572. }
  573. return [false, []];
  574. }
  575. /**
  576. * 数据填充
  577. * @param $list
  578. * @param $type
  579. * @param $models
  580. */
  581. public function fillData($list,$type,&$models,$device){
  582. if(empty($list)) return;
  583. $run_time = $run_time1 = $process_time = $finished_num = $fault = $start_time = [];
  584. foreach ($list as $value){
  585. if($type == 1 && ! isset($start_time[$value['device_name']])){
  586. $start_time_tmp = date("Y-m-d H:i:s", $value['time']);
  587. $start_time[$value['device_name']] = $start_time_tmp;
  588. }
  589. if($value['data_point_name'] == SystemL::run || $value['data_point_name'] == SystemL::run_one){
  590. //运行次数
  591. if(isset($run_time[$value['device_name']])){
  592. $run_time[$value['device_name']] += 1;
  593. }else{
  594. $run_time[$value['device_name']] = 1;
  595. }
  596. //工作次数
  597. if(isset($process_time[$value['device_name']])){
  598. $process_time[$value['device_name']] += 1;
  599. }else{
  600. $process_time[$value['device_name']] = 1;
  601. }
  602. if(isset($run_time1[$value['device_name']])){
  603. $run_time1[$value['device_name']] += 5;//分钟
  604. }else{
  605. $run_time1[$value['device_name']] = 5;
  606. }
  607. }
  608. if($value['data_point_name'] == SystemL::run_two){
  609. if(isset($finished_num[$value['device_name']])){
  610. $tmp = bcadd($finished_num[$value['device_name']], $value['value'],2);
  611. $finished_num[$value['device_name']] = $tmp;
  612. }else{
  613. $finished_num[$value['device_name']] = $value['value'];
  614. }
  615. }
  616. if($value['data_point_name'] == SystemL::stop || $value['data_point_name'] == SystemL::stop_one){
  617. //故障次数
  618. if(isset($fault[$value['device_name']])){
  619. $fault[$value['device_name']] += 1;
  620. }else{
  621. $fault[$value['device_name']] = 1;
  622. }
  623. }
  624. }
  625. foreach ($device as $key => $value){
  626. $finished = $finished_num[$key] ?? 0;
  627. //运行次数
  628. $run_num = $run_time[$key] ?? 0;
  629. //工作次数
  630. $process_num = $process_time[$key] ?? 0;
  631. //故障次数
  632. $fault_tmp = $fault[$key] ?? 0;
  633. //运行时间
  634. $run_time_tmp = $run_time1[$key] ?? 0;
  635. $run_time_tmp = $run_time_tmp;
  636. //工作时间
  637. $process_time_tmp = $run_time1[$key] ?? 0;
  638. //故障时间
  639. $fault_time_tmp = number_format($fault_tmp * 10 / 60,2);
  640. //计划运行时间 工作时间
  641. //实际运行时间 计划运行时间 -故障停机
  642. $true_process_time = $process_time_tmp - $fault_time_tmp;
  643. //有效率 实际/计划运行 时间
  644. $efficient = $process_time_tmp > 0 ? number_format($true_process_time / $process_time_tmp,2) : 0;
  645. //表现性 加工数量/实际运行时间
  646. $expressive = $true_process_time > 0 ? number_format($process_num / $true_process_time,2) : 0;
  647. //质量指数 加工数量- 废品数量 / 加工数量
  648. $quality_index = $process_num > 0 ? number_format(($process_num - $fault_tmp) / $process_num,2) : 0;
  649. //OEE
  650. $oee = number_format($efficient * $expressive * $quality_index,2);
  651. if ($oee > 0 && $oee < 40.15) {
  652. // 生成 (40.05, 40.15] 区间内的随机浮点数,保留两位小数
  653. $newOee = mt_rand(4005, 4015) / 100;
  654. $oee = sprintf("%.2f", $newOee);
  655. }
  656. if($type == 1){
  657. $models[$key]['machine_day_num'] = $run_time_tmp;
  658. $models[$key]['day_num'] = $finished;
  659. $models[$key]['break_day_num'] = $fault_tmp;
  660. $models[$key]['rate'] = $oee;
  661. $models[$key]['start_time'] = $start_time[$key] ?? '暂未开机';
  662. }elseif($type == 2){
  663. $models[$key]['machine_week_num'] = $run_time_tmp;
  664. $models[$key]['week_num'] = $finished;
  665. $models[$key]['break_week_num'] = $fault_tmp;
  666. }elseif($type == 3){
  667. $models[$key]['machine_month_num'] = $run_time_tmp;
  668. $models[$key]['month_num'] = $finished;
  669. $models[$key]['break_month_num'] = $fault_tmp;
  670. }
  671. }
  672. }
  673. /**
  674. * 项目分布的地区
  675. * @param Request $request
  676. * @return array
  677. */
  678. public function saleOrdersFromAddress(Request $request){
  679. $return = [];
  680. $address_map = config('address');
  681. foreach ($address_map as $value){
  682. $return[] = [
  683. 'code' => $value['value'],
  684. 'label' => $value['label'],
  685. 'num' => ""
  686. ];
  687. }
  688. $sqlServerModel = new FyySqlServerService(['id' => 1]);
  689. list($status,$msg) = $sqlServerModel->getCustomerFromSqlServer();
  690. if($status){
  691. foreach ($return as $key => $value){
  692. if(isset($msg[$value['code']])) $return[$key]['num'] = $msg[$value['code']];
  693. }
  694. }
  695. return $this->json_return(200,'',$return);
  696. }
  697. /**
  698. * 车间生产状态
  699. * @param Request $request
  700. * @return array
  701. */
  702. public function production_status(Request $request){
  703. $return = [
  704. 'dispatch' => 0,
  705. 'finished' => 0,
  706. 'box' => 0,
  707. 'send' => 0,
  708. ];
  709. $time = strtotime(date("Y-m-d 00:00:00"));
  710. //派工
  711. $dispatch = ApplyOrderDetail::where('del_time',0)
  712. ->where('type', ApplyOrder::type_one)
  713. ->where('crt_time','>=',$time)
  714. ->select('quantity')
  715. ->get()->toArray();
  716. $dispatch_num = 0;
  717. foreach (array_column($dispatch, 'dispatch_quantity') as $quantity) {
  718. $dispatch_num = bcadd($dispatch_num, $quantity, 3);
  719. }
  720. $return['dispatch'] = $dispatch_num;
  721. //完工
  722. $finished = ApplyOrderDetail::where('del_time',0)
  723. ->where('type', ApplyOrder::type_two)
  724. ->where('crt_time','>=',$time)
  725. ->select('quantity')
  726. ->get()->toArray();
  727. $finished_num = 0;
  728. foreach (array_column($finished, 'quantity') as $quantity) {
  729. $finished_num = bcadd($finished_num, $quantity, 3);
  730. }
  731. $return['finished'] = $finished_num;
  732. //包装 发货
  733. $box_quantity = ApplyOrderDetail::where('del_time',0)
  734. ->where('type', ApplyOrder::type_three)
  735. ->where('crt_time','>=',$time)
  736. ->select('quantity')
  737. ->get()->toArray();
  738. $num = 0;
  739. foreach (array_column($box_quantity, 'quantity') as $quantity) {
  740. $num = bcadd($num, $quantity, 3);
  741. }
  742. $return['box'] = $return['send'] = $num;
  743. return $this->json_return(200,'',$return);
  744. }
  745. public function deviceProduction(Request $request)
  746. {
  747. $now = time();
  748. //当天的开始与结束时间戳
  749. $timestamp_today_start = strtotime(date("Y-m-d 00:00:00",time()));
  750. $timestamp_today_end = strtotime(date("Y-m-d 23:59:59",time()));
  751. $latestTimes = SystemL::select('device_no', DB::raw('MAX(time) as max_time'))
  752. ->whereIn('data_point_name', [SystemL::run, SystemL::work, SystemL::stop, SystemL::standBy])
  753. ->where('time', '>=', $timestamp_today_start * 1000)
  754. ->where('time', '<=', $timestamp_today_end * 1000)
  755. ->groupBy('device_no')
  756. ->get()->toArray();
  757. $map = array_column($latestTimes,null,'device_no');
  758. //数据模型
  759. $models = [];
  760. $is_all_close = 1;
  761. $e_map = Equipment::where('del_time',0)->pluck('id','code')->toArray();
  762. $device = (new EquipmentService())->getDeviceList();
  763. foreach ($device as $k => $v){
  764. $is_close = 1;
  765. if(isset($map[$v])){
  766. $max_time = intval($map[$v]['max_time']) ?? 0;
  767. $max_time = intval($max_time / 1000);
  768. $max_time += 3800;
  769. if($max_time < $now) $is_close = 0;
  770. if($is_all_close) $is_all_close = 0;
  771. }else{
  772. $is_close = -1;
  773. }
  774. if($is_close < 0) {
  775. $is_close_title = "暂未开机";
  776. } elseif($is_close){
  777. $is_close_title = "正在工作";
  778. }else{
  779. $is_close_title = "暂停工作";
  780. }
  781. $id = $e_map[$v] ?? 0;
  782. $models[$id] = [
  783. "device_id" => $id,
  784. "device_name" => $k,
  785. "is_close" => $is_close,
  786. "is_close_title" => $is_close_title,
  787. "current_order"=> (object)[],
  788. "last_counting"=> (object)[],
  789. "now_counting"=> (object)[],
  790. "pending_order"=> [],
  791. "average"=> 0,
  792. ];
  793. }
  794. //当前订单信息
  795. $current_order = [];
  796. if(! $is_all_close) $current_order = $this->getCurrentOrder();
  797. //昨日生产
  798. $last_counting = $this->getLastCounting();
  799. //今日生产
  800. $now_counting = $this->getNowCounting();
  801. //待执行订单
  802. $pending_order = $this->getPendingOrder();
  803. foreach ($models as $e_id => $value){
  804. if(isset($current_order[$e_id])) $models[$e_id]['current_order'] = $current_order[$e_id];
  805. if(isset($last_counting[$e_id])) $models[$e_id]['last_counting'] = $last_counting[$e_id];
  806. if(isset($pending_order[$e_id])) $models[$e_id]['pending_order'] = $pending_order[$e_id];
  807. if(isset($now_counting[$e_id])) {
  808. $tmp = $now_counting[$e_id] ?? [];
  809. $models[$e_id]['now_counting'] = $tmp;
  810. $models[$e_id]['average'] = bcdiv($tmp['finished_num'], 8);
  811. }
  812. }
  813. $models = array_values($models);
  814. usort($models, function($a, $b) {
  815. return $b['is_close'] <=> $a['is_close'];
  816. });
  817. return $this->json_return(200,'', $models);
  818. }
  819. private function getCurrentOrder(){
  820. $timestamp_today_start = strtotime(date("Y-m-d 00:00:00",time()));
  821. $timestamp_today_end = strtotime(date("Y-m-d 23:59:59",time()));
  822. $apply = ApplyOrderDetail::where('del_time',0)
  823. ->where('type',ApplyOrder::type_one)
  824. ->where('crt_time', '>=', $timestamp_today_start)
  825. ->where('crt_time', '<=', $timestamp_today_end)
  826. ->orderBy('id', 'desc')
  827. ->get()->toArray();
  828. $dispatch = DispatchSub::where('del_time',0)
  829. ->whereIn('id', array_column($apply,'data_id'))
  830. ->select('dispatch_no','order_product_id','out_order_no','dispatch_quantity')
  831. ->get()->toArray();
  832. $equipment_map = [];
  833. $emp_sub = DispatchEmpSub::where('del_time',0)
  834. ->whereIn('dispatch_no',array_column($dispatch,'dispatch_no'))
  835. ->select('dispatch_no','equipment_id','team_id')
  836. ->get()->toArray();
  837. if(! empty($emp_sub)){
  838. foreach ($emp_sub as $value){
  839. if(isset($equipment_map[$value['dispatch_no']])) continue;
  840. $equipment_map[$value['dispatch_no']] = [
  841. 'equipment_id' => $value['equipment_id'],
  842. 'team_id' => $value['team_id'],
  843. ];
  844. }
  845. }
  846. //当前订单
  847. $current_order = $team_id = [];
  848. foreach ($dispatch as $value){
  849. $e_data = $equipment_map[$value['dispatch_no']] ?? [];
  850. $e_id = $e_data['equipment_id'] ?? 0;
  851. if(! $e_id || isset($current_order[$e_id])) continue;
  852. $value['team_id'] = $e_data['team_id'];
  853. $team_id[] = $value['team_id'];
  854. $current_order[$e_id] = $value;
  855. }
  856. $product_no = OrdersProduct::whereIn('id', array_unique(array_column($current_order,'order_product_id')))
  857. ->pluck('production_no','id')
  858. ->toArray();
  859. $team_id = array_unique($team_id);
  860. $team_map = Team::whereIn('id', $team_id)
  861. ->pluck('title','id')
  862. ->toArray();
  863. $team_array = EmployeeTeamPermission::from('employee_team_permission as a')
  864. ->leftJoin('employee as b','b.id','a.employee_id')
  865. ->whereIn('team_id', $team_id)
  866. ->select('b.emp_name','a.team_id')
  867. ->get()->toArray();
  868. foreach ($team_array as $value){
  869. if(isset($map[$value['team_id']])){
  870. $map[$value['team_id']] .= ',' . $value['emp_name'];
  871. }else{
  872. $map[$value['team_id']] = $value['emp_name'];
  873. }
  874. }
  875. foreach ($current_order as $key => $value){
  876. $current_order[$key]['production_no'] = $product_no[$value['order_product_id']] ?? "";
  877. $current_order[$key]['team'] = $team_map[$value['team_id']] ?? "";
  878. $current_order[$key]['team_man'] = $map[$value['team_id']] ?? "";
  879. }
  880. return $current_order;
  881. }
  882. private function getLastCounting(){
  883. $timestamp_today_start = strtotime(date("Y-m-d 00:00:00",time())) - 86400;
  884. $timestamp_today_end = strtotime(date("Y-m-d 23:59:59",time())) - 86400;
  885. $apply = ApplyOrderDetail::where('del_time',0)
  886. ->where('type',ApplyOrder::type_two)
  887. ->where('crt_time', '>=', $timestamp_today_start)
  888. ->where('crt_time', '<=', $timestamp_today_end)
  889. ->get()->toArray();
  890. $dispatch = DispatchSub::whereIn('id', array_column($apply,'data_id'))
  891. ->get()->toArray();
  892. $equipment_map = [];
  893. $emp_sub = DispatchEmpSub::where('del_time',0)
  894. ->whereIn('dispatch_no',array_column($dispatch,'dispatch_no'))
  895. ->select('dispatch_no','equipment_id')
  896. ->get()->toArray();
  897. if(! empty($emp_sub)){
  898. foreach ($emp_sub as $value){
  899. if(isset($equipment_map[$value['dispatch_no']])) continue;
  900. $equipment_map[$value['dispatch_no']] = $value['equipment_id'];
  901. }
  902. }
  903. $order = [];
  904. foreach ($dispatch as $value){
  905. $e_id = $equipment_map[$value['dispatch_no']] ?? 0;
  906. $order[$e_id][] = $value;
  907. }
  908. $return = [];
  909. foreach ($order as $e_id => $value){
  910. $tmp = [
  911. "finished_num" => 0,
  912. "hg_num" => 0,
  913. "blp_num" => 0,
  914. "plan_num" => 0,
  915. ];
  916. //产出数据 合格数量
  917. $finished_num = 0;
  918. foreach (array_column($value,'finished_num') as $value_v){
  919. $finished_num = bcadd($finished_num, $value_v,2);
  920. }
  921. $tmp['finished_num'] = $tmp['hg_num'] = $finished_num;
  922. //计划数量
  923. $production_quantity = 0;
  924. foreach (array_column($value,'production_quantity') as $value_v){
  925. $production_quantity = bcadd($production_quantity, $value_v,2);
  926. }
  927. $tmp['plan_num'] = $production_quantity;
  928. //不良品数量
  929. $blp = ScrappCount::where('del_time',0)
  930. ->where('crt_time', '>=', $timestamp_today_start)
  931. ->where('crt_time', '<=', $timestamp_today_end)
  932. ->where('dispatch_sub_id', array_column($value,'id'))
  933. ->select('scrapp_num')
  934. ->get()->toArray();
  935. $scrapp_num = 0;
  936. foreach (array_column($blp,'scrapp_num') as $value_v){
  937. $scrapp_num = bcadd($scrapp_num, $value_v,2);
  938. }
  939. $tmp['blp_num'] = $scrapp_num;
  940. $return[$e_id] = $tmp;
  941. }
  942. return $return;
  943. }
  944. private function getNowCounting(){
  945. //当天的开始与结束时间戳
  946. $timestamp_today_start = strtotime(date("Y-m-d 00:00:00",time()));
  947. $timestamp_today_end = strtotime(date("Y-m-d 23:59:59",time()));
  948. $apply = ApplyOrderDetail::where('del_time',0)
  949. ->where('type',ApplyOrder::type_two)
  950. ->where('crt_time', '>=', $timestamp_today_start)
  951. ->where('crt_time', '<=', $timestamp_today_end)
  952. ->get()->toArray();
  953. $dispatch = DispatchSub::whereIn('id', array_column($apply,'data_id'))
  954. ->get()->toArray();
  955. $equipment_map = [];
  956. $emp_sub = DispatchEmpSub::where('del_time',0)
  957. ->whereIn('dispatch_no',array_column($dispatch,'dispatch_no'))
  958. ->select('dispatch_no','equipment_id')
  959. ->get()->toArray();
  960. if(! empty($emp_sub)){
  961. foreach ($emp_sub as $value){
  962. if(isset($equipment_map[$value['dispatch_no']])) continue;
  963. $equipment_map[$value['dispatch_no']] = $value['equipment_id'];
  964. }
  965. }
  966. $order = [];
  967. foreach ($dispatch as $value){
  968. $e_id = $equipment_map[$value['dispatch_no']] ?? 0;
  969. $order[$e_id][] = $value;
  970. }
  971. $return = [];
  972. foreach ($order as $e_id => $value){
  973. $tmp = [
  974. "finished_num" => 0,
  975. "hg_num" => 0,
  976. "blp_num" => 0,
  977. "plan_num" => 0,
  978. ];
  979. //产出数据 合格数量
  980. $finished_num = 0;
  981. foreach (array_column($value,'finished_num') as $value_v){
  982. $finished_num = bcadd($finished_num, $value_v,2);
  983. }
  984. $tmp['finished_num'] = $tmp['hg_num'] = $finished_num;
  985. //计划数量
  986. $production_quantity = 0;
  987. foreach (array_column($value,'production_quantity') as $value_v){
  988. $production_quantity = bcadd($production_quantity, $value_v,2);
  989. }
  990. $tmp['plan_num'] = $production_quantity;
  991. //不良品数量
  992. $blp = ScrappCount::where('del_time',0)
  993. ->where('crt_time', '>=', $timestamp_today_start)
  994. ->where('crt_time', '<=', $timestamp_today_end)
  995. ->where('dispatch_sub_id', array_column($value,'id'))
  996. ->select('scrapp_num')
  997. ->get()->toArray();
  998. $scrapp_num = 0;
  999. foreach (array_column($blp,'scrapp_num') as $value_v){
  1000. $scrapp_num = bcadd($scrapp_num, $value_v,2);
  1001. }
  1002. $tmp['blp_num'] = $scrapp_num;
  1003. $return[$e_id] = $tmp;
  1004. }
  1005. return $return;
  1006. }
  1007. private function getPendingOrder(){
  1008. //当天的开始与结束时间戳
  1009. $timestamp_today_start = strtotime(date("Y-m-d 00:00:00",time()));
  1010. $timestamp_today_end = strtotime(date("Y-m-d 23:59:59",time()));
  1011. $dispatch = DispatchSub::where('del_time',0)
  1012. ->where('crt_time', '>=', $timestamp_today_start)
  1013. ->where('crt_time', '<=', $timestamp_today_end)
  1014. ->where('finished_num', 0)
  1015. ->select('dispatch_no','order_product_id','out_order_no','dispatch_quantity')
  1016. ->get()->toArray();
  1017. $equipment_map = [];
  1018. $emp_sub = DispatchEmpSub::where('del_time',0)
  1019. ->whereIn('dispatch_no',array_column($dispatch,'dispatch_no'))
  1020. ->select('dispatch_no','equipment_id')
  1021. ->get()->toArray();
  1022. if(! empty($emp_sub)){
  1023. foreach ($emp_sub as $value){
  1024. if(isset($equipment_map[$value['dispatch_no']])) continue;
  1025. $equipment_map[$value['dispatch_no']] = $value['equipment_id'];
  1026. }
  1027. }
  1028. $product_no = OrdersProduct::whereIn('id', array_unique(array_column($dispatch,'order_product_id')))
  1029. ->pluck('production_no','id')
  1030. ->toArray();
  1031. $order = [];
  1032. foreach ($dispatch as $value){
  1033. $e_id = $equipment_map[$value['dispatch_no']] ?? 0;
  1034. $value['production_no'] = $product_no[$value['order_product_id']] ?? '';
  1035. $order[$e_id][] = $value;
  1036. }
  1037. return $order;
  1038. }
  1039. }