123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197 |
- package com.bowintek.practice.services.impl;
- import com.bowintek.practice.mapper.cquery.FactWellCQuery;
- import com.bowintek.practice.model.EsQueryLog;
- import com.bowintek.practice.services.service.FactWellService;
- import com.bowintek.practice.util.multipdb.DBTypeEnum;
- import com.bowintek.practice.util.multipdb.SwitchDataSource;
- import com.bowintek.practice.vo.EsQueryLogVo;
- import com.bowintek.practice.vo.FactWell.FactWellGroupList;
- import com.github.pagehelper.PageHelper;
- import com.github.pagehelper.PageInfo;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.stereotype.Component;
- import java.text.Collator;
- import java.text.SimpleDateFormat;
- import java.util.*;
- import java.util.stream.Collectors;
- @Component
- public class FactWellServiceImpl implements FactWellService {
- @Autowired
- private FactWellCQuery factWellCQuery;
- @Override
- @SwitchDataSource(DBTypeEnum.POSTGRE)
- public List<FactWellGroupList> getFactWellList(String wellId, String timeType){
- if(timeType.equals("day")) return getDayGroupList(wellId);
- if(timeType.equals("month")) return getMonthGroupList(wellId);
- else return getYearGroupList(wellId);
- }
- @Override
- @SwitchDataSource(DBTypeEnum.POSTGRE)
- public List<FactWellGroupList> getFactWellListByTime(String wellId, String timeType, Date startTime, Date endTime){
- if(timeType.equals("day")) return getDayGroupList(wellId, startTime, endTime);
- if(timeType.equals("month")) return getMonthGroupList(wellId, startTime, endTime);
- else return getYearGroupList(wellId, startTime, endTime);
- }
- public List<FactWellGroupList> getDayGroupList(String wellId){
- Calendar calendar = Calendar.getInstance();
- calendar.setTime(getToDay()); // 设置当前天日期
- calendar.set(Calendar.DAY_OF_MONTH, 1);
- calendar.add(Calendar.DAY_OF_MONTH, -1);
- //上月最后一天
- Date endTime = calendar.getTime();
- calendar.add(Calendar.DAY_OF_MONTH, -29);
- Date startTime = calendar.getTime();
- return getDayGroupList(wellId, startTime, endTime);
- }
- public List<FactWellGroupList> getDayGroupList(String wellId, Date startTime, Date endTime){
- //重新计算当前30天日期
- if(startTime==null || endTime==null) return getDayGroupList(wellId);
- List<FactWellGroupList> rtnList = factWellCQuery
- .getDayGroupList(wellId, dateToDayString(startTime), dateToDayString(endTime));
- //补充列表数据
- List<String> dayList = new ArrayList<>();
- for(Date time = startTime;time.getTime()<=endTime.getTime();){
- Calendar calendarTime = Calendar.getInstance();
- calendarTime.setTime(time);
- dayList.add(dateToDayString(calendarTime.getTime()));
- //下一天
- calendarTime.add(Calendar.DAY_OF_MONTH, 1);
- time = calendarTime.getTime();
- }
- rtnList = supplementList(wellId, dayList, rtnList);
- return rtnList;
- }
- public Date getToDay(){
- Calendar calendar = Calendar.getInstance();
- calendar.set(Calendar.HOUR_OF_DAY, 0);
- calendar.set(Calendar.MINUTE, 0);
- calendar.set(Calendar.SECOND, 0);
- calendar.set(Calendar.MILLISECOND, 0);
- return calendar.getTime();
- }
- public List<FactWellGroupList> getMonthGroupList(String wellId){
- Calendar calendar = Calendar.getInstance();
- calendar.setTime(getToDay());
- calendar.set(Calendar.DAY_OF_MONTH, 1);
- calendar.add(Calendar.DAY_OF_MONTH, -1);
- //上一月最后一天
- Date endTime = calendar.getTime();
- //11月前的1号
- calendar.add(Calendar.MONTH, -11);
- calendar.set(Calendar.DAY_OF_MONTH, 1);
- Date startTime = calendar.getTime();
- return getMonthGroupList(wellId, startTime, endTime);
- }
- public List<FactWellGroupList> getMonthGroupList(String wellId, Date startTime, Date endTime){
- //重新计算当前12月
- if(startTime==null || endTime==null) return getMonthGroupList(wellId);
- System.out.println("getMonthGroupList "+dateToDayString(startTime)+" "+dateToDayString(endTime));
- List<FactWellGroupList> rtnList = factWellCQuery
- .getMonthGroupList(wellId, dateToDayString(startTime), dateToDayString(endTime));
- //补充列表数据
- List<String> monthList = new ArrayList<>();
- for(Date time = startTime;time.getTime()<=endTime.getTime();){
- Calendar calendarTime = Calendar.getInstance();
- calendarTime.setTime(time);
- monthList.add(dateToMonthString(calendarTime.getTime()));
- //下一月
- calendarTime.add(Calendar.MONTH, 1);
- time = calendarTime.getTime();
- }
- rtnList = supplementList(wellId, monthList, rtnList);
- return rtnList;
- }
- public List<FactWellGroupList> getYearGroupList(String wellId){
- Calendar calendar = Calendar.getInstance();
- calendar.setTime(getToDay());
- calendar.set(Calendar.MONTH, 12);
- calendar.set(Calendar.DAY_OF_MONTH, 1);
- calendar.add(Calendar.MONTH, 1);
- calendar.add(Calendar.DAY_OF_MONTH, -1);
- //当年12月30日
- Date endTime = calendar.getTime();
- //5年前1月1日
- calendar.add(Calendar.YEAR, -4);
- calendar.set(Calendar.MONTH, 1);
- calendar.set(Calendar.DAY_OF_MONTH, 1);
- Date startTime = calendar.getTime();
- return getYearGroupList(wellId, startTime, endTime);
- }
- public List<FactWellGroupList> getYearGroupList(String wellId, Date startTime, Date endTime){
- //重新计算5年时间
- if(startTime==null || endTime==null) return getYearGroupList(wellId);
- System.out.println("getYearGroupList "+dateToDayString(startTime)+" "+dateToDayString(endTime));
- List<FactWellGroupList> rtnList = factWellCQuery
- .getYearGroupList(wellId, dateToDayString(startTime), dateToDayString(endTime));
- //补充列表数据
- List<String> monthList = new ArrayList<>();
- for(Date time = startTime;time.getTime()<=endTime.getTime();){
- Calendar calendarTime = Calendar.getInstance();
- calendarTime.setTime(time);
- monthList.add(dateToYearString(calendarTime.getTime()));
- //下一年
- calendarTime.add(Calendar.YEAR, 1);
- time = calendarTime.getTime();
- }
- rtnList = supplementList(wellId, monthList, rtnList);
- return rtnList;
- }
- public List<FactWellGroupList> supplementList(String wellId, List<String> timeList, List<FactWellGroupList> source){
- //if(source.stream().filter(r->r.getTime_str().equals()))
- for(int i=0;i<timeList.size();i++){
- final String timeStr = timeList.get(i);
- if(source.stream().filter(r->r.getTime_str().equals(timeStr))
- .collect(Collectors.toList()).size()==0){
- source.add(FactWellGroupList.gen(wellId, timeStr));
- }
- }
- Collections.sort(source, new Comparator<FactWellGroupList>() {
- public int compare(FactWellGroupList o1, FactWellGroupList o2) {
- Collator instance = Collator.getInstance(Locale.CHINA);
- return instance.compare(o1.getTime_str(), o2.getTime_str());
- }
- });
- return source;
- }
- public String dateToDayString(Date time){
- SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
- return sdf.format(time);
- }
- public String dateToMonthString(Date time){
- SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月");
- return sdf.format(time);
- }
- public String dateToYearString(Date time){
- SimpleDateFormat sdf = new SimpleDateFormat("yyyy");
- return sdf.format(time);
- }
- }
|