ScreenController.php 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745
  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\DispatchSub;
  8. use App\Model\OrdersProductProcess;
  9. use App\Model\Process;
  10. use App\Model\SaleOrdersProduct;
  11. use App\Model\SalesFrom;
  12. use App\Model\ScrappCount;
  13. use App\Model\SystemL;
  14. use App\Service\FyySqlServerService;
  15. use App\Service\ReportFormsService;
  16. use Carbon\Carbon;
  17. use Illuminate\Http\Request;
  18. use Illuminate\Support\Facades\Cache;
  19. use Illuminate\Support\Facades\DB;
  20. /**
  21. * 大屏数据展示
  22. * Class ScreenController
  23. * @package App\Http\Controllers\Api
  24. */
  25. class ScreenController extends BaseController
  26. {
  27. /**
  28. * 产值数据全览
  29. * @param Request $request
  30. * @return array
  31. */
  32. public function output_value(Request $request){
  33. $currentYear = Carbon::now()->year;
  34. $lastYear = $currentYear - 1;
  35. $currentMonth = Carbon::now()->month;
  36. $totalValueAllTime = SaleOrdersProduct::where('del_time',0)
  37. ->sum('finished_num');
  38. $totalValueAllTime = $totalValueAllTime . "(吨)";
  39. $totalValueLastYear = SaleOrdersProduct::where('del_time',0)
  40. ->whereRaw("YEAR(FROM_UNIXTIME(crt_time)) = $lastYear")
  41. ->sum('finished_num');
  42. $totalValueLastYear = $totalValueLastYear . "(吨)";
  43. $totalValueCurrentYearMonth = SaleOrdersProduct::where('del_time',0)
  44. ->whereRaw("YEAR(FROM_UNIXTIME(crt_time)) = $currentYear AND MONTH(FROM_UNIXTIME(crt_time)) = $currentMonth")
  45. ->sum('finished_num');
  46. $totalValueCurrentYearMonth = $totalValueCurrentYearMonth . "(吨)";
  47. return $this->json_return(200,'',['total_time'=>$totalValueAllTime, 'total_last_year'=>$totalValueLastYear, 'total_current_month'=>$totalValueCurrentYearMonth]);
  48. }
  49. /**
  50. * 项目进度
  51. * @param Request $request
  52. * @return array
  53. */
  54. public function order_process1(Request $request) {
  55. $result = SaleOrdersProduct::where('del_time',0)
  56. ->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')
  57. ->groupBy('out_order_no')
  58. ->orderBy('id', 'desc') // 添加这一行以按创建时间降序排序
  59. ->take(200) // 添加这一行以限制结果集大小为100
  60. ->get()->toArray();
  61. if(! empty($result)){
  62. foreach ($result as $key => $value){
  63. $rate = bcmul(bcdiv($value['finished_num'], $value['total'],2),100,2);
  64. if($rate > 100) $rate = "100.00";
  65. $result[$key]['rate'] = $rate;
  66. if($result[$key]['rate'] >= 100){
  67. $result[$key]['is_over'] = '否';
  68. }else{
  69. $t = $value['pre_shipment_time'];
  70. if(time() > $t) $result[$key]['is_over'] = '是';
  71. else $result[$key]['is_over'] = '否';
  72. }
  73. unset($result[$key]['total']);
  74. unset($result[$key]['finished_num']);
  75. }
  76. $rate = array_column($result, 'rate'); // 提取列作为排序依据
  77. array_multisort($rate, SORT_DESC,$result);
  78. }
  79. return $this->json_return(200,'',$result);
  80. }
  81. /**
  82. * 历史项目 在手项目
  83. * @param Request $request
  84. * @return array
  85. */
  86. public function project_region(Request $request){
  87. $all = SaleOrdersProduct::where('del_time',0)
  88. ->whereColumn('order_quantity','=','finished_num')
  89. ->select('out_order_no')
  90. ->groupBy('out_order_no')
  91. ->get()
  92. ->toArray();
  93. $all = array_column($all,'out_order_no');
  94. $not_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. $not_all = array_column($not_all,'out_order_no');
  101. $all = array_diff($all, $not_all);
  102. $arr = [
  103. "all_num" => count($all) + 8000,
  104. "num" => count($not_all),
  105. ];
  106. return $this->json_return(200,'',$arr);
  107. }
  108. /**
  109. * 本月质量
  110. * @param Request $request
  111. * @return array
  112. */
  113. public function output_value_month1(Request $request){
  114. $date = date('Ymd',time());
  115. $startDate = strtotime(date('Y-m-01 00:00:00', time())) * 1000;
  116. $endDate = strtotime(date('Y-m-t 23:59:59', time())) * 1000;
  117. $return = $this->getDayInMonth();
  118. //获取数据
  119. $data = ScrappCount::where('crt_time','>=',$startDate)
  120. ->where('crt_time','<',$endDate)
  121. ->where('del_time',0)
  122. ->select('crt_time as time','scrapp_num as value')
  123. ->get()->toArray();
  124. if(! empty($data)){
  125. foreach ($data as $value){
  126. $crt_time = date('Y-m-d',$value['time']);
  127. if(isset($return[$crt_time])){
  128. $return[$crt_time]['num'] += 1;
  129. }
  130. }
  131. ksort($return);
  132. }
  133. $return = array_values($return);
  134. return $this->json_return(200,'',$return);
  135. }
  136. function getDayInMonth(){
  137. $startDate = strtotime(date('Y-m-01 00:00:00', time()));
  138. // 获取当前日期
  139. $currentDate = time();
  140. $dates = array();
  141. while ($currentDate >= $startDate) {
  142. $t = date('Y-m-d', $currentDate);
  143. $dates[$t] = [
  144. 'value' => $t,
  145. 'num' => 0
  146. ];
  147. $currentDate = strtotime('-1 day', $currentDate);
  148. }
  149. return $dates;
  150. }
  151. /**
  152. * 本月质量
  153. * @param Request $request
  154. * @return array
  155. */
  156. public function output_value_month11(Request $request){
  157. $date = date('Ymd',time());
  158. $startDate = strtotime(date('Y-m-01 00:00:00', time()));
  159. $endDate = strtotime(date('Y-m-t 23:59:59', time()));
  160. //工序-----------------------------
  161. $model = new OrdersProductProcess(['channel' => $date]);//当前季度的数据
  162. $data = $model->where('del_time',0)
  163. ->where('status',4)
  164. ->select('crt_time')
  165. ->where('crt_time','>=',$startDate)
  166. ->where('crt_time','<=',$endDate)
  167. ->get()->toArray();
  168. $return = [];
  169. if(! empty($data)){
  170. foreach ($data as $value){
  171. $crt_time = date('Y-m-d',$value['crt_time']);
  172. if(isset($return[$crt_time])){
  173. $return[$crt_time]['num'] += 1;
  174. }else{
  175. $return[$crt_time] = [
  176. 'num' => 1,
  177. 'value' => $crt_time
  178. ];
  179. }
  180. }
  181. }
  182. $return = array_values($return);
  183. return $this->json_return(200,'',$return);
  184. }
  185. /**
  186. * 产量趋势图
  187. * @param Request $request
  188. * @return array
  189. */
  190. public function output_value_efficiency1(Request $request){
  191. // 获取当前时间戳
  192. $currentTimestamp = time();
  193. // 输出过去两周的起止时间(包括当前日期)
  194. $timestamp = strtotime("-13 days", $currentTimestamp);
  195. $startOfDay = strtotime(date('Y-m-d 00:00:00', $timestamp));
  196. $endOfDay = strtotime(date('Y-m-d 23:59:59', $currentTimestamp));
  197. $box = Box::where('del_time',0)
  198. ->where('crt_time','>=',$startOfDay)
  199. ->where('crt_time','<=',$endOfDay)
  200. ->select('crt_time','top_order_no','order_no')
  201. ->orderBy('crt_time','desc')
  202. ->get()->toArray();
  203. $result = [];
  204. if(! empty($box)){
  205. foreach ($box as $value){
  206. $model = new BoxDetail(['channel' => $value['top_order_no']]);
  207. $map = $model->where('del_time',0)
  208. ->where('order_no',$value['order_no'])
  209. ->select('order_no',DB::raw('sum(num) as num'))
  210. ->groupBy('order_no')
  211. ->pluck('num','order_no')
  212. ->toArray();
  213. $output = $map[$value['order_no']] ?? 0;
  214. $times = date('Y-m-d',$value['crt_time']);
  215. if(isset($result[$times])){
  216. $result[$times]['output'] += $output;
  217. }else{
  218. $result[$times] = [
  219. 'time' => $times,
  220. 'output' => $output
  221. ];
  222. }
  223. }
  224. }
  225. $result = array_values($result);
  226. return $this->json_return(200,'',$result);
  227. }
  228. //产量趋势图(完工)
  229. public function output_value_efficiency11(Request $request){
  230. $list = DispatchSub::where('del_time',0)
  231. ->where('finished_num','>=',0)
  232. ->select('crt_time','finished_num')
  233. ->orderBy('id','desc')
  234. ->limit(20)
  235. ->get()->toArray();
  236. $return = [];
  237. foreach ($list as $value){
  238. $date = date("Y-m-d",$value['crt_time']);
  239. if(isset($return[$date])){
  240. $num = bcadd($value['finished_num'], $return[$date]['output'],3);
  241. $return[$date]['output'] = $num;
  242. }else{
  243. $return[$date] = [
  244. 'output' => $value['finished_num'],
  245. 'time' => $date,
  246. ];
  247. }
  248. }
  249. ksort($return);
  250. $result = array_values($return);
  251. return $this->json_return(200,'',$result);
  252. }
  253. //产量趋势图
  254. public function output_value_efficiency(Request $request){
  255. $time = strtotime(date("Y-06-01 00:00:00"));
  256. $return = [];
  257. $data = ApplyOrderDetail::where('del_time',0)
  258. ->where('type',ApplyOrder::type_two)
  259. ->where('crt_time','>=',$time)
  260. ->select('quantity','crt_time','data_id')
  261. ->get()->toArray();
  262. $dispatch = DispatchSub::whereIn('id',array_column($data,'data_id'))
  263. ->pluck("production_quantity",'id')
  264. ->toArray();
  265. $plan = [];
  266. foreach ($data as $value) {
  267. $date = date("Y-m-d", $value['crt_time']);
  268. if (isset($return[$date])) {
  269. $quantity = bcadd($return[$date], $value['quantity'], 3);
  270. $return[$date] = $quantity;
  271. } else {
  272. $return[$date] = $value['quantity'];
  273. }
  274. $n = $dispatch[$value['data_id']] ?? 0;
  275. if (isset($plan[$date])) {
  276. $quantity = bcadd($plan[$date], $n, 3);
  277. $plan[$date] = $quantity;
  278. } else {
  279. $plan[$date] = $n;
  280. }
  281. }
  282. ksort($return);
  283. $result = [];
  284. foreach ($return as $key => $value){
  285. $tmp = [
  286. 'time' => $key,
  287. 'output' => $value,
  288. 'plan' => $plan[$key] ?? 0,
  289. ];
  290. $result[] = $tmp;
  291. }
  292. return $this->json_return(200,'',$result);
  293. }
  294. function getDay(){
  295. // 获取当前季度的开始日期
  296. $month = date('m');
  297. if ($month >= 1 && $month <= 3) {
  298. $quarter = 1;
  299. } elseif ($month >= 4 && $month <= 6) {
  300. $quarter = 2;
  301. } elseif ($month >= 7 && $month <= 9) {
  302. $quarter = 3;
  303. } else {
  304. $quarter = 4;
  305. }
  306. $year = date('Y'); // 获取当前年份
  307. if ($quarter == 1) {
  308. $startDate = strtotime("$year-01-01"); // 第一季度的开始日期
  309. } elseif ($quarter == 2) {
  310. $startDate = strtotime("$year-04-01"); // 第二季度的开始日期
  311. } elseif ($quarter == 3) {
  312. $startDate = strtotime("$year-07-01"); // 第三季度的开始日期
  313. } else {
  314. $startDate = strtotime("$year-10-01"); // 第四季度的开始日期
  315. }
  316. // 获取当前日期
  317. $currentDate = time();
  318. // 生成当前季度到今天为止的所有日期
  319. $dates = array();
  320. while ($currentDate >= $startDate) {
  321. $t = date('Y-m-d', $currentDate);
  322. $dates[$t] = [
  323. 'time' => $t,
  324. 'output' => 0
  325. ];
  326. $currentDate = strtotime('-1 day', $currentDate);
  327. }
  328. return $dates;
  329. }
  330. /**
  331. * 工序负荷全览
  332. * @param Request $request
  333. * @return array
  334. */
  335. public function capacity(Request $request){
  336. $process = Process::where('del_time',0)->get()->toArray();
  337. $list = DispatchSub::where('del_time',0)
  338. ->where('dispatch_quantity','>=',0)
  339. ->select('crt_time','dispatch_quantity')
  340. ->orderBy('id','desc')
  341. ->limit(20)
  342. ->get()->toArray();
  343. $return = [];
  344. foreach ($list as $value){
  345. $date = date("Ymd",$value['crt_time']);
  346. if(isset($return[$date])){
  347. $num = bcadd($value['dispatch_quantity'], $return[$date],3);
  348. $return[$date] = $num;
  349. }else{
  350. $return[$date] = $value['dispatch_quantity'];
  351. }
  352. }
  353. $maxValue = empty($return) ? 0 : max($return);
  354. $today = $return[date("Ymd")] ?? 0;
  355. $rate = $maxValue ? intval($today/$maxValue * 100) : 0;
  356. $array = [];
  357. foreach ($process as $value){
  358. $array[] = [
  359. [
  360. 'title' => $value['title'],
  361. 'rate' => $rate
  362. ]
  363. ];
  364. }
  365. //工序-----------------------------
  366. return $this->json_return(200,'',['data' => $array]);
  367. }
  368. /**
  369. * 设备信息
  370. * @param Request $request
  371. * @return array
  372. */
  373. public function product_num(Request $request){
  374. //数据模型
  375. $models = [];
  376. foreach (SystemL::$device as $k => $v){
  377. $models[$k] = [
  378. "machine_day_num"=> 0,
  379. "machine_month_num"=> 0,
  380. "machine_week_num"=> 0,
  381. "break_day_num"=> 0,
  382. "break_month_num"=> 0,
  383. "break_week_num"=> 0,
  384. "start_time"=> '',
  385. "day_num"=> 0,
  386. "week_num"=> 0,
  387. "month_num"=> 0,
  388. "rate"=> 0
  389. ];
  390. }
  391. //当天的开始与结束时间戳
  392. $timestamp_today_start = strtotime(date("Y-m-d 00:00:00",time()));
  393. $timestamp_today_end = strtotime(date("Y-m-d 23:59:59",time()));
  394. //查询当日
  395. $today = SystemL::where('time','>=',$timestamp_today_start * 1000)
  396. ->where('time','<=',$timestamp_today_end * 1000)
  397. ->select('device_name','data_point_name','time')
  398. ->whereIn('data_point_name',[SystemL::run,SystemL::work,SystemL::stop,SystemL::standBy])
  399. ->get()->toArray();
  400. //组织当日数据
  401. $this->fillData($today,1,$models);
  402. //上周时间
  403. $previousWeekStartDate = Carbon::now()->subWeek()->startOfWeek();
  404. $previousWeekEndDate = Carbon::now()->subWeek()->endOfWeek();
  405. //上周开始结束日期
  406. $start_week = $previousWeekStartDate->toDateString();
  407. $end_week = $previousWeekEndDate->toDateString();
  408. $last_week_key = $start_week . $end_week;
  409. list($status, $return_last_week) = $this->getRedisData($last_week_key);
  410. if(! $status){
  411. $previousWeekStartTimeStamp = $previousWeekStartDate->timestamp * 1000;
  412. $previousWeekEndTimeStamp = $previousWeekEndDate->timestamp * 1000;
  413. //获取上周数据
  414. $list_week = SystemL::where('time','>=',$previousWeekStartTimeStamp)
  415. ->where('time','<=',$previousWeekEndTimeStamp)
  416. ->select('device_name','data_point_name')
  417. ->whereIn('data_point_name',[SystemL::run,SystemL::work,SystemL::stop,SystemL::standBy])
  418. ->get()->toArray();
  419. //组织上周数据
  420. $this->fillData($list_week,2,$models);
  421. //缓存
  422. // Cache::put($last_week_key,json_encode($list_week),86400);
  423. }else{
  424. //有缓存 填充数据 组织上周数据
  425. $this->fillData($return_last_week,2,$models);
  426. }
  427. //上月时间
  428. $previousMonthStartDate = Carbon::now()->subMonth()->startOfMonth();
  429. $previousMonthEndDate = Carbon::now()->subMonth()->endOfMonth();
  430. //上月开始结束日期
  431. $start_month = $previousMonthStartDate->toDateString();
  432. $end_month = $previousMonthEndDate->toDateString();
  433. $last_month_key = $start_month . $end_month;
  434. list($status, $return_last_month) = $this->getRedisData($last_month_key);
  435. if(! $status){
  436. $previousMonthStartTimeStamp = $previousMonthStartDate->timestamp * 1000;
  437. $previousMonthEndTimeStamp = $previousMonthEndDate->timestamp * 1000;
  438. //获取上月数据
  439. $list_month = SystemL::where('time','>=',$previousMonthStartTimeStamp)
  440. ->where('time','<=',$previousMonthEndTimeStamp)
  441. ->select('device_name','data_point_name')
  442. ->whereIn('data_point_name',[SystemL::run,SystemL::work,SystemL::stop,SystemL::standBy])
  443. ->get()->toArray();
  444. //组织上月数据
  445. $this->fillData($list_month,3,$models);
  446. //缓存
  447. // Cache::put($last_month_key,json_encode($list_month),86400);
  448. }else{
  449. //有缓存 填充数据 组织上周数据
  450. $this->fillData($return_last_month,3,$models);
  451. }
  452. $return = [];
  453. foreach ($models as $key => $value){
  454. $value['device_name'] = $key;
  455. $return[] = $value;
  456. }
  457. return $this->json_return(200,'',$return);
  458. }
  459. //在制工单
  460. public function work_order(Request $request){
  461. // 获取当前时间戳
  462. $currentTimestamp = time();
  463. $timestamp = strtotime("-3 months", $currentTimestamp);
  464. $startOfDay = strtotime(date('Y-m-d 00:00:00', $timestamp));
  465. $endOfDay = strtotime(date('Y-m-d 23:59:59', $currentTimestamp));
  466. $result = DispatchSub::where('del_time',0)
  467. ->where('crt_time',">=", $startOfDay)
  468. ->where('crt_time',"<=", $endOfDay)
  469. ->whereColumn('dispatch_quantity','>','finished_num')
  470. ->select('dispatch_no as order_no','process_id','product_title','technology_name','dispatch_quantity as product_num','finished_num as finish_num')
  471. ->get()->toArray();
  472. if(! empty($result)){
  473. $process_id = array_unique(array_column($result,'process_id'));
  474. $processMap = Process::whereIn('id',$process_id)
  475. ->pluck('title','id')
  476. ->toArray();
  477. foreach ($result as $key => $value){
  478. $result[$key]['procedure'] = $processMap[$value['process_id']] ?? '';
  479. $result[$key]['product_title'] = $value['product_title'] . "(". $value['technology_name'] .")";
  480. }
  481. }
  482. return $this->json_return(200,'',$result);
  483. }
  484. /**
  485. * 待加工
  486. * @param Request $request
  487. * @return array
  488. */
  489. public function nu_work_order(Request $request){
  490. $startOfDay = strtotime(date('Y-m-d 00:00:00', strtotime("-20days")));
  491. $result = DispatchSub::where('del_time',0)
  492. // ->where('dispatch_time_start',">=", $startOfDay)
  493. ->where('finished_num',0)
  494. ->select('dispatch_no as order_no','product_title','technology_name','dispatch_quantity as product_num','process_id')
  495. ->orderBy('id','desc')
  496. ->limit(20)
  497. ->get()->toArray();
  498. if(! empty($result)){
  499. $process_id = array_unique(array_column($result,'process_id'));
  500. $processMap = Process::whereIn('id',$process_id)
  501. ->pluck('title','id')
  502. ->toArray();
  503. foreach ($result as $key => $value){
  504. $result[$key]['procedure'] = $processMap[$value['process_id']] ?? '';
  505. $result[$key]['product_title'] = $value['product_title'] . "(". $value['technology_name'] .")";
  506. }
  507. }
  508. return $this->json_return(200,'',$result);
  509. }
  510. //获取缓存
  511. public function getRedisData($cacheKey){
  512. if(Cache::has($cacheKey)){
  513. return [true,json_decode(Cache::get($cacheKey),true)];
  514. }
  515. return [false, []];
  516. }
  517. /**
  518. * 数据填充
  519. * @param $list
  520. * @param $type
  521. * @param $models
  522. */
  523. public function fillData($list,$type,&$models){
  524. if(empty($list)) return;
  525. $run_time = $process_time = $fault = $start_time = [];
  526. foreach ($list as $value){
  527. if($type == 1 && ! isset($start_time[$value['device_name']])){
  528. $start_time_tmp = date("Y-m-d H:i:s", $value['time'] / 1000);
  529. $start_time[$value['device_name']] = $start_time_tmp;
  530. }
  531. if($value['data_point_name'] == SystemL::run || $value['data_point_name'] == SystemL::standBy){
  532. //运行次数
  533. if(isset($run_time[$value['device_name']])){
  534. $run_time[$value['device_name']] += 1;
  535. }else{
  536. $run_time[$value['device_name']] = 1;
  537. }
  538. }
  539. if($value['data_point_name'] == SystemL::standBy){
  540. //工作次数
  541. if(isset($process_time[$value['device_name']])){
  542. $process_time[$value['device_name']] += 1;
  543. }else{
  544. $process_time[$value['device_name']] = 1;
  545. }
  546. }
  547. if($value['data_point_name'] == SystemL::stop){
  548. //故障次数
  549. if(isset($fault[$value['device_name']])){
  550. $fault[$value['device_name']] += 1;
  551. }else{
  552. $fault[$value['device_name']] = 1;
  553. }
  554. }
  555. }
  556. foreach (SystemL::$device as $key => $value){
  557. //运行次数
  558. $run_num = $run_time[$key] ?? 0;
  559. //工作次数
  560. $process_num = $process_time[$key] ?? 0;
  561. //故障次数
  562. $fault_tmp = $fault[$key] ?? 0;
  563. //运行时间
  564. $run_time_tmp = (new ReportFormsService())->calTimeReturnMin($run_num);
  565. //工作时间
  566. $process_time_tmp = (new ReportFormsService())->calTimeReturnMin($process_num);
  567. //故障时间
  568. $fault_time_tmp = (new ReportFormsService())->calTimeReturnMin($fault_tmp);
  569. //计划运行时间 工作时间 - 计划停机 (没有计划停机)
  570. //实际运行时间 计划运行时间 -故障停机 - 设备调整(没有设备调整
  571. $true_process_time = $process_time_tmp - $fault_time_tmp;
  572. //有效率 实际/计划运行 时间
  573. $efficient = $process_time_tmp > 0 ? number_format($true_process_time / $process_time_tmp,2) : 0;
  574. //表现性 加工数量/实际运行时间
  575. $expressive = $true_process_time > 0 ? number_format($process_num / $true_process_time,2) : 0;
  576. //质量指数 加工数量- 废品数量 / 加工数量
  577. $quality_index = $process_num > 0 ? number_format(($process_num - $fault_tmp) / $process_num,2) : 0;
  578. //OEE
  579. $oee = number_format($efficient * $expressive * $quality_index,2);
  580. if($type == 1){
  581. $models[$key]['machine_day_num'] = $run_num;
  582. $models[$key]['day_num'] = $process_num;
  583. $models[$key]['break_day_num'] = $fault_tmp;
  584. $models[$key]['rate'] = $oee;
  585. $models[$key]['start_time'] = $start_time[$key] ?? '暂未开机';
  586. }elseif($type == 2){
  587. $models[$key]['machine_week_num'] = $run_num;
  588. $models[$key]['week_num'] = $process_num;
  589. $models[$key]['break_week_num'] = $fault_tmp;
  590. }elseif($type == 3){
  591. $models[$key]['machine_month_num'] = $run_num;
  592. $models[$key]['month_num'] = $process_num;
  593. $models[$key]['break_month_num'] = $fault_tmp;
  594. }
  595. }
  596. }
  597. /**
  598. * 项目分布的地区
  599. * @param Request $request
  600. * @return array
  601. */
  602. public function saleOrdersFromAddress(Request $request){
  603. $return = [];
  604. $address_map = config('address');
  605. foreach ($address_map as $value){
  606. $return[] = [
  607. 'code' => $value['value'],
  608. 'label' => $value['label'],
  609. 'num' => ""
  610. ];
  611. }
  612. $sqlServerModel = new FyySqlServerService(['id' => 1]);
  613. list($status,$msg) = $sqlServerModel->getCustomerFromSqlServer();
  614. if($status){
  615. foreach ($return as $key => $value){
  616. if(isset($msg[$value['code']])) $return[$key]['num'] = $msg[$value['code']];
  617. }
  618. }
  619. return $this->json_return(200,'',$return);
  620. }
  621. /**
  622. * 车间生产状态
  623. * @param Request $request
  624. * @return array
  625. */
  626. public function production_status(Request $request){
  627. $return = [
  628. 'dispatch' => 0,
  629. 'finished' => 0,
  630. 'box' => 0,
  631. 'send' => 0,
  632. ];
  633. $time = strtotime(date("Y-m-d 00:00:00"));
  634. //派工
  635. $dispatch = ApplyOrderDetail::where('del_time',0)
  636. ->where('type', ApplyOrder::type_one)
  637. ->where('crt_time','>=',$time)
  638. ->select('quantity')
  639. ->get()->toArray();
  640. $dispatch_num = 0;
  641. foreach (array_column($dispatch, 'dispatch_quantity') as $quantity) {
  642. $dispatch_num = bcadd($dispatch_num, $quantity, 3);
  643. }
  644. $return['dispatch'] = $dispatch_num;
  645. //完工
  646. $finished = ApplyOrderDetail::where('del_time',0)
  647. ->where('type', ApplyOrder::type_two)
  648. ->where('crt_time','>=',$time)
  649. ->select('quantity')
  650. ->get()->toArray();
  651. $finished_num = 0;
  652. foreach (array_column($finished, 'quantity') as $quantity) {
  653. $finished_num = bcadd($finished_num, $quantity, 3);
  654. }
  655. $return['finished'] = $finished_num;
  656. //包装 发货
  657. $box_quantity = ApplyOrderDetail::where('del_time',0)
  658. ->where('type', ApplyOrder::type_three)
  659. ->where('crt_time','>=',$time)
  660. ->select('quantity')
  661. ->get()->toArray();
  662. $num = 0;
  663. foreach (array_column($box_quantity, 'quantity') as $quantity) {
  664. $num = bcadd($num, $quantity, 3);
  665. }
  666. $return['box'] = $return['send'] = $num;
  667. return $this->json_return(200,'',$return);
  668. }
  669. }