|
@@ -0,0 +1,176 @@
|
|
|
+package thyyxxk.webserver.service.highreport;
|
|
|
+
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
+import org.jetbrains.annotations.NotNull;
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.stereotype.Service;
|
|
|
+import thyyxxk.webserver.config.exception.ExceptionEnum;
|
|
|
+import thyyxxk.webserver.dao.his.highreport.HighReportDao;
|
|
|
+import thyyxxk.webserver.entity.HeadInfo;
|
|
|
+import thyyxxk.webserver.entity.ResultVo;
|
|
|
+import thyyxxk.webserver.entity.highreport.HighReportDto;
|
|
|
+import thyyxxk.webserver.entity.highreport.ReportBaseInfo;
|
|
|
+import thyyxxk.webserver.entity.highreport.ReportType;
|
|
|
+import thyyxxk.webserver.entity.highreport.TableDisplayInfo;
|
|
|
+import thyyxxk.webserver.utils.ExcelUtil;
|
|
|
+import thyyxxk.webserver.utils.ReportUtil;
|
|
|
+import thyyxxk.webserver.utils.ResultVoUtil;
|
|
|
+import thyyxxk.webserver.utils.StringUtil;
|
|
|
+
|
|
|
+import javax.servlet.http.HttpServletResponse;
|
|
|
+import java.util.ArrayList;
|
|
|
+import java.util.HashMap;
|
|
|
+import java.util.List;
|
|
|
+import java.util.Map;
|
|
|
+import java.util.stream.Collectors;
|
|
|
+
|
|
|
+/**
|
|
|
+ * @Description: 报表统一门户入口
|
|
|
+ * @Author: hsh
|
|
|
+ * @CreateTime: 2022-10-31 17:04
|
|
|
+ * @Version: 1.0
|
|
|
+ */
|
|
|
+@Service
|
|
|
+@Slf4j
|
|
|
+public class HighReportService {
|
|
|
+
|
|
|
+ private final HighReportDao dao;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ public HighReportService(HighReportDao dao) {
|
|
|
+ this.dao = dao;
|
|
|
+ }
|
|
|
+
|
|
|
+ public ResultVo<Map<String, Object>> selectReportPortal(HighReportDto dto) {
|
|
|
+ Map<String, Object> map = new HashMap<>();
|
|
|
+ String reportType = dto.getReportType();
|
|
|
+ String menuId = dto.getMenuId();
|
|
|
+ String title = ReportType.find(reportType);
|
|
|
+ List<ReportBaseInfo> reportList = dao.selectReportPortal(reportType, menuId);
|
|
|
+ map.put("title", title);
|
|
|
+ map.put("data", reportList);
|
|
|
+ return ResultVoUtil.success(map);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * @description: 单个报表查询页面数据展示以及数据结果信息
|
|
|
+ * @author: hsh
|
|
|
+ * @date: 2022/11/4 9:10
|
|
|
+ * @param: [dto]
|
|
|
+ * @return: ResultVo<ReportBaseInfo>
|
|
|
+ **/
|
|
|
+ public ResultVo<ReportBaseInfo> selectReportPortalMenu(HighReportDto dto) {
|
|
|
+ String reportId = dto.getReportId();
|
|
|
+ String menuId = dto.getMenuId();
|
|
|
+ String reportType = dto.getReportType();
|
|
|
+ ReportBaseInfo report = dao.selectReportPortalMenu(reportId, menuId, reportType);
|
|
|
+ if(null == report){
|
|
|
+ log.info("没有配置报表参数");
|
|
|
+ return ResultVoUtil.fail(ExceptionEnum.NO_DATA_EXIST);
|
|
|
+ }
|
|
|
+
|
|
|
+ List<TableDisplayInfo> tableInfoList = dao.selectTableDisplayInfoByReportId(reportId);
|
|
|
+ if(tableInfoList.size() == 0){
|
|
|
+ log.info("没有配置报表页面展示表格信息");
|
|
|
+ return ResultVoUtil.fail(ExceptionEnum.NO_DATA_EXIST);
|
|
|
+ }
|
|
|
+ report.setTableDisplays(tableInfoList);
|
|
|
+
|
|
|
+ // 根据配置的sql查询数据结果
|
|
|
+ // 组装查询sql
|
|
|
+ String baseSql = report.getBaseSql();
|
|
|
+ String gatherSql = report.getGatherSql();
|
|
|
+ String sql = ReportUtil.callSqlFormat(dto, baseSql, gatherSql);
|
|
|
+ log.info("查询sql: {}", sql);
|
|
|
+ // 查询结果
|
|
|
+ List<Map<String, Object>> dataList = dao.selectReportPortalData(reportId, menuId, reportType, sql);
|
|
|
+ report.setDataList(dataList);
|
|
|
+ return ResultVoUtil.success(report);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * @description: 报表数据导出
|
|
|
+ * @author: hsh
|
|
|
+ * @date: 2022/11/7 10:26
|
|
|
+ * @param: [response, dto]
|
|
|
+ * @return: ResultVo<String>
|
|
|
+ **/
|
|
|
+ public ResultVo<Map<String, Object>> exportReportPortalData(HttpServletResponse response, HighReportDto dto){
|
|
|
+ Map<String, Object> map = new HashMap<>();
|
|
|
+ ResultVo<ReportBaseInfo> resultVo = selectReportPortalMenu(dto);
|
|
|
+ ReportBaseInfo report = resultVo.getData();
|
|
|
+ if(null == report || report.getDataList().size() == 0){
|
|
|
+ map.put("code", 1);
|
|
|
+ map.put("massage", "查询数据为空,导出失败!");
|
|
|
+ return ResultVoUtil.success(map);
|
|
|
+ }
|
|
|
+
|
|
|
+ // 数据
|
|
|
+ List<Map<String, Object>> list = report.getDataList();
|
|
|
+ // 合计
|
|
|
+ if(null != report.getTotal() && report.getTotal().size() != 0){
|
|
|
+ Map<String, Object> total = report.getTotal();
|
|
|
+ list.add(total);
|
|
|
+ }
|
|
|
+
|
|
|
+ // 导出标题
|
|
|
+ String exportName = dto.getExportName();
|
|
|
+
|
|
|
+ // 组装表头
|
|
|
+ List<HeadInfo> headInfoList = calHeadList(report.getTableDisplays());
|
|
|
+
|
|
|
+ // 导出
|
|
|
+ ExcelUtil.exportExcelReport(response, list, headInfoList, exportName);
|
|
|
+
|
|
|
+ map.put("code", 0);
|
|
|
+ map.put("massage", "导出成功!");
|
|
|
+ return ResultVoUtil.success(map);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * @description: 组装导出的表头
|
|
|
+ * @author: hsh
|
|
|
+ * @date: 2022/11/7 11:17
|
|
|
+ * @param: [tableDisplays]
|
|
|
+ * @return: List<HeadInfo>
|
|
|
+ **/
|
|
|
+ private @NotNull List<HeadInfo> calHeadList(@NotNull List<TableDisplayInfo> tableDisplays) {
|
|
|
+ List<HeadInfo> headList = new ArrayList<>();
|
|
|
+ Map<String, List<TableDisplayInfo>> map = tableDisplays.stream().collect(
|
|
|
+ Collectors.groupingBy(TableDisplayInfo::getPid, Collectors.toList()));
|
|
|
+
|
|
|
+ for(Map.Entry<String, List<TableDisplayInfo>> entry : map.entrySet()){
|
|
|
+ String pid = entry.getKey();
|
|
|
+ List<TableDisplayInfo> list = entry.getValue();
|
|
|
+ HeadInfo headInfo = new HeadInfo();
|
|
|
+ List<HeadInfo> headInfoChildrenList = new ArrayList<>();
|
|
|
+ for(TableDisplayInfo table : list){
|
|
|
+ String id = table.getId();
|
|
|
+ /*
|
|
|
+ * 重要:一定要维护父id!!!一定要维护父id!!!一定要维护父id!!!
|
|
|
+ * 表格行父id是独一无二的一个,证明是单独一个表格个列,没有多表头,放在同一个表头对象中
|
|
|
+ * 如果表格行id与表格行父id是一样的话,表示这条记录是此表格行的父级行,其余的都是表格的子级行
|
|
|
+ *(例如二级表头pid与id父子关系:1 1,1 2,1 3,1 4 其中1 1是父级表头,1 2、1 3、1 4都是它的子级表头)
|
|
|
+ */
|
|
|
+ if(StringUtil.notBlank(pid) && id.equals(pid)){
|
|
|
+ headInfo.setDisplay(table.getLabel());
|
|
|
+ headInfo.setName(table.getProp());
|
|
|
+ } else if(StringUtil.notBlank(pid) && !id.equals(pid)){
|
|
|
+ HeadInfo headInfoChild = new HeadInfo();
|
|
|
+ headInfoChild.setDisplay(table.getLabel());
|
|
|
+ headInfoChild.setName(table.getProp());
|
|
|
+ headInfoChildrenList.add(headInfoChild);
|
|
|
+ } else {
|
|
|
+ headInfo.setDisplay(table.getLabel());
|
|
|
+ headInfo.setName(table.getProp());
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if(headInfoChildrenList.size() > 0){
|
|
|
+ headInfo.setColumns(headInfoChildrenList);
|
|
|
+ }
|
|
|
+ headList.add(headInfo);
|
|
|
+ }
|
|
|
+ return headList;
|
|
|
+ }
|
|
|
+
|
|
|
+}
|