ScreenController.php 46 KB

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