ScreenController.php 43 KB

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