ScreenController.php 47 KB

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