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 {// 没有这一天的数据,默认补0result.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 {// 没有这一天的数据,默认补0result.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_demowhere 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_tempgroup 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);}
}

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

  1. Java招聘数据统计_拉勾网2019年3月20日招聘数据统计

    ios 北京 招聘数量:804ios 深圳 招聘数量:420ios 上海 招聘数量:419ios 广州 招聘数量:374ios 杭州 招聘数量:184ios 南京 招聘数量:40ios 西安 招聘数量 ...

  2. 数据统计之月增用户统计

    月增用户统计 [统计当月(一个月范围内)每天注册的用户数量] 接口分析 请求方式:GET /meiduo_admin/statistical/month_increment/ # 月增用户统计量url ...

  3. Java编写数据统计程序_个人项目--wc文本统计程序(Java实现)

    Github地址:https://github.com/LYhiha/wcexe 1.项目相关要求 实现一个统计程序,它能正确统计程序文件中的字符数.单词数.行数,以及还具备其他扩展功能,并能够快速地 ...

  4. 2020 java Mybatis 面试题及答案(最全版本持续更新)

    前言 涵盖各大公司会问到的面试点,同时随着版本的升级,可能也会有一些面试题更新,也会同步保持更新,因为篇幅原因(其实是我懒,哈哈)所以列了一部分答案,所有的答案见下文,总共485页合计20个技术点,文 ...

  5. 【指标统计】根据遥控补全遥信

    设计思路:每一个成功的遥控(返信成功)应当对应一个COS和SOE.如果返信成功时间前后5秒内,未找到同一测点相同状态的变位信息,则认为对应遥信已被误删.随即进行恢复(插入到scada_event和sc ...

  6. java下拉模糊查询_select2 智能补全模糊查询select2的下拉选择框使用

    我们在上篇文章中已经在SpringMVC基础框架的基础上应用了BootStrap的后台框架,在此基础上记录select2的使用. 应用bootstrap模板 基础项目源码下载地址为: SpringMV ...

  7. 常识——百度输入法自动补全日期

    文章目录 前言 前言 输入法右键,选择高级设置 选择自定义短语 设置单词缩写触发自定义短语 短语使用获得日期的代码即可 #$(year)年$(month_mm)月$(day_dd)日 星期$(week ...

  8. mysql本周数据没有填充_MySql查询本周、本月、本年数据(没有数据则补全0)

    最近写项目,发现有很多图表统计,需要查询本周.本月.本年数据.但是图表需要两个数组,一个日期数组,一个数据数组.然而数据库查询却只能查询出有数据的日期数据,所以找了很多资料终于能有补全日期和数据的方法 ...

  9. java 网页数据_JAVA获取网页数据

    很多时候用到抓取网页数据的功能,以前工作中曾经用到过,今天总结了一下: 1.抓取网页数据通过指定的URL,获得页面信息,进而对页面用DOM进行 NODE分析,处理得到原始HTML数据,这样做的优势在于 ...

最新文章

  1. 有效快速的学习微信小程序
  2. 【2011-3】【旋转表格】
  3. CodeForces 597A Divisibility
  4. eclipse常用快捷键排行
  5. 软件设计方法--契约式设计Design by contract
  6. windows下 网络命令(待续)
  7. Linux 之十二 Makefile 从入门到放弃全解
  8. 零基础学Python(第二十二章 常用内置函数)
  9. oracle查询第二个字为a,Oracle多表查询 - osc_yqnlq679的个人空间 - OSCHINA - 中文开源技术交流社区...
  10. shell mysql版本_mysql版本5.5.x升级到5.6.x步骤分享
  11. 前端学习(1959)vue之电商管理系统电商系统之通过路由加载分类参数
  12. 论文阅读课11-TEMPROB:Improving Temporal Relation Extraction with a Globally Acquired Statistical Resource
  13. Quartz.NET开源作业调度框架系列(二):CronTrigger-转
  14. QT5新建工程错误-无法打开源文件QtWidgets/QApplication
  15. CSS基础之清除浮动
  16. Android学习之适配器SimpleCursorAdapter
  17. Exchange企业实战技巧(25)将日历发布到Internet
  18. 释放空间后将指针置空
  19. 嘉明的C学习之Day8--数组
  20. 计算机鼠标双击怎么,解决方案:解决鼠标双击问题的方法(最全面,最实用)_计算机基础知识_IT /计算机_信息...

热门文章

  1. Word在引用3个及以上参考文献的时候如何合并
  2. 基于ATmega8的测速计设计
  3. 宝贝上传店铺后把图片空间的图片删了有影响吗
  4. 测试sony电视屏的软件,Mirror for Sony TV 3.6.2 for Mac 索尼电视屏幕镜像工具
  5. 用VC++, OpenCV写大家来找茬外挂
  6. 普通计算器和科学计算器的实现过程另附带画图功能(C# 窗体)
  7. 小红书“户外服务滑雪“类目发布资质要求【小红书上下架erp】
  8. VMware 克隆独立虚拟机及初始配置
  9. python中正切函数_Python tan() 函数
  10. 强烈建议 | 转行Python最好看一下这篇文章