指定月份显示每天的数据

实现功能截图

1.前台传参

 2.

  /***
     * createTime  前台传字符串
     * @param indexNoticeDto
     * @return
     */
    @Override
    public List<IndexNoticeDto> selectIndexNotice(IndexNoticeDto indexNoticeDto) throws ParseException {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        List<IndexNoticeDto> indexNoticeDtoList = new ArrayList<>();

        //不传月份日期表示当月,传日期表示之前月份
        String month = indexNoticeDto.getStrMonth();
        try {
            month = indexNoticeDto.getStrMonth();
        } catch (Exception e) {
            e.printStackTrace();
        }

        if (month == null) {
            indexNoticeDtoList.addAll(publicSqlMapper.selectIndexNotice(indexNoticeDto));
            List<IndexNoticeDto> dayList = getMonthEveryDays(indexNoticeDtoList, null);
            return dayList;
        } else {
            indexNoticeDto.setStrStartTime(month.substring(0, month.length() - 3) + "-01");
            indexNoticeDto.setDtStartTime(sdf.parse(indexNoticeDto.getStrStartTime()));
            int year = Integer.parseInt(month.substring(0, 4));
            int month1 = Integer.parseInt(month.substring(5, 7));

            int max = LocalDate.of(year, month1, 1).lengthOfMonth();
            //月份最大天数
            indexNoticeDto.setStrEndTime(month.substring(0, month.length() - 3) + "-" + String.valueOf(max));
            indexNoticeDto.setDtEndTime(sdf.parse(indexNoticeDto.getStrEndTime()));

            indexNoticeDto.setStrStartTime("'" + month.substring(0, month.length() - 3) + "-01" + "'");
            indexNoticeDto.setStrEndTime("'" + month.substring(0, month.length() - 3) + "-" + String.valueOf(max) + "'");
            List<IndexNoticeDto> indexNoticeDtoList1 = publicSqlMapper.selectIndexNoticeHis(indexNoticeDto);
            List<IndexNoticeDto> dayList = getMonthEveryDays(indexNoticeDtoList1, month.substring(0, month.length() - 3));
            return dayList;
        }
    }

