java+mybatis 数据统计每天/月 并补全日期

service

public List<DemoVO> queryNum(DemoDTO dto) {
		List<DemoVO> list = baseMapper.queryNum(dto);

		/**
		 * 补全时间
		 * 查询范围:1-昨天,2-近?天,3-本月,4-本年
		 */
		List<DemoVO> result = new ArrayList<>();
		int queryRange = dto.getQueryRange();
		if (queryRange == 0 || queryRange == 1){
			if (list.size() != 1) {
				result.add(DemoVO.getTemp(LocalDate.now().minusDays(1).toString()));
			}
		} else if (queryRange == 2) {
			if (list.size() != dto.getQueryDay()) {
				LocalDate now = LocalDate.now();
				String startDate = now.minusDays(Long.parseLong(dto.getQueryDay() - 1 + "")).toString();
				String endDate = now.toString();
				result = this.completeData(1, startDate, endDate, list);
			}
		} else if (queryRange == 3) {
			LocalDate now = LocalDate.now();
			// 获取当前天数
			int day = now.getDayOfMonth();
			if (list.size() != day) {
				String startDate = now.minusDays(Long.parseLong(day - 1 + "")).toString();
				String endDate = now.toString();
				result = this.completeData(1, startDate, endDate, list);
			}
		} else {
			YearMonth now = YearMonth.now();
			// 获取当前月数
			int month = now.getMonthValue();
			if (list.size() != month) {
				String startDate = now.minusMonths(Long.parseLong(month - 1 + "")).toString();
				String endDate = now.toString();
				result = this.completeData(2, startDate, endDate, list);
			}

		}

		return result;
	}

	/**
	 * 补全数据
	 * @param dateType	日期类型:1-日,2-月
	 * @param startDate	开始日期
	 * @param endDate	结束日期
	 * @param target	未补全的列表
	 * @return 补全后的列表
	 */
	private List<DemoVO> completeData(int dateType, String startDate, String endDate, List<DemoVO> target) {

		List<DemoVO> result = new ArrayList<>();

		// 转换数据库查询到的数据
		Map<String, DemoVO> map = target.stream()
			.collect(Collectors.toMap(DemoVO::getResultDate, Function.identity()));

		if (dateType == 1) {
			// 间隔的日期列表
			List<LocalDate> dates = RangeDateUtil.getRangeDays(startDate, endDate);
			// 遍历
			dates.forEach(dateTemp -> {
				if (map.containsKey(dateTemp.toString())) {
					result.add(map.get(dateTemp.toString()));
				} else {
					// 没有这一天的数据,默认补0
					result.add(DemoVO.getTemp(dateTemp.toString()));
				}
			});
		} else {
			// 间隔的月份列表
			List<YearMonth> dates = RangeDateUtil.getRangeYears(startDate, endDate);
			// 遍历
			dates.forEach(dateTemp -> {
				if (map.containsKey(dateTemp.toString())) {
					result.add(map.get(dateTemp.toString()));
				} else {
					// 没有这一天的数据,默认补0
					result.add(DemoVO.getTemp(dateTemp.toString()));
				}
			});
		}
		return result;
	}

