jinlin
2025-02-18 6250f74b2a02770a37f2f0144c762f7952a12bd6
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
package com.zt.life.modules.mainPart.TestScheme.service;
 
import cn.hutool.core.convert.Convert;
import com.zt.common.service.BaseService;
import com.zt.life.modules.mainPart.TestScheme.dao.TestSchemeDao;
import com.zt.life.modules.mainPart.TestScheme.dto.ConditionDto;
import com.zt.life.modules.mainPart.TestScheme.dto.ResultDto;
import com.zt.life.modules.mainPart.TestScheme.model.TestScheme;
import org.apache.commons.math3.distribution.ChiSquaredDistribution;
import org.springframework.stereotype.Service;
 
import java.util.ArrayList;
import java.util.Formatter;
import java.util.List;
 
 
 
/**
 * test_scheme
 *
 * @author zt generator
 * @since 1.0.0 2024-08-07
 */
@Service
public class TestSchemeService extends BaseService<TestSchemeDao, TestScheme> {
 
    /**
     * 删除
     *
     * @param ids
     */
    public void delete(Long[] ids) {
        super.deleteLogic(ids);
    }
 
    public List<ResultDto> getResult(ConditionDto dto) {
        this.insertNum();
        List<ResultDto> result = new ArrayList<>();
        switch (dto.getTjlx()) {
            case "按风险名义值查询":
                result = this.getCondition1(dto);
                break;
            case "不限定生产方风险查询":
                result = this.getCondition2(dto);
                break;
            case "不限定检验上限查询":
                result = this.getCondition3(dto);
                break;
        }
        return result;
    }
 
    private void insertNum() {
        baseDao.insertNum();
    }
 
    private List<ResultDto> getCondition1(ConditionDto dto) {
        List<ResultDto> result = new ArrayList<>();
        int C = 1;
        double oldvalue = 0;
        double strate = dto.getMinAccepValue() / dto.getSpecifiedValue();
        for (int i = 1; i <= 10000; i++) {
            Double data1 = getUpperQuantile(1 - dto.getProductionRisk(), 2 * i + 2);
            Double data2 = getUpperQuantile(dto.getUserRisk(), 2 * i + 2);//算法计算
            if (data1 != null && data2 != null) {
                double value = data1 / data2;
                System.out.println(value);
                if (dto.getUserRisk() < 1 - dto.getProductionRisk()) {
                    if (oldvalue != 0) {
                        if (value > strate) {
                            C = i;
                            if (strate - oldvalue < value - strate) {
                                C = i - 1;
                            }
                            break;
                        }
                    } else {
                        if (value > strate) {
                            C = i;
                            break;
                        }
                    }
                } else {
                    if (oldvalue != 0) {
                        if (value < strate) {
                            C = i;
                            if (oldvalue - strate < strate - value) {
                                C = i - 1;
                            }
                            break;
                        }
                    } else {
                        if (value < strate) {
                            C = i;
                            break;
                        }
                    }
                }
                oldvalue = value;
            }
        }
 
        double T = 0;     //总试验时间
        double TST1 = 0;  //T/θ1
        double TST0 = 0;  //T/θ0
        Double data3 = getUpperQuantile(dto.getUserRisk(), 2 * C + 2);
        if (data3 != null) {
            TST1 = data3 / 2; //T/θ1
            //TST1 = Convert.ToDouble(TST1.ToString("G2")); //取两位有效数字
            if (TST1 < 10) {
                TST1 = TST1 + 0.1;
            }
            T = TST1 * dto.getMinAccepValue();  //总试验时间
            TST0 = T / dto.getSpecifiedValue(); //T/θ0    TST1 * minAccepValue / specifiedValue
        }
 
        double productionRisk = 0;  //生产方风险实际值
        double userRisk = 0;        //使用方风险实际值
        double e = 2.71828182845904523536;  //自然数e
 
        for (int r = 0; r <= C; r++) {
            double a = Math.pow(TST0, r) * Math.pow(e, -TST0) / getjc(r);
           /* if (r>35){
                System.out.println(111);
            }*/
            productionRisk = productionRisk + a;
            userRisk = userRisk + Math.pow(TST1, r) * Math.pow(e, -TST1) / getjc(r);
        }
        productionRisk = 1 - productionRisk;
 
        ResultDto resultDto = new ResultDto();
        resultDto.setAcceptNumber(C);
        resultDto.setTotalTestTime(String.valueOf(Math.round(T)));
        resultDto.setProductionRiskReal(new Formatter().format("%.2f", productionRisk * 100).toString());
        resultDto.setUserRiskReal(new Formatter().format("%.2f", userRisk * 100).toString());
        result.add(resultDto);
        return result;
    }
 
