yeg
2026-09-09 f38ca8a9f8454ac2eebdc268c89efd0e436b3cf2
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
package com.battery.business.report;
 
import java.math.BigDecimal;
import java.math.MathContext;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import com.battery.device.api.StepDirectionResolver.Direction;
 
/** 化成公式只读取报表自身的基础字段和冻结工步,结果不反写电池组或设备事实。 */
final class FormationReportCalculations
{
    static final Set<String> INPUTS = Set.of("H", "Z", "A", "F", "B");
    static final Set<String> RESULTS = Set.of("FN", "C", "chargePerGram", "chargePerGramTotal", "capacity1v", "capacity1vf",
            "dischargeInflection", "dischargeInflectionTotal");
 
    /** 每次保存重算并冻结结果;隐藏公式输入字段不能改变其归属或丢失依赖。 */
    static Map<String, BigDecimal> calculate(List<FormationReportService.Item> items, FormationReportRequest.Settings settings)
    {
        Map<String, BigDecimal> result = new LinkedHashMap<>();
        for (var item : items) {
            String prefix = item.source().selection().testId() + "|" + item.source().selection().testBatteryId() + "|";
            Map<String, BigDecimal> base = new LinkedHashMap<>();
            for (String key : INPUTS) {
                var field = settings.fields().stream().filter(value -> "detail".equals(value.scope()) && key.equals(value.key())).findFirst();
                var stored = item.businessFields().stream().filter(value -> field.isPresent() && field.get().fieldDefinitionId() != null
                        ? field.get().fieldDefinitionId().equals(value.getFieldDefinitionId()) : key.equals(value.getFieldName())).findFirst();
                String value = settings.values().getOrDefault(prefix + "0|" + key, stored.map(valueField -> valueField.getFieldValue()).orElse(null));
                base.put(key, number(value));
            }
            BigDecimal c = multiply(base.get("H"), base.get("Z"), base.get("A"), base.get("F"), base.get("B"));
            result.put(prefix + "0|C", c);
            result.put(prefix + "0|FN", divide(divide(base.get("F"), base.get("H")), base.get("Z")));
            for (int cycle : item.cycleNos()) {
                String row = prefix + cycle + "|";
                BigDecimal manual = number(settings.values().get(row + "manual"));
                result.put(row + "chargePerGram", divide(divide(multiply(manual, c), base.get("H")), base.get("Z")));
                result.put(row + "chargePerGramTotal", divide(multiply(manual, c), base.get("F")));
                var last = item.chargeSteps().getOrDefault(cycle, List.of()).stream().filter(step -> step.direction() == Direction.DISCHARGE)
                        .max(java.util.Comparator.comparingInt(FormationReportService.ChargeStep::stepNo));
                // 工步容量存储为 Ah,报表结果使用 mAh;末工步缺值绝不回退前一工步。
                BigDecimal capacity = last.map(step -> multiply(step.capacity(), BigDecimal.valueOf(1000))).orElse(null);
                result.put(row + "capacity1v", capacity);
                result.put(row + "capacity1vf", divide(capacity, base.get("F")));
                // 仅拐点两项保留用户明确要求的演示占位,固定值随版本保存。
                result.put(row + "dischargeInflection", BigDecimal.valueOf(3.934));
                result.put(row + "dischargeInflectionTotal", BigDecimal.valueOf(3.982));
            }
        }
        return result;
    }
 
    private static BigDecimal number(String value)
    {
        if (value == null || !value.trim().matches("[+-]?(?:\\d+(?:\\.\\d*)?|\\.\\d+)")) return null;
        return new BigDecimal(value.trim());
    }
 
    private static BigDecimal multiply(BigDecimal... values)
    {
        BigDecimal result = BigDecimal.ONE;
        for (var value : values) { if (value == null) return null; result = result.multiply(value, MathContext.DECIMAL128); }
        return result;
    }
 
    private static BigDecimal divide(BigDecimal value, BigDecimal divisor)
    { return value == null || divisor == null || divisor.signum() == 0 ? null : value.divide(divisor, MathContext.DECIMAL128); }
}