monthlyPwOrderRule($data, $user, false); if(!$status) return [$status,$msg]; try { DB::beginTransaction(); $model = Item::where('id',$data['id'])->first(); $model->month = $data['month'] ?? 0; $model->save(); $time = time(); MonthlyPwOrderDetails::where('del_time',0) ->where('main_id', $model->id) ->update(['del_time' => $time]); $this->saveDetail($model->id, $time, $data); DB::commit(); }catch (\Exception $exception){ DB::rollBack(); return [false,$exception->getMessage()]; } return [true, '']; } public function monthlyPwOrderAdd($data,$user){ list($status,$msg) = $this->monthlyPwOrderRule($data, $user); if(!$status) return [$status,$msg]; try { DB::beginTransaction(); $model = new MonthlyPwOrder(); $model->code = $this->generateBillNo([ 'top_depart_id' => $user['top_depart_id'], 'type' => MonthlyPwOrder::Order_type, 'period' => date("Ym", $data['month']) ]); $model->month = $data['month'] ?? 0; $model->crt_id = $user['id']; $model->top_depart_id = $data['top_depart_id']; $model->save(); $this->saveDetail($model->id, time(), $data); DB::commit(); }catch (\Exception $exception){ DB::rollBack(); return [false,$exception->getMessage()]; } return [true, '']; } private function saveDetail($id, $time, $data){ if(! empty($data['details'])){ $unit = []; foreach ($data['details'] as $value){ $unit[] = [ 'main_id' => $id, 'employee_id' => $value['employee_id'], 'total_days' => $value['total_days'], 'rd_total_days' => $value['rd_total_days'], 'total_hours' => $value['total_hours'], 'rd_total_hours' => $value['rd_total_hours'], 'start_time' => $value['start_time'], 'end_time' => $value['end_time'], 'crt_time' => $time, 'top_depart_id' => $value['top_depart_id'], ]; } if(! empty($unit)) MonthlyPwOrderDetails::insert($unit); } } private function getDetail($id){ $data = MonthlyPwOrderDetails::where('del_time',0) ->where('main_id', $id) ->select('employee_id', 'total_days', 'rd_total_days', 'total_hours', 'rd_total_hours', 'start_time', 'end_time') ->get()->toArray(); $id = array_column($data,'employee_id'); $map = Employee::whereIn('id', $id)->select('title','id','number')->get()->toArray(); $map = array_column($map,null,'id'); $unit = []; foreach ($data as $key => $value){ $tmp = $map[$value['employee_id']] ?? []; $merge = []; $merge['employee_title'] = $tmp['title']; $merge['employee_number'] = $tmp['number']; $merge['start_time'] = date("Y-m-d", $value['start_time']); $merge['end_time'] = date("Y-m-d", $value['end_time']); $data[$key] = array_merge($value, $merge); } $detail = [ 'details' => $unit, ]; foreach ($detail as $key => $value) { if (empty($value)) { $detail[$key] = (object)[]; // 转成 stdClass 对象 } } return $detail; } public function monthlyPwOrderDel($data){ if($this->isEmpty($data,'id')) return [false,'请选择数据!']; try { DB::beginTransaction(); $time = time(); MonthlyPwOrder::where('del_time',0) ->whereIn('id',$data['id']) ->update(['del_time' => $time]); MonthlyPwOrderDetails::where('del_time',0) ->whereIn('main_id', $data['id']) ->update(['del_time' => $time]); DB::commit(); }catch (\Exception $exception){ DB::rollBack(); return [false,$exception->getMessage()]; } return [true, '']; } public function monthlyPwOrderDetail($data, $user){ if($this->isEmpty($data,'id')) return [false,'请选择数据!']; $customer = MonthlyPwOrder::where('del_time',0) ->where('id',$data['id']) ->first(); if(empty($customer)) return [false,'项目不存在或已被删除']; $customer = $customer->toArray(); $customer['crt_name'] = Employee::where('id',$customer['crt_id'])->value('title'); $customer['crt_time'] = $customer['crt_time'] ? date("Y-m-d H:i:s",$customer['crt_time']): ''; $customer['month'] = $customer['month'] ? date("Y-m",$customer['month']): ''; $details = $this->getDetail($data['id']); $customer = array_merge($customer, $details); return [true, $customer]; } public function monthlyPwOrderCommon($data,$user, $field = []){ if(empty($field)) $field = MonthlyPwOrder::$field; $model = MonthlyPwOrder::Clear($user,$data); $model = $model->where('del_time',0) ->select($field) ->orderby('id', 'desc'); if(! empty($data['code'])) $model->where('code', 'LIKE', '%'.$data['code'].'%'); if(! empty($data['id'])) $model->whereIn('id', $data['id']); if(! empty($data['crt_time'][0]) && ! empty($data['crt_time'][1])) { $return = $this->changeDateToTimeStampAboutRange($data['crt_time']); $model->where('crt_time','>=',$return[0]); $model->where('crt_time','<=',$return[1]); } return $model; } public function monthlyPwOrderList($data,$user){ $model = $this->monthlyPwOrderCommon($data, $user); $list = $this->limit($model,'',$data); $list = $this->fillData($list); return [true, $list]; } public function monthlyPwOrderRule(&$data, $user, $is_add = true){ if(empty($data['month'])) return [false, '月份不能为空']; $data['month'] = $this->changeDateToDate($data['month']); $data['top_depart_id'] = $user['top_depart_id']; if(empty($data['details'])) return [false, '人员月度工时单明细不能为空']; foreach ($data['details'] as $key => $value){ if(empty($value['employee_id'])) return [false, '人员不能为空']; $res = $this->checkNumber($data['total_days'],0,'non-negative'); if(! $res['valid']) return [false,'出勤总天数:' . $res['error']]; $res = $this->checkNumber($data['rd_total_days'],0,'non-negative'); if(! $res['valid']) return [false,'研发出勤总天数:' . $res['error']]; $res = $this->checkNumber($data['total_hours'],2,'non-negative'); if(! $res['valid']) return [false,'出勤总工时:' . $res['error']]; $res = $this->checkNumber($data['rd_total_hours'],2,'non-negative'); if(! $res['valid']) return [false,'研发总工时:' . $res['error']]; if(empty($value['start_time'])) return [false, '开始时间不能为空']; $data['details'][$key]['start_time'] = $this->changeDateToDate($data['start_time']); if(empty($value['end_time'])) return [false, '结束时间不能为空']; $data['details'][$key]['end_time'] = $this->changeDateToDate($data['end_time']); $data['details'][$key]['top_depart_id'] = $data['top_depart_id']; } list($status, $msg) = $this->checkArrayRepeat($data['details'],'employee_id','人员'); if(! $status) return [false, $msg]; if($is_add){ $bool = MonthlyPwOrder::where('code',$data['code']) ->where('top_depart_id', $data['top_depart_id']) ->where('del_time',0) ->exists(); }else{ if(empty($data['id'])) return [false,'ID不能为空']; $bool = Item::where('code',$data['code']) ->where('top_depart_id', $data['top_depart_id']) ->where('id','<>',$data['id']) ->where('del_time',0) ->exists(); } if($bool) return [false, date("Y-m", $data['month']) . '已存在人员月度研发工时单']; return [true, '']; } public function fillData($data, $is_export = false){ if(empty($data['data'])) return $data; $emp = (new EmployeeService())->getEmployeeMap(array_unique(array_column($data['data'],'crt_id'))); $map = []; if($is_export) $map = $this->getDetailsMap(array_column($data['data'],'id')); foreach ($data['data'] as $key => $value){ $data['data'][$key]['crt_time'] = $value['crt_time'] ? date('Y-m-d H:i:s',$value['crt_time']) : ''; $data['data'][$key]['month'] = $value['month'] ? date('Y-m',$value['month']) : ''; $data['data'][$key]['crt_name'] = $emp[$value['crt_id']] ?? ''; $tmp = $map[$value['id']] ?? []; $data['data'][$key]['details'] = $tmp['man'] ?? ""; } return $data; } public function getDetailsMap($item_id){ // 1. 获取明细数据,注意一定要选出 item_id $data = ItemDetails::where('del_time', 0) ->whereIn('item_id', $item_id) ->select('item_id', 'data_id', 'type') ->get(); // 2. 收集 ID 并按类型分类(去重以减少查询压力) $manIds = $data->where('type', ItemDetails::type_one)->pluck('data_id')->unique()->toArray(); $devIds = $data->where('type', ItemDetails::type_two)->pluck('data_id')->unique()->toArray(); // 3. 一次性查出对应的映射 (假设人员表字段是 number, 设备表是 code) // 注意:pluck('显示内容', 'ID') $manMap = Employee::whereIn('id', $manIds)->pluck('number', 'id')->toArray(); $devMap = Device::whereIn('id', $devIds)->pluck('code', 'id')->toArray(); // 4. 按 item_id 分组处理成字符串 $result = []; foreach ($item_id as $id) { // 初始化每个 item_id 的默认结构 $result[$id] = [ 'man' => '', 'device' => '' ]; } // 5. 遍历明细填充数据 $grouped = $data->groupBy('item_id'); foreach ($grouped as $itemId => $items) { $mans = []; $devices = []; foreach ($items as $item) { if ($item->type == ItemDetails::type_one) { if (isset($manMap[$item->data_id])) { $mans[] = $manMap[$item->data_id]; } } else { if (isset($devMap[$item->data_id])) { $devices[] = $devMap[$item->data_id]; } } } $result[$itemId] = [ 'man' => implode(',', $mans), 'device' => implode(',', $devices) ]; } return $result; } }