diff --git a/niucloud/app/service/api/apiService/PersonnelService.php b/niucloud/app/service/api/apiService/PersonnelService.php index be3833b4..e7e686d7 100644 --- a/niucloud/app/service/api/apiService/PersonnelService.php +++ b/niucloud/app/service/api/apiService/PersonnelService.php @@ -53,8 +53,14 @@ class PersonnelService extends BaseApiService } $data = $model->field($field)->find();//员工信息 - //查询部门信息 - $campus_person_role = CampusPersonRole::where('person_id',$where['id'])->select()->toArray(); + //查询用户角色校区部门信息 + $campus_person_role = CampusPersonRole::where('person_id',$where['id']) + ->with([ + 'campus' => function ($query) {}, + 'sysRole' => function ($query) {}, + 'departments' => function ($query) {} + ]) + ->select()->toArray(); $role_id = array_unique(array_column($campus_person_role, 'role_id')); $dept_id = array_unique(array_column($campus_person_role, 'dept_id')); //查询用户角色 @@ -68,10 +74,47 @@ class PersonnelService extends BaseApiService $role_name_str = implode(',',$role_name_arr);// - //查询部门 - $department_name_arr = Departments::whereIn('id',$dept_id)->column('department_name'); - $department_name_str = implode(',',$department_name_arr); + //校区部门 + // 初始化新的校区-部门合并数组 和 部门名称字符串 + $cameus_dept_arr = []; + $department_name_arr = []; + foreach ($campus_person_role as $v) { + $campusId = $v['campus_id']; + $deptId = $v['dept_id']; + $deptName = $v['dept_id_name']; + + // 收集部门名称 + $department_name_arr[] = $deptName; + + // 构建校区+部门结构 + if (!isset($cameus_dept_arr[$campusId])) { + $cameus_dept_arr[$campusId] = [ + 'campus_id' => $campusId, + 'campus_id_name' => $v['campus_id_name'], + 'dept_arr' => [] + ]; + } + + // 检查去重 + $exists = false; + foreach ($cameus_dept_arr[$campusId]['dept_arr'] as $dept) { + if ($dept['dept_id'] === $deptId) { + $exists = true; + break; + } + } + + if (!$exists) { + $cameus_dept_arr[$campusId]['dept_arr'][] = [ + 'dept_id' => $deptId, + 'dept_name' => $deptName + ]; + } + } + // 最终转换 + $cameus_dept_arr = array_values($cameus_dept_arr); + $department_name_str = implode(',', $department_name_arr); if($data){ $data = $data->toArray(); @@ -79,6 +122,7 @@ class PersonnelService extends BaseApiService $data['role_name_str'] = $role_name_str; $data['role_key_arr'] = $role_key_arr; $data['department_name_str'] = $department_name_str; + $data['cameus_dept_arr'] = $cameus_dept_arr;//校区+部门的组合 $res['code'] = 1; $res['msg'] = '操作成功';