    public List<ResultDto> getCondition2(ConditionDto dto) {
        List<ResultDto> result = new ArrayList<>();
        for (int i = 0; i < dto.getShowFailureTime(); i++) {
            double T;
            double TST1; //  T/θ1
            double TST0;//  T/θ0
 
            Double data2 = getUpperQuantile(dto.getUserRisk(), 2 * i + 2);
            TST1 = data2 / 2;
            T = TST1 * dto.getMinAccepValue();
            TST0 = T / dto.getSpecifiedValue(); //  T/θ0
 
 
            double productionRisk = 0;
            double userRisk = 0;
            double e = 2.71828182845904523536;
            for (int r = 0; r <= i; r++) {
                productionRisk = productionRisk + Math.pow(TST0, r) * Math.pow(e, -TST0) / getjc(r);
                userRisk = userRisk + Math.pow(TST1, r) * Math.pow(e, -TST1) / getjc(r);
            }
            productionRisk = 1 - productionRisk;
 
            ResultDto resultDto = new ResultDto();
            resultDto.setAcceptNumber(i + 1);
            resultDto.setTotalTestTime(String.valueOf(Math.round(T)));
            resultDto.setProductionRiskReal(new Formatter().format("%.2f", productionRisk * 100).toString());
            resultDto.setUserRiskReal(new Formatter().format("%.2f", userRisk * 100).toString());
            result.add(resultDto);
        }
        return result;
    }
 
    private List<ResultDto> getCondition3(ConditionDto dto) {
        List<ResultDto> result = new ArrayList<>();
        for (int i = 0; i < dto.getShowFailureTime(); i++) {
            double T;
            double TST1; //  T/θ1
            double TST0;//  T/θ0
            double ST0; // θ0
 
            Double data1 = getUpperQuantile(dto.getUserRisk(), 2 * i + 2);
            Double data2 = getUpperQuantile(1 - dto.getProductionRisk(), 2 * i + 2);
            TST1 = data1 / 2;
            T = TST1 * dto.getMinAccepValue();
            ST0 = data1 * dto.getMinAccepValue() / data2;
            TST0 = T / ST0;
 
 
            double productionRisk = 0;
            double userRisk = 0;
            double e = 2.71828182845904523536;
            for (int r = 0; r <= i; r++) {
                productionRisk = productionRisk + Math.pow(TST0, r) * Math.pow(e, -TST0) / getjc(r);
                userRisk = userRisk + Math.pow(TST1, r) * Math.pow(e, -TST1) / getjc(r);
            }
            productionRisk = 1 - productionRisk;
 
            ResultDto resultDto = new ResultDto();
            resultDto.setAcceptNumber(i + 1);
            resultDto.setTotalTestTime(String.valueOf(Math.round(T)));
            resultDto.setSpecifiedValue(String.valueOf(Math.round(ST0)));//检验上限
            resultDto.setProductionRiskReal(new Formatter().format("%.2f", productionRisk * 100).toString());
            resultDto.setUserRiskReal(new Formatter().format("%.2f", userRisk * 100).toString());
            result.add(resultDto);
        }
        return result;
    }
 
    //计算阶乘
    Double getjc(int a) {
        double result = 1.0;
        for (int i = a; i > 0; i--)
            result = result * i;
        return result;
    }
 
    //卡方分布的上侧α分位数算法
    public Double getUpperQuantile(Double alpha, Integer degreesOfFreedom) {
        ChiSquaredDistribution chiSquaredDistribution = new ChiSquaredDistribution(degreesOfFreedom);
        Double upperQuantile = chiSquaredDistribution.inverseCumulativeProbability(1 - alpha);
        String str = String.format("%.4f", upperQuantile);
        upperQuantile = Convert.toDouble(str);
        return upperQuantile;
    }
}