|  | @@ -1,20 +1,19 @@
 | 
	
		
			
				|  |  |  package cn.hnthyy.thmz.service.impl.his.bb;
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  | -import cn.hnthyy.thmz.Utils.DateUtil;
 | 
	
		
			
				|  |  | +import cn.hnthyy.thmz.Utils.ReportUsedUtil;
 | 
	
		
			
				|  |  |  import cn.hnthyy.thmz.entity.his.bb.ReportBaseInfo;
 | 
	
		
			
				|  |  |  import cn.hnthyy.thmz.mapper.his.bb.ReportStaticMapper;
 | 
	
		
			
				|  |  |  import cn.hnthyy.thmz.service.his.bb.ReportStaticService;
 | 
	
		
			
				|  |  |  import cn.hnthyy.thmz.vo.ReportVo;
 | 
	
		
			
				|  |  | -import org.apache.commons.lang3.StringUtils;
 | 
	
		
			
				|  |  |  import org.springframework.beans.factory.annotation.Autowired;
 | 
	
		
			
				|  |  |  import org.springframework.stereotype.Service;
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  |  import java.util.ArrayList;
 | 
	
		
			
				|  |  | -import java.util.Date;
 | 
	
		
			
				|  |  |  import java.util.HashMap;
 | 
	
		
			
				|  |  |  import java.util.List;
 | 
	
		
			
				|  |  |  import java.util.Map;
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  |  /**
 | 
	
		
			
				|  |  |   * @Description: 报表统计实现类
 | 
	
		
			
				|  |  |   * @Author: hsh
 | 
	
	
		
			
				|  | @@ -24,44 +23,6 @@ import java.util.Map;
 | 
	
		
			
				|  |  |  @Service
 | 
	
		
			
				|  |  |  public class ReportStaticServiceImpl implements ReportStaticService {
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  | -    private static final String START_TIME = ":startTime";
 | 
	
		
			
				|  |  | -    private static final String END_TIME = ":endTime";
 | 
	
		
			
				|  |  | -    /**
 | 
	
		
			
				|  |  | -     * 科室(下拉选查询使用)
 | 
	
		
			
				|  |  | -     **/
 | 
	
		
			
				|  |  | -    private static final String DEPT = "dept";
 | 
	
		
			
				|  |  | -    /**
 | 
	
		
			
				|  |  | -     * 医生(下拉选查询使用)
 | 
	
		
			
				|  |  | -     **/
 | 
	
		
			
				|  |  | -    private static final String DOCTOR = "doctor";
 | 
	
		
			
				|  |  | -    /**
 | 
	
		
			
				|  |  | -     * 科室id
 | 
	
		
			
				|  |  | -     **/
 | 
	
		
			
				|  |  | -    private static final String DEPT_ID = "deptId";
 | 
	
		
			
				|  |  | -    /**
 | 
	
		
			
				|  |  | -     * 医生id
 | 
	
		
			
				|  |  | -     **/
 | 
	
		
			
				|  |  | -    private static final String DOCTOR_ID = "doctorId";
 | 
	
		
			
				|  |  | -    /**
 | 
	
		
			
				|  |  | -     * 诊断(模糊查询使用)
 | 
	
		
			
				|  |  | -     **/
 | 
	
		
			
				|  |  | -    private static final String DIAGN = "diagn";
 | 
	
		
			
				|  |  | -
 | 
	
		
			
				|  |  | -    /**
 | 
	
		
			
				|  |  | -     * 住院号
 | 
	
		
			
				|  |  | -     **/
 | 
	
		
			
				|  |  | -    private static final String ZYH = "zyh";
 | 
	
		
			
				|  |  | -    /**
 | 
	
		
			
				|  |  | -     * 姓名(模糊查询使用)
 | 
	
		
			
				|  |  | -     **/
 | 
	
		
			
				|  |  | -    private static final String XM = "xm";
 | 
	
		
			
				|  |  | -    /**
 | 
	
		
			
				|  |  | -     * 号别(下拉选查询使用)
 | 
	
		
			
				|  |  | -     **/
 | 
	
		
			
				|  |  | -    private static final String REG_LEVEL = "regLevel";
 | 
	
		
			
				|  |  | -
 | 
	
		
			
				|  |  | -    private static final String OTHER_PARAM = "其他";
 | 
	
		
			
				|  |  | -
 | 
	
		
			
				|  |  |      @SuppressWarnings("all")
 | 
	
		
			
				|  |  |      @Autowired
 | 
	
		
			
				|  |  |      private ReportStaticMapper reportStaticMapper;
 | 
	
	
		
			
				|  | @@ -73,13 +34,13 @@ public class ReportStaticServiceImpl implements ReportStaticService {
 | 
	
		
			
				|  |  |          String reportId = vo.getReportId();
 | 
	
		
			
				|  |  |          List<String> reportIds = vo.getReportIds();
 | 
	
		
			
				|  |  |          String type = vo.getType();
 | 
	
		
			
				|  |  | -        List<Map<String, Object>> list = new ArrayList<>();
 | 
	
		
			
				|  |  |          if(reportIds != null && reportIds.size() > 0){
 | 
	
		
			
				|  |  |              for (String id: reportIds) {
 | 
	
		
			
				|  |  | +                List<Map<String, Object>> list = new ArrayList<>();
 | 
	
		
			
				|  |  |                  Map<String, Object> map = new HashMap<>();
 | 
	
		
			
				|  |  |                  ReportBaseInfo info = reportStaticMapper.selectReportBaseInfo(id, vo.getReportType(), null);
 | 
	
		
			
				|  |  |                  if("1".equals(type)){
 | 
	
		
			
				|  |  | -                    String sql = callSqlFormat(vo, info.getBaseSql(), info.getGatherSql(), "", info.getReportSort());
 | 
	
		
			
				|  |  | +                    String sql = ReportUsedUtil.callSqlFormat(vo, info.getBaseSql(), info.getGatherSql(), "", info.getReportSort());
 | 
	
		
			
				|  |  |                      String result = reportStaticMapper.selectReportTotal(sql);
 | 
	
		
			
				|  |  |                      map.put("label", info.getReportName());
 | 
	
		
			
				|  |  |                      map.put("value", result);
 | 
	
	
		
			
				|  | @@ -87,16 +48,22 @@ public class ReportStaticServiceImpl implements ReportStaticService {
 | 
	
		
			
				|  |  |                      resultMap.put(info.getReportId(), list);
 | 
	
		
			
				|  |  |                  } else if("2".equals(type)) {
 | 
	
		
			
				|  |  |                      String gatherSql = info.getGatherSql() + " as value,  t." + vo.getGroupColumn() + " ";
 | 
	
		
			
				|  |  | -                    String sql = callSqlFormat(vo, info.getBaseSql(), gatherSql, vo.getGroupColumn(), info.getReportSort());
 | 
	
		
			
				|  |  | +                    String sql = ReportUsedUtil.callSqlFormat(vo, info.getBaseSql(), gatherSql, vo.getGroupColumn(), vo.getGroupColumn());
 | 
	
		
			
				|  |  |                      List<Map<String, Object>> l = reportStaticMapper.selectReportRegList(sql);
 | 
	
		
			
				|  |  | +                    List<Object> vl = new ArrayList<>();
 | 
	
		
			
				|  |  | +                    for (Map<String, Object> m : l) {
 | 
	
		
			
				|  |  | +                        vl.add(ReportUsedUtil.formatDataForUse(vo.getGroupColumn(), m.get(vo.getGroupColumn())));
 | 
	
		
			
				|  |  | +                    }
 | 
	
		
			
				|  |  |                      resultMap.put(info.getReportId(), l);
 | 
	
		
			
				|  |  | +                    resultMap.put(vo.getGroupColumn(), vl);
 | 
	
		
			
				|  |  |                  }
 | 
	
		
			
				|  |  |              }
 | 
	
		
			
				|  |  |          } else {
 | 
	
		
			
				|  |  |              Map<String, Object> map = new HashMap<>();
 | 
	
		
			
				|  |  | +            List<Map<String, Object>> list = new ArrayList<>();
 | 
	
		
			
				|  |  |              ReportBaseInfo info = reportStaticMapper.selectReportBaseInfo(reportId, vo.getReportType(), null);
 | 
	
		
			
				|  |  |              if("1".equals(type)) {
 | 
	
		
			
				|  |  | -                String sql = callSqlFormat(vo, info.getBaseSql(), info.getGatherSql(), "", info.getReportSort());
 | 
	
		
			
				|  |  | +                String sql = ReportUsedUtil.callSqlFormat(vo, info.getBaseSql(), info.getGatherSql(), "", info.getReportSort());
 | 
	
		
			
				|  |  |                  String result = reportStaticMapper.selectReportTotal(sql);
 | 
	
		
			
				|  |  |                  map.put("label", info.getReportName());
 | 
	
		
			
				|  |  |                  map.put("value", result);
 | 
	
	
		
			
				|  | @@ -104,74 +71,17 @@ public class ReportStaticServiceImpl implements ReportStaticService {
 | 
	
		
			
				|  |  |                  resultMap.put(reportId, list);
 | 
	
		
			
				|  |  |              } else if("2".equals(type)) {
 | 
	
		
			
				|  |  |                  String gatherSql = info.getGatherSql() + " as value,  t." + vo.getGroupColumn() + " ";
 | 
	
		
			
				|  |  | -                String sql = callSqlFormat(vo, info.getBaseSql(), gatherSql, vo.getGroupColumn(), info.getReportSort());
 | 
	
		
			
				|  |  | +                String sql = ReportUsedUtil.callSqlFormat(vo, info.getBaseSql(), gatherSql, vo.getGroupColumn(), vo.getGroupColumn());
 | 
	
		
			
				|  |  |                  List<Map<String, Object>> l = reportStaticMapper.selectReportRegList(sql);
 | 
	
		
			
				|  |  | +                List<Object> vl = new ArrayList<>();
 | 
	
		
			
				|  |  | +                for (Map<String, Object> m : l) {
 | 
	
		
			
				|  |  | +                    vl.add(ReportUsedUtil.formatDataForUse(vo.getGroupColumn(), m.get(vo.getGroupColumn())));
 | 
	
		
			
				|  |  | +                }
 | 
	
		
			
				|  |  |                  resultMap.put(info.getReportId(), l);
 | 
	
		
			
				|  |  | +                resultMap.put(vo.getGroupColumn(), vl);
 | 
	
		
			
				|  |  |              }
 | 
	
		
			
				|  |  |          }
 | 
	
		
			
				|  |  |          return resultMap;
 | 
	
		
			
				|  |  |      }
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  | -    /**
 | 
	
		
			
				|  |  | -     * @description:
 | 
	
		
			
				|  |  | -     * @author: hsh
 | 
	
		
			
				|  |  | -     * @date: 2023/8/14 17:39
 | 
	
		
			
				|  |  | -     * @param: [vo, baseSql, gatherSql, groupColumn, reportSort] vo 参数实体类;baseSql:基础sql;gatherSql:聚合sql;groupColumn:分组;reportSort:排序;
 | 
	
		
			
				|  |  | -     * @return: String
 | 
	
		
			
				|  |  | -     **/
 | 
	
		
			
				|  |  | -    private String callSqlFormat(ReportVo dto, String baseSql, String gatherSql, String groupColumn, String reportSort) {
 | 
	
		
			
				|  |  | -
 | 
	
		
			
				|  |  | -        // 查询必填字段(包括开始时间,结束时间,钻取科室,医生等sql用:拼接的字符)替换
 | 
	
		
			
				|  |  | -        if(baseSql.contains(START_TIME)){
 | 
	
		
			
				|  |  | -            if(StringUtils.isNotEmpty(dto.getStartTime())){
 | 
	
		
			
				|  |  | -                baseSql = baseSql.replaceAll(START_TIME, "'" + dto.getStartTime() + "'");
 | 
	
		
			
				|  |  | -            } else {
 | 
	
		
			
				|  |  | -                baseSql = baseSql.replaceAll(START_TIME,"'2012-01-01 00:00:00'");
 | 
	
		
			
				|  |  | -            }
 | 
	
		
			
				|  |  | -        }
 | 
	
		
			
				|  |  | -        if(baseSql.contains(END_TIME)){
 | 
	
		
			
				|  |  | -            if(StringUtils.isNotEmpty(dto.getEndTime())){
 | 
	
		
			
				|  |  | -                baseSql = baseSql.replaceAll(END_TIME, "'" + dto.getEndTime() + "'");
 | 
	
		
			
				|  |  | -            } else {
 | 
	
		
			
				|  |  | -                baseSql = baseSql.replaceAll(END_TIME, DateUtil.fomart(DateUtil.getLastSecond(new Date()), "yyyy-MM-dd HH:mm:ss"));
 | 
	
		
			
				|  |  | -            }
 | 
	
		
			
				|  |  | -        }
 | 
	
		
			
				|  |  | -        // 查询非必填字段(一般是下拉选险种,模糊查询科室、诊断等sql用_拼接的字符)
 | 
	
		
			
				|  |  | -        StringBuilder sql = new StringBuilder();
 | 
	
		
			
				|  |  | -        sql.append(" select ").append(gatherSql).append(" from (").append(baseSql).append(") t where 1 = 1 ");
 | 
	
		
			
				|  |  | -        if(baseSql.contains(DEPT_ID) && StringUtils.isNotEmpty(dto.getDeptId())){
 | 
	
		
			
				|  |  | -            sql.append(" and t.deptId = '").append(dto.getDeptId()).append("' ");
 | 
	
		
			
				|  |  | -        }
 | 
	
		
			
				|  |  | -        if(baseSql.contains(DOCTOR_ID) && StringUtils.isNotEmpty(dto.getDoctorId())){
 | 
	
		
			
				|  |  | -            sql.append(" and t.doctorId = '").append(dto.getDoctorId()).append("' ");
 | 
	
		
			
				|  |  | -        }
 | 
	
		
			
				|  |  | -        if(baseSql.contains(DEPT) && StringUtils.isNotEmpty(dto.getDept())){
 | 
	
		
			
				|  |  | -            sql.append(" and t.dept = '").append(dto.getDept()).append("' ");
 | 
	
		
			
				|  |  | -        }
 | 
	
		
			
				|  |  | -        if(baseSql.contains(DOCTOR) && StringUtils.isNotEmpty(dto.getDoctor())){
 | 
	
		
			
				|  |  | -            sql.append(" and t.doctor = '").append(dto.getDoctor()).append("' ");
 | 
	
		
			
				|  |  | -        }
 | 
	
		
			
				|  |  | -        if(baseSql.contains(REG_LEVEL) && StringUtils.isNotEmpty(dto.getRegLevel())){
 | 
	
		
			
				|  |  | -            sql.append(" and t.regLevel = '").append(dto.getRegLevel()).append("' ");
 | 
	
		
			
				|  |  | -        }
 | 
	
		
			
				|  |  | -        if(baseSql.contains(DIAGN) && StringUtils.isNotEmpty(dto.getDiagn())){
 | 
	
		
			
				|  |  | -            sql.append(" and t.diagn like '%").append(dto.getDiagn()).append("%' ");
 | 
	
		
			
				|  |  | -        }
 | 
	
		
			
				|  |  | -        if(baseSql.contains(ZYH) && StringUtils.isNotEmpty(dto.getZyh())){
 | 
	
		
			
				|  |  | -            sql.append(" and t.zyh = '").append(dto.getZyh()).append("' ");
 | 
	
		
			
				|  |  | -        }
 | 
	
		
			
				|  |  | -        if(baseSql.contains(XM) && StringUtils.isNotEmpty(dto.getXm())){
 | 
	
		
			
				|  |  | -            sql.append(" and t.xm like '%").append(dto.getXm()).append("%' ");
 | 
	
		
			
				|  |  | -        }
 | 
	
		
			
				|  |  | -        // 如果存在分组情况,需要添加分组sql字段
 | 
	
		
			
				|  |  | -        if(StringUtils.isNotEmpty(groupColumn)){
 | 
	
		
			
				|  |  | -            sql.append(" group by t.").append(groupColumn);
 | 
	
		
			
				|  |  | -        }
 | 
	
		
			
				|  |  | -        // 如果存在默认排序,需要添加排序sql
 | 
	
		
			
				|  |  | -        if(StringUtils.isNotEmpty(reportSort)){
 | 
	
		
			
				|  |  | -            sql.append(" order by t.").append(reportSort);
 | 
	
		
			
				|  |  | -        }
 | 
	
		
			
				|  |  | -        return sql.toString();
 | 
	
		
			
				|  |  | -    }
 | 
	
		
			
				|  |  | -
 | 
	
		
			
				|  |  |  }
 |