feat:报告数据算法逻辑调整;增加分页逻辑

This commit is contained in:
FCL
2025-12-09 14:39:13 +08:00
parent b7f30012c8
commit 920292cfac
3 changed files with 118 additions and 53 deletions

View File

@@ -62,16 +62,12 @@ public class ReportDocumentDataController extends AbstractFileUploadController i
@GetMapping("/queryReportDetail")
@Operation(summary = "查询报告明细数据")
@Parameter(name = "mainId", description = "报告id", required = true, example = "1024")
public CommonResult<List<JSONObject>> queryReportDetail(@RequestParam("mainId") Long mainId) {
public CommonResult<?> queryReportDetail(@RequestParam("mainId") Long mainId, @RequestParam(name = "pageFlag", required = false) String pageFlag) {
ReportDocumentMainDO mainDO = reportDocumentMainService.getReportDocumentMain(mainId);
Long typeId = mainDO.getReportDocumentTypeId();
ReportDocumentTypeDO typeDO = reportDocumentTypeService.getReportDocumentType(typeId);
Long confId = typeDO.getConfigReportTypeId();
// Map<String, Object> param = new HashMap<>();
// List<ReportDocumentDataDO> list = reportDocumentDataService.listByMainDataId(mainId).getData();
// return success(BeanUtils.toBean(list, ReportDocumentDataRespVO.class));
CommonResult<List<JSONObject>> result = reportDocumentDataService.assembleDynamicData(mainDO, typeDO);
CommonResult<JSONArray> result = reportDocumentDataService.assembleDynamicData(mainDO, typeDO, pageFlag);
return result;
}

View File

@@ -19,7 +19,7 @@ import java.util.List;
*/
public interface ReportDocumentDataService {
CommonResult<List<JSONObject>> assembleDynamicData(ReportDocumentMainDO mainData, ReportDocumentTypeDO reportConfig);
CommonResult<JSONArray> assembleDynamicData(ReportDocumentMainDO mainData, ReportDocumentTypeDO reportConfig, String pageFlag);
CommonResult<List<ReportDocumentDataDO>> listByMainDataId(Long mainDataId);
CommonResult<Long> countMainDataId(Long mainDataId);

View File

@@ -44,6 +44,13 @@ public class ReportDocumentDataServiceImpl implements ReportDocumentDataService
private final String colPrefix = "col";
private final String sampleCodeKey = "SMP_CD";
private final String sampleNameKey = "SMP_NAME";
private final String sampleNameAndCodeKey = "SMP_NAME_CD";
private final String entrustSampleNameKey = "ENTT_SMP_NAME";
private final String entrustSampleCodeKey = "ENTT_SMP_CD";
private final String entrustSampleNameCodeKey = "ENTT_SMP_NAME_CD";
private final String rowTypeKey = "rowType"; //行类型
private final String rowTypeTitle = "title"; //行类型-标题
private final String rowTypeLimit = "limit"; //行类型-检出限
private final String rangeKey = "minLimitValue";
private final String emptyText = "以下为空白";
@@ -52,7 +59,7 @@ public class ReportDocumentDataServiceImpl implements ReportDocumentDataService
* 组装动态报表数据
* */
@Override
public CommonResult<List<JSONObject>> assembleDynamicData(ReportDocumentMainDO mainData, ReportDocumentTypeDO typeDO) {
public CommonResult<JSONArray> assembleDynamicData(ReportDocumentMainDO mainData, ReportDocumentTypeDO typeDO, String pageFlag) {
if(typeDO == null || typeDO.getConfigReportTypeId() == null)
return CommonResult.error(REPORT_DOCUMENT_TYPE_NOT_EXISTS.getCode(), "报告配置为空,或未配置报表类型,请联系管理员处理!");
String customConfig = typeDO.getCustomConfig();
@@ -85,7 +92,7 @@ public class ReportDocumentDataServiceImpl implements ReportDocumentDataService
}
if(dataListGroup.isEmpty())
dataListGroup.add(dataList);
List<JSONObject> rowList = new ArrayList<>();
JSONArray rowList = new JSONArray();
for(int i = 0; i < dataListGroup.size(); i++){
List<ReportDocumentDataDO> dataListByKey = dataListGroup.get(i);
//处理字段,提取有数据的字段
@@ -121,7 +128,11 @@ public class ReportDocumentDataServiceImpl implements ReportDocumentDataService
//处理空数据,填充/
rowList = assembleEmpty(configJson, rowList);
//todo 处理数据分页
//处理数据分页
if("1".equals(pageFlag)){
JSONArray pageRowList = assemblePageRowList(rowList, configJson);
return CommonResult.success(pageRowList);
}
//以下为空白
if(rowList.size() < maxRowCount){
@@ -137,8 +148,70 @@ public class ReportDocumentDataServiceImpl implements ReportDocumentDataService
return CommonResult.success(rowList);
}
private List<JSONObject> assembleEmpty(JSONObject configJson, List<JSONObject> rowList){
JSONArray fixedCol = configJson.getJSONArray("fixedCol"); //固定列,举例:["sampleName", "sampleCode"]
/*
* 处理分页
*
* */
private JSONArray assemblePageRowList(JSONArray rowList, JSONObject configJson){
JSONArray pageRowList = new JSONArray();
Integer maxRowCount = 20;
String maxRowCountStr = configJson.getString("maxRowCount"); //最大行数
if(!ObjectUtils.isEmpty(maxRowCountStr)) maxRowCount = Integer.parseInt(maxRowCountStr);
int rowLength = rowList.size();
if(rowLength <= maxRowCount){
pageRowList.add(rowList);
return pageRowList;
}
JSONArray remainingRows = new JSONArray();
remainingRows.addAll(rowList);
while(remainingRows.size() > 0){
JSONArray pageRow = new JSONArray();
for(int i = 0; i < maxRowCount ; i++){
if(remainingRows.isEmpty())
break;
pageRow.add(remainingRows.getJSONObject(0));
remainingRows.remove(0);
}
/*
在每页开头插入“标题行”
如果最后一行是标题,直接移到下一页;如果不是,往上寻找最近的“标题行”
*/
JSONObject lastTitleRow = pageRow.getJSONObject(pageRow.size() - 1);
String rowType = lastTitleRow.getString(rowTypeKey);
if(rowTypeTitle.equals(rowType)){
//如果最后一行是“标题行”,移除
pageRow.remove(pageRow.size() - 1);
}else {
for(int i = pageRow.size() - 1; i >= 0; i--){
JSONObject t = pageRow.getJSONObject(i);
rowType = t.getString(rowTypeKey);
if(rowTypeTitle.equals(rowType)){
lastTitleRow = t;
break;
}
}
}
if(!remainingRows.isEmpty()){
JSONObject firstRemainingRow = remainingRows.getJSONObject(0);
if(!rowTypeTitle.equals(firstRemainingRow.getString(rowTypeKey))){
remainingRows.add(0, lastTitleRow);
}
}
pageRowList.add(pageRow);
}
//在最后一页插入“以下为空”
JSONArray pageRow = pageRowList.getJSONArray(pageRowList.size() - 1);
if(pageRow.size() < maxRowCount){
JSONObject t = new JSONObject();
t.put(colPrefix + "01", emptyText);
pageRow.add(t.clone());
}
return pageRowList;
}
private JSONArray assembleEmpty(JSONObject configJson, JSONArray rowList){
JSONArray fixedCol = configJson.getJSONArray("fixedCol"); //固定列,举例:["SMP_NAME", "SMP_CD"]
if(fixedCol == null) fixedCol = new JSONArray();
JSONArray fixedColRight = configJson.getJSONArray("fixedColRight"); //右侧固定列
if(fixedColRight == null) fixedColRight = new JSONArray();
@@ -150,7 +223,8 @@ public class ReportDocumentDataServiceImpl implements ReportDocumentDataService
fixedColCount_left = fixedCol.size();
fixedColCount_right = fixedColRight.size();
for(JSONObject row : rowList){
for(int i = 0; i < rowList.size(); i++){
JSONObject row = rowList.getJSONObject(i);
if(" ".equals(row.getString(colPrefix + "01")))
continue;
int colIndex = 1;
@@ -169,15 +243,15 @@ public class ReportDocumentDataServiceImpl implements ReportDocumentDataService
* @param fieldList 要显示的字段列表。固定字段在最前
* @param customConfig 报告配置项
* */
private List<JSONObject> assembleStep1(List<JSONObject> rowList, List<ConfigReportFieldDO> fieldList, List<ReportDocumentDataDO> dataList, String customConfig, int groupIndex){
private JSONArray assembleStep1(JSONArray rowList, List<ConfigReportFieldDO> fieldList, List<ReportDocumentDataDO> dataList, String customConfig, int groupIndex){
if(dataList.isEmpty())
return new ArrayList<>();
return new JSONArray();
JSONObject jsonObject = JSONObject.parseObject(customConfig);
Integer dynamicColCount = 3;
Integer fixedColCount_left = 0;
String dynamicColCountStr = jsonObject.getString("dynamicColCount"); //动态列(检测项)数量
JSONArray fixedCol = jsonObject.getJSONArray("fixedCol"); //固定列,举例:["sampleName", "sampleCode"]
JSONArray fixedCol = jsonObject.getJSONArray("fixedCol"); //固定列,举例:["SMP_NAME", "SMP_CD"]
if(fixedCol == null) fixedCol = new JSONArray();
JSONArray fixedColRight = jsonObject.getJSONArray("fixedColRight"); //右侧固定列
if(fixedColRight == null) fixedColRight = new JSONArray();
@@ -225,7 +299,9 @@ public class ReportDocumentDataServiceImpl implements ReportDocumentDataService
for(int i = 0; i < dynamicFieldListGroup.size(); i++){
List<ConfigReportFieldDO> dynamicFieldList = dynamicFieldListGroup.get(i);
t = new JSONObject();
t.put(rowTypeKey, rowTypeTitle);
r = new JSONObject();
r.put(rowTypeKey, rowTypeLimit);
colIndex = fixedColCount_left + 1; //第二组以后,从固定列数开始
for(ConfigReportFieldDO fieldDO : dynamicFieldList) {
String field = fieldDO.getField();
@@ -267,7 +343,7 @@ public class ReportDocumentDataServiceImpl implements ReportDocumentDataService
* @param
*
* */
private void addTitleToRowList(JSONArray fixedCol, JSONArray fixedColRight, Integer dynamicColCount, JSONObject t, List<JSONObject> rowList, List<ConfigReportFieldDO> fieldList){
private void addTitleToRowList(JSONArray fixedCol, JSONArray fixedColRight, Integer dynamicColCount, JSONObject t, JSONArray rowList, List<ConfigReportFieldDO> fieldList){
//处理固定列
int index = 1;
String colKey = "";
@@ -281,12 +357,12 @@ public class ReportDocumentDataServiceImpl implements ReportDocumentDataService
String field = fieldDO.getField();
if (FIELD_DYNAMIC.equals(fieldType) || FIELD_CALCULATED.equals(fieldType))
continue;
if("sampleNameCode".equals( col) && "SMP_NAME_CD".equals(field)
|| "sampleName".equals( col) && "SMP_NAME".equals(field)
|| "sampleCode".equals( col) && "SMP_CD".equals(field)
|| "entrustSampleName".equals( col) && "ENTT_SMP_NAME".equals(field)
|| "entrustSampleCode".equals( col) && "ENTT_SMP_CD".equals(field)
|| "entrustSampleNameCode".equals( col) && "ENTT_SMP_NAME_CD".equals(field)){
if(sampleNameAndCodeKey.equals( col) && sampleNameAndCodeKey.equals(field)
|| sampleNameKey.equals( col) && sampleNameKey.equals(field)
|| sampleCodeKey.equals( col) && sampleCodeKey.equals(field)
|| entrustSampleNameKey.equals( col) && entrustSampleNameKey.equals(field)
|| entrustSampleCodeKey.equals( col) && entrustSampleCodeKey.equals(field)
|| entrustSampleNameCodeKey.equals( col) && entrustSampleNameCodeKey.equals(field)){
colKey = parseNumToString(index, 2);
}
if(!ObjectUtils.isEmpty(colKey)){
@@ -306,12 +382,12 @@ public class ReportDocumentDataServiceImpl implements ReportDocumentDataService
String field = fieldDO.getField();
if (FIELD_DYNAMIC.equals(fieldType) || FIELD_CALCULATED.equals(fieldType))
continue;
if("sampleNameCode".equals( col) && "SMP_NAME_CD".equals(field)
|| "sampleName".equals( col) && "SMP_NAME".equals(field)
|| "sampleCode".equals( col) && "SMP_CD".equals(field)
|| "entrustSampleName".equals( col) && "ENTT_SMP_NAME".equals(field)
|| "entrustSampleCode".equals( col) && "ENTT_SMP_CD".equals(field)
|| "entrustSampleNameCode".equals( col) && "ENTT_SMP_NAME_CD".equals(field)){
if(sampleNameAndCodeKey.equals( col) && sampleNameAndCodeKey.equals(field)
|| sampleNameKey.equals( col) && sampleNameKey.equals(field)
|| sampleCodeKey.equals( col) && sampleCodeKey.equals(field)
|| entrustSampleNameKey.equals( col) && entrustSampleNameKey.equals(field)
|| entrustSampleCodeKey.equals( col) && entrustSampleCodeKey.equals(field)
|| entrustSampleNameCodeKey.equals( col) && entrustSampleNameCodeKey.equals(field)){
colKey = parseNumToString(index, 2);
}
if(!ObjectUtils.isEmpty(colKey)){
@@ -324,7 +400,7 @@ public class ReportDocumentDataServiceImpl implements ReportDocumentDataService
}
//添加行数据
private void addDataToRowList(JSONArray fixedCol, JSONArray fixedColRight, List<ConfigReportFieldDO> fieldList, List<ReportDocumentDataDO> dataList, List<JSONObject> rowList, int fixedColCount, int dynamicColCount, String hasRemark){
private void addDataToRowList(JSONArray fixedCol, JSONArray fixedColRight, List<ConfigReportFieldDO> fieldList, List<ReportDocumentDataDO> dataList, JSONArray rowList, int fixedColCount, int dynamicColCount, String hasRemark){
int colIndex = 0;
JSONObject t = new JSONObject();
for(ReportDocumentDataDO dataDO : dataList){
@@ -385,19 +461,19 @@ public class ReportDocumentDataServiceImpl implements ReportDocumentDataService
for(int i=0;i<fixedCol.size();i++){
String col = fixedCol.getString(i);
colKey = parseNumToString(index, 2);
if("sampleName".equals(col)){
if(sampleNameKey.equals(col)){
t.put(colPrefix + colKey, dataDO.getSampleName());
}
else if("sampleCode".equals(col)){
else if(sampleCodeKey.equals(col)){
t.put(colPrefix + colKey, dataDO.getSampleCode());
}
else if("sampleNameCode".equals( col)){
else if(sampleNameAndCodeKey.equals( col)){
t.put(colPrefix + colKey, dataDO.getSampleName() + " " + dataDO.getSampleCode());
}else if("entrustSampleName".equals( col)){
}else if(entrustSampleNameKey.equals( col)){
t.put(colPrefix + colKey, dataDO.getEntrustSampleName());
}else if("entrustSampleCode".equals( col)){
}else if(entrustSampleCodeKey.equals( col)){
t.put(colPrefix + colKey, dataDO.getEntrustSampleCode());
}else if("entrustSampleNameCode".equals( col)){
}else if(entrustSampleNameCodeKey.equals( col)){
t.put(colPrefix + colKey, dataDO.getEntrustSampleName() + " " + dataDO.getEntrustSampleCode());
}
index ++;
@@ -407,19 +483,19 @@ public class ReportDocumentDataServiceImpl implements ReportDocumentDataService
for(int i=0;i<fixedColRight.size();i++){
String col = fixedColRight.getString(i);
colKey = parseNumToString(index, 2);
if("sampleName".equals(col)){
if(sampleNameKey.equals(col)){
t.put(colPrefix + colKey, dataDO.getSampleName());
}
else if("sampleCode".equals(col)){
else if(sampleCodeKey.equals(col)){
t.put(colPrefix + colKey, dataDO.getSampleCode());
}
else if("sampleNameCode".equals( col)){
else if(sampleNameAndCodeKey.equals( col)){
t.put(colPrefix + colKey, dataDO.getSampleName() + " " + dataDO.getSampleCode());
}else if("entrustSampleName".equals( col)){
}else if(entrustSampleNameKey.equals( col)){
t.put(colPrefix + colKey, dataDO.getEntrustSampleName());
}else if("entrustSampleCode".equals( col)){
}else if(entrustSampleCodeKey.equals( col)){
t.put(colPrefix + colKey, dataDO.getEntrustSampleCode());
}else if("entrustSampleNameCode".equals( col)){
}else if(entrustSampleNameCodeKey.equals( col)){
t.put(colPrefix + colKey, dataDO.getEntrustSampleName() + " " + dataDO.getEntrustSampleCode());
}
index ++;
@@ -429,13 +505,6 @@ public class ReportDocumentDataServiceImpl implements ReportDocumentDataService
}
}
/**
* 处理检出限
* 如果是
* */
private void addRangeToRowList(JSONObject r, List<JSONObject> rowList){
rowList.add(r.clone());
}
private String parseNumToString(int num, int len){
StringBuilder sb = new StringBuilder();
@@ -448,7 +517,7 @@ public class ReportDocumentDataServiceImpl implements ReportDocumentDataService
/**
* 组装固定检测项的数据以检测项作为数据Key
* */
private List<JSONObject> assembleFixedFieldsData(List<ConfigReportFieldDO> fieldList, List<ReportDocumentDataDO> dataList, String customConfig){
private JSONArray assembleFixedFieldsData(List<ConfigReportFieldDO> fieldList, List<ReportDocumentDataDO> dataList, String customConfig){
JSONObject jsonObject = JSONObject.parseObject(customConfig);
Integer maxRowCount = 3;
@@ -459,7 +528,7 @@ public class ReportDocumentDataServiceImpl implements ReportDocumentDataService
JSONArray fixedFieldsArray = JSONArray.parseArray(fixedFields);
if(!ObjectUtils.isEmpty(maxRowCountStr)) maxRowCount = Integer.parseInt(maxRowCountStr);
List<JSONObject> rowList = new ArrayList<>();
JSONArray rowList = new JSONArray();
for(int i = 0; i < maxRowCount; i++){
JSONObject row = new JSONObject();
row.put("sampleNameCode", " ");
@@ -520,7 +589,7 @@ public class ReportDocumentDataServiceImpl implements ReportDocumentDataService
* 组装纵表数据
* 第1列固定为元素 第2列方法检出限%);第三列开始为各个样品
* */
private List<JSONObject> assembleVerticalData(List<ConfigReportFieldDO> fieldList, List<ReportDocumentDataDO> dataList, String customConfig){
private JSONArray assembleVerticalData(List<ConfigReportFieldDO> fieldList, List<ReportDocumentDataDO> dataList, String customConfig){
JSONObject jsonObject = JSONObject.parseObject(customConfig);
Integer dynamicColCount = 3;
Integer fixedColCount = 2;
@@ -539,7 +608,7 @@ public class ReportDocumentDataServiceImpl implements ReportDocumentDataService
if(!ObjectUtils.isEmpty(content)){
firstData = JSONObject.parseObject(content);
}
List<JSONObject> rowList = new ArrayList<>();
JSONArray rowList = new JSONArray();
//处理第一行-样品编号
int colIndex = fixedColCount + 1;
int rowIndex = 1;