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;
|
}
|
}
|