報表查詢通用介面(只需要傳入資料庫檢視或者表名)
阿新 • • 發佈:2022-05-09
Controller
/** * @Classname GeneralReportModuleController * 報表查詢通用介面 * @Date 2022/3/16 11:10 上午 * @Author wjh */ @RestController public class GeneralReportModuleController implements GeneralReportModuleApi { @Autowired private GeneralReportModuleImpl generalReportModuleService; @Overridepublic ResponseEntity<ProcessSeccussResponse> generalReportModuleSearch(@Valid GeneralReportModulePostRequest body, String authorization) { int pageSize =body.getPageSize(); int pageNum = body.getPageNum() ; String contractId = body.getContractId(); String reportName= body.getReportName(); int year = body.getYear(); int month = body.getMonth() ; List<Map<Object, Object>> result = generalReportModuleService.generalReportModuleSearch(reportName,contractId, year,month,pageSize,pageNum); return ResponseEntity.ok(ResponseUtils.ok(result)); } }
Service
@Service("generalReportModule") @Slf4j public class GeneralReportModuleImpl implements GeneralReportModuleService { @Autowired private HttpServletRequest httpRequest; @Autowired private GeneralReportModuleMapper generalReportModuleMapper; private static final SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); private static final String DEFAULT_CURRENCY = "CNY"; public List<Map<Object, Object>> generalReportModuleSearch(String reportName,String contractId, int year, int month, int pageSize, int pageNum) { List<Map<Object, Object>> result = this.generalReportModuleMapper.generalReportModuleSearch(reportName,contractId, year, month,pageSize,pageNum); return result; } }
mapper
@Mapper public interface GeneralReportModuleMapper { @SelectProvider(type = GeneralReportModuleSqlProvider.class, method = "generalReportModuleSearch") List<Map<Object, Object>> generalReportModuleSearch(@Param("reportName") String reportName, @Param("contractId") String contractId, @Param("year") int year, @Param("month") int month, @Param("pageSize") int pageSize, @Param("pageNum") int pageNum); }
通用模組封裝
/* 通用報表模組封裝 */ public class GeneralReportModuleSqlProvider { private Logger logger = LoggerFactory.getLogger(GeneralReportModuleSqlProvider.class); public String generalReportModuleSearch(Map<String, Object> params) { //int pageSize = Integer.valueOf(params.get("pageSize").toString()); //int pageNum = Integer.valueOf(params.get("pageNum").toString()) ; String contractId = params.get("contractId").toString(); int year = Integer.valueOf(params.get("year").toString()); int month = Integer.valueOf(params.get("month").toString()) ; String reportName = params.get("reportName").toString(); if (StringUtils.isBlank(contractId) || year == 0 || month == 0) { this.logger.error("無效的入參contractId:{}, year: {}, month: {}", contractId, year, month); throw new ICostException(500, "無效的入參"); } String sql = "select * from "+reportName+ " where uuid= '" + contractId + "'"; String firstDayOfMonth = null; String lastDayOfMonth = null; firstDayOfMonth = getFirstDayOfMonth(year, month); lastDayOfMonth = getLastDayOfMonth(year, month); /* if (StringUtils.isNotBlank(firstDayOfMonth) && StringUtils.isNotBlank(lastDayOfMonth)) { sql += " and update_date >= '" + firstDayOfMonth + "' "; sql += " and update_date < '" + lastDayOfMonth + "' "; }*/ logger.info("通用報表查詢sql---"+sql); return sql; } public static String getFirstDayOfMonth(int year, int month) { Calendar cal = Calendar.getInstance(); //設定年份 cal.set(Calendar.YEAR, year); //設定月份 cal.set(Calendar.MONTH, month - 1); //獲取某月最小天數 int firstDay = cal.getActualMinimum(Calendar.DAY_OF_MONTH); //設定日曆中月份的最小天數 cal.set(Calendar.DAY_OF_MONTH, firstDay); //格式化日期 SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd HH:mm:ss"); String firstDayOfMonth = sdf.format(cal.getTime()); return firstDayOfMonth; } public static String getLastDayOfMonth(int year, int month) { Calendar cal = Calendar.getInstance(); //設定年份 cal.set(Calendar.YEAR, year); //設定月份 cal.set(Calendar.MONTH, month - 1); //獲取某月最大天數 int lastDay = cal.getActualMaximum(Calendar.DAY_OF_MONTH); //設定日曆中月份的最大天數 cal.set(Calendar.DAY_OF_MONTH, lastDay); //格式化日期 SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd HH:mm:ss"); String lastDayOfMonth = sdf.format(cal.getTime()); return lastDayOfMonth; } }