ScreenController.php 46 KB

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