PersonWorkService.php 25 KB

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