ScreenController.php 46 KB

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