废话不多说,直接上代码,实践过的代码,希望对看到的朋友有所帮助
1、后台部分关键代码:
一、日增长动态曲线(按月查询)
1)// 获取日增长数据集合 List<MonthIncrNumVO> gMonthList = productMapper.getGatewayMonthIncrNum(productQuery.getGyMonth(), productQuery.getProductSlug()); // 构建日增长数据 Map<Integer, Integer> gMonthMap = new HashMap<>(31); if (!CollectionUtils.isEmpty(gMonthList)) { gatewayMap = buildMonthMap(gMonthList, productQuery); }
2)// buildMonthMap方法
private Map<Integer, Integer> buildMonthMap(List<MonthIncrNumVO> gMonthList, ProductQuery productQuery) { Map<Integer, Integer> tempMap = new HashMap<>(31); gMonthList.forEach(g -> { int day = Integer.valueOf(g.getDay().substring(8)); tempMap.put(day, g.getDayCount()); }); int year = Integer.valueOf(productQuery.getGyMonth().substring(0, 4)); int month = Integer.valueOf(productQuery.getGyMonth().substring(5)); int days = getDays(year, month); Map<Integer, Integer> monthMap = new HashMap<>(31); for (int i = 1; i <= days; i++) { if (tempMap.containsKey(i)) { monthMap.put(i, tempMap.get(i)); } else { monthMap.put(i, 0);// 没有对应日期,数量补0 } } return monthMap; }
3)获取日增长动态数据SQL
SELECT substr( t.create_date, 1, 10 ) AS day, count( * ) AS dayCount FROM tableA t WHERE t.create_date like CONCAT('%', #{month}, '%') GROUP BY substr( t.create_date, 1, 10)
// tableA 为查询的主表,month为查询的月份
二、获取月增长曲线动态数据(按年查询)
1)后台部分代码:
List<YearIncrNumVO> gyList = productMapper.getGatewayYearIncrNum(productQuery.getGYear(), productQuery.getProductSlug()); // 构建网关月增长数据 if (!CollectionUtils.isEmpty(gyList)) { gatewayMap = buildYearMap(gyList); }
2)// buildYearMap方法
private Map<Integer, Integer> buildYearMap(List<YearIncrNumVO> gyList) { Map<Integer, Integer> tempMap = new HashMap<>(12); gyList.forEach(g -> { int month = Integer.valueOf(g.getTime().substring(5)); tempMap.put(month, g.getMonCount()); }); Map<Integer, Integer> yMap = new HashMap<>(12); for (int i = 1; i <= 12; i++) { if (tempMap.containsKey(i)) { yMap.put(i, tempMap.get(i)); } else { yMap.put(i, 0);// 没有对应的月份,数量补0 } } return yMap; }
3)获取月增长动态数据SQL
SELECT substr( t.join_time, 1, 7 ) AS time, count( * ) AS monCount FROM table t WHERE t.join_time LIKE CONCAT(#{year}, '%') GROUP BY substr( t.join_time, 1, 7 )
// table为查询的主表,year 为查询的年份