Merge remote-tracking branch 'origin/main'

This commit is contained in:
liuzhaotian
2024-07-24 09:11:13 +08:00
5 changed files with 72 additions and 33 deletions
@@ -1,6 +1,7 @@
package com.cf.imes.framework.mybatis.core.type;
import com.cf.imes.framework.common.util.json.JsonUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.type.JdbcType;
import org.apache.ibatis.type.StringTypeHandler;
@@ -19,25 +20,35 @@ public class CompressStringTypeHandler extends StringTypeHandler {
@Override
public void setNonNullParameter(PreparedStatement ps, int i, String parameter, JdbcType jdbcType)
throws SQLException {
if (StringUtils.isNotEmpty(parameter)) {
String compressString = JsonUtils.zipString(parameter);
ps.setString(i, compressString);
}
}
@Override
public String getNullableResult(ResultSet rs, String columnName) throws SQLException {
String compressedString = rs.getString(columnName);
return JsonUtils.unzipString(compressedString);
return unzipString(compressedString);
}
@Override
public String getNullableResult(ResultSet rs, int columnIndex) throws SQLException {
String compressedString = rs.getString(columnIndex);
return JsonUtils.unzipString(compressedString);
return unzipString(compressedString);
}
@Override
public String getNullableResult(CallableStatement cs, int columnIndex) throws SQLException {
String compressedString = cs.getString(columnIndex);
return unzipString(compressedString);
}
private String unzipString(String compressedString) {
if (StringUtils.isNotEmpty(compressedString)) {
return JsonUtils.unzipString(compressedString);
} else {
return null;
}
}
}
@@ -12,7 +12,6 @@ public final class ErrorCodeConstants {
// ========== UREPORT template模块 1-003-001-000 ==========
public static ErrorCode TEMPLATE_NOT_EXISTS = new ErrorCode(1_003_001_001, "报表模板信息不存在");
public static ErrorCode TEMPLATE_GENERATE_EXCEL_ERROR = new ErrorCode(1_003_001_002, "模板excel生成异常");
// ========== UREPORT datasource模块 1-003-002-000 ==========
public static ErrorCode DATASOURCE_NOT_EXISTS = new ErrorCode(1_003_002_001, "报表数据源不存在");
@@ -62,6 +62,14 @@ public class ReportDatasetDO extends BaseDO {
@TableField(typeHandler = CompressObjectListTypeHandler.class)
private List<ReportDatasetParameterVO> parameters;
/**
* 字段列表
*/
@TableField(typeHandler = CompressObjectListTypeHandler.class)
private List<ReportDatasetFieldVO> fields;
/**
* 组织id
*/
private Long organId;
}
@@ -2,6 +2,7 @@ package com.cf.imes.module.report.dal.dataobject.template;
import com.baomidou.mybatisplus.annotation.*;
import com.cf.imes.framework.mybatis.core.dataobject.BaseDO;
import com.cf.imes.framework.mybatis.core.type.CompressStringTypeHandler;
import com.cf.imes.module.report.dal.dataobject.datasource.ReportDatasourceDO;
import lombok.*;
@@ -13,7 +14,7 @@ import java.util.List;
* @author Gqr
* @since 2024/7/8 11:01
*/
@TableName("report_template")
@TableName(value = "report_template", autoResultMap = true)
@KeySequence("report_template_seq") // 用于 Oracle、PostgreSQL、Kingbase、DB2、H2 数据库的主键自增。如果是 MySQL 等数据库,可不写。
@Data
@EqualsAndHashCode(callSuper = true)
@@ -34,6 +35,7 @@ public class ReportTemplateDO extends BaseDO {
/**
* 报表模板
*/
@TableField(typeHandler = CompressStringTypeHandler.class)
private String content;
/**
* 模板类型,0内置、1自定义
@@ -38,7 +38,6 @@ import com.cf.imes.module.report.dal.dataobject.dataset.ReportDatasetDO;
import com.cf.imes.module.report.dal.dataobject.datasource.ReportDatasourceDO;
import com.cf.imes.module.report.dal.dataobject.template.ReportTemplateDO;
import com.cf.imes.module.report.dal.mysql.template.ReportTemplateMapper;
import com.cf.imes.module.report.enums.ErrorCodeConstants;
import com.cf.imes.module.report.service.dataset.ReportDatasetService;
import com.cf.imes.module.report.service.datasource.ReportDatasourceService;
import org.apache.commons.lang3.StringUtils;
@@ -54,6 +53,7 @@ import java.util.List;
import java.util.Map;
import static com.cf.imes.framework.common.exception.util.ServiceExceptionUtil.exception;
import static com.cf.imes.module.report.enums.ErrorCodeConstants.TEMPLATE_NOT_EXISTS;
/**
* 报表模板信息 Service 实现类
@@ -165,12 +165,14 @@ public class ReportTemplateServiceImpl implements ReportTemplateService {
*/
private void validateTemplateExists(Long id) {
if (templateMapper.selectById(id) == null) {
throw exception(ErrorCodeConstants.TEMPLATE_NOT_EXISTS);
throw exception(TEMPLATE_NOT_EXISTS);
}
}
@Override
public ReportTemplateDO getReportTemplate(Long id) {
// 校验模板
validateTemplateExists(id);
// 查询模板
ReportTemplateDO template = templateMapper.selectById(id);
if (ObjectUtil.isNotNull(template)) {
@@ -188,15 +190,29 @@ public class ReportTemplateServiceImpl implements ReportTemplateService {
@Override
public ReportDefinitionWrapper getReportTemplateDefinition(Long id) {
// 校验模板
validateTemplateExists(id);
// 查询模板
ReportTemplateDO template = templateMapper.selectById(id);
String xmlContent = template.getContent();
// 解析xml
ReportDefinition reportDefinition = new ReportParser().parse(new ByteArrayInputStream(xmlContent.getBytes()), template.getName());
List<DatasourceDefinition> datasourceDefinitions = new ArrayList<>();
if (ObjectUtil.isNotNull(template)) {
queryDatasource(template.getId(), reportDefinition);
}
return new ReportDefinitionWrapper(reportDefinition);
}
/**
* 查询数据源信息(替代ureport xml中的数据源)
*
* @param templateId 模板id
* @param reportDefinition 报表定义
*/
private void queryDatasource(Long templateId, ReportDefinition reportDefinition) {
List<DatasourceDefinition> datasourceDefinitions = new ArrayList<>();
// 查询数据源
List<ReportDatasourceDO> datasourceList = datasourceService.getTemplateDatasourceList(ReportDatasourceReqVO.builder().templateId(template.getId()).build());
List<ReportDatasourceDO> datasourceList = datasourceService.getTemplateDatasourceList(ReportDatasourceReqVO.builder().templateId(templateId).build());
for (ReportDatasourceDO ds : datasourceList) {
// 查询数据集
List<ReportDatasetDO> datasetList = datasetService.getDatasetList(ReportDatasetReqVO.builder().datasourceId(ds.getId()).build());
@@ -218,9 +234,7 @@ public class ReportTemplateServiceImpl implements ReportTemplateService {
}
}
}
}
reportDefinition.setDatasources(datasourceDefinitions);
return new ReportDefinitionWrapper(reportDefinition);
}
@Override
@@ -282,12 +296,17 @@ public class ReportTemplateServiceImpl implements ReportTemplateService {
@Override
public Report generateReport(ReportTemplateGenerateReqDTO reqDTO) {
Long templateId = reqDTO.getTemplateId();
// 校验模板
validateTemplateExists(templateId);
// 查询模板
ReportTemplateDO template = templateMapper.selectById(reqDTO.getTemplateId());
ReportTemplateDO template = templateMapper.selectById(templateId);
String xmlContent = template.getContent();
ReportRender reportRender = new ReportRender();
// 解析xml
ReportDefinition reportDefinition = reportRender.getReportDefinition(xmlContent, CharsetUtil.UTF_8);
// 查询数据源
queryDatasource(template.getId(), reportDefinition);
// 设置参数
PreviewParameters previewParameters = new PreviewParameters();
previewParameters.setQuery(reqDTO.getParams());