monthlyDwOrderRule($data, $user, false); if(!$status) return [$status,$msg]; try { DB::beginTransaction(); $model = MonthlyDwOrder::where('id',$data['id'])->first(); // $model->month = $data['month'] ?? 0; // $model->save(); $time = time(); MonthlyDwOrderDetails::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 monthlyDwOrderAdd($data,$user){ list($status,$msg) = $this->monthlyDwOrderRule($data, $user); if(!$status) return [$status,$msg]; try { DB::beginTransaction(); $model = new MonthlyDwOrder(); $model->code = $this->generateBillNo([ 'top_depart_id' => $user['top_depart_id'], 'type' => MonthlyDwOrder::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, 'device_id' => $value['device_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)) MonthlyDwOrderDetails::insert($unit); } } private function getDetail($id){ $data = MonthlyDwOrderDetails::where('del_time',0) ->where('main_id', $id) ->select('device_id', 'total_days', 'rd_total_days', 'total_hours', 'rd_total_hours', 'start_time', 'end_time') ->get()->toArray(); $id = array_column($data,'device_id'); $map = Device::whereIn('id', $id)->select('title','id','code')->get()->toArray(); $map = array_column($map,null,'id'); foreach ($data as $key => $value){ $tmp = $map[$value['device_id']] ?? []; $merge = []; $merge['device_title'] = $tmp['title']; $merge['device_code'] = $tmp['code']; $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' => $data, ]; foreach ($detail as $key => $value) { if (empty($value)) { $detail[$key] = (object)[]; // 转成 stdClass 对象 } } return $detail; } public function monthlyDwOrderDel($data){ if($this->isEmpty($data,'id')) return [false,'请选择数据!']; try { DB::beginTransaction(); $time = time(); MonthlyDwOrder::where('del_time',0) ->whereIn('id',$data['id']) ->update(['del_time' => $time]); MonthlyDwOrderDetails::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 monthlyDwOrderDetail($data, $user){ if($this->isEmpty($data,'id')) return [false,'请选择数据!']; $customer = MonthlyDwOrder::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 monthlyDwOrderCommon($data,$user, $field = []){ if(empty($field)) $field = MonthlyDwOrder::$field; $model = MonthlyDwOrder::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 monthlyDwOrderList($data,$user){ $model = $this->monthlyDwOrderCommon($data, $user); $list = $this->limit($model,'',$data); $list = $this->fillData($list); return [true, $list]; } public function monthlyDwOrderRule(&$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['device_id'])) return [false, '设备不能为空']; $res = $this->checkNumber($value['total_days'],0,'non-negative'); if(! $res['valid']) return [false,'出勤总天数:' . $res['error']]; $res = $this->checkNumber($value['rd_total_days'],0,'non-negative'); if(! $res['valid']) return [false,'研发出勤总天数:' . $res['error']]; $res = $this->checkNumber($value['total_hours'],2,'non-negative'); if(! $res['valid']) return [false,'出勤总工时:' . $res['error']]; $res = $this->checkNumber($value['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($value['start_time']); if(empty($value['end_time'])) return [false, '结束时间不能为空']; $data['details'][$key]['end_time'] = $this->changeDateToDate($value['end_time']); $data['details'][$key]['top_depart_id'] = $data['top_depart_id']; } list($status, $msg) = $this->checkArrayRepeat($data['details'],'device_id','设备'); if(! $status) return [false, $msg]; if($is_add){ $bool = MonthlyDwOrder::where('top_depart_id', $data['top_depart_id']) ->where('month', $data['month']) ->where('del_time',0) ->exists(); }else{ if(empty($data['id'])) return [false,'ID不能为空']; $bool = MonthlyDwOrder::where('top_depart_id', $data['top_depart_id']) ->where('month', $data['month']) ->where('id','<>',$data['id']) ->where('del_time',0) ->exists(); } if($bool) return [false, date("Y-m", $data['month']) . '已存在设备月度研发工时单']; return [true, '']; } public function fillData($data){ if(empty($data['data'])) return $data; $emp = (new EmployeeService())->getEmployeeMap(array_unique(array_column($data['data'],'crt_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']] ?? ''; } return $data; } public function fillDataForExport($data, $column, &$return) { if(empty($data)) return; $mainIds = array_column($data, 'id'); // 获取详情映射 [main_id => [details...]] $detailsMap = $this->getDetailsMap($mainIds); // 默认空行模板 $defaultRow = array_fill_keys($column, ''); foreach ($data as $main) { $mainId = $main['id']; $details = $detailsMap[$mainId] ?? []; // 提取主表信息 $mainInfo = [ 'code' => $main['code'], 'month' => $main['month'] ? date('Y-m', $main['month']) : '', ]; if (empty($details)) { // 如果没有详情,至少导出一行主表信息(可选) $return[] = array_merge($defaultRow, $mainInfo); } else { // 核心:遍历详情,每一行详情都合并主表信息 foreach ($details as $sub) { // 合并主表字段 + 详情字段 $fullRow = array_merge($mainInfo, $sub); // 过滤掉不在导出列里的字段,并补充缺失列 $return[] = array_merge($defaultRow, array_intersect_key($fullRow, $defaultRow)); } } } } public function getDetailsMap($main_ids) { // 获取详情 $details = MonthlyDwOrderDetails::where('del_time', 0) ->whereIn('main_id', $main_ids) ->get(); // 获取设备信息 $empIds = $details->pluck('device_id')->unique(); $empMap = Device::whereIn('id', $empIds)->get()->keyBy('id'); $res = []; foreach ($details as $item) { $tmpEmp = $empMap[$item->device_id] ?? null; // 组装每一行详情需要展示的字段 $res[$item->main_id][] = [ 'device_code' => $tmpEmp ? $tmpEmp->code : '', 'total_days' => $item->total_days, 'rd_total_days' => $item->rd_total_days, 'total_hours' => $item->total_hours, 'rd_total_hours' => $item->rd_total_hours, 'start_time' => $item->start_time ? date("Y-m-d", $item->start_time) : '', 'end_time' => $item->end_time ? date("Y-m-d", $item->end_time) : '', ]; } return $res; // 返回 [main_id => [detail_row, detail_row]] } }