ScreenController.php 47 KB

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