liusheng
9 天以前 f0a09ee996ab2605869844d6a3d812709e2bf0fa
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
package com.ruoyi.satisfaction.service.strategy;
 
import com.ruoyi.satisfaction.domain.ServiceSubtaskDetailRatioExport;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Component;
 
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.List;
 
/**
 * 通用口径(南华使用)的综合满意度得分策略。
 * <p>
 * <b>口径与原实现完全一致</b>(等于 {@code PatSatisfactionServiceImpl#averageScoreByStat}):
 * <pre>
 *   分子 = Σ(可解析的 score × 该分值的明细行数)
 *   分母 = 全部明细行数(<b>包含分值为空的行</b>)
 *   结果 = 分子 / 分母,保留 1 位、HALF_UP;无行时 0.0
 * </pre>
 * ⚠️ 两个"不能改"的点(原注释已强调,这里原样保留):
 * <ol>
 *   <li>分母是<b>明细行数</b>,不是"有分值的行数" —— 改成后者会改变 {@code averageScore};</li>
 *   <li>分值解析用 {@code Double.parseDouble},脏值会抛 {@code NumberFormatException}
 *       (原实现即如此,未加 try/catch,保持一致)。</li>
 * </ol>
 */
@Component
public class CommonSatisfactionScoreStrategy implements SatisfactionScoreStrategy {
 
    /**
     * 通用口径的保留位数(1 位,与 {@code averageScoreByStat} 一致)
     */
    private static final int SCALE = 1;
 
    @Override
    public String site() {
        return SatisfactionSites.COMMON;
    }
 
    @Override
    public String siteName() {
        return "通用口径(南华)";
    }
 
    @Override
    public BigDecimal comprehensiveScore(List<ServiceSubtaskDetailRatioExport> rows) {
        if (CollectionUtils.isEmpty(rows)) {
            return BigDecimal.ZERO.setScale(SCALE);
        }
 
        double sum = 0.0;
        long totalRows = 0L;
        for (ServiceSubtaskDetailRatioExport row : rows) {
            long count = parseCount(row.getCount());
            // 与原文一致:非正数的聚合行直接跳过
            if (count <= 0L) continue;
            totalRows += count;
            String score = row.getScore();
            if (score != null && !score.isEmpty()) {
                sum += Double.parseDouble(score) * count;
            }
        }
        if (totalRows == 0L) {
            return BigDecimal.ZERO.setScale(SCALE);
        }
        return new BigDecimal(sum / totalRows).setScale(SCALE, RoundingMode.HALF_UP);
    }
 
    /**
     * 明细行数解析:与 {@code ScoreCount.getCount()} 等价(原始是 long,这里从字符串取)
     */
    private long parseCount(String count) {
        if (count == null || count.isEmpty()) {
            return 0L;
        }
        try {
            return Long.parseLong(count.trim());
        } catch (NumberFormatException e) {
            return 0L;
        }
    }
}