gamma模块saveToTXT功能

This commit is contained in:
qiaoqinzheng 2023-10-11 18:47:52 +08:00
parent b3e0a4fc73
commit 4a17dca010
3 changed files with 136 additions and 50 deletions

View File

@ -476,9 +476,9 @@ public class GammaController {
return gammaService.saveToDB(fileName, request); return gammaService.saveToDB(fileName, request);
} }
@GetMapping("saveTxt") @GetMapping("saveToTxt")
public Result saveTxt(String fileName, HttpServletRequest request) { public void saveToTxt(String fileName, HttpServletRequest request, HttpServletResponse response) {
return gammaService.saveTxt(fileName, request); gammaService.saveToTxt(fileName, request, response);
} }
} }

View File

@ -160,6 +160,6 @@ public interface IGammaService{
Result saveToDB(String fileName, HttpServletRequest request); Result saveToDB(String fileName, HttpServletRequest request);
Result saveTxt(String fileName, HttpServletRequest request); void saveToTxt(String fileName, HttpServletRequest request, HttpServletResponse response);
} }

View File

@ -25,6 +25,7 @@ import org.jeecg.common.properties.ParameterProperties;
import org.jeecg.common.properties.SpectrumPathProperties; import org.jeecg.common.properties.SpectrumPathProperties;
import org.jeecg.common.system.util.JwtUtil; import org.jeecg.common.system.util.JwtUtil;
import org.jeecg.common.util.*; import org.jeecg.common.util.*;
import org.jeecg.modules.base.abstracts.AbstractLogOrReport;
import org.jeecg.modules.base.bizVo.GammaRLR; import org.jeecg.modules.base.bizVo.GammaRLR;
import org.jeecg.modules.base.entity.configuration.GardsNuclLib; import org.jeecg.modules.base.entity.configuration.GardsNuclLib;
import org.jeecg.modules.base.entity.configuration.GardsNuclLinesLib; import org.jeecg.modules.base.entity.configuration.GardsNuclLinesLib;
@ -69,7 +70,7 @@ import static org.jeecg.modules.base.enums.ExportTemplate.*;
@Service(value = "gammaService") @Service(value = "gammaService")
@DS("ora") @DS("ora")
public class GammaServiceImpl implements IGammaService { public class GammaServiceImpl extends AbstractLogOrReport implements IGammaService {
@Autowired @Autowired
private LocalCache localCache; private LocalCache localCache;
@ -3920,30 +3921,35 @@ public class GammaServiceImpl implements IGammaService {
} }
@Override @Override
public Result saveTxt(String fileName, HttpServletRequest request) { public void saveToTxt(String fileName, HttpServletRequest request, HttpServletResponse response) {
Result result = new Result();
String userName = JwtUtil.getUserNameByToken(request); String userName = JwtUtil.getUserNameByToken(request);
Cache<String, PHDFile> phdCache = localCache.getPHDCache(); Cache<String, PHDFile> phdCache = localCache.getPHDCache();
PHDFile phd = phdCache.getIfPresent(fileName+"-"+userName); PHDFile phd = phdCache.getIfPresent(fileName+"-"+userName);
StringBuilder strBuild = new StringBuilder(); StringBuilder strBuild = new StringBuilder();
//txt文本内容 //txt文本内容
//文本内容第一块内容匹配格式 //文本内容第一块头部信息
String title1 = " %s The Results of Peak Searching %s"; String title1 = " %s The Results of Peak Searching %s";
//文本内容第一块内容匹配 //文本内容第一块头部信息匹配
strBuild.append(titleFormat(title1, 51, StringPool.ASTERISK)); strBuild.append(titleFormat(title1, 51, StringPool.ASTERISK, StringPool.ASTERISK));
//换行
strBuild.append(System.lineSeparator());
//换行 //换行
strBuild.append(System.lineSeparator()); strBuild.append(System.lineSeparator());
//文本内容第二块 //文本内容第二块
//文本内容第二块匹配格式 //文本内容第二块匹配格式
String title2 = "%-12s %-12s %-12s %-12s %-12s %-12s %-12s %-12s %-12s %-12s"; String title2 = "%-12s %-12s %-12s %-12s %-12s %-12s %-12s %-12s %-12s %-12s";
//文本内容第二块第一部分数据信息 //文本内容第二块头部信息
String[] titleArgs2 = new String[]{"PeakID", "Energy(keV)", "Centroid", "Multiplet", "FWHM(keV)", "NetArea", "NAErr%", "Signif", "Sensit", "Nuclide"}; String[] titleArgs2 = new String[]{"PeakID", "Energy(keV)", "Centroid", "Multiplet", "FWHM(keV)", "NetArea", "NAErr%", "Signif", "Sensit", "Nuclide"};
//文本内容第二块第一部分数据匹配 //文本内容第二块头部信息匹配
strBuild.append(rowFormat(title2, titleArgs2)); strBuild.append(rowFormat(title2, titleArgs2));
//遍历数组进行文本内容第二块第二部分数据匹配 //换行
strBuild.append(System.lineSeparator());
//换行
strBuild.append(System.lineSeparator());
//遍历数组进行文本内容第二块数据匹配
for (int i=0; i<phd.getVPeak().size(); i++) { for (int i=0; i<phd.getVPeak().size(); i++) {
PeakInfo peakInfo = phd.getVPeak().get(i); PeakInfo peakInfo = phd.getVPeak().get(i);
String peakId = String.valueOf(peakInfo.index); String peakId = String.valueOf(i+1);
String energy = NumberFormatUtil.numberFormat(String.valueOf(peakInfo.energy)); String energy = NumberFormatUtil.numberFormat(String.valueOf(peakInfo.energy));
String peakCentroid = NumberFormatUtil.numberFormat(String.valueOf(peakInfo.peakCentroid)); String peakCentroid = NumberFormatUtil.numberFormat(String.valueOf(peakInfo.peakCentroid));
String multiIndex = String.valueOf(peakInfo.multiIndex); String multiIndex = String.valueOf(peakInfo.multiIndex);
@ -3954,48 +3960,128 @@ public class GammaServiceImpl implements IGammaService {
String sensit = NumberFormatUtil.numberFormat(String.valueOf(peakInfo.sensitivity)); String sensit = NumberFormatUtil.numberFormat(String.valueOf(peakInfo.sensitivity));
String nuclide = StringUtils.join(peakInfo.nuclides, StringPool.SEMICOLON); String nuclide = StringUtils.join(peakInfo.nuclides, StringPool.SEMICOLON);
strBuild.append(rowFormat(title2, peakId, energy, peakCentroid, multiIndex, fwhm, area, areaErr, signif, sensit, nuclide)); strBuild.append(rowFormat(title2, peakId, energy, peakCentroid, multiIndex, fwhm, area, areaErr, signif, sensit, nuclide));
//换行
strBuild.append(System.lineSeparator());
} }
//换行 //换行
strBuild.append(System.lineSeparator()); strBuild.append(System.lineSeparator());
//文本内容第三块 //文本内容第三块
//文本内容第三块头部信息
String title3 = " %s The Results of Nuclide Identify %s";
result.setSuccess(true); strBuild.append(titleFormat(title3, 12, StringPool.ASTERISK, StringPool.ASTERISK));
result.setResult(strBuild.toString()); //换行
return result; strBuild.append(System.lineSeparator());
//换行
strBuild.append(System.lineSeparator());
//文本内容第三块数据
List<List<String>> peakNuclides = phd.getVPeak().stream().map(item -> item.nuclides).collect(Collectors.toList());
List<String> nuclides = new LinkedList<>();
for (int i=0; i<peakNuclides.size(); i++) {
List<String> peakNuclide = peakNuclides.get(i);
nuclides.addAll(peakNuclide);
} }
nuclides = nuclides.stream().distinct().collect(Collectors.toList());
/** String nuclideStr = "";
* 行日志格式化 for (int i=0; i<nuclides.size(); i++) {
* @param source 需要格式化的字符串 nuclideStr+=" "+nuclides.get(i);
* @param symbolNum 格式化的符号数量
* @param args 格式化替换参数数组
* @return
*/
private String titleFormat(String source, Integer symbolNum, String... args){
StringBuilder firstParam = new StringBuilder();
for (int i=0;i<symbolNum;i++){
firstParam.append(args[0]);
} }
args[0] = firstParam.toString(); strBuild.append(nuclideStr);
//换行
StringBuilder lastParam = new StringBuilder(); strBuild.append(System.lineSeparator());
for (int i=0;i<symbolNum;i++){ //换行
lastParam.append(args[args.length-1]); strBuild.append(System.lineSeparator());
//文本内容第四块
//文本内容第四块头部信息
String title4 = " %s Nuclide's Activity and Concentration %s";
//文本内容第四块头部信息匹配
strBuild.append(titleFormat(title4, 28, StringPool.ASTERISK, StringPool.ASTERISK));
//换行
strBuild.append(System.lineSeparator());
//文本内容第四块第一部分数据
String data1 = "Activity Reference Time: %s";
String actRefTime = DateUtils.formatDate(phd.getUsedSetting().getRefTime_act(), "yyyy/MM/dd HH:mm:ss");
strBuild.append(rowFormat(data1, actRefTime));
//换行
strBuild.append(System.lineSeparator());
//文本内容第四块第二部分数据
String data2 = "Concentration Reference Time: %s";
String concRefTime = DateUtils.formatDate(phd.getUsedSetting().getRefTime_conc(), "yyyy/MM/dd HH:mm:ss");
strBuild.append(rowFormat(data2, concRefTime));
//换行
strBuild.append(System.lineSeparator());
//文本内容第五块
//文本内容第五块头部信息
String title5 = "%-12s %-12s %-12s %-12s %-12s %-12s %-12s %-12s %-12s %-12s";
String[] titleArgs5 = new String[]{"Nuclide", "Halflife", "Yield%", "Energy(keV)", "Efficiency", "Activity(Bq)", "ActErr%", "MDA(Bq)", "Conc(uBq/m^3)", "MDC(uBq/m^3)"};
strBuild.append(rowFormat(title5, titleArgs5));
//换行
strBuild.append(System.lineSeparator());
//文本内容第五块数据
Map<String, NuclideActMda> mapNucActMda = phd.getMapNucActMda();
for (Map.Entry<String, NuclideActMda> entry:mapNucActMda.entrySet()) {
String key = entry.getKey();
NuclideActMda nuc = entry.getValue();
String halflifeValue = "";
if(nuc.isBCalculateMDA()) {
String units = "S";
double halflife = nuc.getHalflife();
if(halflife >= 31556736) {// 1年 = 365.24 * 24 * 60 * 60 = 31556736s
halflife /= 31556736;
units = "A";
} else if(halflife >= 86400) {// 1天 = 24 * 60 * 60 = 86400s
halflife /= 86400;
units = "D";
} else if(halflife >= 3600) {
halflife /= 3600;
units = "H";
}
halflifeValue = NumberFormatUtil.numberFormat(String.valueOf(halflife)) + units;
}
String efficiency = NumberFormatUtil.numberFormat(String.valueOf(nuc.getEfficiency()));
String activity = NumberFormatUtil.numberFormat(String.valueOf(nuc.getActivity()));
String actErr = NumberFormatUtil.numberFormat(String.valueOf(nuc.getAct_err()/nuc.getActivity()*100));
String mda = NumberFormatUtil.numberFormat(String.valueOf(nuc.getMda()));
String conc = NumberFormatUtil.numberFormat(String.valueOf(nuc.getConcentration()));
String mdc = NumberFormatUtil.numberFormat(String.valueOf(nuc.getMdc()));
if(nuc.getCalculateIdx() >= 0 && nuc.getCalculateIdx() < nuc.getVEnergy().size()) {
String yield = NumberFormatUtil.numberFormat(String.valueOf(nuc.getVYield().get(nuc.getCalculateIdx())*100));
String energy = NumberFormatUtil.numberFormat(String.valueOf(nuc.getVEnergy().get(nuc.getCalculateIdx())));
strBuild.append(rowFormat(title5, key, halflifeValue, yield, energy, efficiency, activity, actErr, mda, conc, mdc));
strBuild.append(System.lineSeparator());
} else {
strBuild.append(rowFormat(title5, key, halflifeValue, "NULL", "NULL", efficiency, activity, actErr, mda, conc, mdc));
strBuild.append(System.lineSeparator());
}
}
strBuild.append(System.lineSeparator());
String detectorCode = phd.getHeader().getDetector_code();
String date = phd.getAcq().getAcquisition_start_date().replace("/", "");
String time = phd.getAcq().getAcquisition_start_time().replace(":", "").substring(0, 4);
String dataType = phd.getMsgInfo().getData_type().substring(0, 1);
String format = ".txt";
String txtFileName = String.format("%s-%s_%s_%s_RESULT%s", detectorCode, date, time, dataType, format);
//导出数据内容到txt文本
OutputStream fos = null;
try {
//设置响应类型
response.setContentType("application/octet-stream");
//解决中文不能生成文件
response.setHeader("Content-Disposition", "attachment; fileName=" + URLEncoder.encode(txtFileName,"UTF-8"));
fos = response.getOutputStream();
fos.write(strBuild.toString().getBytes());
} catch (FileNotFoundException e) {
throw new RuntimeException(e);
} catch (IOException e) {
throw new RuntimeException(e);
} finally {
try {
if (Objects.nonNull(fos)) {
fos.close();
}
} catch (IOException e) {
throw new RuntimeException(e);
} }
args[args.length-1] = lastParam.toString();
return String.format(source,args);
} }
/**
* 行格式化
* @param source
* @param args
* @return
*/
private String rowFormat(String source, String... args){
return String.format(source,args);
} }
} }