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