feat:报告数据算法逻辑调整

This commit is contained in:
FCL
2025-12-08 10:27:36 +08:00
parent 6b006513af
commit 000cfca842
9 changed files with 218 additions and 238 deletions

View File

@@ -109,4 +109,8 @@ public class BusinessAssayReportDataDO extends BusinessBaseDO {
//样品创建时间
@TableField(exist = false)
private LocalDateTime baseSampleCreateTime;
//样品类型key
@TableField(exist = false)
private String sampleTypeKey;
}

View File

@@ -43,4 +43,7 @@ public class ReportDocumentDataPageReqVO extends PageParam {
@Schema(description = "样品大类名称")
private String baseSampleName;
@Schema(description = "样品类型key")
private String sampleTypeKey;
}

View File

@@ -56,6 +56,10 @@ public class ReportDocumentDataRespVO {
@ExcelProperty("样品大类名称")
private String baseSampleName;
@Schema(description = "样品类型key")
@ExcelProperty("样品类型key")
private String sampleTypeKey;
//==================扩展字段===========
@Schema(description = "样品创建时间")

View File

@@ -40,4 +40,7 @@ public class ReportDocumentDataSaveReqVO {
@Schema(description = "样品大类名称")
private String baseSampleName;
@Schema(description = "样品类型key")
private String sampleTypeKey;
}

View File

@@ -75,7 +75,9 @@ public class ReportDocumentDataDO extends BusinessBaseDO {
@TableField("BSE_SMP_NAME")
private String baseSampleName;
//样品类型key
@TableField("SMP_TP_KY")
private String sampleTypeKey;
//==================扩展字段===========

View File

@@ -67,6 +67,24 @@ public class ReportDocumentDataServiceImpl implements ReportDocumentDataService
if(fieldListAll.isEmpty())
return CommonResult.error(REPORT_DOCUMENT_TYPE_NOT_EXISTS.getCode(), "未配置报表字段,请联系管理员处理!");
List<ReportDocumentDataDO> dataList = listByMainDataId(mainData.getId()).getData();
//拆分dataList按样品分类key拆分
List<List<ReportDocumentDataDO>> dataListGroup = new ArrayList<>();
String[] sampleTypeKeys = new String[]{"inspectionAnalysisSample", "comprehensiveInspectionSample"}; //商检分析样、商检综合样
for(String sampleTypeKey : sampleTypeKeys){
List<ReportDocumentDataDO> dataListByKey = new ArrayList<>();
for(ReportDocumentDataDO dataDO: dataList){
if(sampleTypeKey.equals(dataDO.getSampleTypeKey())){
dataListByKey.add(dataDO);
}
}
dataListGroup.add(dataListByKey);
}
if(dataListGroup.isEmpty())
dataListGroup.add(dataList);
List<JSONObject> rowList = new ArrayList<>();
for(int i = 0; i < dataListGroup.size(); i++){
List<ReportDocumentDataDO> dataListByKey = dataListGroup.get(i);
//处理字段,提取有数据的字段
List<ConfigReportFieldDO> fieldList = new ArrayList<>();
List<String> hasFields = new ArrayList<>();
@@ -82,7 +100,7 @@ public class ReportDocumentDataServiceImpl implements ReportDocumentDataService
}
continue;
}
for(ReportDocumentDataDO dataDO : dataList){
for(ReportDocumentDataDO dataDO : dataListByKey){
String documentContent = dataDO.getDocumentContent();
JSONObject dataJson = JSONObject.parseObject(documentContent);
if(dataJson == null)
@@ -95,10 +113,20 @@ public class ReportDocumentDataServiceImpl implements ReportDocumentDataService
}
}
}
assembleStep1(rowList, fieldList, dataListByKey, customConfig, i); //处理后的组数对象
}
//组装数据
List<JSONObject> step1Arr = assembleStep1(fieldList, dataList, customConfig); //处理后的组数对象
return CommonResult.success(step1Arr);
//todo 处理数据分页
//todo 以下为空白
// JSONObject t = new JSONObject();
// t.put(colPrefix + "01", emptyText);
// rowList.add(t.clone());
//todo 检出限
return CommonResult.success(rowList);
}
/**
@@ -106,7 +134,7 @@ public class ReportDocumentDataServiceImpl implements ReportDocumentDataService
* @param fieldList 要显示的字段列表。固定字段在最前
* @param customConfig 报告配置项
* */
private List<JSONObject> assembleStep1(List<ConfigReportFieldDO> fieldList, List<ReportDocumentDataDO> dataList, String customConfig){
private List<JSONObject> assembleStep1(List<JSONObject> rowList, List<ConfigReportFieldDO> fieldList, List<ReportDocumentDataDO> dataList, String customConfig, int groupIndex){
if(dataList.isEmpty())
return new ArrayList<>();
JSONObject jsonObject = JSONObject.parseObject(customConfig);
@@ -114,11 +142,8 @@ public class ReportDocumentDataServiceImpl implements ReportDocumentDataService
Integer fixedColCount = 0;
Integer maxRowCount = 3;
String dynamicColCountStr = jsonObject.getString("dynamicColCount"); //动态列(检测项)数量
// String fixedColCountStr = jsonObject.getString("fixedColCount"); //固定列(样品名称、样品编号等)数量
JSONArray fixedCol = jsonObject.getJSONArray("fixedCol"); //固定列,举例:["sampleName", "sampleCode"]
JSONArray fixedColShow = jsonObject.getJSONArray("fixedColShow"); //固定列,举例:["产品名称", "编 号"]
String maxRowCountStr = jsonObject.getString("maxRowCount"); //最大行数
// String nameCodeType = jsonObject.getString("nameCodeType"); //名称、编号处理方式merge-合并, name-只显示名称, code-只显示编号, split-2列分开显示
String hasRemark = jsonObject.getString("hasRemark"); //是否有备注
String hasRange = jsonObject.getString("hasRange"); //是否有检出限
String verticalFlag = jsonObject.getString("verticalFlag"); //vertical-纵表, 否则为横表
@@ -133,102 +158,146 @@ public class ReportDocumentDataServiceImpl implements ReportDocumentDataService
if(fixedCol != null) fixedColCount = fixedCol.size();
if(!ObjectUtils.isEmpty(maxRowCountStr)) maxRowCount = Integer.parseInt(maxRowCountStr);
Integer eleCount = 0; //元素数量,即元素列总数
for(ConfigReportFieldDO fieldDO : fieldList){
String fieldType = fieldDO.getFieldType();
if(FIELD_DYNAMIC.equals(fieldType))
eleCount ++;
}
Integer rowCountOneSample = eleCount / dynamicColCount + (eleCount % dynamicColCount == 0 ? 0 : 1); //每个样品要占的行数
Integer colCountOneSample = fixedColCount + dynamicColCount; //每个样品要占的列数
Integer dataLength = dataList.size();
Integer emptyRowCount = dataLength < maxRowCount ? 1 : 0; //空行数
// Integer allRowCount = 1 + dataLength + emptyRowCount + 1; //标题 + 样品数 + 空行
if("1".equals(hasRange))
emptyRowCount ++;
// if(rowCountOneSample > 1){
// allRowCount = (1 + dataLength + emptyRowCount + 9) * rowCountOneSample + emptyRowCount; //(标题 + 样品数 + 空行) * 但样品行数 + 末尾行
// }
List<JSONObject> rowList = new ArrayList<>();
for(int i = 0; i < maxRowCount; i++){
JSONObject row = new JSONObject();
row.put("col01", " ");
rowList.add( row);
}
//=============处理表头============
JSONObject t = new JSONObject();
JSONObject r = new JSONObject(); //检出限
int rowAssist = 1;
int colIndex = fixedColCount + 1;
boolean lastObjFlag = true;
//取第一行数据,用来处理检出限
JSONObject firstData = new JSONObject(); //取第一行数据,用于处理检出限
if(dataLength > 0)
firstData = JSONObject.parseObject(dataList.get(0).getDocumentContent());
JSONObject firstData = JSONObject.parseObject(dataList.get(0).getDocumentContent()); //取第一行数据,用于处理检出限
if("1".equals(hasRemark)){
//在最后一列增加备注
t.put(colPrefix + parseNumToString(colCountOneSample + 1, 2), "备注");
}
for(ConfigReportFieldDO fieldDO : fieldList){
//取fieldList的动态字段并分组
List<List<ConfigReportFieldDO>> dynamicFieldListGroup = new ArrayList<>();
int fieldIndex = 1;
List<ConfigReportFieldDO> list = new ArrayList<>();
for(int i = 0; i < fieldList.size(); i++){
ConfigReportFieldDO fieldDO = fieldList.get(i);
String fieldType = fieldDO.getFieldType();
String field = fieldDO.getField();
if(FIELD_FIXED.equals(fieldType)){ //这里只处理动态列。固定列在 addTitleToRowList 处理
if(FIELD_FIXED.equals(fieldType))
continue;
list.add(fieldDO);
if(fieldIndex % dynamicColCount == 0){
dynamicFieldListGroup.add(list);
list = new ArrayList<>();
}
fieldIndex ++;
}
int colIndex = 1;
for(int i = 0; i < dynamicFieldListGroup.size(); i++){
List<ConfigReportFieldDO> dynamicFieldList = dynamicFieldListGroup.get(i);
t = new JSONObject();
r = new JSONObject();
colIndex = fixedColCount + 1; //第二组以后,从固定列数开始
for(ConfigReportFieldDO fieldDO : dynamicFieldList) {
String field = fieldDO.getField();
String fieldName = fieldDO.getFieldName();
String colKey = parseNumToString(colIndex, 2);
t.put(colPrefix + colKey, fieldName);
//查询当前字段的检出限
JSONObject fieldObj = firstData.getJSONObject( field);
JSONObject fieldObj = firstData.getJSONObject(field);
String rangeVal = "";
if(fieldObj != null){
if (fieldObj != null) {
rangeVal = fieldObj.getString(rangeKey);
}
r.put(colPrefix + colKey, rangeVal);
r.put(colPrefix + "01", "方法检出限");
lastObjFlag = true;
if(colIndex % colCountOneSample == 0){
addTitleToRowList(fixedCol, rowAssist, t, dataLength, emptyRowCount, rowList, fieldList);
if("1".equals(hasRange))
addRangeToRowList(rowAssist, r, dataLength, emptyRowCount, rowList);
t = new JSONObject();
r = new JSONObject();
if("1".equals(hasRemark)){
colIndex ++;
}
//处理备注列
if(i == 0 && groupIndex == 0 && "1".equals(hasRemark)){
//在最后一列增加备注
t.put(colPrefix + parseNumToString(colCountOneSample + 1, 2), "备注");
}
rowAssist++;
colIndex = fixedColCount + 1; //第二组以后,从固定列数开始
lastObjFlag = false;
continue;
if(i > 0 || groupIndex > 0){ //插入空行
JSONObject emptyRow = new JSONObject();
emptyRow.put(colPrefix + "01", " ");
rowList.add(emptyRow);
}
colIndex++;
addTitleToRowList(fixedCol, t, rowList, fieldList);
//插入样品数据
addDataToRowList(fixedCol, dynamicFieldList, dataList, rowList, fixedColCount, colCountOneSample, hasRemark);
}
if(lastObjFlag){
addTitleToRowList(fixedCol, rowAssist, t, dataLength, emptyRowCount, rowList, fieldList);
if("1".equals(hasRange))
addRangeToRowList(rowAssist, r, dataLength, emptyRowCount, rowList);
//todo 方法检出限
if("1".equals(hasRange)){
rowList.add(r);
}
return rowList;
}
//=============处理数据============
int dataIndex = 1;
/**
* @param
*
* */
private void addTitleToRowList(JSONArray fixedCol, JSONObject t, List<JSONObject> rowList, List<ConfigReportFieldDO> fieldList){
// int rowIndex = (1 + dataLength + emptyRowCount) * (rowAssist - 1) + 1; //(标题 + 数据行 + 空行)
//处理固定列
//todo 来样名称、来样编号、来样名称编号
int index = 1;
String colKey = "";
for(int i=0;i<fixedCol.size();i++) {
String col = fixedCol.getString(i);
for(ConfigReportFieldDO fieldDO : fieldList) {
String fieldName = fieldDO.getFieldName();
String fieldType = fieldDO.getFieldType();
String field = fieldDO.getField();
if (FIELD_DYNAMIC.equals(fieldType))
continue;
if("sampleNameCode".equals( col) && "SMP_NAME_CD".equals(field)){
colKey = parseNumToString(index, 2);
t.put(colPrefix + colKey, fieldName);
index++;
continue;
}
if("sampleName".equals( col) && "SMP_NAME".equals(field)){
colKey = parseNumToString(index, 2);
t.put(colPrefix + colKey, fieldName);
index++;
continue;
}
if("sampleCode".equals( col) && "SMP_CD".equals(field)){
colKey = parseNumToString(index, 2);
t.put(colPrefix + colKey, fieldName);
index++;
continue;
}
}
}
rowList.add(t.clone());
}
//添加行数据
private void addDataToRowList(JSONArray fixedCol, List<ConfigReportFieldDO> fieldList, List<ReportDocumentDataDO> dataList, List<JSONObject> rowList, int fixedColCount, int colCountOneSample, String hasRemark){
int colIndex = 0;
JSONObject t = new JSONObject();
for(ReportDocumentDataDO dataDO : dataList){
String documentContent = dataDO.getDocumentContent();
String externalContent = dataDO.getExternalContent();
JSONObject s = JSONObject.parseObject(documentContent);
JSONObject e = new JSONObject();
if(!ObjectUtils.isEmpty(externalContent)){
e = JSONObject.parseObject(externalContent);
//判断样品是否有值,若无值则跳过
boolean hasValue = false;
for(ConfigReportFieldDO fieldDO : fieldList) {
String fieldType = fieldDO.getFieldType();
if (FIELD_FIXED.equals(fieldType)) //这里只处理动态列。固定列在 addDataToRowList 处理
continue;
String field = fieldDO.getField();
JSONObject fieldObj = s.getJSONObject(field);
String fieldValue = "";
if (fieldObj != null)
fieldValue = fieldObj.getString("fieldValue");
if(!ObjectUtils.isEmpty(fieldValue))
hasValue = true;
}
if(!hasValue)
continue;
JSONObject e = new JSONObject();
if(!ObjectUtils.isEmpty(externalContent))
e = JSONObject.parseObject(externalContent);
String remark = e.getString("remark");
colIndex = fixedColCount + 1;
rowAssist = 1;
t = new JSONObject();
if("1".equals(hasRemark)){
t.put(colPrefix + parseNumToString(colCountOneSample + 1, 2), remark);
@@ -249,103 +318,12 @@ public class ReportDocumentDataServiceImpl implements ReportDocumentDataService
}
if(!ObjectUtils.isEmpty(mathSymbol) && !"=".equals(mathSymbol))
fieldValue = mathSymbol + fieldValue;
if(ObjectUtils.isEmpty(fieldValue))
fieldValue = "/";
t.put(colPrefix + colKey, fieldValue);
lastObjFlag = true;
if(colIndex % colCountOneSample == 0){
addFixedDataToRowList(fixedCol, dataDO, rowAssist, t, dataLength, emptyRowCount, dataIndex, rowList, fieldList);
t = new JSONObject();
if("1".equals(hasRemark)){
t.put(colPrefix + parseNumToString(colCountOneSample + 1, 2), remark);
}
rowAssist++;
colIndex = fixedColCount + 1; //第二组以后,从固定列数开始
lastObjFlag = false;
continue;
}
colIndex++;
}
if(lastObjFlag){
addFixedDataToRowList(fixedCol, dataDO, rowAssist, t, dataLength, emptyRowCount, dataIndex, rowList, fieldList);
}
dataIndex ++;
}
//==============以下为空白=================
//实际使用行数小于总行数,显示“”以下为空白“
if(dataLength < maxRowCount){
String colKey = colPrefix + "01";
t = new JSONObject();
t.put(colKey, emptyText);
int rowIndex = (dataLength + emptyRowCount) * (rowAssist) + 2;
if(rowAssist == 1)
rowIndex = dataLength + emptyRowCount;
if(rowCountOneSample == 1)
rowIndex = dataLength + 2;
rowList.set(rowIndex, t.clone());
}
//前面的计数是从1开始移除第一个元素
if(!rowList.isEmpty())
rowList.remove(0);
return rowList;
}
/**
* @param
*
* */
private void addTitleToRowList(JSONArray fixedCol, Integer rowAssist, JSONObject t, Integer dataLength, Integer emptyRowCount, List<JSONObject> rowList, List<ConfigReportFieldDO> fieldList){
int rowIndex = (1 + dataLength + emptyRowCount) * (rowAssist - 1) + 1; //(标题 + 数据行 + 空行)
//处理固定列
int index = 1;
String colKey = "";
for(int i=0;i<fixedCol.size();i++) {
String col = fixedCol.getString(i);
for(ConfigReportFieldDO fieldDO : fieldList) {
String fieldName = fieldDO.getFieldName();
String fieldType = fieldDO.getFieldType();
String field = fieldDO.getField();
if (FIELD_DYNAMIC.equals(fieldType))
continue;
if("sampleNameCode".equals( col) && "SMP_NAME_CD".equals(field)){
// String sampleNameCode = "样品名称及编号";
// String sampleNameCode = fieldName;
colKey = parseNumToString(index, 2);
t.put(colPrefix + colKey, fieldName);
index++;
continue;
}
if("sampleName".equals( col) && "SMP_NAME".equals(field)){
colKey = parseNumToString(index, 2);
t.put(colPrefix + colKey, fieldName);
index++;
continue;
}
if("sampleCode".equals( col) && "SMP_CD".equals(field)){
colKey = parseNumToString(index, 2);
t.put(colPrefix + colKey, fieldName);
index++;
continue;
}
}
}
rowList.set(rowIndex, t.clone());
}
/**
* 处理检出限
* 如果是
* */
private void addRangeToRowList(Integer rowAssist, JSONObject r, Integer dataLength, Integer emptyRowCount, List<JSONObject> rowList){
int rowIndex = (dataLength + emptyRowCount + 1 ) * (rowAssist);
rowList.set(rowIndex, r.clone());
}
/**
* 添加固定列的数据
* */
private void addFixedDataToRowList(JSONArray fixedCol, ReportDocumentDataDO dataDO, Integer rowAssist, JSONObject t, Integer dataLength, Integer emptyRowCount, Integer dataIndex, List<JSONObject> rowList, List<ConfigReportFieldDO> fieldList){
int rowIndex = (1 + dataLength + emptyRowCount) * (rowAssist - 1) + dataIndex + 1; //(标题 + 数据行 + 空行)
t.put("id", dataDO.getId());
//处理固定列
int index = 1;
@@ -364,42 +342,23 @@ public class ReportDocumentDataServiceImpl implements ReportDocumentDataService
else if("sampleNameCode".equals( col)){
t.put(colPrefix + colKey, dataDO.getSampleName() + " " + dataDO.getSampleCode());
}
//todo 来样名称、来样编号、来样名称编号
index ++;
}
}
// if(name_code_merge.equals(nameCodeType)){
// String merge = dataDO.getSampleName() + " " + dataDO.getSampleCode();
// t.put(colPrefix + colKey, merge);
// rowList.set(rowIndex, t.clone());
// return;
// }
// if(name_code_name.equals(nameCodeType)){
// t.put(colPrefix + colKey, dataDO.getSampleName());
// rowList.set(rowIndex, t.clone());
// return;
// }
// if(name_code_code.equals(nameCodeType)){
// t.put(colPrefix + colKey, dataDO.getSampleCode());
// rowList.set(rowIndex, t.clone());
// return;
// }
// for(ConfigReportFieldDO fieldDO : fieldList){
// String fieldName = fieldDO.getFieldName();
// String fieldType = fieldDO.getFieldType();
// String field = fieldDO.getField();
// if(FIELD_FIXED.equals(fieldType)){
// colKey = parseNumToString(index, 2);
// String val = "";
// if(sampleNameKey.equals( field))
// val = dataDO.getSampleName();
// else if(sampleCodeKey.equals( field))
// val = dataDO.getSampleCode();
// t.put(colPrefix + colKey, val);
// index++;
// }
// }
rowList.set(rowIndex, t.clone());
//添加入行数据
rowList.add(t.clone());
}
}
/**
* 处理检出限
* 如果是
* */
private void addRangeToRowList(JSONObject r, List<JSONObject> rowList){
rowList.add(r.clone());
}
private String parseNumToString(int num, int len){
StringBuilder sb = new StringBuilder();
for(int i = 0; i < len - String.valueOf(num).length(); i++){

View File

@@ -108,7 +108,9 @@ public class ReportDocumentMainServiceImpl implements ReportDocumentMainService,
public CommonResult<String> execAddDataByEntrust(ReportDocumentMainSaveReqVO reqVO) {
Long id = reqVO.getId();
String businessSampleEntrustRegistrationIds = reqVO.getBusinessSampleEntrustRegistrationIds();
if(ObjectUtils.isEmpty(businessSampleEntrustRegistrationIds) || id == null)
if(id == null)
return error(REPORT_DOCUMENT_DATA_NOT_EXISTS.getCode(), "缺少id参数请刷新后重试");
if(ObjectUtils.isEmpty(businessSampleEntrustRegistrationIds))
return error(REPORT_DOCUMENT_DATA_NOT_EXISTS.getCode(), "缺少businessSampleEntrustRegistrationIds参数请刷新后重试");
List<ReportDocumentMainCorrelationDO> reportRelationList = reportDocumentMainCorrelationService.listByMainId(id);
//todo 判断修改的场景。处理方法删除reportRelation、ReportDocumentData
@@ -152,6 +154,7 @@ public class ReportDocumentMainServiceImpl implements ReportDocumentMainService,
reportDocumentDataDO.setSampleName(assayReportData.getSampleName());
reportDocumentDataDO.setBaseSampleName(assayReportData.getBaseSampleName());
reportDocumentDataDO.setDocumentContent(assayReportData.getAssayData());
reportDocumentDataDO.setSampleTypeKey(assayReportData.getSampleTypeKey());
insertList.add(reportDocumentDataDO);
}
if(!insertList.isEmpty())

View File

@@ -30,7 +30,8 @@
d.DELETED as deleted,
s.CREATE_TIME as baseSampleCreateTime,
s.SMP_NAME as sampleName,
s.BSE_SMP_NAME as baseSampleName
s.BSE_SMP_NAME as baseSampleName,
(select KY from T_DIC_BSN dic where dic.id = s.DIC_BSN_ID) as sampleTypeKey
FROM T_BSN_ASY_RPT_DAT d
inner join T_BSN_BSE_SMP s on d.BSN_BSE_SMP_ID = s.id
<where>

View File

@@ -56,6 +56,7 @@
m.SYS_DEPT_CD as systemDepartmentCode,
m.UPD_CNT as updateCount,
m.RMK as remark,
m.CREATE_TIME as createTime,
es.name as configEntrustSourceName
FROM T_BSN_SMP_ENTT_REG m
left join T_CFG_ENTT_SRC es on m.CFG_ENTT_SRC_ID = es.id