ScreenController.php 43 KB

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