Procházet zdrojové kódy

es统计 数据生成代码

liuwenbin před 7 roky
rodič
revize
93b498fba3

+ 10 - 0
common/common-entity/src/main/java/com/yihu/wlyy/entity/job/QuartzJobConfig.java

@ -27,6 +27,7 @@ public class QuartzJobConfig implements java.io.Serializable {
	private String sqlYear;
	private String cacheKey;//缓存的key
	private String extractType;// 1或者为空:数据库 2ES
	private Integer incrementInterval; //增量的时间间隔(1天,2周,3月)
@ -199,4 +200,13 @@ public class QuartzJobConfig implements java.io.Serializable {
	public void setEndTime(String endTime) {
		this.endTime = endTime;
	}
	@Column(name = "increment_interval", length = 2)
	public Integer getIncrementInterval() {
		return incrementInterval;
	}
	public void setIncrementInterval(Integer incrementInterval) {
		this.incrementInterval = incrementInterval;
	}
}

+ 31 - 0
patient-co/patient-co-statistics-es/src/main/java/com/yihu/wlyy/statistics/controller/JobController.java

@ -317,4 +317,35 @@ public class JobController extends BaseController {
            return invalidUserException(e, -1, "启动失败:" + e.getMessage());
        }
    }
    /************************************************************************************/
    /**
     * 生成过去某一天到某一天的全部的数据
     *
     * @param start
     * @param end
     * @return
     */
    @ApiOperation(value = "生成过去某一天到某一天的全部的数据(包含头尾)按周或月统计")
    @RequestMapping(value = "productDataByDayToDay2", method = RequestMethod.GET)
    public String productDataByDayToDay2( @ApiParam(name = "start", value = "yyyy-MM-dd", required = true)@RequestParam(value = "start", required = true)String start,
                                         @ApiParam(name = "end", value = "yyyy-MM-dd", required = true)@RequestParam(value = "end", required = true)String end,
                                          @ApiParam(name = "incrementInterval", value = "2周,3月", required = true)@RequestParam(value = "incrementInterval", required = true)Integer incrementInterval,
                                          @ApiParam(name = "id", value = "指标id,逗号分隔(为空表示全部)", required = false)@RequestParam(value = "id", required = false)String id) {
        try {
            if(incrementInterval==2){
                jobService.productWeekByDayToDay(start,end,id);
            }else if(incrementInterval==3){
                jobService.productMonthByDayToDay(start,end,id);
            }else{
                return success("启动失败!");
            }
            return success("启动成功!");
        } catch (Exception e) {
            error(e);
            return invalidUserException(e, -1, "启动失败:" + e.getMessage());
        }
    }
}

+ 117 - 1
patient-co/patient-co-statistics-es/src/main/java/com/yihu/wlyy/statistics/service/JobService.java

