ScreenController.php 47 KB

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