19 changed files with 411 additions and 34 deletions
@ -0,0 +1,22 @@
|
||||
package com.biutag.supervision.config; |
||||
|
||||
import org.springframework.context.annotation.Bean; |
||||
import org.springframework.context.annotation.Configuration; |
||||
import org.springframework.scheduling.TaskScheduler; |
||||
import org.springframework.scheduling.config.ScheduledTaskRegistrar; |
||||
|
||||
/** |
||||
* @author wxc |
||||
* @date 2024/10/17 |
||||
*/ |
||||
@Configuration |
||||
public class ScheduledConfig { |
||||
|
||||
@Bean |
||||
public ScheduledTaskRegistrar scheduledTaskRegistrar(TaskScheduler taskScheduler) { |
||||
ScheduledTaskRegistrar scheduledTaskRegistrar = new ScheduledTaskRegistrar(); |
||||
scheduledTaskRegistrar.setTaskScheduler(taskScheduler); |
||||
return scheduledTaskRegistrar; |
||||
} |
||||
|
||||
} |
||||
@ -0,0 +1,10 @@
|
||||
package com.biutag.supervision.constants.enums; |
||||
|
||||
/** |
||||
* @author wxc |
||||
* @date 2024/10/17 |
||||
*/ |
||||
public enum DistributionCycleEnum { |
||||
day, |
||||
weekly |
||||
} |
||||
@ -0,0 +1,17 @@
|
||||
package com.biutag.supervision.constants.enums; |
||||
|
||||
import lombok.AllArgsConstructor; |
||||
import lombok.Getter; |
||||
|
||||
/** |
||||
* @author wxc |
||||
* @date 2024/10/17 |
||||
*/ |
||||
@AllArgsConstructor |
||||
@Getter |
||||
public enum DistributionFlowEnum { |
||||
|
||||
SECOND("2"), |
||||
THIRD("3"); |
||||
private String value; |
||||
} |
||||
@ -0,0 +1,23 @@
|
||||
package com.biutag.supervision.constants.enums; |
||||
|
||||
import lombok.AllArgsConstructor; |
||||
import lombok.Getter; |
||||
|
||||
/** |
||||
* @author wxc |
||||
* @date 2024/10/17 |
||||
*/ |
||||
@AllArgsConstructor |
||||
@Getter |
||||
public enum DistributionMethodEnum { |
||||
|
||||
// 问题下发
|
||||
NEGATIVE_DISTRIBUTE("1"), |
||||
// 数据保存
|
||||
DATA_SAVE("2"), |
||||
// 预警通知
|
||||
WARNING_NOTIFICATION("3"); |
||||
|
||||
private String value; |
||||
|
||||
} |
||||
@ -0,0 +1,20 @@
|
||||
package com.biutag.supervision.constants.enums; |
||||
|
||||
import lombok.AllArgsConstructor; |
||||
import lombok.Getter; |
||||
|
||||
/** |
||||
* @author wxc |
||||
* @date 2024/10/17 |
||||
*/ |
||||
@AllArgsConstructor |
||||
public enum ModelDistributionStateEnum { |
||||
|
||||
// 未分发
|
||||
UNDISTRIBUTED("0"), |
||||
// 已分发
|
||||
DISTRIBUTED("1"); |
||||
|
||||
@Getter |
||||
private String value; |
||||
} |
||||
@ -1,11 +1,55 @@
|
||||
package com.biutag.supervision.service; |
||||
|
||||
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; |
||||
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; |
||||
import com.biutag.supervision.pojo.entity.Model; |
||||
import com.biutag.supervision.common.UserContextHolder; |
||||
import com.biutag.supervision.constants.enums.DistributionCycleEnum; |
||||
import com.biutag.supervision.constants.enums.DistributionMethodEnum; |
||||
import com.biutag.supervision.mapper.ModelMapper; |
||||
import com.biutag.supervision.pojo.entity.Model; |
||||
import com.biutag.supervision.pojo.model.UserAuth; |
||||
import com.biutag.supervision.util.ExpressionBuilder; |
||||
import lombok.RequiredArgsConstructor; |
||||
import org.springframework.stereotype.Service; |
||||
|
||||
import java.time.LocalDateTime; |
||||
import java.util.List; |
||||
|
||||
@RequiredArgsConstructor |
||||
@Service |
||||
public class ModelService extends ServiceImpl<ModelMapper, Model> { |
||||
|
||||
public List<Model> listByDistributionMethodOfNegative() { |
||||
return list(new LambdaQueryWrapper<Model>().eq(Model::getDistributionMethod, DistributionMethodEnum.NEGATIVE_DISTRIBUTE.getValue())); |
||||
} |
||||
|
||||
public boolean saveModel(Model model) { |
||||
if (DistributionMethodEnum.NEGATIVE_DISTRIBUTE.getValue().equals(model.getDistributionMethod())) { |
||||
String expression = DistributionCycleEnum.day.name().equals(model.getDistributionCycle()) ? ExpressionBuilder.build(model.getDistributionCycleTime()) : |
||||
ExpressionBuilder.build(model.getDistributionCycleTime(), model.getDistributionCycleDayOfWeek()); |
||||
model.setDistributionCycleExpression(expression); |
||||
} |
||||
model.setCreateTime(LocalDateTime.now()); |
||||
model.setUpdateTime(LocalDateTime.now()); |
||||
UserAuth user = UserContextHolder.getCurrentUser(); |
||||
model.setCreateDepartId(user.getDepartId()); |
||||
model.setCreateDepartName(user.getDepartName()); |
||||
boolean save = save(model); |
||||
return save; |
||||
} |
||||
|
||||
public boolean updateModel(Model model) { |
||||
if (DistributionMethodEnum.NEGATIVE_DISTRIBUTE.getValue().equals(model.getDistributionMethod())) { |
||||
String expression = DistributionCycleEnum.day.name().equals(model.getDistributionCycle()) ? ExpressionBuilder.build(model.getDistributionCycleTime()) : |
||||
ExpressionBuilder.build(model.getDistributionCycleTime(), model.getDistributionCycleDayOfWeek()); |
||||
model.setDistributionCycleExpression(expression); |
||||
} |
||||
model.setUpdateTime(LocalDateTime.now()); |
||||
return updateById(model); |
||||
} |
||||
|
||||
public boolean remove(Integer modelId) { |
||||
return removeById(modelId); |
||||
} |
||||
|
||||
} |
||||
|
||||
@ -0,0 +1,78 @@
|
||||
package com.biutag.supervision.support; |
||||
|
||||
import cn.hutool.core.util.StrUtil; |
||||
import com.biutag.supervision.pojo.entity.Model; |
||||
import com.biutag.supervision.service.ModelClueService; |
||||
import com.biutag.supervision.service.ModelService; |
||||
import jakarta.annotation.PostConstruct; |
||||
import lombok.RequiredArgsConstructor; |
||||
import lombok.extern.slf4j.Slf4j; |
||||
import org.springframework.scheduling.config.CronTask; |
||||
import org.springframework.scheduling.config.ScheduledTask; |
||||
import org.springframework.scheduling.config.ScheduledTaskRegistrar; |
||||
import org.springframework.stereotype.Component; |
||||
|
||||
import java.util.List; |
||||
import java.util.Map; |
||||
import java.util.Objects; |
||||
import java.util.concurrent.ConcurrentHashMap; |
||||
|
||||
/** |
||||
* 模型线索数据分发 |
||||
* @author wxc |
||||
* @date 2024/10/17 |
||||
*/ |
||||
@Slf4j |
||||
@RequiredArgsConstructor |
||||
@Component |
||||
public class ModelClueDistributionAware { |
||||
|
||||
private final ScheduledTaskRegistrar taskRegistrar; |
||||
private final Map<Integer, ScheduledTask> taskMap = new ConcurrentHashMap<>(); |
||||
|
||||
private final ModelService modelService; |
||||
|
||||
private final ModelClueService modelClueService; |
||||
|
||||
@PostConstruct |
||||
public void init() { |
||||
log.info("初始化模型分发任务-------------------------------------------"); |
||||
List<Model> models = modelService.listByDistributionMethodOfNegative(); |
||||
for (Model model : models) { |
||||
try { |
||||
put(model); |
||||
} catch (RuntimeException e) { |
||||
log.info("模型分发异常", e); |
||||
} |
||||
} |
||||
} |
||||
|
||||
public void put(Model model) { |
||||
log.info("新增模型【{}】分发任务", model.getModelName()); |
||||
String expression = model.getDistributionCycleExpression(); |
||||
if (StrUtil.isBlank(expression)) { |
||||
throw new RuntimeException("分发周期配置[expression]为空"); |
||||
} |
||||
if (taskMap.containsKey(model.getId())) { |
||||
remove(model.getId()); |
||||
} |
||||
Runnable task = () -> { |
||||
log.info("开始执行任务-------------------------------------------"); |
||||
// 线索下发
|
||||
modelClueService.distribution(model.getId()); |
||||
}; |
||||
log.info("expression : {}", expression); |
||||
ScheduledTask scheduledTask = taskRegistrar.scheduleTriggerTask(new CronTask(task, expression)); |
||||
taskMap.put(model.getId(), scheduledTask); |
||||
} |
||||
|
||||
public void remove(Integer key) { |
||||
log.info("移除模型【{}】分发任务", key); |
||||
ScheduledTask scheduledTask = taskMap.get(key); |
||||
if (Objects.nonNull(scheduledTask)) { |
||||
scheduledTask.cancel(); |
||||
taskMap.remove(key); |
||||
} |
||||
|
||||
} |
||||
} |
||||
@ -0,0 +1,45 @@
|
||||
package com.biutag.supervision.support; |
||||
|
||||
import com.biutag.supervision.pojo.entity.Model; |
||||
import lombok.RequiredArgsConstructor; |
||||
import lombok.extern.slf4j.Slf4j; |
||||
import org.aspectj.lang.JoinPoint; |
||||
import org.aspectj.lang.annotation.*; |
||||
import org.springframework.stereotype.Component; |
||||
|
||||
/** |
||||
* @author wxc |
||||
* @date 2024/10/17 |
||||
*/ |
||||
@Slf4j |
||||
@RequiredArgsConstructor |
||||
@Aspect |
||||
@Component |
||||
public class ModelEditAspect { |
||||
|
||||
private final ModelClueDistributionAware modelClueDistributionAware; |
||||
|
||||
@Pointcut("execution(* com.biutag.supervision.service.ModelService.saveModel(..))") |
||||
public void saveMethod() {} |
||||
|
||||
@Pointcut("execution(* com.biutag.supervision.service.ModelService.updateModel(..))") |
||||
public void updateMethod() {} |
||||
|
||||
@Pointcut("execution(* com.biutag.supervision.service.ModelService.remove(Integer))") |
||||
public void removeMethod() {} |
||||
|
||||
@After("saveMethod() || updateMethod()") |
||||
public void afterSaveOrUpdateAdvice(JoinPoint joinPoint) { |
||||
Model model = (Model) joinPoint.getArgs()[0]; |
||||
modelClueDistributionAware.put(model); |
||||
log.info("afterSaveOrUpdateAdvice---------------"); |
||||
} |
||||
|
||||
@After("removeMethod()") |
||||
public void afterRemove(JoinPoint joinPoint) { |
||||
Integer modelId = (Integer) joinPoint.getArgs()[0]; |
||||
modelClueDistributionAware.remove(modelId); |
||||
log.info("afterRemove---------------"); |
||||
} |
||||
|
||||
} |
||||
@ -0,0 +1,22 @@
|
||||
package com.biutag.supervision.util; |
||||
|
||||
import java.util.Optional; |
||||
|
||||
/** |
||||
* @author wxc |
||||
* @date 2024/10/17 |
||||
*/ |
||||
public class ExpressionBuilder { |
||||
|
||||
public static String build(String time) { |
||||
return build(time, null); |
||||
} |
||||
|
||||
public static String build(String time, String week) { |
||||
String expressionFormat = "%s %s %s * * %s"; |
||||
String[] split = time.split(":"); |
||||
String weekArg = Optional.ofNullable(week).orElse("*"); |
||||
return String.format(expressionFormat, Integer.parseInt(split[2]), Integer.parseInt(split[1]), Integer.parseInt(split[0]), weekArg); |
||||
} |
||||
|
||||
} |
||||
Loading…
Reference in new issue