|
@ -0,0 +1,378 @@
|
|
|
|
package com.yihu.wlyy.figure.label.service;
|
|
|
|
|
|
|
|
import com.yihu.figure_label.entity.FLJobConfig;
|
|
|
|
import com.yihu.wlyy.figure.label.dao.FLJobConfigDao;
|
|
|
|
import com.yihu.wlyy.figure.label.util.QuartzHelper;
|
|
|
|
import org.apache.commons.lang3.time.DateFormatUtils;
|
|
|
|
import org.springframework.beans.BeanUtils;
|
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
|
import org.springframework.beans.factory.annotation.Value;
|
|
|
|
import org.springframework.jdbc.core.BeanPropertyRowMapper;
|
|
|
|
import org.springframework.jdbc.core.JdbcTemplate;
|
|
|
|
import org.springframework.stereotype.Service;
|
|
|
|
import org.springframework.transaction.annotation.Transactional;
|
|
|
|
import org.springframework.util.StringUtils;
|
|
|
|
import java.text.ParseException;
|
|
|
|
import java.text.SimpleDateFormat;
|
|
|
|
import java.util.*;
|
|
|
|
|
|
|
|
@Service
|
|
|
|
public class JobService {
|
|
|
|
@Value("${JobService.sleepTime}")
|
|
|
|
private Integer sleepTime;
|
|
|
|
|
|
|
|
@Autowired
|
|
|
|
private QuartzHelper quartzHelper;
|
|
|
|
|
|
|
|
@Autowired
|
|
|
|
private FLJobConfigDao flJobConfigDao;
|
|
|
|
@Autowired
|
|
|
|
private JdbcTemplate jdbcTemplate;
|
|
|
|
|
|
|
|
|
|
|
|
@Transactional
|
|
|
|
public void stopById(String id) throws Exception {
|
|
|
|
FLJobConfig flJobConfig = flJobConfigDao.findByIdAndStatus(id, "1");
|
|
|
|
if (flJobConfig != null) {
|
|
|
|
for (int j = 1; j <= 2; j++) {
|
|
|
|
quartzHelper.removeJob(flJobConfig.getId() + "-" + j);
|
|
|
|
flJobConfigDao.updateStatus(flJobConfig.getId().toString(),"0") ;;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
throw new Exception("任务已经停止");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Transactional
|
|
|
|
public void startById(String id) throws Exception {
|
|
|
|
FLJobConfig FLJobConfig = flJobConfigDao.findByIdAndStatus(id, "0");
|
|
|
|
if (FLJobConfig != null) {
|
|
|
|
startOneJob(FLJobConfig);
|
|
|
|
} else {
|
|
|
|
throw new Exception("任务已经启动");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Transactional
|
|
|
|
public void stopAll() throws Exception {
|
|
|
|
List<FLJobConfig> FLJobConfigs = flJobConfigDao.findByAll("1");
|
|
|
|
if (FLJobConfigs != null && FLJobConfigs.size() > 0) {
|
|
|
|
for (FLJobConfig FLJobConfig : FLJobConfigs) {
|
|
|
|
for (int j = 1; j <= 2; j++) {
|
|
|
|
quartzHelper.removeJob(FLJobConfig.getId() + "-" + j);
|
|
|
|
|
|
|
|
flJobConfigDao.updateStatus(FLJobConfig.getId().toString(),"0") ;;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
throw new Exception("任务已经全部停止");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Transactional
|
|
|
|
public void startAll() throws Exception {
|
|
|
|
List<FLJobConfig> FLJobConfigs = flJobConfigDao.findByAll("0");
|
|
|
|
if (FLJobConfigs != null && FLJobConfigs.size() > 0) {
|
|
|
|
for (FLJobConfig FLJobConfig : FLJobConfigs) {
|
|
|
|
startOneJob(FLJobConfig);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
throw new Exception("任务已经全部启动");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 启动单个任务
|
|
|
|
*
|
|
|
|
* @param FLJobConfig
|
|
|
|
* @throws Exception
|
|
|
|
*/
|
|
|
|
@Transactional
|
|
|
|
public void startOneJob(FLJobConfig FLJobConfig) throws Exception {
|
|
|
|
|
|
|
|
FLJobConfig flJobConfigVO = new FLJobConfig();
|
|
|
|
BeanUtils.copyProperties(FLJobConfig, flJobConfigVO);
|
|
|
|
|
|
|
|
Map<String, Object> params = new HashMap<String, Object>();
|
|
|
|
params.put("jobConfig", flJobConfigVO.getId());
|
|
|
|
//表里设置的增量查询类型和增量查询初始值,如果值没有配置,以当前时间为准
|
|
|
|
if(!StringUtils.isEmpty(flJobConfigVO.getSqlFieldValue())){
|
|
|
|
params.put(flJobConfigVO.getSqlField(),flJobConfigVO.getSqlFieldValue());
|
|
|
|
}else{
|
|
|
|
params.put(flJobConfigVO.getSqlField(), DateFormatUtils.format(new Date(),"yyyy-MM-dd HH:mm:ss"));
|
|
|
|
}
|
|
|
|
if (!StringUtils.isEmpty(FLJobConfig.getJobClass())) {
|
|
|
|
//往quartz框架添加任务
|
|
|
|
quartzHelper.addJob(getRightClass(FLJobConfig), FLJobConfig.getQuartzCron(), FLJobConfig.getId().toString(), params);
|
|
|
|
flJobConfigDao.updateStatus(FLJobConfig.getId().toString(), "1");//设置任务状态是启动 }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public void startNowById(String id) throws Exception {
|
|
|
|
FLJobConfig FLJobConfig = flJobConfigDao.findOne(id);
|
|
|
|
|
|
|
|
FLJobConfig wlyyJobConfigVO = new FLJobConfig();
|
|
|
|
BeanUtils.copyProperties(FLJobConfig, wlyyJobConfigVO);
|
|
|
|
|
|
|
|
Map<String, String> params = new HashMap<String, String>();
|
|
|
|
params.put("jobConfig", wlyyJobConfigVO.getId().toString());
|
|
|
|
//往quartz框架添加任务
|
|
|
|
if (!StringUtils.isEmpty(FLJobConfig.getJobClass())) {
|
|
|
|
quartzHelper.startNow(getRightClass(FLJobConfig), FLJobConfig.getId() + UUID.randomUUID().toString().replace("-", ""), params);
|
|
|
|
Thread.sleep(sleepTime);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public static String getYesterday(Integer day) {
|
|
|
|
Calendar cal = Calendar.getInstance();
|
|
|
|
cal.add(Calendar.DATE, day);
|
|
|
|
String yesterday = new SimpleDateFormat("yyyy-MM-dd").format(cal.getTime());
|
|
|
|
return yesterday;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void productDataByOneDay(String yesterday) throws Exception {
|
|
|
|
String sql="select * from wlyy_job_config_new a where a.del='1' and a.id !=11 order by a.id asc";
|
|
|
|
SimpleDateFormat dataSimple = new SimpleDateFormat("yyyy-MM-dd");
|
|
|
|
|
|
|
|
Date date = dataSimple.parse(yesterday);
|
|
|
|
if (date == null) {
|
|
|
|
throw new Exception("时间格式错误");
|
|
|
|
}
|
|
|
|
Calendar calendar = new GregorianCalendar();
|
|
|
|
calendar.setTime(date);
|
|
|
|
calendar.add(Calendar.DATE, -1);//把日期往后增加一天.整数往后推,负数往前移动
|
|
|
|
Date nowDate = calendar.getTime(); //这个时间就是日期往后推一天的结果
|
|
|
|
String daybefore = new SimpleDateFormat("yyyy-MM-dd").format(nowDate.getTime());
|
|
|
|
List<FLJobConfig> FLJobConfigs = jdbcTemplate.query(sql,new BeanPropertyRowMapper(FLJobConfig.class));
|
|
|
|
for (FLJobConfig FLJobConfig : FLJobConfigs) {
|
|
|
|
|
|
|
|
FLJobConfig flJobConfig = new FLJobConfig();
|
|
|
|
BeanUtils.copyProperties(FLJobConfig, flJobConfig);
|
|
|
|
|
|
|
|
Map<String, String> params = new HashMap<String, String>();
|
|
|
|
params.put("jobConfig", flJobConfig.getId().toString());
|
|
|
|
//往quartz框架添加任务
|
|
|
|
params.put("sqlFiledValue", daybefore+","+yesterday);
|
|
|
|
params.put("sqlFiledCondition", "<");
|
|
|
|
if (!StringUtils.isEmpty(FLJobConfig.getJobClass())) {
|
|
|
|
quartzHelper.startNow(getRightClass(FLJobConfig), FLJobConfig.getId() + UUID.randomUUID().toString().replace("-", ""), params);
|
|
|
|
Thread.sleep(sleepTime);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param FLJobConfig
|
|
|
|
* @return
|
|
|
|
* @throws ClassNotFoundException
|
|
|
|
*/
|
|
|
|
private Class getRightClass(FLJobConfig FLJobConfig) throws ClassNotFoundException {
|
|
|
|
return Class.forName(FLJobConfig.getJobClass());
|
|
|
|
}
|
|
|
|
|
|
|
|
public void productDataByOneDayWithId(String yesterday, String id) throws Exception {
|
|
|
|
SimpleDateFormat dataSimple = new SimpleDateFormat("yyyy-MM-dd");
|
|
|
|
Date date = dataSimple.parse(yesterday);
|
|
|
|
if (date == null) {
|
|
|
|
throw new Exception("时间格式错误");
|
|
|
|
}
|
|
|
|
Calendar calendar = new GregorianCalendar();
|
|
|
|
calendar.setTime(date);
|
|
|
|
calendar.add(Calendar.DATE, -1);//把日期往后增加一天.整数往后推,负数往前移动
|
|
|
|
Date nowDate = calendar.getTime(); //这个时间就是日期往后推一天的结果
|
|
|
|
String daybefore = new SimpleDateFormat("yyyy-MM-dd").format(nowDate.getTime());
|
|
|
|
|
|
|
|
FLJobConfig FLJobConfig = flJobConfigDao.findById(id);
|
|
|
|
if (FLJobConfig == null) {
|
|
|
|
throw new Exception("id不存在");
|
|
|
|
}
|
|
|
|
FLJobConfig flJobConfigVO = new FLJobConfig();
|
|
|
|
BeanUtils.copyProperties(FLJobConfig, flJobConfigVO);
|
|
|
|
|
|
|
|
Map<String, String> params = new HashMap<String, String>();
|
|
|
|
params.put("jobConfig", flJobConfigVO.getId().toString());
|
|
|
|
|
|
|
|
//往quartz框架添加任务
|
|
|
|
params.put("sqlFiledValue", daybefore + "," + yesterday);
|
|
|
|
params.put("sqlFiledCondition", "<");
|
|
|
|
if (!StringUtils.isEmpty(FLJobConfig.getJobClass())) {
|
|
|
|
quartzHelper.startNow(getRightClass(FLJobConfig), FLJobConfig.getId() + UUID.randomUUID().toString().replace("-", ""), params);
|
|
|
|
Thread.sleep(sleepTime);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public void productDataByDayAndId(Integer day, String id) throws Exception {
|
|
|
|
FLJobConfig FLJobConfig = flJobConfigDao.findById(id);
|
|
|
|
if (FLJobConfig == null) {
|
|
|
|
throw new Exception("id不存在");
|
|
|
|
}
|
|
|
|
FLJobConfig flJobConfigVO = new FLJobConfig();
|
|
|
|
BeanUtils.copyProperties(FLJobConfig, flJobConfigVO);
|
|
|
|
|
|
|
|
Map<String, String> params = new HashMap<String, String>();
|
|
|
|
params.put("jobConfig", flJobConfigVO.getId().toString());
|
|
|
|
for (int i = 1; i <= day; i++) {
|
|
|
|
//往quartz框架添加任务
|
|
|
|
params.put("sqlFiledValue", getYesterday(0 - i - 1)+","+getYesterday(0 - i));
|
|
|
|
params.put("sqlFiledCondition", "<");
|
|
|
|
if (!StringUtils.isEmpty(FLJobConfig.getJobClass())) {
|
|
|
|
quartzHelper.startNow(getRightClass(FLJobConfig), FLJobConfig.getId() + UUID.randomUUID().toString().replace("-", ""), params);
|
|
|
|
Thread.sleep(sleepTime);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public void productDataByDayToDay(String start, String end) 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("日期参数错误");
|
|
|
|
}
|
|
|
|
int day = daysBetween(startDate, endDate);
|
|
|
|
for (int i = 0; i < day; i++) {
|
|
|
|
productDataByOneDay(getYesterday(i, startDate));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public static String getYesterday(Integer day, Date startDate) {
|
|
|
|
Calendar cal = Calendar.getInstance();
|
|
|
|
cal.setTime(startDate);
|
|
|
|
cal.add(Calendar.DAY_OF_MONTH, day);
|
|
|
|
String yesterday = new SimpleDateFormat("yyyy-MM-dd").format(cal.getTime());
|
|
|
|
return yesterday;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static int daysBetween(Date smdate, Date bdate) throws ParseException {
|
|
|
|
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
|
|
|
|
smdate = sdf.parse(sdf.format(smdate));
|
|
|
|
bdate = sdf.parse(sdf.format(bdate));
|
|
|
|
Calendar cal = Calendar.getInstance();
|
|
|
|
cal.setTime(smdate);
|
|
|
|
long time1 = cal.getTimeInMillis();
|
|
|
|
cal.setTime(bdate);
|
|
|
|
long time2 = cal.getTimeInMillis();
|
|
|
|
long between_days = (time2 - time1) / (1000 * 3600 * 24);
|
|
|
|
|
|
|
|
return Integer.parseInt(String.valueOf(between_days));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public void productDataByDayToDayAndId(String start, String end, String ids) 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("日期参数错误");
|
|
|
|
}
|
|
|
|
int day = daysBetween(startDate, endDate);
|
|
|
|
String [] idStr=ids.split(",");
|
|
|
|
for (int i = 0; i < day; i++) {
|
|
|
|
for(String id:idStr){
|
|
|
|
productDataByOneDayWithId(getYesterday(i, startDate), id);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//================================================没有休眠时间=============================================================
|
|
|
|
|
|
|
|
public void productDataByDayToDayAndIdNoSleep(String start, String end, String id,Long sleepTime) 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("日期参数错误");
|
|
|
|
}
|
|
|
|
int day = daysBetween(startDate, endDate);
|
|
|
|
for (int i = 0; i < day; i++) {
|
|
|
|
productDataByOneDayWithIdNoSleep(getYesterday(i, startDate), id,sleepTime);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public void productDataByOneDayWithIdNoSleep(String yesterday, String id,Long sleepTime) throws Exception {
|
|
|
|
SimpleDateFormat dataSimple = new SimpleDateFormat("yyyy-MM-dd");
|
|
|
|
Date date = dataSimple.parse(yesterday);
|
|
|
|
if (date == null) {
|
|
|
|
throw new Exception("时间格式错误");
|
|
|
|
}
|
|
|
|
Calendar calendar = new GregorianCalendar();
|
|
|
|
calendar.setTime(date);
|
|
|
|
calendar.add(Calendar.DATE, -1);//把日期往后增加一天.整数往后推,负数往前移动
|
|
|
|
Date nowDate = calendar.getTime(); //这个时间就是日期往后推一天的结果
|
|
|
|
String daybefore = new SimpleDateFormat("yyyy-MM-dd").format(nowDate.getTime());
|
|
|
|
|
|
|
|
FLJobConfig FLJobConfig = flJobConfigDao.findById(id);
|
|
|
|
if (FLJobConfig == null) {
|
|
|
|
throw new Exception("id不存在");
|
|
|
|
}
|
|
|
|
FLJobConfig flJobConfigVO = new FLJobConfig();
|
|
|
|
BeanUtils.copyProperties(FLJobConfig, flJobConfigVO);
|
|
|
|
|
|
|
|
Map<String, String> params = new HashMap<String, String>();
|
|
|
|
params.put("jobConfig", flJobConfigVO.getId().toString());
|
|
|
|
|
|
|
|
//往quartz框架添加任务
|
|
|
|
params.put("sqlFiledValue", daybefore + "," + yesterday);
|
|
|
|
params.put("sqlFiledCondition", "<");
|
|
|
|
if (!StringUtils.isEmpty(FLJobConfig.getJobClass())) {
|
|
|
|
quartzHelper.startNow(getRightClass(FLJobConfig), FLJobConfig.getId() + UUID.randomUUID().toString().replace("-", ""), params);
|
|
|
|
}
|
|
|
|
Thread.sleep(sleepTime * 1000L);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public void productDataByOneDayNoSleep(String yesterday,Long sleepTime) throws Exception {
|
|
|
|
String sql="select * from wlyy_job_config_new a where a.del='1' and a.id !=11 order by a.id asc";
|
|
|
|
SimpleDateFormat dataSimple = new SimpleDateFormat("yyyy-MM-dd");
|
|
|
|
|
|
|
|
Date date = dataSimple.parse(yesterday);
|
|
|
|
if (date == null) {
|
|
|
|
throw new Exception("时间格式错误");
|
|
|
|
}
|
|
|
|
Calendar calendar = new GregorianCalendar();
|
|
|
|
calendar.setTime(date);
|
|
|
|
calendar.add(Calendar.DATE, -1);//把日期往后增加一天.整数往后推,负数往前移动
|
|
|
|
Date nowDate = calendar.getTime(); //这个时间就是日期往后推一天的结果
|
|
|
|
String daybefore = new SimpleDateFormat("yyyy-MM-dd").format(nowDate.getTime());
|
|
|
|
List<FLJobConfig> FLJobConfigs = jdbcTemplate.query(sql,new BeanPropertyRowMapper(FLJobConfig.class));
|
|
|
|
for (FLJobConfig FLJobConfig : FLJobConfigs) {
|
|
|
|
FLJobConfig flJobConfigVO = new FLJobConfig();
|
|
|
|
BeanUtils.copyProperties(FLJobConfig, flJobConfigVO);
|
|
|
|
|
|
|
|
Map<String, String> params = new HashMap<String, String>();
|
|
|
|
params.put("jobConfig", flJobConfigVO.getId().toString());
|
|
|
|
//往quartz框架添加任务
|
|
|
|
params.put("sqlFiledValue", daybefore + "," + yesterday);
|
|
|
|
params.put("sqlFiledCondition", "<");
|
|
|
|
|
|
|
|
if (!StringUtils.isEmpty(FLJobConfig.getJobClass())) {
|
|
|
|
quartzHelper.startNow(getRightClass(FLJobConfig), FLJobConfig.getId() + UUID.randomUUID().toString().replace("-", ""), params);
|
|
|
|
}
|
|
|
|
Thread.sleep(sleepTime * 1000L);
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* public void startCleanCacheJob() throws Exception {
|
|
|
|
if (!quartzHelper.isExistJob(CacheCleanJob.jobKey)) {
|
|
|
|
quartzHelper.addJob(CacheCleanJob.class, CacheCleanJob.cron, CacheCleanJob.jobKey, new HashMap<>());
|
|
|
|
} else {
|
|
|
|
throw new Exception("已经启动");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public void stopCleanCacheJob() throws Exception {
|
|
|
|
if (quartzHelper.isExistJob(CacheCleanJob.jobKey)) {
|
|
|
|
quartzHelper.removeJob(CacheCleanJob.jobKey);
|
|
|
|
} else {
|
|
|
|
throw new Exception("已经停止");
|
|
|
|
}
|
|
|
|
}*/
|
|
|
|
}
|