@ -6,6 +6,7 @@ import com.yihu.wlyy.statistics.dao.QuartzJobConfigDao;
import com.yihu.wlyy.statistics.dao.SignFamilyDao;
import com.yihu.wlyy.statistics.etl.cache.Cache;
import com.yihu.wlyy.statistics.job.business.CacheCleanJob;
import com.yihu.wlyy.statistics.util.DateUtil;
import com.yihu.wlyy.statistics.util.QuartzHelper;
import com.yihu.wlyy.statistics.vo.WlyyJobConfigVO;
import org.springframework.beans.BeanUtils;
@ -141,7 +142,7 @@ public class JobService {
    }
    private boolean breakPoint(WlyyJobConfigVO wlyyJobConfigVO, int i) {
        if (Integer.valueOf(wlyyJobConfigVO.getId()) > 86 && i == 2) {
        if (Integer.valueOf(wlyyJobConfigVO.getId()) > 86 && i == 2||wlyyJobConfigVO.getIncrementInterval()!=1) {
            return true;
        }
        return false;
@ -454,4 +455,119 @@ public class JobService {
            throw new Exception("已经停止");
        }
    }
    /**************************************按周或按月****************************************/
    public void productWeekByDayToDay(String start, String end,String id) throws Exception {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Date startDate = sdf.parse(start);
        Date endDate = sdf.parse(end);
        if (startDate.after(endDate)) {
            throw new Exception("日期参数错误");
        }
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(startDate);
        int a = calendar.get(Calendar.YEAR);
        calendar.clear();
        calendar.setTime(endDate);
        int b = calendar.get(Calendar.YEAR);
        int startWeek = DateUtil.week(startDate);
        if(a!=b){
            Date lastDate = DateUtil.getYearLast(a);
            int lastWeek = DateUtil.week(lastDate);
            for(int i=startWeek;i<=lastWeek;i++){
                start = getDate(startDate ,i,2);
                end = getDate(startDate ,i+1,1);
                productDataByOneDay2(start,end,2,id);
            }
            startDate =lastDate;
        }
        startWeek = DateUtil.week(startDate);
        int endWeek = DateUtil.week(endDate);
        for(int i=startWeek;i<=endWeek;i++){
            start = getDate(startDate ,i,2);
            end = getDate(startDate ,i+1,1);
            productDataByOneDay2(start,end,2,id);
        }
    }
    public void productMonthByDayToDay(String start, String end,String id) throws Exception {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Date startDate = sdf.parse(start);
        Date endDate = sdf.parse(end);
        if (startDate.after(endDate)) {
            throw new Exception("日期参数错误");
        }
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(startDate);
        int a = calendar.get(Calendar.YEAR);
        int a1 = calendar.get(Calendar.MONTH);
        calendar.clear();
        calendar.setTime(endDate);
        int b = calendar.get(Calendar.YEAR);
        int b1 = calendar.get(Calendar.MONTH);
        if(a!=b){
            for(int i=a1;i<=12;i++){
//            start = getDate(startDate ,i,1)+" 00:00:00";
//            end = getDate(startDate ,i,7)+" 59:59:59";
                start = DateUtil.getFristDayOfMonth(startDate);
                end = DateUtil.getLastDayOfMonth(startDate);
                productDataByOneDay2(start,end,3,id);
            }
            a1=1;
            calendar.clear();
            calendar.set(Calendar.YEAR, b);
            startDate = calendar.getTime();
        }
        for(int i=a1;i<=b1;i++){
            start = DateUtil.getFristDayOfMonth(startDate);
            end = DateUtil.getLastDayOfMonth(startDate);
            productDataByOneDay2(start,end,3,id);
        }
    }
    public void productDataByOneDay2(String start, String end,Integer incrementInterval,String id) throws Exception {
        String condition = "";
        if(!StringUtils.isEmpty(id)){
            condition+=" and a.id in ("+id+") ";
        }
        if(incrementInterval!=null){
            condition +=" and a.increment_interval ="+incrementInterval.intValue();
        }
        String sql = "select * from wlyy_job_config_new a where  a.del='1' and a.id !=11 "+condition+" order by a.id asc";
        List<QuartzJobConfig> quartzJobConfigs = jdbcTemplate.query(sql, new BeanPropertyRowMapper(QuartzJobConfig.class));
        for (QuartzJobConfig quartzJobConfig : quartzJobConfigs) {
            WlyyJobConfigVO wlyyJobConfigVO = new WlyyJobConfigVO();
            BeanUtils.copyProperties(quartzJobConfig, wlyyJobConfigVO);
            Map<String, String> params = new HashMap<String, String>();
            params.put("jobConfig", wlyyJobConfigVO.getId());
            //往quartz框架添加任务
            params.put("startTime", start);
            params.put("endTime", end);
            for (int j = 1; j <= 2; j++) {
//                if (Integer.valueOf(wlyyJobConfigVO.getId()) > 86 && j == 2) continue;
                params.put("timeLevel", j + "");
                if (!StringUtils.isEmpty(quartzJobConfig.getJobClass())) {
                    quartzHelper.startNow(getRightClass(quartzJobConfig), quartzJobConfig.getId() + UUID.randomUUID().toString().replace("-", ""), params);
                    Thread.sleep(sleepTime);
                }
            }
        }
    }
    public String getDate(Date date ,int weekNum,int day){
        SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd");
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.set(Calendar.WEEK_OF_YEAR, weekNum); // 设置为2016年的第10周
        cal.set(Calendar.DAY_OF_WEEK, day); // 1表示周日,2表示周一,7表示周六
        return sf.format(cal.getTime());
    }
}

