/**
* Copyright (C),2019
* FileName: ChartServiceImpl
* Author: m
* Date: 2019/7/25 22:16
* Description:
* History:
* <author> <time> <version> <desc>
* 作者姓名 修改时间 版本号 描述
*/
package com.cm.service.impl;
import com.cm.mapper.ChartMapper;
import com.cm.model.ChartBo;
import com.cm.model.ChartVo;
import com.cm.service.ChartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
/**
* 〈一句话功能简述〉<br>
* 〈〉
*
* @author
* @create 2019/8/3
* @since 1.0.0
*/
@Service
public class ChartServiceImpl implements ChartService {
/**
* 注入mapper层
*/
@Autowired
private ChartMapper chartMapper;
/**
* 查询指定时间区间内数据
* @param chartBo 查询条件 --时间区间 且时间不为空已验证
* @return 数据集合
*/
@Override
public List<ChartVo> getChart(ChartBo chartBo) {
//存放日期列表
List<String> dateList = new ArrayList<>();
//存放查询后的数据
//sql需要按照时间类型分组查询,即db返回数据应为对应的时间类型分组数据 如: 按月查询(时间:2019-06 数量:30)
//具体sql可留言
List<ChartVo> chartVo = chartMapper.getChart(chartBo);
try {
//时间类型转换
SimpleDateFormat sim = new SimpleDateFormat("yyyy-MM-dd");
//转换日期为date类型 --转换起始时间
Date startDate = sim.parse(chartBo.getStartTime());
//转换结束时间
Date endDate = sim.parse(chartBo.getEndTime());
//使用calendar进行日期比较判断
Calendar calendar = Calendar.getInstance();
//获取开始到结束日期中的天列表 --dateType=1 为天
if(chartBo.getDateType()==1){
while (startDate.getTime() <= endDate.getTime()){
//把日期添加到集合
dateList.add(sim.format(startDate));
//设置日期
calendar.setTime(startDate);
//把日期增加一天
calendar.add(Calendar.DATE,1);
//获取增加后的日期
startDate = calendar.getTime();
}
}
//获取开始到结束日期中的周列表 --dateType=2 为天周
if(chartBo.getDateType()==2){
while (startDate.getTime() <= endDate.getTime()){
//把日期添加到集合
dateList.add(sim.format(startDate));
//设置日期
calendar.setTime(startDate);
//把日期增加一周
calendar.add(Calendar.DAY_OF_WEEK,1);
//获取增加后的日期
startDate = calendar.getTime();
}
}
//获取开始到结束日期中的月份列表 --dateType=3 为月
if(chartBo.getDateType()==3){
while (startDate.getTime() <= endDate.getTime()){
//把日期添加到集合
dateList.add(sim.format(startDate));
//设置日期
calendar.setTime(startDate);
//把日期增加一月
calendar.add(Calendar.MONTH,1);
//获取增加后的日期
startDate = calendar.getTime();
}
}
//获取开始到结束日期中的年份列表 --dateType=4 为年
if(chartBo.getDateType()==4){
while (startDate.getTime() <= endDate.getTime()){
//把日期添加到集合
dateList.add(sim.format(startDate));
//设置日期
calendar.setTime(startDate);
//把日期增加一年
calendar.add(Calendar.YEAR,1);
//获取增加后的日期
startDate = calendar.getTime();
}
}
} catch (ParseException e) {
e.printStackTrace();
}
//迭代器去重
for (ChartVo chart:chartVo) {
Iterator<String> iterator = dateList.iterator();
while(iterator.hasNext()){
String next = iterator.next();
if(next.equals(chart.getTimes())){
iterator.remove();
}
}
}
//排序
chartVo.stream().sorted(Comparator.comparing(ChartVo::getTimes));
return chartVo;
}
}