|
|
@@ -184,6 +184,7 @@ public class CreditServiceImpl implements CreditService {
|
|
|
private Map<String, Object> toLedgerVO(TrainCreditLedger l) {
|
|
|
Map<String, Object> m = new HashMap<>();
|
|
|
m.put("ledgerId", l.getId());
|
|
|
+ m.put("sourceId", l.getSourceId());
|
|
|
m.put("sourceType", l.getSourceType());
|
|
|
m.put("sourceCategory", l.getSourceCategory());
|
|
|
m.put("hours", l.getHours() == null ? 0 : l.getHours()); // 分钟
|
|
|
@@ -371,26 +372,64 @@ public class CreditServiceImpl implements CreditService {
|
|
|
trainAttendanceMapper.updateById(att);
|
|
|
}
|
|
|
}
|
|
|
- // 账户累加:effective + 分类 + total
|
|
|
- TrainCreditAccount acc = ensureAccount(cur.getUserId(), Year.now().getValue());
|
|
|
+ // 账户物化:effective + 分类 + total
|
|
|
+ // ★唯一账户写回路径:applyEffective 与异议补救恢复(restoreEffective)共用 accrueAccount,
|
|
|
+ // 禁止另写一份账户物化逻辑。pending_hours 的增减由各调用方按语义显式处理,此处不触碰。
|
|
|
int h = cur.getHours() == null ? 0 : cur.getHours();
|
|
|
- int pending = acc.getPendingHours() == null ? 0 : acc.getPendingHours();
|
|
|
+ deductPending(cur.getUserId(), h);
|
|
|
+ accrueAccount(cur.getUserId(), cur.getSourceCategory(), h);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 账户物化写回(唯一路径,见 applyEffective 注释):effective + 分类分列 + total 累加 h 分钟。
|
|
|
+ * 不触碰 pending_hours(pending 累加在 ledgerPending、扣减在 object 时已完成),避免重复扣减。
|
|
|
+ */
|
|
|
+ private void accrueAccount(Long userId, String category, int h) {
|
|
|
+ TrainCreditAccount acc = ensureAccount(userId, Year.now().getValue());
|
|
|
int effective = acc.getEffectiveHours() == null ? 0 : acc.getEffectiveHours();
|
|
|
int theory = acc.getTheoryHours() == null ? 0 : acc.getTheoryHours();
|
|
|
int practice = acc.getPracticeHours() == null ? 0 : acc.getPracticeHours();
|
|
|
int total = acc.getTotalHours() == null ? 0 : acc.getTotalHours();
|
|
|
acc.setEffectiveHours(effective + h);
|
|
|
- acc.setPendingHours(Math.max(0, pending - h));
|
|
|
- if (CAT_THEORY.equals(cur.getSourceCategory())) {
|
|
|
+ if (CAT_THEORY.equals(category)) {
|
|
|
acc.setTheoryHours(theory + h);
|
|
|
- } else if (CAT_PRACTICE.equals(cur.getSourceCategory())) {
|
|
|
+ } else if (CAT_PRACTICE.equals(category)) {
|
|
|
acc.setPracticeHours(practice + h);
|
|
|
}
|
|
|
acc.setTotalHours(total + h);
|
|
|
- acc.setUpdatedAt(now);
|
|
|
+ acc.setUpdatedAt(new Date());
|
|
|
trainCreditAccountMapper.updateById(acc);
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * 异议补救恢复(单事务,经代理自注入调用):ledger objected→effective + attendance 翻转 + 账户物化。
|
|
|
+ * 与 applyEffective 的差异:object() 时已扣减 pending_hours,此处不得再扣(否则重复扣减)。
|
|
|
+ */
|
|
|
+ @Override
|
|
|
+ @Transactional(rollbackFor = Exception.class)
|
|
|
+ public void restoreEffective(TrainCreditLedger l) {
|
|
|
+ TrainCreditLedger cur = trainCreditLedgerMapper.selectById(l.getId());
|
|
|
+ if (cur == null || !ST_OBJECTED.equals(cur.getStatus())) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ Date now = new Date();
|
|
|
+ cur.setStatus(ST_EFFECTIVE);
|
|
|
+ cur.setEffectiveAt(now);
|
|
|
+ cur.setUpdatedAt(now);
|
|
|
+ trainCreditLedgerMapper.updateById(cur);
|
|
|
+ // attendance 双写翻转(objected→effective)
|
|
|
+ if (SRC_ATTENDANCE.equals(cur.getSourceType())) {
|
|
|
+ TrainAttendance att = trainAttendanceMapper.selectById(parseLong(cur.getSourceId()));
|
|
|
+ if (att != null && ST_OBJECTED.equals(att.getStatus())) {
|
|
|
+ att.setStatus(ST_EFFECTIVE);
|
|
|
+ att.setEffectiveAt(now);
|
|
|
+ att.setUpdatedAt(now);
|
|
|
+ trainAttendanceMapper.updateById(att);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ accrueAccount(cur.getUserId(), cur.getSourceCategory(), cur.getHours() == null ? 0 : cur.getHours());
|
|
|
+ }
|
|
|
+
|
|
|
@Override
|
|
|
public int rollBackObjected() {
|
|
|
List<TrainCreditLedger> list = trainCreditLedgerMapper.selectList(
|
|
|
@@ -465,4 +504,197 @@ public class CreditServiceImpl implements CreditService {
|
|
|
data.put("list", rows);
|
|
|
return Result.success(data);
|
|
|
}
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public Result<Map<String, Object>> accountList(Boolean isAdmin, Integer year, String keyword, Integer page, Integer size) {
|
|
|
+ String noAuth = roleGuard.requireAdminRole(isAdmin);
|
|
|
+ if (noAuth != null) {
|
|
|
+ return Result.error(noAuth);
|
|
|
+ }
|
|
|
+ int p = page == null || page < 1 ? 1 : page;
|
|
|
+ int s = size == null || size < 1 ? 20 : Math.min(size, 100);
|
|
|
+ int offset = (p - 1) * s;
|
|
|
+ LambdaQueryWrapper<TrainCreditAccount> q = new LambdaQueryWrapper<>();
|
|
|
+ if (year != null) {
|
|
|
+ q.eq(TrainCreditAccount::getYear, year);
|
|
|
+ }
|
|
|
+ if (keyword != null && !keyword.trim().isEmpty()) {
|
|
|
+ q.like(TrainCreditAccount::getUserId, keyword.trim());
|
|
|
+ }
|
|
|
+ Long total = trainCreditAccountMapper.selectCount(q);
|
|
|
+ List<TrainCreditAccount> list = trainCreditAccountMapper.selectList(
|
|
|
+ q.orderByDesc(TrainCreditAccount::getId).last("LIMIT " + offset + "," + s));
|
|
|
+ // 一次性取本页 user 的最近生效时间(聚合无单一生效时间,取 max),避免 N+1
|
|
|
+ Map<String, Date> effMap = latestEffectiveAt(list);
|
|
|
+ List<Map<String, Object>> rows = new ArrayList<>();
|
|
|
+ for (TrainCreditAccount a : list) {
|
|
|
+ rows.add(accountVO(a, effMap.get(a.getUserId() + ":" + a.getYear())));
|
|
|
+ }
|
|
|
+ Map<String, Object> data = new HashMap<>();
|
|
|
+ data.put("total", total == null ? 0 : total);
|
|
|
+ data.put("page", p);
|
|
|
+ data.put("size", s);
|
|
|
+ data.put("list", rows);
|
|
|
+ return Result.success(data);
|
|
|
+ }
|
|
|
+
|
|
|
+ /** 一次查询取本页 user 的最近生效时间(键 userId:year) */
|
|
|
+ private Map<String, Date> latestEffectiveAt(List<TrainCreditAccount> page) {
|
|
|
+ Map<String, Date> out = new HashMap<>();
|
|
|
+ if (page == null || page.isEmpty()) {
|
|
|
+ return out;
|
|
|
+ }
|
|
|
+ List<Long> userIds = new ArrayList<>();
|
|
|
+ for (TrainCreditAccount a : page) {
|
|
|
+ if (a.getUserId() != null) {
|
|
|
+ userIds.add(a.getUserId());
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if (userIds.isEmpty()) {
|
|
|
+ return out;
|
|
|
+ }
|
|
|
+ List<TrainCreditLedger> effs = trainCreditLedgerMapper.selectList(
|
|
|
+ new LambdaQueryWrapper<TrainCreditLedger>()
|
|
|
+ .select(TrainCreditLedger::getUserId, TrainCreditLedger::getYear, TrainCreditLedger::getEffectiveAt)
|
|
|
+ .eq(TrainCreditLedger::getStatus, ST_EFFECTIVE)
|
|
|
+ .eq(TrainCreditLedger::getDeleted, 0)
|
|
|
+ .in(TrainCreditLedger::getUserId, userIds)
|
|
|
+ .last("LIMIT 5000"));
|
|
|
+ for (TrainCreditLedger l : effs) {
|
|
|
+ Date at = l.getEffectiveAt();
|
|
|
+ if (at == null) {
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ String k = l.getUserId() + ":" + l.getYear();
|
|
|
+ Date old = out.get(k);
|
|
|
+ if (old == null || at.after(old)) {
|
|
|
+ out.put(k, at);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return out;
|
|
|
+ }
|
|
|
+
|
|
|
+ /** 账户行 VO:前端实际读取 totalHours/theoryHours/practiceHours(Credits.vue),
|
|
|
+ * 同时保留短别名 total/theory/practice/effective(管理端契约命名),同值不重复计算。 */
|
|
|
+ private Map<String, Object> accountVO(TrainCreditAccount a, Date effectiveAt) {
|
|
|
+ int total = a.getTotalHours() == null ? 0 : a.getTotalHours();
|
|
|
+ int theory = a.getTheoryHours() == null ? 0 : a.getTheoryHours();
|
|
|
+ int practice = a.getPracticeHours() == null ? 0 : a.getPracticeHours();
|
|
|
+ int effective = a.getEffectiveHours() == null ? 0 : a.getEffectiveHours();
|
|
|
+ int pending = a.getPendingHours() == null ? 0 : a.getPendingHours();
|
|
|
+ Map<String, Object> m = new HashMap<>();
|
|
|
+ m.put("userId", a.getUserId());
|
|
|
+ m.put("year", a.getYear());
|
|
|
+ m.put("totalHours", total);
|
|
|
+ m.put("theoryHours", theory);
|
|
|
+ m.put("practiceHours", practice);
|
|
|
+ m.put("effectiveHours", effective);
|
|
|
+ m.put("pendingHours", pending);
|
|
|
+ m.put("effectiveAt", effectiveAt);
|
|
|
+ m.put("status", a.getStatus());
|
|
|
+ m.put("total", total);
|
|
|
+ m.put("theory", theory);
|
|
|
+ m.put("practice", practice);
|
|
|
+ m.put("effective", effective);
|
|
|
+ return m;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public Result<List<Map<String, Object>>> ledgerDetail(Boolean isAdmin, Long userId, Integer year) {
|
|
|
+ String noAuth = roleGuard.requireAdminRole(isAdmin);
|
|
|
+ if (noAuth != null) {
|
|
|
+ return Result.error(noAuth);
|
|
|
+ }
|
|
|
+ if (userId == null) {
|
|
|
+ return Result.error("userId 不能为空");
|
|
|
+ }
|
|
|
+ int y = year == null ? Year.now().getValue() : year;
|
|
|
+ List<TrainCreditLedger> list = trainCreditLedgerMapper.selectList(
|
|
|
+ new LambdaQueryWrapper<TrainCreditLedger>()
|
|
|
+ .eq(TrainCreditLedger::getUserId, userId)
|
|
|
+ .eq(TrainCreditLedger::getYear, y)
|
|
|
+ .eq(TrainCreditLedger::getDeleted, 0)
|
|
|
+ .orderByDesc(TrainCreditLedger::getId));
|
|
|
+ List<Map<String, Object>> rows = new ArrayList<>();
|
|
|
+ for (TrainCreditLedger l : list) {
|
|
|
+ rows.add(toLedgerVO(l));
|
|
|
+ }
|
|
|
+ return Result.success(rows);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public Result<Map<String, Object>> objectedList(Boolean isAdmin, Integer page, Integer size) {
|
|
|
+ String noAuth = roleGuard.requireAdminRole(isAdmin);
|
|
|
+ if (noAuth != null) {
|
|
|
+ return Result.error(noAuth);
|
|
|
+ }
|
|
|
+ int p = page == null || page < 1 ? 1 : page;
|
|
|
+ int s = size == null || size < 1 ? 20 : Math.min(size, 100);
|
|
|
+ int offset = (p - 1) * s;
|
|
|
+ Long total = trainCreditLedgerMapper.selectCount(
|
|
|
+ new LambdaQueryWrapper<TrainCreditLedger>()
|
|
|
+ .eq(TrainCreditLedger::getStatus, ST_OBJECTED)
|
|
|
+ .eq(TrainCreditLedger::getDeleted, 0));
|
|
|
+ List<TrainCreditLedger> list = trainCreditLedgerMapper.selectList(
|
|
|
+ new LambdaQueryWrapper<TrainCreditLedger>()
|
|
|
+ .eq(TrainCreditLedger::getStatus, ST_OBJECTED)
|
|
|
+ .eq(TrainCreditLedger::getDeleted, 0)
|
|
|
+ .orderByDesc(TrainCreditLedger::getId)
|
|
|
+ .last("LIMIT " + offset + "," + s));
|
|
|
+ List<Map<String, Object>> rows = new ArrayList<>();
|
|
|
+ for (TrainCreditLedger l : list) {
|
|
|
+ rows.add(toLedgerVO(l));
|
|
|
+ }
|
|
|
+ Map<String, Object> data = new HashMap<>();
|
|
|
+ data.put("total", total == null ? 0 : total);
|
|
|
+ data.put("page", p);
|
|
|
+ data.put("size", s);
|
|
|
+ data.put("list", rows);
|
|
|
+ return Result.success(data);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ @Transactional(rollbackFor = Exception.class)
|
|
|
+ public Result<Boolean> disposeObjected(Boolean isAdmin, Long operatorId, Long ledgerId, Boolean approved, String reason) {
|
|
|
+ String noAuth = roleGuard.requireAdminRole(isAdmin);
|
|
|
+ if (noAuth != null) {
|
|
|
+ return Result.error(noAuth);
|
|
|
+ }
|
|
|
+ if (ledgerId == null) {
|
|
|
+ return Result.error("id 不能为空");
|
|
|
+ }
|
|
|
+ if (approved == null) {
|
|
|
+ return Result.error("approved 不能为空");
|
|
|
+ }
|
|
|
+ TrainCreditLedger l = trainCreditLedgerMapper.selectById(ledgerId);
|
|
|
+ if (l == null || (l.getDeleted() != null && l.getDeleted() == 1)) {
|
|
|
+ return Result.error("学时记录不存在");
|
|
|
+ }
|
|
|
+ if (Boolean.TRUE.equals(approved)) {
|
|
|
+ // 驳回异议 → 恢复生效(幂等:已 effective 直接成功)
|
|
|
+ if (ST_EFFECTIVE.equals(l.getStatus())) {
|
|
|
+ return Result.success(true);
|
|
|
+ }
|
|
|
+ if (!ST_OBJECTED.equals(l.getStatus())) {
|
|
|
+ return Result.error("当前状态不允许该处置");
|
|
|
+ }
|
|
|
+ if (reason != null && !reason.trim().isEmpty()) {
|
|
|
+ l.setRemark(reason.trim());
|
|
|
+ l.setOperatorId(operatorId);
|
|
|
+ l.setUpdatedAt(new Date());
|
|
|
+ trainCreditLedgerMapper.updateById(l);
|
|
|
+ }
|
|
|
+ self.restoreEffective(l);
|
|
|
+ return Result.success(true);
|
|
|
+ }
|
|
|
+ // 否决撤销 → objected→rejected(终态),账户不增加(pending 已在 object 时扣减,沿用既有回滚路径)
|
|
|
+ if (ST_REJECTED.equals(l.getStatus())) {
|
|
|
+ return Result.success(true);
|
|
|
+ }
|
|
|
+ if (!ST_OBJECTED.equals(l.getStatus())) {
|
|
|
+ return Result.error("当前状态不允许该处置");
|
|
|
+ }
|
|
|
+ self.applyRollBack(l);
|
|
|
+ return Result.success(true);
|
|
|
+ }
|
|
|
}
|