DeviceWorkService.php 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714
  1. <?php
  2. namespace App\Service;
  3. use App\Model\DailyDwOrder;
  4. use App\Model\DailyDwOrderDetails;
  5. use App\Model\Device;
  6. use App\Model\Employee;
  7. use App\Model\Item;
  8. use App\Model\MonthlyDwOrder;
  9. use App\Model\MonthlyDwOrderDetails;
  10. use Illuminate\Support\Facades\DB;
  11. class DeviceWorkService extends Service
  12. {
  13. //设备月工时单--------------------------------------------
  14. public function monthlyDwOrderEdit($data,$user){
  15. list($status,$msg) = $this->monthlyDwOrderRule($data, $user, false);
  16. if(!$status) return [$status,$msg];
  17. try {
  18. DB::beginTransaction();
  19. $model = MonthlyDwOrder::where('id',$data['id'])->first();
  20. // $model->month = $data['month'] ?? 0;
  21. // $model->save();
  22. $time = time();
  23. MonthlyDwOrderDetails::where('del_time',0)
  24. ->where('main_id', $model->id)
  25. ->update(['del_time' => $time]);
  26. $this->saveDetail($model->id, $time, $data);
  27. DB::commit();
  28. }catch (\Exception $exception){
  29. DB::rollBack();
  30. return [false,$exception->getMessage()];
  31. }
  32. return [true, ''];
  33. }
  34. public function monthlyDwOrderAdd($data,$user){
  35. list($status,$msg) = $this->monthlyDwOrderRule($data, $user);
  36. if(!$status) return [$status,$msg];
  37. try {
  38. DB::beginTransaction();
  39. $model = new MonthlyDwOrder();
  40. $model->code = $this->generateBillNo([
  41. 'top_depart_id' => $user['top_depart_id'],
  42. 'type' => MonthlyDwOrder::Order_type,
  43. 'period' => date("Ym", $data['month'])
  44. ]);
  45. $model->month = $data['month'] ?? 0;
  46. $model->crt_id = $user['id'];
  47. $model->top_depart_id = $data['top_depart_id'];
  48. $model->save();
  49. $this->saveDetail($model->id, time(), $data);
  50. DB::commit();
  51. }catch (\Exception $exception){
  52. DB::rollBack();
  53. return [false,$exception->getMessage()];
  54. }
  55. return [true, ''];
  56. }
  57. private function saveDetail($id, $time, $data){
  58. if(! empty($data['details'])){
  59. $unit = [];
  60. foreach ($data['details'] as $value){
  61. $unit[] = [
  62. 'main_id' => $id,
  63. 'device_id' => $value['device_id'],
  64. 'total_days' => $value['total_days'],
  65. 'rd_total_days' => $value['rd_total_days'],
  66. 'total_hours' => $value['total_hours'],
  67. 'rd_total_hours' => $value['rd_total_hours'],
  68. 'start_time' => $value['start_time'],
  69. 'end_time' => $value['end_time'],
  70. 'crt_time' => $time,
  71. 'top_depart_id' => $value['top_depart_id'],
  72. ];
  73. }
  74. if(! empty($unit)) MonthlyDwOrderDetails::insert($unit);
  75. }
  76. }
  77. private function getDetail($id){
  78. $data = MonthlyDwOrderDetails::where('del_time',0)
  79. ->where('main_id', $id)
  80. ->select('device_id', 'total_days', 'rd_total_days', 'total_hours', 'rd_total_hours', 'start_time', 'end_time')
  81. ->get()->toArray();
  82. $id = array_column($data,'device_id');
  83. $map = Device::whereIn('id', $id)->select('title','id','code')->get()->toArray();
  84. $map = array_column($map,null,'id');
  85. foreach ($data as $key => $value){
  86. $tmp = $map[$value['device_id']] ?? [];
  87. $merge = [];
  88. $merge['device_title'] = $tmp['title'];
  89. $merge['device_code'] = $tmp['code'];
  90. $merge['start_time'] = date("Y-m-d", $value['start_time']);
  91. $merge['end_time'] = date("Y-m-d", $value['end_time']);
  92. $data[$key] = array_merge($value, $merge);
  93. }
  94. $detail = [
  95. 'details' => $data,
  96. ];
  97. foreach ($detail as $key => $value) {
  98. if (empty($value)) {
  99. $detail[$key] = (object)[]; // 转成 stdClass 对象
  100. }
  101. }
  102. return $detail;
  103. }
  104. public function monthlyDwOrderDel($data){
  105. if($this->isEmpty($data,'id')) return [false,'请选择数据!'];
  106. try {
  107. DB::beginTransaction();
  108. $time = time();
  109. MonthlyDwOrder::where('del_time',0)
  110. ->whereIn('id',$data['id'])
  111. ->update(['del_time' => $time]);
  112. MonthlyDwOrderDetails::where('del_time',0)
  113. ->whereIn('main_id', $data['id'])
  114. ->update(['del_time' => $time]);
  115. DB::commit();
  116. }catch (\Exception $exception){
  117. DB::rollBack();
  118. return [false,$exception->getMessage()];
  119. }
  120. return [true, ''];
  121. }
  122. public function monthlyDwOrderDetail($data, $user){
  123. if($this->isEmpty($data,'id')) return [false,'请选择数据!'];
  124. $customer = MonthlyDwOrder::where('del_time',0)
  125. ->where('id',$data['id'])
  126. ->first();
  127. if(empty($customer)) return [false,'设备月度工时单不存在或已被删除'];
  128. $customer = $customer->toArray();
  129. $customer['crt_name'] = Employee::where('id',$customer['crt_id'])->value('title');
  130. $customer['crt_time'] = $customer['crt_time'] ? date("Y-m-d H:i:s",$customer['crt_time']): '';
  131. $customer['month'] = $customer['month'] ? date("Y-m",$customer['month']): '';
  132. $details = $this->getDetail($data['id']);
  133. $customer = array_merge($customer, $details);
  134. return [true, $customer];
  135. }
  136. public function monthlyDwOrderCommon($data,$user, $field = []){
  137. if(empty($field)) $field = MonthlyDwOrder::$field;
  138. $model = MonthlyDwOrder::Clear($user,$data);
  139. $model = $model->where('del_time',0)
  140. ->select($field)
  141. ->orderby('id', 'desc');
  142. if(! empty($data['code'])) $model->where('code', 'LIKE', '%'.$data['code'].'%');
  143. if(! empty($data['id'])) $model->whereIn('id', $data['id']);
  144. if(! empty($data['crt_time'][0]) && ! empty($data['crt_time'][1])) {
  145. $return = $this->changeDateToTimeStampAboutRange($data['crt_time']);
  146. $model->where('crt_time','>=',$return[0]);
  147. $model->where('crt_time','<=',$return[1]);
  148. }
  149. return $model;
  150. }
  151. public function monthlyDwOrderList($data,$user){
  152. $model = $this->monthlyDwOrderCommon($data, $user);
  153. $list = $this->limit($model,'',$data);
  154. $list = $this->fillData($list);
  155. return [true, $list];
  156. }
  157. public function monthlyDwOrderRule(&$data, $user, $is_add = true){
  158. if(empty($data['month'])) return [false, '月份不能为空'];
  159. $data['month'] = $this->changeDateToDate($data['month']);
  160. $data['top_depart_id'] = $user['top_depart_id'];
  161. if(empty($data['details'])) return [false, '设备月度工时单明细不能为空'];
  162. foreach ($data['details'] as $key => $value){
  163. if(empty($value['device_id'])) return [false, '设备不能为空'];
  164. $res = $this->checkNumber($value['total_days'],0,'non-negative');
  165. if(! $res['valid']) return [false,'出勤总天数:' . $res['error']];
  166. $res = $this->checkNumber($value['rd_total_days'],0,'non-negative');
  167. if(! $res['valid']) return [false,'研发出勤总天数:' . $res['error']];
  168. $res = $this->checkNumber($value['total_hours'],2,'non-negative');
  169. if(! $res['valid']) return [false,'出勤总工时:' . $res['error']];
  170. $res = $this->checkNumber($value['rd_total_hours'],2,'non-negative');
  171. if(! $res['valid']) return [false,'研发总工时:' . $res['error']];
  172. if(empty($value['start_time'])) return [false, '开始时间不能为空'];
  173. $data['details'][$key]['start_time'] = $this->changeDateToDate($value['start_time']);
  174. if(empty($value['end_time'])) return [false, '结束时间不能为空'];
  175. $data['details'][$key]['end_time'] = $this->changeDateToDate($value['end_time']);
  176. $data['details'][$key]['top_depart_id'] = $data['top_depart_id'];
  177. }
  178. list($status, $msg) = $this->checkArrayRepeat($data['details'],'device_id','设备');
  179. if(! $status) return [false, $msg];
  180. if($is_add){
  181. $bool = MonthlyDwOrder::where('top_depart_id', $data['top_depart_id'])
  182. ->where('month', $data['month'])
  183. ->where('del_time',0)
  184. ->exists();
  185. }else{
  186. if(empty($data['id'])) return [false,'ID不能为空'];
  187. $bool = MonthlyDwOrder::where('top_depart_id', $data['top_depart_id'])
  188. ->where('month', $data['month'])
  189. ->where('id','<>',$data['id'])
  190. ->where('del_time',0)
  191. ->exists();
  192. }
  193. if($bool) return [false, date("Y-m", $data['month']) . '已存在设备月度研发工时单'];
  194. return [true, ''];
  195. }
  196. public function fillData($data){
  197. if(empty($data['data'])) return $data;
  198. $emp = (new EmployeeService())->getEmployeeMap(array_unique(array_column($data['data'],'crt_id')));
  199. foreach ($data['data'] as $key => $value){
  200. $data['data'][$key]['crt_time'] = $value['crt_time'] ? date('Y-m-d H:i:s',$value['crt_time']) : '';
  201. $data['data'][$key]['month'] = $value['month'] ? date('Y-m',$value['month']) : '';
  202. $data['data'][$key]['crt_name'] = $emp[$value['crt_id']] ?? '';
  203. }
  204. return $data;
  205. }
  206. public function fillDataForExport($data, $column, &$return)
  207. {
  208. if(empty($data)) return;
  209. $mainIds = array_column($data, 'id');
  210. // 获取详情映射 [main_id => [details...]]
  211. $detailsMap = $this->getDetailsMap($mainIds);
  212. // 默认空行模板
  213. $defaultRow = array_fill_keys($column, '');
  214. foreach ($data as $main) {
  215. $mainId = $main['id'];
  216. $details = $detailsMap[$mainId] ?? [];
  217. // 提取主表信息
  218. $mainInfo = [
  219. 'code' => $main['code'],
  220. 'month' => $main['month'] ? date('Y-m', $main['month']) : '',
  221. ];
  222. if (empty($details)) {
  223. // 如果没有详情,至少导出一行主表信息(可选)
  224. $return[] = array_merge($defaultRow, $mainInfo);
  225. } else {
  226. // 核心:遍历详情,每一行详情都合并主表信息
  227. foreach ($details as $sub) {
  228. // 合并主表字段 + 详情字段
  229. $fullRow = array_merge($mainInfo, $sub);
  230. // 过滤掉不在导出列里的字段,并补充缺失列
  231. $return[] = array_merge($defaultRow, array_intersect_key($fullRow, $defaultRow));
  232. }
  233. }
  234. }
  235. }
  236. public function getDetailsMap($main_ids)
  237. {
  238. // 获取详情
  239. $details = MonthlyDwOrderDetails::where('del_time', 0)
  240. ->whereIn('main_id', $main_ids)
  241. ->get();
  242. // 获取设备信息
  243. $empIds = $details->pluck('device_id')->unique();
  244. $empMap = Device::whereIn('id', $empIds)->get()->keyBy('id');
  245. $res = [];
  246. foreach ($details as $item) {
  247. $tmpEmp = $empMap[$item->device_id] ?? null;
  248. // 组装每一行详情需要展示的字段
  249. $res[$item->main_id][] = [
  250. 'device_code' => $tmpEmp ? $tmpEmp->code : '',
  251. 'total_days' => $item->total_days,
  252. 'rd_total_days' => $item->rd_total_days,
  253. 'total_hours' => $item->total_hours,
  254. 'rd_total_hours' => $item->rd_total_hours,
  255. 'start_time' => $item->start_time ? date("Y-m-d", $item->start_time) : '',
  256. 'end_time' => $item->end_time ? date("Y-m-d", $item->end_time) : '',
  257. ];
  258. }
  259. return $res; // 返回 [main_id => [detail_row, detail_row]]
  260. }
  261. //设备日工时单----------------------------------------------
  262. public function dailyDwOrderEdit($data,$user){
  263. list($status,$msg) = $this->dailyDwOrderRule($data, $user, false);
  264. if(!$status) return [$status,$msg];
  265. try {
  266. DB::beginTransaction();
  267. $model = DailyDwOrder::where('id',$data['id'])->first();
  268. $model->item_id = $data['item_id'] ?? 0;
  269. $model->save();
  270. $time = time();
  271. DailyDwOrderDetails::where('del_time',0)
  272. ->where('main_id', $model->id)
  273. ->update(['del_time' => $time]);
  274. $this->saveDetailDaily($model->id, $time, $data);
  275. DB::commit();
  276. }catch (\Exception $exception){
  277. DB::rollBack();
  278. return [false,$exception->getMessage()];
  279. }
  280. return [true, ''];
  281. }
  282. public function dailyDwOrderAdd($data,$user){
  283. list($status,$msg) = $this->dailyDwOrderRule($data, $user);
  284. if(!$status) return [$status,$msg];
  285. try {
  286. DB::beginTransaction();
  287. $model = new DailyDwOrder();
  288. $model->code = $this->generateBillNo([
  289. 'top_depart_id' => $user['top_depart_id'],
  290. 'type' => DailyDwOrder::Order_type,
  291. 'period' => date("Ym", $data['order_time'])
  292. ]);
  293. $model->order_time = $data['order_time'] ?? 0;
  294. $model->item_id = $data['item_id'] ?? 0;
  295. $model->crt_id = $user['id'];
  296. $model->top_depart_id = $data['top_depart_id'];
  297. $model->save();
  298. $this->saveDetailDaily($model->id, time(), $data);
  299. DB::commit();
  300. }catch (\Exception $exception){
  301. DB::rollBack();
  302. return [false,$exception->getMessage()];
  303. }
  304. return [true, ''];
  305. }
  306. private function saveDetailDaily($id, $time, $data){
  307. if(! empty($data['details'])){
  308. $unit = [];
  309. foreach ($data['details'] as $value){
  310. $unit[] = [
  311. 'main_id' => $id,
  312. 'device_id' => $value['device_id'],
  313. 'start_time_hour' => $value['start_time_hour'],
  314. 'start_time_min' => $value['start_time_min'],
  315. 'end_time_hour' => $value['end_time_hour'],
  316. 'end_time_min' => $value['end_time_min'],
  317. 'total_work_min' => $value['total_work_min'],
  318. 'crt_time' => $time,
  319. 'top_depart_id' => $value['top_depart_id'],
  320. ];
  321. }
  322. if(! empty($unit)) DailyDwOrderDetails::insert($unit);
  323. }
  324. }
  325. private function getDetailDaily($id){
  326. $data = DailyDwOrderDetails::where('del_time',0)
  327. ->where('main_id', $id)
  328. ->select('device_id', 'start_time_hour', 'start_time_min', 'end_time_hour', 'end_time_min', 'total_work_min')
  329. ->get()->toArray();
  330. $id = array_column($data,'device_id');
  331. $map = Device::whereIn('id',$id)
  332. ->select('title','id','number')
  333. ->get()
  334. ->keyBy('id')
  335. ->toArray();
  336. foreach ($data as $key => $value){
  337. $tmp = $map[$value['device_id']] ?? [];
  338. $merge = [];
  339. $merge['device_title'] = $tmp['title'];
  340. $merge['device_code'] = $tmp['code'];
  341. $data[$key] = array_merge($value, $merge);
  342. }
  343. $detail = [
  344. 'details' => $data,
  345. ];
  346. foreach ($detail as $key => $value) {
  347. if (empty($value)) {
  348. $detail[$key] = (object)[]; // 转成 stdClass 对象
  349. }
  350. }
  351. return $detail;
  352. }
  353. public function dailyDwOrderDel($data){
  354. if($this->isEmpty($data,'id')) return [false,'请选择数据!'];
  355. try {
  356. DB::beginTransaction();
  357. $time = time();
  358. DailyDwOrder::where('del_time',0)
  359. ->whereIn('id',$data['id'])
  360. ->update(['del_time' => $time]);
  361. DailyDwOrderDetails::where('del_time',0)
  362. ->whereIn('main_id', $data['id'])
  363. ->update(['del_time' => $time]);
  364. DB::commit();
  365. }catch (\Exception $exception){
  366. DB::rollBack();
  367. return [false,$exception->getMessage()];
  368. }
  369. return [true, ''];
  370. }
  371. public function dailyDwOrderDetail($data, $user){
  372. if($this->isEmpty($data,'id')) return [false,'请选择数据!'];
  373. $customer = DailyDwOrder::where('del_time',0)
  374. ->where('id',$data['id'])
  375. ->first();
  376. if(empty($customer)) return [false,'设备日工时单不存在或已被删除'];
  377. $customer = $customer->toArray();
  378. $customer['crt_name'] = Employee::where('id',$customer['crt_id'])->value('title');
  379. $customer['crt_time'] = $customer['crt_time'] ? date("Y-m-d H:i:s",$customer['crt_time']): '';
  380. $item = Item::where('id', $customer['item_id'])->first();
  381. $customer['item_title'] = $item->title;
  382. $customer['item_code'] = $item->code;
  383. $customer['order_time'] = $customer['order_time'] ? date("Y-m-d",$customer['order_time']): '';
  384. $details = $this->getDetailDaily($data['id']);
  385. $customer = array_merge($customer, $details);
  386. return [true, $customer];
  387. }
  388. public function dailyDwOrderCommon($data,$user, $field = []){
  389. if(empty($field)) $field = DailyDwOrder::$field;
  390. $model = DailyDwOrder::Clear($user,$data);
  391. $model = $model->where('del_time',0)
  392. ->select($field)
  393. ->orderby('id', 'desc');
  394. if(! empty($data['code'])) $model->where('code', 'LIKE', '%'.$data['code'].'%');
  395. if(! empty($data['id'])) $model->whereIn('id', $data['id']);
  396. if(! empty($data['crt_time'][0]) && ! empty($data['crt_time'][1])) {
  397. $return = $this->changeDateToTimeStampAboutRange($data['crt_time']);
  398. $model->where('crt_time','>=',$return[0]);
  399. $model->where('crt_time','<=',$return[1]);
  400. }
  401. return $model;
  402. }
  403. public function dailyDwOrderList($data,$user){
  404. $model = $this->dailyDwOrderCommon($data, $user);
  405. $list = $this->limit($model,'',$data);
  406. $list = $this->fillDataDaily($list);
  407. return [true, $list];
  408. }
  409. public function dailyDwOrderRule(&$data, $user, $is_add = true){
  410. if(empty($data['order_time'])) return [false, '单据日期不能为空'];
  411. $data['order_time'] = $this->changeDateToDate($data['order_time']);
  412. $orderTime = $data['order_time'];
  413. $itemId = $data['item_id'] ?? 0;
  414. if(empty($itemId)) return [false, '项目不能为空'];
  415. $bool = Item::where('del_time',0)->where('id', $itemId)->exists();
  416. if(!$bool) return [false, '项目不存在或已被删除'];
  417. $data['top_depart_id'] = $user['top_depart_id'];
  418. if(empty($data['details'])) return [false, '设备日工时单明细不能为空'];
  419. // --- 1. 批量预获取人员信息,用于报错提示 ---
  420. $allEmpIds = array_filter(array_unique(array_column($data['details'], 'device_id')));
  421. $empDisplayMap = Device::whereIn('id', $allEmpIds)
  422. ->get(['id', 'code', 'title'])
  423. ->mapWithKeys(function($item){
  424. return [$item->id => "[{$item->code}]{$item->title}"];
  425. })->toArray();
  426. // 2. 本次提交内部重叠记录
  427. $internalOverlap = [];
  428. foreach ($data['details'] as $key => $value){
  429. $empId = $value['device_id'] ?? 0;
  430. if(empty($empId)) return [false, '设备不能为空'];
  431. $empName = $empDisplayMap[$empId] ?? "ID:{$empId}";
  432. // 校验数字有效性 (修正为使用 $value)
  433. $res = $this->checkNumber($value['start_time_hour'], 0, 'non-negative');
  434. if(!$res['valid']) return [false, "设备{$empName}开始点:" . $res['error']];
  435. $res = $this->checkNumber($value['start_time_min'], 0, 'non-negative');
  436. if(!$res['valid']) return [false, "设备{$empName}开始分:" . $res['error']];
  437. $res = $this->checkNumber($value['end_time_hour'], 0, 'non-negative');
  438. if(!$res['valid']) return [false, "设备{$empName}结束点:" . $res['error']];
  439. $res = $this->checkNumber($value['end_time_min'], 0, 'non-negative');
  440. if(!$res['valid']) return [false, "设备{$empName}结束分:" . $res['error']];
  441. $currentStart = $value['start_time_hour'] * 60 + $value['start_time_min'];
  442. $currentEnd = $value['end_time_hour'] * 60 + $value['end_time_min'];
  443. if ($currentStart >= $currentEnd) {
  444. return [false, "设备{$empName}:开始时间必须早于结束时间"];
  445. }
  446. // --- 3. 内部重叠校验(防止一次提交多行重复) ---
  447. if (isset($internalOverlap[$empId])) {
  448. foreach ($internalOverlap[$empId] as $period) {
  449. if ($currentStart < $period['e'] && $period['s'] < $currentEnd) {
  450. return [false, "设备{$empName}在本次提交的多行明细中时间段重叠"];
  451. }
  452. }
  453. }
  454. $internalOverlap[$empId][] = ['s' => $currentStart, 'e' => $currentEnd];
  455. $query = DB::table('daily_dw_order_details as d')
  456. ->join('daily_dw_order as m', 'd.main_id', '=', 'm.id')
  457. ->where('m.top_depart_id', $data['top_depart_id'])
  458. ->where('m.order_time', $orderTime)
  459. ->where('m.item_id', $itemId)
  460. ->where('d.device_id', $empId)
  461. ->where('m.del_time', 0)
  462. ->where('d.del_time', 0);
  463. if (!$is_add && !empty($data['id'])) {
  464. $query->where('m.id', '<>', $data['id']);
  465. }
  466. $existingPeriods = $query->select('d.start_time_hour', 'd.start_time_min', 'd.end_time_hour', 'd.end_time_min')->get();
  467. foreach ($existingPeriods as $p) {
  468. $exStart = $p->start_time_hour * 60 + $p->start_time_min;
  469. $exEnd = $p->end_time_hour * 60 + $p->end_time_min;
  470. if ($currentStart < $exEnd && $exStart < $currentEnd) {
  471. return [false, "设备{$empName}在该项目该日已有其他工时单创建重叠的时间段数据"];
  472. }
  473. }
  474. $data['details'][$key]['top_depart_id'] = $data['top_depart_id'];
  475. }
  476. if(!$is_add){
  477. if(empty($data['id'])) return [false,'ID不能为空'];
  478. $bool = DailyDwOrder::where('top_depart_id', $data['top_depart_id'])
  479. ->where('id',$data['id'])
  480. ->where('del_time',0)
  481. ->exists();
  482. if(!$bool) return [false, '设备日工时单不存在或已被删除'];
  483. }
  484. return [true, ''];
  485. }
  486. public function fillDataDaily($data){
  487. if(empty($data['data'])) return $data;
  488. $emp = (new EmployeeService())->getEmployeeMap(array_unique(array_column($data['data'],'crt_id')));
  489. $item = (new ItemService())->getItemMap(array_unique(array_column($data['data'],'item_id')));
  490. foreach ($data['data'] as $key => $value){
  491. $data['data'][$key]['crt_time'] = $value['crt_time'] ? date('Y-m-d H:i:s',$value['crt_time']) : '';
  492. $data['data'][$key]['order_time'] = $value['order_time'] ? date('Y-m-d',$value['order_time']) : '';
  493. $data['data'][$key]['crt_name'] = $emp[$value['crt_id']] ?? '';
  494. $item_tmp = $item[$value['item_id']] ?? [];
  495. $data['data'][$key]['item_title'] = $item_tmp['title'] ?? '';
  496. $data['data'][$key]['item_code'] = $item_tmp['code'] ?? '';
  497. }
  498. return $data;
  499. }
  500. public function fillDataForExportDaily($data, $column, &$return)
  501. {
  502. if (empty($data)) return;
  503. $mainIds = array_column($data, 'id');
  504. // 1. 获取详情及所有关联档案(项目、设备)的映射
  505. $detailsMap = $this->getDwDailyDetailsMap($mainIds, $data);
  506. foreach ($data as $main) {
  507. $mainId = $main['id'];
  508. $details = $detailsMap[$mainId] ?? [];
  509. // 2. 提取并格式化主表共有信息
  510. $mainInfo = [
  511. 'code' => $main['code'],
  512. 'order_time' => !empty($main['order_time']) ? date('Y-m-d', $main['order_time']) : '',
  513. ];
  514. if (empty($details)) {
  515. // 无明细时只导出一行主表信息
  516. $tempRow = [];
  517. foreach ($column as $col) {
  518. $tempRow[] = $mainInfo[$col] ?? '';
  519. }
  520. $return[] = $tempRow;
  521. } else {
  522. // 3. 平铺:将设备明细信息、项目信息与主表信息合并
  523. foreach ($details as $sub) {
  524. $fullRowData = array_merge($mainInfo, $sub);
  525. $tempRow = [];
  526. foreach ($column as $col) {
  527. $tempRow[] = $fullRowData[$col] ?? '';
  528. }
  529. $return[] = $tempRow;
  530. }
  531. }
  532. }
  533. }
  534. public function getDwDailyDetailsMap($mainIds, $mainData)
  535. {
  536. // 1. 获取设备工时子表记录
  537. $details = DB::table('daily_dw_order_details')
  538. ->where('del_time', 0)
  539. ->whereIn('main_id', $mainIds)
  540. ->get();
  541. // 2. 提取关联 ID(设备 ID 和 项目 ID)
  542. $deviceIds = $details->pluck('device_id')->unique();
  543. $itemIds = array_unique(array_column($mainData, 'item_id'));
  544. // 3. 批量获取设备档案和项目档案
  545. $deviceMap = DB::table('device')
  546. ->whereIn('id', $deviceIds)
  547. ->get(['id', 'title', 'code'])
  548. ->keyBy('id');
  549. $itemMap = DB::table('item')
  550. ->whereIn('id', $itemIds)
  551. ->get(['id', 'title', 'code'])
  552. ->keyBy('id');
  553. // 4. 预挂载主表的项目信息(item_code, item_title)
  554. $mainItemInfo = [];
  555. foreach ($mainData as $m) {
  556. $proj = $itemMap[$m['item_id']] ?? null;
  557. $mainItemInfo[$m['id']] = [
  558. 'item_code' => $proj ? $proj->code : '',
  559. 'item_title' => $proj ? $proj->title : '',
  560. ];
  561. }
  562. $res = [];
  563. // 如果没有详情,初始化空结构
  564. if ($details->isEmpty()) {
  565. foreach ($mainItemInfo as $mId => $info) {
  566. $res[$mId] = [];
  567. }
  568. return $res;
  569. }
  570. // 5. 循环子表,合并设备档案信息
  571. foreach ($details as $item) {
  572. $device = $deviceMap[$item->device_id] ?? null;
  573. $detailRow = [
  574. // 设备信息(对应 Excel 配置中的 key)
  575. 'device_code' => $device ? $device->code : '',
  576. 'device_title' => $device ? $device->title : '',
  577. // 时间信息
  578. 'start_time' => sprintf('%02d:%02d', $item->start_time_hour, $item->start_time_min),
  579. 'end_time' => sprintf('%02d:%02d', $item->end_time_hour, $item->end_time_min),
  580. // 项目信息(由主表平铺而来)
  581. 'item_code' => $mainItemInfo[$item->main_id]['item_code'] ?? '',
  582. 'item_title' => $mainItemInfo[$item->main_id]['item_title'] ?? '',
  583. ];
  584. $res[$item->main_id][] = $detailRow;
  585. }
  586. return $res;
  587. }
  588. }