3. 上月月末+当月全月+下月月初数据

  /****
     * 上月月末+当月全月+下月月初数据
     * @param date
     * @return
     */
    public List<IndexNoticeDto> getMonthEveryDays(List<IndexNoticeDto> list, String month) throws ParseException {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

        int year = Integer.parseInt(month.substring(0, 4));
        int month1 = Integer.parseInt(month.substring(5, 7));
        //指定当前月份最大天数
        int max = LocalDate.of(year, month1, 1).lengthOfMonth();

        //当前月初开始日期属于周几
        int nowStartWeek = getWeekday(month + "-01");
        //当前月末结束日期属于周几
        int nowEndWeek = getWeekday(month + "-" + max);

        //上月年
        int lastYear = 0;
        //上月月份
        int lastMonth = Integer.parseInt(month.substring(5, 7)) - 1;
        String strLastMonth = "";
        if (lastMonth == 1) {
            lastMonth = 12;
            lastYear = Integer.parseInt(month.substring(0, 4)) - 1;
        } else {
            lastYear = year;
        }
        //上月最大天数
        int lastMonthMaxDay = LocalDate.of(lastYear, lastMonth, 1).lengthOfMonth();
        //拼接上月月末数据
        List<IndexNoticeDto> lastIndexNoticeDtoList = new ArrayList<>();
        int sort = 0;
        //如果当前月份开始日期是周天,则不拼上月天数,我是根据前台的日期组件,一周开始属于周天拼接的
        if (nowStartWeek != 7) {
            for (int i = 0; i < nowStartWeek - 1; i++) {
                int lastDay = lastMonthMaxDay - i;
                IndexNoticeDto indexNoticeDto = new IndexNoticeDto();
                if (lastMonth < 10) {
                    indexNoticeDto.setCreateTime(lastYear + "-" + "0" + lastMonth + "-" + lastDay);
                } else {
                    indexNoticeDto.setCreateTime(lastYear + "-" + lastMonth + "-" + lastDay);
                }
                indexNoticeDto.setDtCreateTime(sdf.parse(lastYear + "-" + lastMonth + "-" + lastDay));
                if (nowStartWeek == 1) {
                    indexNoticeDto.setWeek(7);
                } else {
                    indexNoticeDto.setWeek(nowStartWeek - (1 + i));
                }
                indexNoticeDto.setDay(lastDay);
                indexNoticeDto.setTotal(0);//拼给前台的默认值,没有任何意义
                sort = i;
                indexNoticeDto.setSort(sort);
                lastIndexNoticeDtoList.add(indexNoticeDto);
            }
        }

        //本月数据
        List<IndexNoticeDto> indexNoticeDtoList = new ArrayList<>();
        //上月数据
        List<IndexNoticeDto> collect1 = lastIndexNoticeDtoList.stream().sorted(Comparator.comparing(IndexNoticeDto::getSort).reversed()).collect(Collectors.toList());
        indexNoticeDtoList.addAll(collect1);
        Calendar calendar = Calendar.getInstance();

        for (int i = 0; i < max; i++) {
            IndexNoticeDto indexNoticeDto = new IndexNoticeDto();
            if (month == null) {
                calendar.set(Calendar.DAY_OF_MONTH, i + 1);
            }
            String day = sdf.format(calendar.getTime());
            indexNoticeDto.setDay(i + 1);
            if (month != null) {

                int ii = i + 1;
                if (i < 9) {
                    day = month + "-" + "0" + ii;
                } else {
                    day = month + "-" + ii;
                }
            }
            indexNoticeDto.setCreateTime(day);
            indexNoticeDto.setDtCreateTime(sdf.parse(day));
            indexNoticeDto.setWeek(getWeekday(day));
            String finalDay = day;
            List<IndexNoticeDto> collect = list.stream().filter(s -> s.getCreateTime().equals(finalDay)).collect(Collectors.toList());
            if (collect.size() > 0) {
                indexNoticeDto.setTotal(collect.size());
            } else {
                indexNoticeDto.setTotal(0);
            }
            indexNoticeDto.setSort(sort + 1 + i);

            indexNoticeDtoList.add(indexNoticeDto);
        }

        //下个月数据
        int nextYear = 0;
        int nextMonth = 0;
        String strNextMonth = "";
        if (month1 == 12) {
            nextYear = year + 1;
            nextMonth = 1;
            strNextMonth = "0" + nextMonth;
        } else if (month1 != 12) {
            nextYear = year;
            nextMonth = month1 + 1;
            if (nextMonth < 1 && nextMonth < 10) {
                strNextMonth = "0" + nextMonth;
            } else {
                strNextMonth = String.valueOf(nextMonth);
            }
        }

        //日历组件方格 一共展示42天的数据
        int nextMonthDayNum = 42 - indexNoticeDtoList.size();
        for (int i = 0; i < nextMonthDayNum; i++) {
            String nextDay = "";
            if (i < 10) {
                nextDay = "0" + String.valueOf(i + 1);
            }
            IndexNoticeDto indexNoticeDto = new IndexNoticeDto();
            indexNoticeDto.setCreateTime(nextYear + "-" + strNextMonth + "-" + nextDay);
            indexNoticeDto.setDtCreateTime(sdf.parse(nextYear + "-" + strNextMonth + "-" + nextDay));
            indexNoticeDto.setDay(i + 1);
            indexNoticeDto.setWeek(getWeekday(nextYear + "-" + strNextMonth + "-" + nextDay));
            indexNoticeDto.setTotal(0);//拼给前台的默认值,没有任何意义
            sort = i;
            indexNoticeDto.setSort(sort);
            indexNoticeDtoList.add(indexNoticeDto);
        }

        return indexNoticeDtoList;
    }

4.制定月份每一天数据,与数据库中的数据进行比较,有数据的时候total的值 >0

5.计算指定日期星期几

 /***
     * 实现给定某日期,判断是星期几
     * 必须yyyy-MM-dd
     * @param date
     * @return
     */
    public static int getWeekday(String date) {
        SimpleDateFormat sd = new SimpleDateFormat("yyyy-MM-dd");
        SimpleDateFormat sdw = new SimpleDateFormat("E");
        Date d = null;
        try {
            d = sd.parse(date);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        String strWeek = sdw.format(d); //输出 星期一。。。。。
        int intWeek = 0;
        switch (strWeek) {
            case "星期-":
                intWeek = 1;
                break;
            case "星期二":
                intWeek = 2;
                break;
            case "星期三":
                intWeek = 3;
                break;
            case "星期四":
                intWeek = 4;
                break;
            case "星期五":
                intWeek = 5;
                break;
            case "星期六":
                intWeek = 6;
                break;
            case "星期天":
                intWeek = 7;
                break;
            default:
                break;
        }
        return intWeek;
    }

6.数据库查询

  <!--通知通告自定义月份每天统计信息-->
    <select id="selectIndexNoticeHis" parameterType="IndexNoticeDto" resultMap="IndexNoticeDtoResult">

        SELECT
              total,
              day,
              CONCAT((date_format( ${strStartTime},'%yyy-')),(date_format( ${strStartTime},'%m-')), day) as create_time
        FROM
               (
                 SELECT
                        DAY(create_time) as day,
                        count(notice_id) AS total
                 FROM sys_notice
                 WHERE create_time &gt;= ${strStartTime} and create_time  &lt;= ${strEndTime}
                 GROUP BY DAY(create_time)
               ) as a
        WHERE a.day is not null

    </select>

7.数据返回(一个日期组件一共展示42天的数据)

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

王大锤4391

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值