ScreenController.php 46 KB

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