@@ -1,8 +1,8 @@
|
||||
package com.yunhai.campusnotice.common.listener;
|
||||
|
||||
import com.yunhai.campusnotice.entity.BaseEntity;
|
||||
import com.yunhai.platform.auth.LoginUser;
|
||||
import com.yunhai.platform.auth.UserContextHolder;
|
||||
import com.yunhai.platform.auth.spring.CurrentBoaUser;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import javax.persistence.PrePersist;
|
||||
@@ -10,20 +10,26 @@ import javax.persistence.PreUpdate;
|
||||
import java.time.LocalDateTime;
|
||||
|
||||
/**
|
||||
* JPA 审计监听器:在实体持久化/更新前自动填充创建人/更新人(平台当前登录人)与时间戳。
|
||||
* JPA 审计监听器:在实体持久化/更新前自动填充创建人/更新人与时间戳。
|
||||
* 对应 {@link BaseEntity} 的 createBy / updateBy / createTime / updateTime 字段。
|
||||
* 创建人/更新人存平台用户ID({@link LoginUser#getUserId()}),不再存登录账号;
|
||||
* 无登录态或登录态无 userId 时留空(不做账号兜底,避免同列混存两种口径)。
|
||||
* 创建人/更新人存用户中心数字用户ID({@link BoaUser#getId()},经 {@link CurrentBoaUser#getUserId()}),
|
||||
* 不存登录账号;无登录态(定时任务等)或未匹配到用户表时留空,不做账号兜底,
|
||||
* 避免同列混存两种口径。
|
||||
* <p>注意:平台 CAS 登录的 {@code LoginUser.userId} 实际填的是登录账号(非数字),
|
||||
* 故此处不能取 UserContextHolder,必须走 {@link CurrentBoaUser}。
|
||||
*/
|
||||
@Component
|
||||
public class NoticeAuditListener {
|
||||
|
||||
@Autowired
|
||||
private CurrentBoaUser currentBoaUser;
|
||||
|
||||
@PrePersist
|
||||
public void prePersist(BaseEntity entity) {
|
||||
LocalDateTime now = LocalDateTime.now();
|
||||
entity.setCreateTime(now);
|
||||
entity.setUpdateTime(now);
|
||||
String userId = currentUserId();
|
||||
Integer userId = currentUserId();
|
||||
if (userId != null) {
|
||||
if (entity.getCreateBy() == null) {
|
||||
entity.setCreateBy(userId);
|
||||
@@ -35,14 +41,22 @@ public class NoticeAuditListener {
|
||||
@PreUpdate
|
||||
public void preUpdate(BaseEntity entity) {
|
||||
entity.setUpdateTime(LocalDateTime.now());
|
||||
String userId = currentUserId();
|
||||
Integer userId = currentUserId();
|
||||
if (userId != null) {
|
||||
entity.setUpdateBy(userId);
|
||||
}
|
||||
}
|
||||
|
||||
private String currentUserId() {
|
||||
LoginUser user = UserContextHolder.get();
|
||||
return user == null ? null : user.getUserId();
|
||||
/** 当前登录人数字用户ID(用户中心 BoaUser.id;无登录态/未匹配用户时为 null) */
|
||||
private Integer currentUserId() {
|
||||
if (currentBoaUser == null) {
|
||||
return null;
|
||||
}
|
||||
try {
|
||||
return currentBoaUser.getUserId();
|
||||
} catch (Exception e) {
|
||||
// 无请求上下文(定时任务/启动期)时 ThreadLocal 链路可能不可用,审计留空
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -24,10 +24,10 @@ public abstract class BaseEntity implements Serializable {
|
||||
private Integer id;
|
||||
|
||||
/**
|
||||
* 创建人用户ID(平台 LoginUser.userId;历史存量数据为登录账号,口径变更见 sql/audit_by_user_id.sql)
|
||||
* 创建人用户ID(用户中心 BoaUser.id;历史存量数据为登录账号字符串,迁移见 sql/audit_by_user_id.sql)
|
||||
*/
|
||||
@Column(name = "create_by", updatable = false)
|
||||
private String createBy;
|
||||
private Integer createBy;
|
||||
|
||||
/**
|
||||
* 创建时间
|
||||
@@ -36,10 +36,10 @@ public abstract class BaseEntity implements Serializable {
|
||||
private LocalDateTime createTime;
|
||||
|
||||
/**
|
||||
* 更新人用户ID(平台 LoginUser.userId;历史存量数据为登录账号)
|
||||
* 更新人用户ID(用户中心 BoaUser.id;历史存量数据为登录账号字符串)
|
||||
*/
|
||||
@Column(name = "update_by")
|
||||
private String updateBy;
|
||||
private Integer updateBy;
|
||||
|
||||
/**
|
||||
* 更新时间
|
||||
|
||||
@@ -84,11 +84,11 @@ public class NoticeVo {
|
||||
/** 附件ID串("1,2,3",列表页下载入口使用;对齐旧 .NET UpFile 字段) */
|
||||
private String UpFile;
|
||||
|
||||
/** 创建人用户ID(审计字段直传;历史存量数据为登录账号) */
|
||||
private String createBy;
|
||||
/** 创建人用户ID(用户中心数字ID;历史存量数据为登录账号字符串) */
|
||||
private Integer createBy;
|
||||
private LocalDateTime createTime;
|
||||
/** 更新人用户ID(审计字段直传;历史存量数据为登录账号) */
|
||||
private String updateBy;
|
||||
/** 更新人用户ID(用户中心数字ID;历史存量数据为登录账号字符串) */
|
||||
private Integer updateBy;
|
||||
private LocalDateTime updateTime;
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user