Commit 40f2dec3 by 杨林龙

修改定时任务的时间

1 parent 29961a70
package cn.com.glsx.scheduler.common.contants;
/**
* @author zhouhaibao
* @date 2021/3/4 9:53
*/
public class Constants {
/**
* 涂鸦立牌rediskey
*/
public static final String ACCESSTOKEN_REDISKEY = "accessToken_redisKey";
/**
* 设备状态触发条件key
*/
public static final String CONDITION_STATUS = "scene_condition_status";
/**
* 时间触发条件key
*/
public static final String CONDITION_TIME = "scene_condition_time";
/**
* 手动触发条件key
*/
public static final String CONDITION_CLICK = "scene_condition_click";
public static final String SCENE_STATUS = "scene_status";
}
package cn.com.glsx.scheduler.common.enums;
/**
* @author fengzhi
* @date 2021/3/9 17:05
* @description 工作日,节假日类型枚举
*/
public enum EDay {
WORKDAY {
@Override
public Integer getCode() {
return 0;
}
@Override
public Integer getValue() {
return 2;
}
@Override
public String getName() {
return "工作日";
}
},
HOLIDAY {
@Override
public Integer getCode() {
return 1;
}
@Override
public Integer getValue() {
return 3;
}
@Override
public String getName() {
return "节假日";
}
},
WEEKDAY {
@Override
public Integer getCode() {
return 2;
}
@Override
public Integer getValue() {
return 3;
}
@Override
public String getName() {
return "双休日";
}
},
LEAVE_OFF_DAY {
@Override
public Integer getCode() {
return 3;
}
@Override
public Integer getValue() {
return 2;
}
@Override
public String getName() {
return "调休日";
}
},
;
public static Integer getValueByCode(Integer code) {
for (EDay day : EDay.values()) {
if (day.getCode().equals(code)) {
return day.getValue();
}
}
return null;
}
public static String getNameByCode(Integer code) {
for (EDay day : EDay.values()) {
if (day.getCode().equals(code)) {
return day.getName();
}
}
return null;
}
/* 0表示工作日、为1、节假日,为2双休日、3为调休日(上班)*/
public abstract Integer getCode();
// 依据接口整合为平台定义的值,1每天 2工作日 3法定节假日
public abstract Integer getValue();
public abstract String getName();
}
package cn.com.glsx.scheduler.common.enums;
/**
* @author fengzhi
* @date 2021/3/19 17:40
* @description 枚举,触发条件的逻辑判断
*/
public enum ELogic {
LESS_THAN {
@Override
public Integer getCode() {
return 3;
}
@Override
public boolean getValue(String deviceOrderValue, String deviceOrderValueReal) {
return Double.parseDouble(deviceOrderValueReal) < Double.parseDouble(deviceOrderValue);
}
@Override
public boolean getDate(long dateCondition, long dateNow) {
return dateNow < dateCondition;
}
@Override
public String getName() {
return "小于";
}
},
GREATER_THAN {
@Override
public Integer getCode() {
return 2;
}
@Override
public boolean getValue(String deviceOrderValue, String deviceOrderValueReal) {
return Double.parseDouble(deviceOrderValueReal) > Double.parseDouble(deviceOrderValue);
}
@Override
public boolean getDate(long dateCondition, long dateNow) {
return dateNow > dateCondition;
}
@Override
public String getName() {
return "大于";
}
},
EQUALS {
@Override
public Integer getCode() {
return 1;
}
@Override
public boolean getValue(String deviceOrderValue, String deviceOrderValueReal) {
return deviceOrderValueReal.equals(deviceOrderValue);
}
@Override
public boolean getDate(long dateCondition, long dateNow) {
return dateNow == dateCondition;
}
@Override
public String getName() {
return "等于";
}
},
;
public static boolean getValueByCode(Integer code, String deviceOrderValue, String deviceOrderValueReal) {
for (ELogic day : ELogic.values()) {
if (day.getCode().equals(code)) {
return day.getValue(deviceOrderValue, deviceOrderValueReal);
}
}
return false;
}
public static boolean getDateByCode(Integer code, long dateCondition, long dateNow) {
for (ELogic day : ELogic.values()) {
if (day.getCode().equals(code)) {
return day.getDate(dateCondition, dateNow);
}
}
return false;
}
public static String getNameByCode(Integer code) {
for (ELogic day : ELogic.values()) {
if (day.getCode().equals(code)) {
return day.getName();
}
}
return null;
}
/* 逻辑(1:<,2:>,3:=)*/
public abstract Integer getCode();
//deviceOrderValue:触发条件中的设备状态值,deviceOrderValueReal:数据库中的设备状态值
public abstract boolean getValue(String deviceOrderValue, String deviceOrderValueReal);
public abstract boolean getDate(long deviceOrderValue, long deviceOrderValueReal);
public abstract String getName();
}
package cn.com.glsx.scheduler.common.enums;
/**
* @author fengzhi
* @date 2021/3/9 17:05
* @description 月份枚举
*/
public enum EMonth {
MONTH1 {
@Override
public Integer getCode() {
return 1;
}
@Override
public String getValue() {
return "01";
}
@Override
public String getBeanName() {
return "一月";
}
},
MONTH2 {
@Override
public Integer getCode() {
return 2;
}
@Override
public String getValue() {
return "02";
}
@Override
public String getBeanName() {
return "二月";
}
},
MONTH3 {
@Override
public Integer getCode() {
return 3;
}
@Override
public String getValue() {
return "03";
}
@Override
public String getBeanName() {
return "三月";
}
},
MONTH4 {
@Override
public Integer getCode() {
return 4;
}
@Override
public String getValue() {
return "04";
}
@Override
public String getBeanName() {
return "四月";
}
},
MONTH5 {
@Override
public Integer getCode() {
return 5;
}
@Override
public String getValue() {
return "05";
}
@Override
public String getBeanName() {
return "五月";
}
},
MONTH6 {
@Override
public Integer getCode() {
return 6;
}
@Override
public String getValue() {
return "06";
}
@Override
public String getBeanName() {
return "六月";
}
},
MONTH7 {
@Override
public Integer getCode() {
return 7;
}
@Override
public String getValue() {
return "07";
}
@Override
public String getBeanName() {
return "七月";
}
},
MONTH8 {
@Override
public Integer getCode() {
return 8;
}
@Override
public String getValue() {
return "08";
}
@Override
public String getBeanName() {
return "八月";
}
},
MONTH9 {
@Override
public Integer getCode() {
return 9;
}
@Override
public String getValue() {
return "09";
}
@Override
public String getBeanName() {
return "九月";
}
},
MONTH10 {
@Override
public Integer getCode() {
return 10;
}
@Override
public String getValue() {
return "10";
}
@Override
public String getBeanName() {
return "十月";
}
},
MONTH11 {
@Override
public Integer getCode() {
return 11;
}
@Override
public String getValue() {
return "11";
}
@Override
public String getBeanName() {
return "十一月";
}
},
MONTH12 {
@Override
public Integer getCode() {
return 12;
}
@Override
public String getValue() {
return "12";
}
@Override
public String getBeanName() {
return "十二月";
}
},
;
public static String getValueByCode(Integer code) {
for (EMonth month : EMonth.values()) {
if (month.getCode().equals(code)) {
return month.getValue();
}
}
return null;
}
public static String getBeanNameByCode(Integer code) {
for (EMonth month : EMonth.values()) {
if (month.getCode().equals(code)) {
return month.getBeanName();
}
}
return null;
}
public abstract Integer getCode();
public abstract String getValue();
public abstract String getBeanName();
}
package cn.com.glsx.scheduler.task.common.constant;
import com.glsx.plat.core.constant.BasicConstants;
/**
* 〈一句话功能简述〉<br>
* 公共常量类
*
* @author liuyf
* @since 1.0.0
*/
public final class Constants extends BasicConstants {
public final static String SERVER_NAME = "glsx-ne-shield-job";
}
package cn.com.glsx.scheduler.task.common.exception;
import com.glsx.plat.exception.SystemMessage;
import lombok.Getter;
import lombok.Setter;
/**
* @author payu
*/
@Setter
@Getter
public class AdminException extends RuntimeException {
private int errorCode;
public AdminException() {
}
public AdminException(String message) {
super(message);
}
public AdminException(int errorCode, String message) {
super(message);
this.errorCode = errorCode;
}
public AdminException(Throwable cause) {
super(cause);
}
public AdminException(String message, Throwable cause) {
super(message, cause);
}
public AdminException of(SystemMessage message) {
return new AdminException(message.getCode(), message.getMsg());
}
}
package cn.com.glsx.scheduler.task.common.exception;
import com.glsx.plat.core.web.R;
import com.glsx.plat.exception.ExceptionLevel;
import com.glsx.plat.exception.GlobalExceptionHandler;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.binding.BindingException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
/**
* 全局异常处理
*
* @author payu
*/
@Slf4j
@RestControllerAdvice
public class AdminExceptionHandler extends GlobalExceptionHandler {
@ExceptionHandler(AdminException.class)
public R handleAdminException(AdminException e) {
return R.error(e.getMessage());
}
/**
* 引入Feign服务的话
*
* @param e
* @return
*/
// @ExceptionHandler(FeignException.class)
// public R handleFeignException(FeignException e) {
// return R.error(e.status(), e.getMessage());
// }
/**
* Mybatis BindingException
*
* @param e
* @return
*/
@ExceptionHandler(BindingException.class)
public R handleBindingException(BindingException e) {
return R.error(e.getMessage());
}
@Override
protected void saveException(Exception e, ExceptionLevel level) {
super.saveException(e, level);
}
}
package cn.com.glsx.scheduler.task.config;
import com.dangdang.ddframe.job.api.simple.SimpleJob;
import com.dangdang.ddframe.job.config.JobCoreConfiguration;
import com.dangdang.ddframe.job.config.simple.SimpleJobConfiguration;
import com.dangdang.ddframe.job.lite.api.JobScheduler;
import com.dangdang.ddframe.job.lite.config.LiteJobConfiguration;
import com.dangdang.ddframe.job.reg.zookeeper.ZookeeperConfiguration;
import com.dangdang.ddframe.job.reg.zookeeper.ZookeeperRegistryCenter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class ElasticJobConfig {
@Autowired
private ZookeeperRegistryCenter regCenter;
@Bean(initMethod = "init")
public ZookeeperRegistryCenter regCenter(@Value("${elaticjob.zookeeper.server-lists}") final String serverList, @Value("${elaticjob.zookeeper.namespace}") final String namespace) {
return new ZookeeperRegistryCenter(new ZookeeperConfiguration(serverList, namespace));
}
/**
* 动态添加
*
* @param jobClass
* @param cron
* @param shardingTotalCount
* @param shardingItemParameters
*/
public void addSimpleJobScheduler(final Class<? extends SimpleJob> jobClass,
final String cron,
final int shardingTotalCount,
final String shardingItemParameters) {
JobCoreConfiguration coreConfig = JobCoreConfiguration.newBuilder(jobClass.getName(), cron, shardingTotalCount).shardingItemParameters(shardingItemParameters).jobParameter("job参数").build();
SimpleJobConfiguration simpleJobConfig = new SimpleJobConfiguration(coreConfig, jobClass.getCanonicalName());
JobScheduler jobScheduler = new JobScheduler(regCenter, LiteJobConfiguration.newBuilder(simpleJobConfig).build());
jobScheduler.init();
}
}
\ No newline at end of file
package cn.com.glsx.scheduler.task.entity;
import lombok.Data;
import javax.persistence.Column;
import javax.persistence.Id;
import javax.persistence.Table;
import java.io.Serializable;
import java.util.Date;
/**
* 试驾统计数据表
*
* @TableName t_statistics_fence_drive
*/
@Table(name = "t_statistics_fence_drive")
@Data
public class StatisticsDrive implements Serializable {
private static final long serialVersionUID = 1L;
/**
*
*/
@Id
private Integer id;
/**
* 设备号
*/
private String sn;
/**
* 当天有效试驾次数
*/
@Column(name = "drive_num")
private Integer driveNum;
/**
* 当天试驾总次数
*/
@Column(name = "drive_total")
private Integer driveTotal;
/**
* 试驾日期
*/
@Column(name = "drive_time")
private String driveTime;
/**
* 创建时间
*/
@Column(name = "created_time")
private Date createdTime;
/**
* 更新时间
*/
@Column(name = "updated_time")
private Date updatedTime;
}
\ No newline at end of file
package cn.com.glsx.scheduler.task.mapper;
import cn.com.glsx.scheduler.task.entity.StatisticsDrive;
import com.glsx.plat.mybatis.mapper.CommonBaseMapper;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import java.util.List;
/**
* @Entity generator.domain.StatisticsFenceDrive
*/
@Mapper
public interface StatisticsDriveMapper extends CommonBaseMapper<StatisticsDrive> {
/**
* 获取最大的试驾天数
*
* @return 最大的试驾天数
*/
String getMaxDriveTime();
/**
* 统计每一天的数据总和
*
* @param yesterDayFormat 昨天的日期
* @return 统计数据的集合
*/
List<StatisticsDrive> statisticsDriveDataEveryDay(@Param("yesterDayFormat") String yesterDayFormat);
}
package cn.com.glsx.scheduler.task.modules.service;
import cn.com.glsx.scheduler.task.entity.StatisticsDrive;
import cn.com.glsx.scheduler.task.mapper.StatisticsDriveMapper;
import com.google.common.collect.Lists;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
/**
* @author liuyf
* @date 2021年01月26日 下午2:37:40
*/
@Service
public class MySimpleJobService {
@Autowired
private StatisticsDriveMapper statisticsDriveMapper;
public void statisticsDriveDataEveryDay() {
// 获取昨天的日期
Calendar cal = Calendar.getInstance();
cal.add(Calendar.DATE, -1);
String yesterDayFormat = new SimpleDateFormat("yyyy-MM-dd").format(cal.getTime());
List<StatisticsDrive> statisticsDriveList = Lists.newArrayList();
statisticsDriveList = statisticsDriveMapper.statisticsDriveDataEveryDay(yesterDayFormat);
for (StatisticsDrive statisticsDrive : statisticsDriveList) {
statisticsDrive.setCreatedTime(new Date());
statisticsDriveMapper.insertSelective(statisticsDrive);
}
}
}
package cn.com.glsx.scheduler.task.taskscheduler;
import cn.com.glsx.scheduler.task.modules.service.MySimpleJobService;
import com.dangdang.ddframe.job.api.ShardingContext;
import com.dangdang.ddframe.job.api.simple.SimpleJob;
import com.dangdang.elasticjob.lite.annotation.ElasticSimpleJob;
import com.glsx.plat.common.utils.DateUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.util.Date;
@Slf4j
@Component
@ElasticSimpleJob(jobName = "MySimpleJob", cron = "0 15 5 * * ?")
public class MySimpleJob implements SimpleJob {
@Autowired
private MySimpleJobService simpleJobService;
@Override
public void execute(ShardingContext shardingContext) {
log.info("Thread ID: {}, 作业分片总数: {}, " +
"当前分片项: {}.当前参数: {}," +
"作业名称: {}.作业自定义参数: {}",
Thread.currentThread().getId(),
shardingContext.getShardingTotalCount(),
shardingContext.getShardingItem(),
shardingContext.getShardingParameter(),
shardingContext.getJobName(),
shardingContext.getJobParameter()
);
String normalDate = DateUtils.formatNormal(new Date());
log.info("---------------{}-开始----------------", normalDate);
simpleJobService.statisticsDriveDataEveryDay();
log.info("---------------{}-结束--------------", normalDate);
}
}
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!