|
|
|
@ -0,0 +1,353 @@
|
|
|
|
|
|
|
|
package com.sztzjy.blockchainsmartclaim.service.impl;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import cn.hutool.crypto.digest.DigestUtil;
|
|
|
|
|
|
|
|
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
|
|
|
|
|
|
|
import com.sztzjy.blockchainsmartclaim.entity.training.TrainingRecord;
|
|
|
|
|
|
|
|
import com.sztzjy.blockchainsmartclaim.entity.training.TrainingScore;
|
|
|
|
|
|
|
|
import com.sztzjy.blockchainsmartclaim.mapper.TrainingRecordMapper;
|
|
|
|
|
|
|
|
import com.sztzjy.blockchainsmartclaim.mapper.TrainingScoreMapper;
|
|
|
|
|
|
|
|
import com.sztzjy.blockchainsmartclaim.service.TrainingRecordService;
|
|
|
|
|
|
|
|
import lombok.RequiredArgsConstructor;
|
|
|
|
|
|
|
|
import org.springframework.stereotype.Service;
|
|
|
|
|
|
|
|
import org.springframework.util.StringUtils;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import java.time.format.DateTimeFormatter;
|
|
|
|
|
|
|
|
import java.util.ArrayList;
|
|
|
|
|
|
|
|
import java.util.Arrays;
|
|
|
|
|
|
|
|
import java.util.HashMap;
|
|
|
|
|
|
|
|
import java.util.HashSet;
|
|
|
|
|
|
|
|
import java.util.LinkedHashMap;
|
|
|
|
|
|
|
|
import java.util.List;
|
|
|
|
|
|
|
|
import java.util.Map;
|
|
|
|
|
|
|
|
import java.util.Set;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Service
|
|
|
|
|
|
|
|
@RequiredArgsConstructor
|
|
|
|
|
|
|
|
public class TrainingRecordServiceImpl implements TrainingRecordService {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** 时间格式化 */
|
|
|
|
|
|
|
|
private static final DateTimeFormatter TIME_FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
|
|
* 任务字典:仅保留「模块二 · 核保」「模块四 · 续保」两个模块,共 5 个任务。
|
|
|
|
|
|
|
|
* key = 模块名(前端展示用),value = 该模块下的任务名列表(与前端保存时传的 task 值保持一致)。
|
|
|
|
|
|
|
|
* 进度 = 已完成任务数 / 字典内任务总数 × 100。
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
private static final Map<String, List<String>> TASK_DICTIONARY = new LinkedHashMap<String, List<String>>() {{
|
|
|
|
|
|
|
|
put("核保", Arrays.asList("单证核验", "风险评估", "出具核保结论"));
|
|
|
|
|
|
|
|
put("续保", Arrays.asList("续保资格评估(4个客户)", "速达续保方案"));
|
|
|
|
|
|
|
|
}};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** 需要展示的任务全集:任务名 → 所属模块名(任务名全局唯一,作为匹配键) */
|
|
|
|
|
|
|
|
private static final Map<String, String> TASK_MODULE_MAP = new LinkedHashMap<String, String>() {{
|
|
|
|
|
|
|
|
TASK_DICTIONARY.forEach((moduleName, tasks) ->
|
|
|
|
|
|
|
|
tasks.forEach(task -> put(task, moduleName)));
|
|
|
|
|
|
|
|
}};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** 总任务数:字典内任务总数(当前 5 个),全部完成 = 100%。 */
|
|
|
|
|
|
|
|
private static final int TOTAL_TASK_COUNT = TASK_MODULE_MAP.size();
|
|
|
|
|
|
|
|
/** 每个任务满分 100;总满分 = 任务数 × 100(当前 500)。 */
|
|
|
|
|
|
|
|
private static final int TOTAL_SCORE = TOTAL_TASK_COUNT * 100;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** 创世前区块哈希:首条存证没有上一块,用 64 个 0。 */
|
|
|
|
|
|
|
|
private static final String GENESIS_PREV_HASH = "0000000000000000000000000000000000000000000000000000000000000000";
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private final TrainingRecordMapper trainingRecordMapper;
|
|
|
|
|
|
|
|
private final TrainingScoreMapper trainingScoreMapper;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
|
|
|
public TrainingRecord save(String userId, String bigModule, String module, String task,
|
|
|
|
|
|
|
|
Integer score, Integer passedPointCount, String dataJson) {
|
|
|
|
|
|
|
|
// 先取链上最新区块哈希作为本块的前区块哈希(每次提交都会产生新区块)
|
|
|
|
|
|
|
|
String prevHash = findLatestBlockHash(userId);
|
|
|
|
|
|
|
|
String blockHash = buildBlockHash(bigModule, module, task, userId, dataJson, prevHash);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
TrainingRecord existing = findRecord(userId, bigModule, module, task);
|
|
|
|
|
|
|
|
if (existing != null) {
|
|
|
|
|
|
|
|
// 同一任务重复提交:修改原记录(不新增行),成绩保留历史最高分,数据取最新一次
|
|
|
|
|
|
|
|
int previousScore = existing.getScore() == null ? 0 : existing.getScore();
|
|
|
|
|
|
|
|
existing.setScore(Math.max(previousScore, score));
|
|
|
|
|
|
|
|
// 作答点通过数只增不减,取历史最大值;该字段不参与成绩表计算
|
|
|
|
|
|
|
|
int previousPassed = existing.getPassedPointCount() == null ? 0 : existing.getPassedPointCount();
|
|
|
|
|
|
|
|
existing.setPassedPointCount(Math.max(previousPassed, passedPointCount == null ? 0 : passedPointCount));
|
|
|
|
|
|
|
|
existing.setDataJson(dataJson);
|
|
|
|
|
|
|
|
// 每次更新都重算并覆盖区块哈希(新区块基于当前链尾)
|
|
|
|
|
|
|
|
existing.setBlockHash(blockHash);
|
|
|
|
|
|
|
|
trainingRecordMapper.updateById(existing);
|
|
|
|
|
|
|
|
refreshScore(userId);
|
|
|
|
|
|
|
|
return existing;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
TrainingRecord record = new TrainingRecord();
|
|
|
|
|
|
|
|
record.setUserId(userId);
|
|
|
|
|
|
|
|
record.setBigModule(bigModule);
|
|
|
|
|
|
|
|
record.setModule(module);
|
|
|
|
|
|
|
|
record.setTask(task);
|
|
|
|
|
|
|
|
record.setScore(score);
|
|
|
|
|
|
|
|
record.setPassedPointCount(passedPointCount);
|
|
|
|
|
|
|
|
record.setBlockHash(blockHash);
|
|
|
|
|
|
|
|
record.setDataJson(dataJson);
|
|
|
|
|
|
|
|
record.setIsDel(0);
|
|
|
|
|
|
|
|
trainingRecordMapper.insert(record);
|
|
|
|
|
|
|
|
refreshScore(userId);
|
|
|
|
|
|
|
|
return record;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
|
|
|
public TrainingRecord get(String userId, String bigModule, String module, String task) {
|
|
|
|
|
|
|
|
return findRecord(userId, bigModule, module, task);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
|
|
|
public TrainingScore delete(String userId, String bigModule, String module, String task) {
|
|
|
|
|
|
|
|
LambdaQueryWrapper<TrainingRecord> wrapper = new LambdaQueryWrapper<TrainingRecord>()
|
|
|
|
|
|
|
|
.eq(TrainingRecord::getUserId, userId)
|
|
|
|
|
|
|
|
.eq(TrainingRecord::getBigModule, bigModule)
|
|
|
|
|
|
|
|
.eq(TrainingRecord::getModule, module);
|
|
|
|
|
|
|
|
if (StringUtils.hasText(task)) {
|
|
|
|
|
|
|
|
// 只重做单个任务
|
|
|
|
|
|
|
|
wrapper.eq(TrainingRecord::getTask, task);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
// @TableLogic:delete 实际执行 update ... set is_del = 1,保留审计痕迹
|
|
|
|
|
|
|
|
trainingRecordMapper.delete(wrapper);
|
|
|
|
|
|
|
|
refreshScore(userId);
|
|
|
|
|
|
|
|
return findScore(userId, module);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
|
|
|
public TrainingSummary summary(String userId, String module) {
|
|
|
|
|
|
|
|
TrainingSummary summary = new TrainingSummary();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 该模块的成绩行(可能被逻辑删除 / 不存在)
|
|
|
|
|
|
|
|
TrainingScore scoreRow = findScore(userId, module);
|
|
|
|
|
|
|
|
summary.setCurrentScore(scoreRow == null || scoreRow.getCurrentScore() == null ? 0 : scoreRow.getCurrentScore());
|
|
|
|
|
|
|
|
summary.setTotalScore(TOTAL_SCORE);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 完成进度:按字典内 5 个任务计算(成绩行里存的也是这个值)
|
|
|
|
|
|
|
|
List<TrainingRecord> validRecords = findValidRecords(userId);
|
|
|
|
|
|
|
|
int completed = countCompletedTasks(validRecords);
|
|
|
|
|
|
|
|
summary.setProgress(Math.min(completed * 100 / TOTAL_TASK_COUNT, 100));
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 作答点通过数合计:该模块数据表累加得到
|
|
|
|
|
|
|
|
int passedTotal = 0;
|
|
|
|
|
|
|
|
for (TrainingRecord record : validRecords) {
|
|
|
|
|
|
|
|
if (module.equals(record.getModule()) && record.getPassedPointCount() != null) {
|
|
|
|
|
|
|
|
passedTotal += record.getPassedPointCount();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
summary.setPassedPointTotal(passedTotal);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 实训成绩记录时间:成绩表的创建时间
|
|
|
|
|
|
|
|
summary.setScoreTime(scoreRow == null || scoreRow.getCreateTime() == null
|
|
|
|
|
|
|
|
? null : TIME_FORMATTER.format(scoreRow.getCreateTime()));
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 任务完成情况列表:核保/续保 2 模块 5 个任务全量返回(未做过的「未通过」、0 分)
|
|
|
|
|
|
|
|
summary.setTasks(buildTaskStatusList(validRecords));
|
|
|
|
|
|
|
|
return summary;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
|
|
|
public List<ChainEntry> chain(String userId, String bigModule, String module, String operator) {
|
|
|
|
|
|
|
|
// 升序遍历以便推算前区块哈希(每条的前块 = 时间上紧邻的上一条当前哈希)
|
|
|
|
|
|
|
|
LambdaQueryWrapper<TrainingRecord> wrapper = new LambdaQueryWrapper<TrainingRecord>()
|
|
|
|
|
|
|
|
.eq(TrainingRecord::getUserId, userId)
|
|
|
|
|
|
|
|
.isNotNull(TrainingRecord::getBlockHash)
|
|
|
|
|
|
|
|
.orderByAsc(TrainingRecord::getUpdateTime);
|
|
|
|
|
|
|
|
if (StringUtils.hasText(bigModule)) {
|
|
|
|
|
|
|
|
wrapper.eq(TrainingRecord::getBigModule, bigModule);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
if (StringUtils.hasText(module)) {
|
|
|
|
|
|
|
|
wrapper.eq(TrainingRecord::getModule, module);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
List<TrainingRecord> ascending = trainingRecordMapper.selectList(wrapper);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
List<ChainEntry> entries = new ArrayList<>();
|
|
|
|
|
|
|
|
String prevHash = GENESIS_PREV_HASH;
|
|
|
|
|
|
|
|
for (TrainingRecord record : ascending) {
|
|
|
|
|
|
|
|
ChainEntry entry = new ChainEntry();
|
|
|
|
|
|
|
|
entry.setBigModule(record.getBigModule());
|
|
|
|
|
|
|
|
entry.setModule(record.getModule());
|
|
|
|
|
|
|
|
entry.setTask(record.getTask());
|
|
|
|
|
|
|
|
entry.setAction("实训任务提交:" + record.getBigModule() + "/" + record.getModule() + "/" + record.getTask());
|
|
|
|
|
|
|
|
entry.setOperator(operator);
|
|
|
|
|
|
|
|
entry.setData(record.getDataJson());
|
|
|
|
|
|
|
|
entry.setPrevBlockHash(prevHash);
|
|
|
|
|
|
|
|
entry.setBlockHash(record.getBlockHash());
|
|
|
|
|
|
|
|
entry.setBlockTime(record.getUpdateTime() == null ? null : TIME_FORMATTER.format(record.getUpdateTime()));
|
|
|
|
|
|
|
|
entries.add(entry);
|
|
|
|
|
|
|
|
prevHash = record.getBlockHash();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
// 按修改时间降序输出(最新区块在前)
|
|
|
|
|
|
|
|
java.util.Collections.reverse(entries);
|
|
|
|
|
|
|
|
return entries;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
|
|
|
public TrainingScore currentScore(String userId, String module) {
|
|
|
|
|
|
|
|
TrainingScore score = findScore(userId, module);
|
|
|
|
|
|
|
|
if (score != null) {
|
|
|
|
|
|
|
|
return score;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
TrainingScore empty = new TrainingScore();
|
|
|
|
|
|
|
|
empty.setUserId(userId);
|
|
|
|
|
|
|
|
empty.setModule(module);
|
|
|
|
|
|
|
|
empty.setCurrentScore(0);
|
|
|
|
|
|
|
|
empty.setTotalScore(TOTAL_SCORE);
|
|
|
|
|
|
|
|
empty.setProgress(countCompletedTasks(findValidRecords(userId)) * 100 / TOTAL_TASK_COUNT);
|
|
|
|
|
|
|
|
empty.setIsDel(0);
|
|
|
|
|
|
|
|
return empty;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
|
|
* 取该用户链上最新区块的哈希(按更新时间倒序的第一条有效记录);
|
|
|
|
|
|
|
|
* 没有任何记录时返回创世前区块哈希(64 个 0)。
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
private String findLatestBlockHash(String userId) {
|
|
|
|
|
|
|
|
TrainingRecord latest = trainingRecordMapper.selectOne(new LambdaQueryWrapper<TrainingRecord>()
|
|
|
|
|
|
|
|
.eq(TrainingRecord::getUserId, userId)
|
|
|
|
|
|
|
|
.isNotNull(TrainingRecord::getBlockHash)
|
|
|
|
|
|
|
|
.orderByDesc(TrainingRecord::getUpdateTime)
|
|
|
|
|
|
|
|
.last("limit 1"));
|
|
|
|
|
|
|
|
return latest == null || latest.getBlockHash() == null ? GENESIS_PREV_HASH : latest.getBlockHash();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
|
|
* 生成区块哈希:SHA-256(动作 + 经办人 + 数据 + 时间戳 + 前区块哈希)。
|
|
|
|
|
|
|
|
* 动作 = 「实训任务提交:大模块/模块/任务」;经办人 = token 中的 userId;数据 = dataJson。
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
private String buildBlockHash(String bigModule, String module, String task, String userId,
|
|
|
|
|
|
|
|
String dataJson, String prevHash) {
|
|
|
|
|
|
|
|
String action = "实训任务提交:" + bigModule + "/" + module + "/" + task;
|
|
|
|
|
|
|
|
String timestamp = TIME_FORMATTER.format(java.time.LocalDateTime.now());
|
|
|
|
|
|
|
|
String input = action + userId + dataJson + timestamp + prevHash;
|
|
|
|
|
|
|
|
return DigestUtil.sha256Hex(input);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private TrainingRecord findRecord(String userId, String bigModule, String module, String task) { return trainingRecordMapper.selectOne(new LambdaQueryWrapper<TrainingRecord>()
|
|
|
|
|
|
|
|
.eq(TrainingRecord::getUserId, userId)
|
|
|
|
|
|
|
|
.eq(TrainingRecord::getBigModule, bigModule)
|
|
|
|
|
|
|
|
.eq(TrainingRecord::getModule, module)
|
|
|
|
|
|
|
|
.eq(TrainingRecord::getTask, task)
|
|
|
|
|
|
|
|
.orderByDesc(TrainingRecord::getCreateTime)
|
|
|
|
|
|
|
|
.last("limit 1"));
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private List<TrainingRecord> findValidRecords(String userId) {
|
|
|
|
|
|
|
|
// @TableLogic 自动过滤 is_del = 1
|
|
|
|
|
|
|
|
return trainingRecordMapper.selectList(new LambdaQueryWrapper<TrainingRecord>()
|
|
|
|
|
|
|
|
.eq(TrainingRecord::getUserId, userId)
|
|
|
|
|
|
|
|
.isNotNull(TrainingRecord::getScore));
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private TrainingScore findScore(String userId, String module) {
|
|
|
|
|
|
|
|
// 成绩行的 is_del 为手工管理,这里只取正常行
|
|
|
|
|
|
|
|
return trainingScoreMapper.selectOne(new LambdaQueryWrapper<TrainingScore>()
|
|
|
|
|
|
|
|
.eq(TrainingScore::getUserId, userId)
|
|
|
|
|
|
|
|
.eq(TrainingScore::getModule, module)
|
|
|
|
|
|
|
|
.eq(TrainingScore::getIsDel, 0)
|
|
|
|
|
|
|
|
.last("limit 1"));
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
|
|
* 统计已完成的任务数:只统计字典内(需展示)的任务,按任务名去重。
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
private int countCompletedTasks(List<TrainingRecord> validRecords) {
|
|
|
|
|
|
|
|
Set<String> completed = new HashSet<>();
|
|
|
|
|
|
|
|
for (TrainingRecord record : validRecords) {
|
|
|
|
|
|
|
|
if (TASK_MODULE_MAP.containsKey(record.getTask())) {
|
|
|
|
|
|
|
|
completed.add(record.getTask());
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return completed.size();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
|
|
* 构建需展示任务的完成情况列表(按任务字典顺序,未做过的也返回):
|
|
|
|
|
|
|
|
* 已完成的标「通过」并带得分,未做过的标「未通过」、分数 0。
|
|
|
|
|
|
|
|
* 按任务名匹配(任务名全局唯一),不依赖前端 bigModule/module 的取值约定。
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
private List<TaskStatus> buildTaskStatusList(List<TrainingRecord> validRecords) {
|
|
|
|
|
|
|
|
// 任务名 -> 得分(同任务多条时取最高)
|
|
|
|
|
|
|
|
Map<String, Integer> scoreByTask = new HashMap<>();
|
|
|
|
|
|
|
|
for (TrainingRecord record : validRecords) {
|
|
|
|
|
|
|
|
int score = record.getScore() == null ? 0 : record.getScore();
|
|
|
|
|
|
|
|
scoreByTask.merge(record.getTask(), score, Math::max);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
List<TaskStatus> tasks = new ArrayList<>();
|
|
|
|
|
|
|
|
for (Map.Entry<String, List<String>> entry : TASK_DICTIONARY.entrySet()) {
|
|
|
|
|
|
|
|
String moduleName = entry.getKey();
|
|
|
|
|
|
|
|
for (String taskName : entry.getValue()) {
|
|
|
|
|
|
|
|
Integer score = scoreByTask.get(taskName);
|
|
|
|
|
|
|
|
TaskStatus status = new TaskStatus();
|
|
|
|
|
|
|
|
status.setModule(moduleName);
|
|
|
|
|
|
|
|
status.setTask(taskName);
|
|
|
|
|
|
|
|
status.setPassed(score != null);
|
|
|
|
|
|
|
|
// 未做题也返回 0 分(不返回 null,便于前端直接展示)
|
|
|
|
|
|
|
|
status.setScore(score == null ? 0 : score);
|
|
|
|
|
|
|
|
status.setChainAction("—");
|
|
|
|
|
|
|
|
tasks.add(status);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return tasks;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
|
|
* 按有效记录重算成绩表(按模块一行):
|
|
|
|
|
|
|
|
* - progress:全局口径,字典内任务完成数 / 任务总数 × 100,封顶 100;
|
|
|
|
|
|
|
|
* - current_score:该模块下各任务得分之和(封顶总满分);
|
|
|
|
|
|
|
|
* - 该模块下已无有效任务时,对应成绩行逻辑删除。
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
private void refreshScore(String userId) {
|
|
|
|
|
|
|
|
List<TrainingRecord> validRecords = findValidRecords(userId);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 全局进度
|
|
|
|
|
|
|
|
int progress = Math.min(countCompletedTasks(validRecords) * 100 / TOTAL_TASK_COUNT, 100);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 按模块分组求和
|
|
|
|
|
|
|
|
Map<String, Integer> scoreByModule = new HashMap<>();
|
|
|
|
|
|
|
|
for (TrainingRecord record : validRecords) {
|
|
|
|
|
|
|
|
String key = record.getModule();
|
|
|
|
|
|
|
|
scoreByModule.merge(key, record.getScore() == null ? 0 : record.getScore(), Integer::sum);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 已有的成绩行(含被逻辑删除的,便于同唯一键恢复)
|
|
|
|
|
|
|
|
List<TrainingScore> allRows = trainingScoreMapper.selectList(
|
|
|
|
|
|
|
|
new LambdaQueryWrapper<TrainingScore>()
|
|
|
|
|
|
|
|
.eq(TrainingScore::getUserId, userId));
|
|
|
|
|
|
|
|
Map<String, TrainingScore> rowByModule = new HashMap<>();
|
|
|
|
|
|
|
|
for (TrainingScore row : allRows) {
|
|
|
|
|
|
|
|
rowByModule.put(row.getModule(), row);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 更新 / 恢复有任务记录的模块行
|
|
|
|
|
|
|
|
for (Map.Entry<String, Integer> entry : scoreByModule.entrySet()) {
|
|
|
|
|
|
|
|
String module = entry.getKey();
|
|
|
|
|
|
|
|
int moduleScore = Math.min(entry.getValue(), TOTAL_SCORE);
|
|
|
|
|
|
|
|
TrainingScore row = rowByModule.get(module);
|
|
|
|
|
|
|
|
if (row == null) {
|
|
|
|
|
|
|
|
row = new TrainingScore();
|
|
|
|
|
|
|
|
row.setUserId(userId);
|
|
|
|
|
|
|
|
row.setModule(module);
|
|
|
|
|
|
|
|
row.setCurrentScore(moduleScore);
|
|
|
|
|
|
|
|
row.setTotalScore(TOTAL_SCORE);
|
|
|
|
|
|
|
|
row.setProgress(progress);
|
|
|
|
|
|
|
|
row.setIsDel(0);
|
|
|
|
|
|
|
|
trainingScoreMapper.insert(row);
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
row.setCurrentScore(moduleScore);
|
|
|
|
|
|
|
|
row.setTotalScore(TOTAL_SCORE);
|
|
|
|
|
|
|
|
row.setProgress(progress);
|
|
|
|
|
|
|
|
row.setIsDel(0);
|
|
|
|
|
|
|
|
trainingScoreMapper.updateById(row);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 无有效任务的模块行:逻辑删除(手工 set is_del = 1)
|
|
|
|
|
|
|
|
for (TrainingScore row : allRows) {
|
|
|
|
|
|
|
|
if (!scoreByModule.containsKey(row.getModule()) && (row.getIsDel() == null || row.getIsDel() == 0)) {
|
|
|
|
|
|
|
|
row.setIsDel(1);
|
|
|
|
|
|
|
|
trainingScoreMapper.updateById(row);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|