+ 99 - 1
patient-co/patient-co-statistics-es/src/main/java/com/yihu/wlyy/statistics/util/DateUtil.java

@ -179,7 +179,7 @@ public class DateUtil {
	 * 将短时间格式时间转换为字符串 yyyy-MM-dd
	 *
	 * @param dateDate
	 * @param k
	 * @param
	 * @return
	 */
	public static String dateToStr(Date dateDate, String format) {
@ -717,4 +717,102 @@ public class DateUtil {
		Calendar cal = Calendar.getInstance();
		return cal.get(Calendar.YEAR);
	}
	/**
	 * 获取周一
	 * @return
	 */
	public static String getMondayOfThisDate(Date date) {
		SimpleDateFormat df2 = new SimpleDateFormat(YYYY_MM_DD_HH_MM_SS);
		return df2.format(getMondayOfThisDayToDate(date));
	}
	/**
	 * 获取周一
	 * @return
	 */
	public static Date getMondayOfThisDayToDate(Date date) {
		Calendar c = Calendar.getInstance();
		c.setTime(date);
		int day_of_week = c.get(Calendar.DAY_OF_WEEK) - 1;
		if (day_of_week == 0)
			day_of_week = 7;
		c.add(Calendar.DATE, -day_of_week + 1);
		return c.getTime();
	}
    /**
     * 得到本周周日
     *
     * @return yyyy-MM-dd
     */
    public static String getSundayOfThisDate(Date date) {
        SimpleDateFormat df2 = new SimpleDateFormat("yyyy-MM-dd");
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        int day_of_week = c.get(Calendar.DAY_OF_WEEK) - 1;
        if (day_of_week == 0)
            day_of_week = 7;
        c.add(Calendar.DATE, -day_of_week + 7);
        return df2.format(c.getTime());
    }
	/**
	 * 获取当月第一天
	 * @return
	 */
	public static String getFristDayOfMonth(Date date) {
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
		return format.format(getFristDayOfMonthToDate(date));
	}
	public static Date getFristDayOfMonthToDate(Date date) {
		// 获取前月的第一天
		Calendar c = Calendar.getInstance();
		c.setTime(date);
		c.add(Calendar.MONTH, 0);
		c.set(Calendar.DAY_OF_MONTH,1);//设置为1号,当前日期既为本月第一天
		return c.getTime();
	}
	/**
	 * 获取当月的最后一天
	 * @param date
	 * @return
	 */
	public static String getLastDayOfMonth(Date date) {
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
		return format.format(getLastDayOfMonthToDate(date));
	}
	public static Date getLastDayOfMonthToDate(Date date){
		Calendar ca = Calendar.getInstance();
		ca.setTime(date);
		ca.set(Calendar.DAY_OF_MONTH, ca.getActualMaximum(Calendar.DAY_OF_MONTH));
		return ca.getTime();
	}
	//获取输入时间是当前年度的第几周
	public static Integer week(Date date){
		Calendar calendar = Calendar.getInstance();
		calendar.setFirstDayOfWeek(Calendar.MONDAY);
		calendar.setTime(date);
		return calendar.get(Calendar.WEEK_OF_YEAR);
	}
	//获取输入时间是当前年度的第几周
	public static Integer month(Date date){
		Calendar calendar = Calendar.getInstance();
		calendar.setFirstDayOfWeek(Calendar.MONDAY);
		calendar.setTime(date);
		return calendar.get(Calendar.MONTH);
	}
	/**
	 * 获取某年最后一天日期
	 * @param year 年份
	 * @return Date
	 */
	public static Date getYearLast(int year){
		Calendar calendar = Calendar.getInstance();
		calendar.clear();
		calendar.set(Calendar.YEAR, year);
		calendar.roll(Calendar.DAY_OF_YEAR, -1);
		Date currYearLast = calendar.getTime();
		return currYearLast;
	}
}

+ 9 - 0
patient-co/patient-co-statistics-es/src/main/java/com/yihu/wlyy/statistics/vo/WlyyJobConfigVO.java

@ -22,6 +22,7 @@ public class WlyyJobConfigVO implements  Serializable {
    private String sqlDay;//按日统计
    private String sqlYear;//按年统计
    private String cacheKey;//缓存的key
    private Integer incrementInterval;//增量的时间间隔(1天,2周,3月)
    public WlyyJobConfigVO() {
@ -154,4 +155,12 @@ public class WlyyJobConfigVO implements  Serializable {
    public void setCacheKey(String cacheKey) {
        this.cacheKey = cacheKey;
    }
    public Integer getIncrementInterval() {
        return incrementInterval;
    }
    public void setIncrementInterval(Integer incrementInterval) {
        this.incrementInterval = incrementInterval;
    }
}

+ 16 - 5
patient-co/patient-co-wlyy/src/main/java/com/yihu/wlyy/web/statistic/EsStatisticsController.java

@ -2345,6 +2345,7 @@ public class EsStatisticsController extends BaseController {
        //                                   @RequestParam(required = true) String index,
                                           @ApiParam(name = "level", value = "地区级别,1 团队,2 机构,3 区,4 市") @RequestParam(required = true) int level,
                                           @ApiParam(name = "activityCode", value = "活动的id") @RequestParam(required = false) String activityCode,
//                                           @ApiParam(name = "index", value = "报名量91,活跃量(95天,90周,94月)") @RequestParam(required = true) String index,
                                           @RequestParam(required = false) String year) {
        try {
            level = elasticsearchUtil.changeLevel(level);
@ -2352,10 +2353,12 @@ public class EsStatisticsController extends BaseController {
//            JSONObject json = statisticsESService.getDateTotal(startDate, endDate, interval, area, level, "90", lowCode, year,SaveModel.timeLevel_ZL);
//            result.put("index_89", json);
            String index = "";
            if(StringUtils.isNotEmpty(activityCode)){
                index="91,90";
            }else{
            if(interval==1){
                index="91,95";
            }else if(interval==2){
                index="91,90";
            }else if(interval==2){
                index="91,94";
            }
            if (index != null) {
                if (org.springframework.util.StringUtils.isEmpty(year)) {
@ -2363,9 +2366,17 @@ public class EsStatisticsController extends BaseController {
                    year =  Constant.getNowYearByDate(endDate);
                }
                String[] indexes = index.split(",");
                for (String idx : indexes) {
                    JSONObject json = statisticsESService.getDateTotal(startDate, endDate, interval, area, level, idx, activityCode, year,SaveModel.timeLevel_DDL);
                    result.put("index_"+idx, json);
                    if("91".equals(idx)){
                        JSONObject json = statisticsESService.getDateTotal(startDate, endDate, interval, area, level, idx, activityCode, year,SaveModel.timeLevel_DDL);
                        result.put("index_"+idx, json);
                    }else{
                        JSONObject json = statisticsESService.getDateTotal(startDate, endDate, interval, area, level, idx, activityCode, year,SaveModel.timeLevel_ZL);
                        result.put("index_90", json);
                    }
                }
            }
            return write(200, "查询成功!", "data", result);