刘光辉
10 小时以前 06f314545ac3514d7ce28939920866ae8188d31c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
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();
    }
}