feat: 新增统一用户名获取方法
This commit is contained in:
parent
26db274aaf
commit
bb21405761
@ -0,0 +1,14 @@
|
||||
package com.yfd.platform.annotation;
|
||||
|
||||
import java.lang.annotation.ElementType;
|
||||
import java.lang.annotation.Retention;
|
||||
import java.lang.annotation.RetentionPolicy;
|
||||
import java.lang.annotation.Target;
|
||||
|
||||
/**
|
||||
* 标记字段为用户ID字段,用于用户名称自动填充
|
||||
*/
|
||||
@Target(ElementType.FIELD)
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
public @interface UserIdField {
|
||||
}
|
||||
@ -0,0 +1,14 @@
|
||||
package com.yfd.platform.annotation;
|
||||
|
||||
import java.lang.annotation.ElementType;
|
||||
import java.lang.annotation.Retention;
|
||||
import java.lang.annotation.RetentionPolicy;
|
||||
import java.lang.annotation.Target;
|
||||
|
||||
/**
|
||||
* 标记字段为用户名字段,用于接收自动填充的用户名称
|
||||
*/
|
||||
@Target(ElementType.FIELD)
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
public @interface UserNameField {
|
||||
}
|
||||
@ -0,0 +1,190 @@
|
||||
package com.yfd.platform.common.utils;
|
||||
|
||||
import cn.hutool.core.collection.CollUtil;
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import com.yfd.platform.annotation.UserIdField;
|
||||
import com.yfd.platform.annotation.UserNameField;
|
||||
import com.yfd.platform.system.domain.SysUser;
|
||||
import com.yfd.platform.system.mapper.SysUserMapper;
|
||||
import jakarta.annotation.Resource;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.lang.reflect.Field;
|
||||
import java.util.*;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
/**
|
||||
* 用户名称自动填充工具类
|
||||
* <p>
|
||||
* 使用方式:
|
||||
* 1. 在实体类的用户ID字段上添加 @UserIdField 注解
|
||||
* 2. 在对应需要填充用户名的字段上添加 @UserNameField 注解
|
||||
* 3. 调用 {@link #fillUserNames(List)} 方法自动填充
|
||||
*
|
||||
* <pre>
|
||||
* 示例:
|
||||
* {@code
|
||||
* @Data
|
||||
* public class MyEntity {
|
||||
* @UserIdField
|
||||
* private String userId;
|
||||
*
|
||||
* @UserNameField
|
||||
* private String userName;
|
||||
* }
|
||||
*
|
||||
* // 使用
|
||||
* List<MyEntity> list = ...;
|
||||
* userNameFillHelper.fillUserNames(list);
|
||||
* }
|
||||
* </pre>
|
||||
*/
|
||||
@Component
|
||||
public class UserNameFillHelper {
|
||||
|
||||
@Resource
|
||||
private SysUserMapper sysUserMapper;
|
||||
|
||||
/**
|
||||
* 批量填充用户名称
|
||||
*
|
||||
* @param list 需要填充的实体列表
|
||||
*/
|
||||
public <T> void fillUserNames(List<T> list) {
|
||||
if (CollUtil.isEmpty(list)) {
|
||||
return;
|
||||
}
|
||||
|
||||
Class<?> clazz = list.get(0).getClass();
|
||||
Field[] fields = clazz.getDeclaredFields();
|
||||
|
||||
Field idField = null;
|
||||
Field nameField = null;
|
||||
|
||||
for (Field field : fields) {
|
||||
if (field.isAnnotationPresent(UserIdField.class)) {
|
||||
idField = field;
|
||||
idField.setAccessible(true);
|
||||
}
|
||||
if (field.isAnnotationPresent(UserNameField.class)) {
|
||||
nameField = field;
|
||||
nameField.setAccessible(true);
|
||||
}
|
||||
}
|
||||
|
||||
if (idField == null || nameField == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
Set<String> userIds = new HashSet<>();
|
||||
for (T item : list) {
|
||||
try {
|
||||
Object idValue = idField.get(item);
|
||||
if (idValue != null && StrUtil.isNotBlank(idValue.toString())) {
|
||||
userIds.add(idValue.toString());
|
||||
}
|
||||
} catch (IllegalAccessException e) {
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
if (userIds.isEmpty()) {
|
||||
return;
|
||||
}
|
||||
|
||||
List<SysUser> users = sysUserMapper.selectBatchIds(userIds);
|
||||
Map<String, String> userNameMap = users.stream()
|
||||
.collect(Collectors.toMap(SysUser::getId, SysUser::getNickname, (a, b) -> a));
|
||||
|
||||
for (T item : list) {
|
||||
try {
|
||||
Object idValue = idField.get(item);
|
||||
if (idValue != null && StrUtil.isNotBlank(idValue.toString())) {
|
||||
String userName = userNameMap.get(idValue.toString());
|
||||
nameField.set(item, StrUtil.blankToDefault(userName, "未知"));
|
||||
}
|
||||
} catch (IllegalAccessException e) {
|
||||
continue;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 批量填充用户名称(支持多个用户ID字段)
|
||||
* <p>
|
||||
* 使用方式:在实体类中定义多个 @UserIdField 和 @UserNameField 配对字段
|
||||
*
|
||||
* @param list 需要填充的实体列表
|
||||
* @param idFields 用户ID字段名数组
|
||||
* @param nameFields 用户名字段名数组
|
||||
*/
|
||||
public <T> void fillUserNames(List<T> list, String[] idFields, String[] nameFields) {
|
||||
if (CollUtil.isEmpty(list) || idFields == null || nameFields == null || idFields.length != nameFields.length) {
|
||||
return;
|
||||
}
|
||||
|
||||
Class<?> clazz = list.get(0).getClass();
|
||||
Field[] allFields = clazz.getDeclaredFields();
|
||||
Map<String, Field> fieldMap = new HashMap<>();
|
||||
for (Field field : allFields) {
|
||||
fieldMap.put(field.getName(), field);
|
||||
}
|
||||
|
||||
List<Field> idFieldList = new ArrayList<>();
|
||||
List<Field> nameFieldList = new ArrayList<>();
|
||||
for (String idFieldName : idFields) {
|
||||
Field f = fieldMap.get(idFieldName);
|
||||
if (f != null) {
|
||||
f.setAccessible(true);
|
||||
idFieldList.add(f);
|
||||
}
|
||||
}
|
||||
for (String nameFieldName : nameFields) {
|
||||
Field f = fieldMap.get(nameFieldName);
|
||||
if (f != null) {
|
||||
f.setAccessible(true);
|
||||
nameFieldList.add(f);
|
||||
}
|
||||
}
|
||||
|
||||
if (idFieldList.isEmpty() || nameFieldList.isEmpty()) {
|
||||
return;
|
||||
}
|
||||
|
||||
Set<String> userIds = new HashSet<>();
|
||||
for (T item : list) {
|
||||
for (Field idField : idFieldList) {
|
||||
try {
|
||||
Object idValue = idField.get(item);
|
||||
if (idValue != null && StrUtil.isNotBlank(idValue.toString())) {
|
||||
userIds.add(idValue.toString());
|
||||
}
|
||||
} catch (IllegalAccessException e) {
|
||||
continue;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (userIds.isEmpty()) {
|
||||
return;
|
||||
}
|
||||
|
||||
List<SysUser> users = sysUserMapper.selectBatchIds(userIds);
|
||||
Map<String, String> userNameMap = users.stream()
|
||||
.collect(Collectors.toMap(SysUser::getId, SysUser::getNickname, (a, b) -> a));
|
||||
|
||||
for (T item : list) {
|
||||
for (int i = 0; i < idFieldList.size() && i < nameFieldList.size(); i++) {
|
||||
try {
|
||||
Object idValue = idFieldList.get(i).get(item);
|
||||
if (idValue != null && StrUtil.isNotBlank(idValue.toString())) {
|
||||
String userName = userNameMap.get(idValue.toString());
|
||||
nameFieldList.get(i).set(item, StrUtil.blankToDefault(userName, "未知"));
|
||||
}
|
||||
} catch (IllegalAccessException e) {
|
||||
continue;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -2,6 +2,7 @@ package com.yfd.platform.data.controller;
|
||||
|
||||
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
||||
import com.yfd.platform.common.DataSourceRequest;
|
||||
import com.yfd.platform.common.utils.UserNameFillHelper;
|
||||
import com.yfd.platform.config.ResponseResult;
|
||||
import com.yfd.platform.data.domain.ApprovalChangeLog;
|
||||
import com.yfd.platform.data.domain.ApprovalLog;
|
||||
@ -28,6 +29,9 @@ public class ApprovalChangeLogController {
|
||||
@Resource
|
||||
private IApprovalChangeLogService approvalChangeLogService;
|
||||
|
||||
@Resource
|
||||
private UserNameFillHelper userNameFillHelper;
|
||||
|
||||
@GetMapping("/list")
|
||||
@Operation(summary = "查询变更记录列表")
|
||||
public ResponseResult list() {
|
||||
@ -39,7 +43,8 @@ public class ApprovalChangeLogController {
|
||||
@Operation(summary = "分页查询变更记录列表(通用)")
|
||||
public ResponseResult queryPageList(@RequestBody DataSourceRequest request) {
|
||||
Page<ApprovalChangeLog> approvalChangeLogPage = DataSourceRequestUtil.executeQuery(request, ApprovalChangeLog.class, approvalChangeLogService);
|
||||
approvalChangeLogService.fillUserNames(approvalChangeLogPage.getRecords());
|
||||
userNameFillHelper.fillUserNames(approvalChangeLogPage.getRecords());
|
||||
// approvalChangeLogService.fillUserNames(approvalChangeLogPage.getRecords());
|
||||
return ResponseResult.successData(approvalChangeLogPage);
|
||||
}
|
||||
|
||||
|
||||
@ -1,6 +1,8 @@
|
||||
package com.yfd.platform.data.domain;
|
||||
|
||||
import com.baomidou.mybatisplus.annotation.*;
|
||||
import com.yfd.platform.annotation.UserIdField;
|
||||
import com.yfd.platform.annotation.UserNameField;
|
||||
import lombok.Data;
|
||||
import lombok.EqualsAndHashCode;
|
||||
|
||||
@ -59,12 +61,14 @@ public class ApprovalChangeLog implements Serializable {
|
||||
/**
|
||||
* 操作人ID
|
||||
*/
|
||||
@UserIdField
|
||||
private String operatorId;
|
||||
|
||||
/**
|
||||
* 操作人名称
|
||||
*/
|
||||
@TableField(exist = false)
|
||||
@UserNameField
|
||||
private String operatorName;
|
||||
|
||||
/**
|
||||
|
||||
@ -17,7 +17,7 @@ public interface IApprovalChangeLogService extends IService<ApprovalChangeLog> {
|
||||
*/
|
||||
List<ApprovalChangeLog> getByApprovalId(String approvalId);
|
||||
|
||||
void fillUserNames(List<ApprovalChangeLog> list);
|
||||
// void fillUserNames(List<ApprovalChangeLog> list);
|
||||
|
||||
/**
|
||||
* 根据草稿数据ID查询变更记录
|
||||
|
||||
@ -29,42 +29,42 @@ public class ApprovalChangeLogServiceImpl extends ServiceImpl<ApprovalChangeLogM
|
||||
public List<ApprovalChangeLog> getByApprovalId(String approvalId) {
|
||||
return approvalChangeLogMapper.selectByApprovalId(approvalId);
|
||||
}
|
||||
@Override
|
||||
public void fillUserNames(List<ApprovalChangeLog> list) {
|
||||
if (list == null || list.isEmpty()) {
|
||||
return;
|
||||
}
|
||||
|
||||
Set<String> userIds = new HashSet<>();
|
||||
for (ApprovalChangeLog vo : list) {
|
||||
if (StrUtil.isNotBlank(vo.getApprovalId())) {
|
||||
userIds.add(vo.getApprovalId());
|
||||
}
|
||||
if (StrUtil.isNotBlank(vo.getOperatorId())) {
|
||||
userIds.add(vo.getOperatorId());
|
||||
}
|
||||
}
|
||||
|
||||
if (userIds.isEmpty()) {
|
||||
return;
|
||||
}
|
||||
|
||||
Map<String, String> userNameMap = new HashMap<>();
|
||||
List<SysUser> users = sysUserMapper.selectBatchIds(userIds);
|
||||
for (SysUser user : users) {
|
||||
userNameMap.put(user.getId(), user.getNickname());
|
||||
}
|
||||
|
||||
for (ApprovalChangeLog vo : list) {
|
||||
if (StrUtil.isNotBlank(vo.getApprovalId())) {
|
||||
vo.setApprovalName(userNameMap.get(vo.getApprovalId()));
|
||||
}
|
||||
if (StrUtil.isNotBlank(vo.getOperatorId())) {
|
||||
vo.setOperatorName(userNameMap.get(vo.getOperatorId()));
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
// @Override
|
||||
// public void fillUserNames(List<ApprovalChangeLog> list) {
|
||||
// if (list == null || list.isEmpty()) {
|
||||
// return;
|
||||
// }
|
||||
//
|
||||
// Set<String> userIds = new HashSet<>();
|
||||
// for (ApprovalChangeLog vo : list) {
|
||||
// if (StrUtil.isNotBlank(vo.getApprovalId())) {
|
||||
// userIds.add(vo.getApprovalId());
|
||||
// }
|
||||
// if (StrUtil.isNotBlank(vo.getOperatorId())) {
|
||||
// userIds.add(vo.getOperatorId());
|
||||
// }
|
||||
// }
|
||||
//
|
||||
// if (userIds.isEmpty()) {
|
||||
// return;
|
||||
// }
|
||||
//
|
||||
// Map<String, String> userNameMap = new HashMap<>();
|
||||
// List<SysUser> users = sysUserMapper.selectBatchIds(userIds);
|
||||
// for (SysUser user : users) {
|
||||
// userNameMap.put(user.getId(), user.getNickname());
|
||||
// }
|
||||
//
|
||||
// for (ApprovalChangeLog vo : list) {
|
||||
// if (StrUtil.isNotBlank(vo.getApprovalId())) {
|
||||
// vo.setApprovalName(userNameMap.get(vo.getApprovalId()));
|
||||
// }
|
||||
// if (StrUtil.isNotBlank(vo.getOperatorId())) {
|
||||
// vo.setOperatorName(userNameMap.get(vo.getOperatorId()));
|
||||
// }
|
||||
//
|
||||
// }
|
||||
// }
|
||||
@Override
|
||||
public List<ApprovalChangeLog> getByDataId(String dataId) {
|
||||
return approvalChangeLogMapper.selectByDataId(dataId);
|
||||
|
||||
Loading…
Reference in New Issue
Block a user