@@ -1,8 +1,7 @@
|
||||
package com.yunhai.dormnotice.common.listener;
|
||||
|
||||
import com.yunhai.dormnotice.entity.BaseEntity;
|
||||
import com.yunhai.platform.auth.LoginUser;
|
||||
import com.yunhai.platform.auth.UserContextHolder;
|
||||
import com.yunhai.dormnotice.utils.UserContext;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import javax.persistence.PrePersist;
|
||||
@@ -10,9 +9,10 @@ import javax.persistence.PreUpdate;
|
||||
import java.time.LocalDateTime;
|
||||
|
||||
/**
|
||||
* JPA 审计监听器:在实体持久化/更新前自动填充创建人/更新人(平台当前登录人)与时间戳。
|
||||
* 对应 {@link BaseEntity} 的 createBy / updateBy / createTime / updateTime 字段。
|
||||
* 当前登录人取自基础平台 {@link UserContextHolder}。
|
||||
* JPA 审计监听器:实体持久化/更新前自动填充创建人/更新人与时间戳。
|
||||
* <p>决策 D4:{@code create_by / update_by} 存**当前登录用户 ID**(数字 id),不再存登录账号。
|
||||
* id 取自 {@link UserContext#getUserId()}(由 ControllerUserContextAspect 在请求线程填充);
|
||||
* 无登录上下文(启动种子/系统任务)时为 null,保持原值。</p>
|
||||
*/
|
||||
@Component
|
||||
public class NoticeAuditListener {
|
||||
@@ -22,26 +22,26 @@ public class NoticeAuditListener {
|
||||
LocalDateTime now = LocalDateTime.now();
|
||||
entity.setCreateTime(now);
|
||||
entity.setUpdateTime(now);
|
||||
String account = currentAccount();
|
||||
if (account != null) {
|
||||
Integer uid = currentUserId();
|
||||
if (uid != null) {
|
||||
if (entity.getCreateBy() == null) {
|
||||
entity.setCreateBy(account);
|
||||
entity.setCreateBy(uid);
|
||||
}
|
||||
entity.setUpdateBy(account);
|
||||
entity.setUpdateBy(uid);
|
||||
}
|
||||
}
|
||||
|
||||
@PreUpdate
|
||||
public void preUpdate(BaseEntity entity) {
|
||||
entity.setUpdateTime(LocalDateTime.now());
|
||||
String account = currentAccount();
|
||||
if (account != null) {
|
||||
entity.setUpdateBy(account);
|
||||
Integer uid = currentUserId();
|
||||
if (uid != null) {
|
||||
entity.setUpdateBy(uid);
|
||||
}
|
||||
}
|
||||
|
||||
private String currentAccount() {
|
||||
LoginUser user = UserContextHolder.get();
|
||||
return user == null ? null : user.getLoginName();
|
||||
/** 当前登录用户 ID(数字);无上下文返回 null。 */
|
||||
private Integer currentUserId() {
|
||||
return UserContext.getUserId();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -42,26 +42,31 @@ public class ReadBackfillRunner implements ApplicationRunner {
|
||||
if (!enabled) {
|
||||
return;
|
||||
}
|
||||
List<Receipt> acted = receiptRepository.findByStatusIn(java.util.Arrays.asList("confirmed", "filled"));
|
||||
int added = 0;
|
||||
for (Receipt r : acted) {
|
||||
if (r.getNoticeId() == null || r.getStudentId() == null) {
|
||||
continue;
|
||||
try {
|
||||
List<Receipt> acted = receiptRepository.findByStatusIn(java.util.Arrays.asList("confirmed", "filled"));
|
||||
int added = 0;
|
||||
for (Receipt r : acted) {
|
||||
if (r.getNoticeId() == null || r.getStudentId() == null) {
|
||||
continue;
|
||||
}
|
||||
if (readRepository.existsByNoticeIdAndStudentId(r.getNoticeId(), r.getStudentId())) {
|
||||
continue;
|
||||
}
|
||||
DormNoticeRead rd = new DormNoticeRead();
|
||||
rd.setNoticeId(r.getNoticeId());
|
||||
rd.setStudentId(r.getStudentId());
|
||||
rd.setRoomId(r.getRoomId());
|
||||
rd.setReadAt(r.getSubmittedAt() != null ? r.getSubmittedAt() : LocalDateTime.now());
|
||||
rd.setSchId(r.getSchId());
|
||||
readRepository.save(rd);
|
||||
added++;
|
||||
}
|
||||
if (readRepository.existsByNoticeIdAndStudentId(r.getNoticeId(), r.getStudentId())) {
|
||||
continue;
|
||||
if (added > 0) {
|
||||
log.info("[ReadBackfill] 依据已确认/已填报回执回填阅读记录 {} 条", added);
|
||||
}
|
||||
DormNoticeRead rd = new DormNoticeRead();
|
||||
rd.setNoticeId(r.getNoticeId());
|
||||
rd.setStudentId(r.getStudentId());
|
||||
rd.setRoomId(r.getRoomId());
|
||||
rd.setReadAt(r.getSubmittedAt() != null ? r.getSubmittedAt() : LocalDateTime.now());
|
||||
rd.setSchId(r.getSchId());
|
||||
readRepository.save(rd);
|
||||
added++;
|
||||
}
|
||||
if (added > 0) {
|
||||
log.info("[ReadBackfill] 依据已确认/已填报回执回填阅读记录 {} 条", added);
|
||||
} catch (Exception e) {
|
||||
// 回填失败不得阻断启动(如列类型未迁移等)
|
||||
log.warn("[ReadBackfill] 阅读记录回填跳过:{}", e.getMessage());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -24,10 +24,10 @@ public abstract class BaseEntity implements Serializable {
|
||||
private Integer id;
|
||||
|
||||
/**
|
||||
* 创建人登录账号
|
||||
* 创建人用户ID(数字;决策 D4,不存登录账号)
|
||||
*/
|
||||
@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(数字;决策 D4,不存登录账号)
|
||||
*/
|
||||
@Column(name = "update_by")
|
||||
private String updateBy;
|
||||
private Integer updateBy;
|
||||
|
||||
/**
|
||||
* 更新时间
|
||||
|
||||
Reference in New Issue
Block a user