mapper.xml

	<!--数据统计-->
    <select id="queryNum" resultType="com.soldier.vo.DemoVO">
        select
        <!-- 日期,按年时截取到月份 -->
        <choose>
            <when test="queryRange == 4">
                DATE_FORMAT(create_time, '%Y-%m') resultDate,
            </when>
            <otherwise>
                DATE_FORMAT(create_time, '%Y-%m-%d') resultDate,
            </otherwise>
        </choose>
        <!-- 总数 -->
        count(id) as resultNum,
        from (
            select * from t_demo
            where is_deleted = 0
            <!-- 查询范围:1-昨天,2-近?天,3-本月,4-本年 -->
            <choose>
                <when test="queryRange == 1">
                    and TO_DAYS(NOW())-TO_DAYS(create_time) = 1
                </when>
                <when test="queryRange == 2">
                    and DATE_SUB(CURDATE(), INTERVAL #{queryDay} DAY) &lt;= date(update_time)
                </when>
                <when test="queryRange == 3">
                    and DATE_FORMAT(update_time, '%Y%m') = DATE_FORMAT(CURDATE() , '%Y%m')
                </when>
                <otherwise>
                    and DATE_FORMAT(update_time, '%Y') = DATE_FORMAT(CURDATE() , '%Y')
                </otherwise>
            </choose>
        ) table_temp
        group by resultDate;
    </select>

DTO和VO对象

@Data
public class DemoDTO implements Serializable {

	private static final long serialVersionUID = 1L;

	/**
	 * 查询范围:1-昨天,2-近?天,3-本月,4-本年
	 */
	private Integer queryRange;

	/**
	 * 查询天数:查询范围为2时指定的天数
	 */
	private Integer queryDay;

}

@Data
public class DemoVO implements Serializable {

	private static final long serialVersionUID = 1L;

	/**
	 * 日期
	 */
	private String resultDate;

	/**
	 * 总数
	 */
	private int resultNum;
	
	/**
	 * 空模板
	 */
	public static DemoVO getTemp(String resultDate) {
		OrderDataStatisticsVO temp = new OrderDataStatisticsVO();
		temp.setResultDate(resultDate);
		return temp;
	}
}

获取间隔的日期列表工具类

public class RangeDateUtil {

////////////////////////////////////////////  YearMonth ////////////////////////////////////////////
	/**
	 * 获取间隔的月份列表
	 * @param preYear 开始月份
	 * @param endYear 截止月份
	 */
	public static List<YearMonth> getRangeYears(YearMonth preYear, YearMonth endYear) {
		List<YearMonth> years = new ArrayList<>();
		// 间隔的月数
		long betweenYears = ChronoUnit.MONTHS.between(preYear, endYear);
		if (betweenYears < 1) {
			// 开始日期<=截止日期
			return years;
		}
		// 创建一个从开始日期、每次加一个月的无限流,限制到截止月份为止
		Stream.iterate(preYear, c -> c.plusMonths(1))
			.limit(betweenYears + 1)
			.forEach(years::add);
		return years;
	}

	/**
	 * 获取指定月份前的指定月数的日期列表
	 * @param endYear		截止月份
	 * @param betweenYears	间隔月数
	 */
	public static List<YearMonth> getPreRangeYears(YearMonth endYear, int betweenYears) {
		YearMonth preYear = endYear.minusYears(betweenYears);
		return getRangeYears(preYear, endYear);
	}

	/**
	 * 获取指定月份前的指定月数的日期列表
	 * @param preYear		开始月份
	 * @param betweenYears	间隔月数
	 */
	public static List<YearMonth> getEndRangeYears(YearMonth preYear, int betweenYears) {
		YearMonth endYear = preYear.plusMonths(betweenYears);
		return getRangeYears(preYear, endYear);
	}

////////////////////////////////////////////  LocalDate ////////////////////////////////////////////
	/**
	 * 获取间隔的日期列表
	 * @param preDate 开始日期
	 * @param endDate 截止日期
	 */
	public static List<LocalDate> getRangeDays(LocalDate preDate, LocalDate endDate) {
		List<LocalDate> dates = new ArrayList<>();
		// 间隔的天数
		long betweenDays = ChronoUnit.DAYS.between(preDate, endDate);
		if (betweenDays < 1) {
			// 开始日期<=截止日期
			return dates;
		}
		// 创建一个从开始日期、每次加一天的无限流,限制到截止日期为止
		Stream.iterate(preDate, c -> c.plusDays(1))
			.limit(betweenDays + 1)
			.forEach(dates::add);
		return dates;
	}

	/**
	 * 获取指定日期前的指定天数的日期列表
	 * @param endDate		截止日期
	 * @param betweenDays	间隔天数
	 */
	public static List<LocalDate> getPreRangeDays(LocalDate endDate, int betweenDays) {
		LocalDate preDate = endDate.minusDays(betweenDays);
		return getRangeDays(preDate, endDate);
	}

	/**
	 * 获取指定日期后的指定天数的日期列表
	 * @param preDate		开始日期
	 * @param betweenDays	间隔天数
	 */
	public static List<LocalDate> getEndRangeDays(LocalDate preDate, int betweenDays) {
		LocalDate endDate = preDate.plusDays(betweenDays);
		return getRangeDays(preDate, endDate);
	}

////////////////////////////////////////////  Date ////////////////////////////////////////////
	/**
	 * 获取间隔的日期列表
	 * @param preDate 开始日期
	 * @param endDate 截止日期
	 */
	public static List<Date> getRangeDays(Date preDate, Date endDate) {
		List<Date> dates = new ArrayList<>();
		// 间隔的天数
		int betweenDays = (int) ((endDate.getTime() - preDate.getTime()) / (1000*3600*24));
		if (betweenDays < 1) {
			// 开始日期<=截止日期
			return dates;
		}
		// 创建一个从开始日期、每次加一天的无限流,限制到截止日期为止
		Stream.iterate(preDate, c -> DateUtil.plusDays(c, 1))
			.limit(betweenDays + 1)
			.forEach(dates::add);
		return dates;
	}

	/**
	 * 获取指定日期前的指定天数的日期列表
	 * @param endDate		截止日期
	 * @param betweenDays	间隔天数
	 */
	public static List<Date> getPreRangeDays(Date endDate, int betweenDays) {
		Date preDate = DateUtil.minusDays(endDate, betweenDays);
		return getRangeDays(preDate, endDate);
	}

	/**
	 * 获取指定日期后的指定天数的日期列表
	 * @param preDate		开始日期
	 * @param betweenDays	间隔天数
	 */
	public static List<Date> getEndRangeDays(Date preDate, int betweenDays) {
		Date endDate = DateUtil.plusDays(preDate, betweenDays);
		return getRangeDays(preDate, endDate);
	}

////////////////////////////////////////////  String ////////////////////////////////////////////
	/**
	 * 获取间隔的月份列表
	 * @param preYear 开始月份(yyyy-MM格式)
	 * @param endYear 截止月份(yyyy-MM格式)
	 */
	public static List<YearMonth> getRangeYears(String preYear, String endYear) {
		YearMonth pDate = YearMonth.parse(preYear);
		YearMonth eDate = YearMonth.parse(endYear);
		return getRangeYears(pDate, eDate);
	}
	/**
	 * 获取间隔的日期列表
	 * @param preDate 开始日期(yyyy-MM-dd格式)
	 * @param endDate 截止日期(yyyy-MM-dd格式)
	 */
	public static List<LocalDate> getRangeDays(String preDate, String endDate) {
		LocalDate pDate = LocalDate.parse(preDate);
		LocalDate eDate = LocalDate.parse(endDate);
		return getRangeDays(pDate, eDate);
	}
}
posted @ 2021-08-02 17:39  soldier_cnblogs  阅读(1408)  评论(0编辑  收藏  举报