package jnpf.tmsUtil;
|
|
import jnpf.tmsEntity.TmsQuizScoreEntity;
|
import jnpf.tmsEntity.TmsTaskQuizEntity;
|
import jnpf.tmsEntity.record.TmsQuizAnswerItemVO;
|
import jnpf.tmsEntity.record.TmsQuizAnswerPaperVO;
|
import jnpf.util.StringUtil;
|
|
import java.math.BigDecimal;
|
import java.util.ArrayList;
|
import java.util.Collections;
|
import java.util.HashMap;
|
import java.util.List;
|
import java.util.Locale;
|
import java.util.Map;
|
|
/**
|
* 提问人数与答卷组装。
|
*/
|
public final class TmsQuizHelper {
|
|
private TmsQuizHelper() {
|
}
|
|
/**
|
* @param signedCount 签到人数
|
* @param quizRatio 提问比例(百分比,如 10 表示 10%)
|
*/
|
public static int quizCount(int signedCount, Integer quizRatio) {
|
if (signedCount <= 0 || quizRatio == null || quizRatio <= 0) {
|
return 0;
|
}
|
return (int) Math.ceil(signedCount * quizRatio / 100.0);
|
}
|
|
/** trim + 忽略大小写全等 */
|
public static boolean answersMatch(String expect, String actual) {
|
String a = expect == null ? "" : expect.trim().toLowerCase(Locale.ROOT);
|
String b = actual == null ? "" : actual.trim().toLowerCase(Locale.ROOT);
|
return a.equals(b);
|
}
|
|
/**
|
* 组装提问答卷:学员答案来自 {@code tms_quiz_score},参考答案按题干匹配预设。
|
*/
|
public static TmsQuizAnswerPaperVO buildAnswerPaper(
|
String recordId,
|
String taskId,
|
String userId,
|
String userName,
|
String passFlag,
|
String quizTime,
|
List<TmsQuizScoreEntity> scores,
|
List<TmsTaskQuizEntity> presets) {
|
TmsQuizAnswerPaperVO paper = new TmsQuizAnswerPaperVO();
|
paper.setRecordId(recordId);
|
paper.setTaskId(taskId);
|
paper.setUserId(userId);
|
paper.setUserName(userName);
|
paper.setPassFlag(passFlag);
|
paper.setQuizTime(quizTime);
|
|
Map<String, String> refByQuestion = new HashMap<>();
|
Map<String, Integer> sortByQuestion = new HashMap<>();
|
if (presets != null) {
|
for (TmsTaskQuizEntity p : presets) {
|
if (p == null || StringUtil.isEmpty(p.getQuestion())) {
|
continue;
|
}
|
String key = normalizeQuestion(p.getQuestion());
|
refByQuestion.putIfAbsent(key, p.getAnswer());
|
if (p.getSortNo() != null) {
|
sortByQuestion.putIfAbsent(key, p.getSortNo());
|
}
|
}
|
}
|
|
List<TmsQuizAnswerItemVO> items = new ArrayList<>();
|
BigDecimal total = BigDecimal.ZERO;
|
if (scores != null) {
|
int fallbackNo = 1;
|
for (TmsQuizScoreEntity row : scores) {
|
if (row == null) {
|
continue;
|
}
|
String key = normalizeQuestion(row.getQuestion());
|
String ref = refByQuestion.get(key);
|
BigDecimal score = row.getScore() == null ? BigDecimal.ZERO : row.getScore();
|
total = total.add(score);
|
boolean correct = score.compareTo(BigDecimal.ZERO) > 0
|
|| (ref != null && answersMatch(ref, row.getAnswer()));
|
|
TmsQuizAnswerItemVO item = new TmsQuizAnswerItemVO();
|
item.setId(row.getId());
|
item.setSortNo(sortByQuestion.getOrDefault(key, fallbackNo));
|
item.setQuestion(row.getQuestion());
|
item.setUserAnswer(row.getAnswer());
|
item.setRefAnswer(ref);
|
item.setScore(score);
|
item.setCorrect(correct);
|
items.add(item);
|
fallbackNo++;
|
}
|
}
|
items.sort((a, b) -> {
|
int sa = a.getSortNo() == null ? 0 : a.getSortNo();
|
int sb = b.getSortNo() == null ? 0 : b.getSortNo();
|
return Integer.compare(sa, sb);
|
});
|
paper.setItems(items);
|
paper.setTotalScore(total);
|
return paper;
|
}
|
|
private static String normalizeQuestion(String q) {
|
return q == null ? "" : q.trim();
|
}
|
|
public static List<TmsQuizScoreEntity> emptyScores() {
|
return Collections.emptyList();
|
}
|
}
|