@@ -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 | | g roupIndex > 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 + + ) {