From 82aa438f33006a25760c3aa0bff09880db8cc906 Mon Sep 17 00:00:00 2001 From: wxc <191104855@qq.com> Date: Thu, 17 Oct 2024 17:11:58 +0800 Subject: [PATCH] =?UTF-8?q?fit:=20=E6=A8=A1=E5=9E=8B=E9=99=90=E6=97=B6?= =?UTF-8?q?=E8=87=AA=E5=8A=A8=E4=B8=8B=E5=8F=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../supervision/config/ScheduledConfig.java | 22 ++++++ .../enums/DistributionCycleEnum.java | 10 +++ .../constants/enums/DistributionFlowEnum.java | 17 ++++ .../enums/DistributionMethodEnum.java | 23 ++++++ .../enums/ModelDistributionStateEnum.java | 20 +++++ .../constants/enums/ProblemSourcesEnum.java | 3 +- .../controller/LoginController.java | 2 +- .../sensitivePerception/ModelController.java | 19 ++--- .../supervision/mapper/BaseAccountMapper.java | 4 + .../biutag/supervision/pojo/entity/Model.java | 3 + .../supervision/pojo/entity/ModelClue.java | 11 ++- .../pojo/param/ModelClueQueryParam.java | 7 +- .../service/BaseAccountService.java | 4 + .../supervision/service/ModelClueService.java | 76 +++++++++++++++++- .../supervision/service/ModelService.java | 46 ++++++++++- .../support/ModelClueDistributionAware.java | 78 +++++++++++++++++++ .../supervision/support/ModelEditAspect.java | 45 +++++++++++ .../supervision/util/ExpressionBuilder.java | 22 ++++++ .../SupervisionApplicationTests.java | 33 +++++--- 19 files changed, 411 insertions(+), 34 deletions(-) create mode 100644 src/main/java/com/biutag/supervision/config/ScheduledConfig.java create mode 100644 src/main/java/com/biutag/supervision/constants/enums/DistributionCycleEnum.java create mode 100644 src/main/java/com/biutag/supervision/constants/enums/DistributionFlowEnum.java create mode 100644 src/main/java/com/biutag/supervision/constants/enums/DistributionMethodEnum.java create mode 100644 src/main/java/com/biutag/supervision/constants/enums/ModelDistributionStateEnum.java create mode 100644 src/main/java/com/biutag/supervision/support/ModelClueDistributionAware.java create mode 100644 src/main/java/com/biutag/supervision/support/ModelEditAspect.java create mode 100644 src/main/java/com/biutag/supervision/util/ExpressionBuilder.java diff --git a/src/main/java/com/biutag/supervision/config/ScheduledConfig.java b/src/main/java/com/biutag/supervision/config/ScheduledConfig.java new file mode 100644 index 0000000..705eef7 --- /dev/null +++ b/src/main/java/com/biutag/supervision/config/ScheduledConfig.java @@ -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; + } + +} diff --git a/src/main/java/com/biutag/supervision/constants/enums/DistributionCycleEnum.java b/src/main/java/com/biutag/supervision/constants/enums/DistributionCycleEnum.java new file mode 100644 index 0000000..2130ac6 --- /dev/null +++ b/src/main/java/com/biutag/supervision/constants/enums/DistributionCycleEnum.java @@ -0,0 +1,10 @@ +package com.biutag.supervision.constants.enums; + +/** + * @author wxc + * @date 2024/10/17 + */ +public enum DistributionCycleEnum { + day, + weekly +} diff --git a/src/main/java/com/biutag/supervision/constants/enums/DistributionFlowEnum.java b/src/main/java/com/biutag/supervision/constants/enums/DistributionFlowEnum.java new file mode 100644 index 0000000..25f8809 --- /dev/null +++ b/src/main/java/com/biutag/supervision/constants/enums/DistributionFlowEnum.java @@ -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; +} diff --git a/src/main/java/com/biutag/supervision/constants/enums/DistributionMethodEnum.java b/src/main/java/com/biutag/supervision/constants/enums/DistributionMethodEnum.java new file mode 100644 index 0000000..76aa7d5 --- /dev/null +++ b/src/main/java/com/biutag/supervision/constants/enums/DistributionMethodEnum.java @@ -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; + +} diff --git a/src/main/java/com/biutag/supervision/constants/enums/ModelDistributionStateEnum.java b/src/main/java/com/biutag/supervision/constants/enums/ModelDistributionStateEnum.java new file mode 100644 index 0000000..ffe61b7 --- /dev/null +++ b/src/main/java/com/biutag/supervision/constants/enums/ModelDistributionStateEnum.java @@ -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; +} diff --git a/src/main/java/com/biutag/supervision/constants/enums/ProblemSourcesEnum.java b/src/main/java/com/biutag/supervision/constants/enums/ProblemSourcesEnum.java index c313f56..9cfc09b 100644 --- a/src/main/java/com/biutag/supervision/constants/enums/ProblemSourcesEnum.java +++ b/src/main/java/com/biutag/supervision/constants/enums/ProblemSourcesEnum.java @@ -28,7 +28,8 @@ public enum ProblemSourcesEnum { JYDC("警意调查", "3"), CFJD("持法监督", "4"), JCKH("检查考核", "8"), - QT("其他", "9"); + QT("其他", "9"), + MXCS("模型超市", "10"); private String label; diff --git a/src/main/java/com/biutag/supervision/controller/LoginController.java b/src/main/java/com/biutag/supervision/controller/LoginController.java index eacac18..41d4624 100644 --- a/src/main/java/com/biutag/supervision/controller/LoginController.java +++ b/src/main/java/com/biutag/supervision/controller/LoginController.java @@ -39,7 +39,7 @@ public class LoginController { @Operation(summary = "登录") @PostMapping("login") public Result login(@RequestBody AccountDto account) { - BaseAccount baseAccount = accountService.getByAccount(account.getAccount()); + BaseAccount baseAccount = accountService.getByLogin(account.getAccount()); try { if (Objects.isNull(baseAccount) || !BCrypt.checkpw(account.getPassword(), baseAccount.getPassword())) { throw new RuntimeException("用户名或密码有误,请重新输入"); diff --git a/src/main/java/com/biutag/supervision/controller/sensitivePerception/ModelController.java b/src/main/java/com/biutag/supervision/controller/sensitivePerception/ModelController.java index 4af015e..49a15ce 100644 --- a/src/main/java/com/biutag/supervision/controller/sensitivePerception/ModelController.java +++ b/src/main/java/com/biutag/supervision/controller/sensitivePerception/ModelController.java @@ -3,11 +3,9 @@ package com.biutag.supervision.controller.sensitivePerception; import cn.hutool.core.util.StrUtil; import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; import com.baomidou.mybatisplus.extension.plugins.pagination.Page; -import com.biutag.supervision.common.UserContextHolder; import com.biutag.supervision.pojo.Result; import com.biutag.supervision.pojo.entity.Model; import com.biutag.supervision.pojo.entity.ModelClass; -import com.biutag.supervision.pojo.model.UserAuth; import com.biutag.supervision.pojo.param.ModelQueryParam; import com.biutag.supervision.pojo.vo.ModelTree; import com.biutag.supervision.service.ModelClassService; @@ -15,7 +13,6 @@ import com.biutag.supervision.service.ModelService; import lombok.RequiredArgsConstructor; import org.springframework.web.bind.annotation.*; -import java.time.LocalDateTime; import java.util.ArrayList; import java.util.List; import java.util.Objects; @@ -59,26 +56,20 @@ public class ModelController { } @PostMapping - public Result add(@RequestBody Model model) { - model.setCreateTime(LocalDateTime.now()); - model.setUpdateTime(LocalDateTime.now()); - UserAuth user = UserContextHolder.getCurrentUser(); - model.setCreateDepartId(user.getDepartId()); - model.setCreateDepartName(user.getDepartName()); - modelService.save(model); - return Result.success(); + public Result add(@RequestBody Model model) { + + return Result.success(modelService.saveModel(model)); } @PutMapping public Result update(@RequestBody Model model) { - model.setUpdateTime(LocalDateTime.now()); - modelService.updateById(model); + modelService.updateModel(model); return Result.success(model); } @DeleteMapping("{id}") public Result del(@PathVariable Integer id) { - return Result.success(modelService.removeById(id)); + return Result.success(modelService.remove(id)); } } diff --git a/src/main/java/com/biutag/supervision/mapper/BaseAccountMapper.java b/src/main/java/com/biutag/supervision/mapper/BaseAccountMapper.java index b108d96..5e9a799 100644 --- a/src/main/java/com/biutag/supervision/mapper/BaseAccountMapper.java +++ b/src/main/java/com/biutag/supervision/mapper/BaseAccountMapper.java @@ -3,8 +3,12 @@ package com.biutag.supervision.mapper; import com.baomidou.dynamic.datasource.annotation.DS; import com.baomidou.mybatisplus.core.mapper.BaseMapper; import com.biutag.supervision.pojo.entity.BaseAccount; +import org.apache.ibatis.annotations.Select; @DS("slave") public interface BaseAccountMapper extends BaseMapper { + @Select("select a.* from base_account a left join negative.sup_police p on a.account = p.id_code where a.account = #{account} or p.emp_no = #{account}") + BaseAccount selectByLogin(String account); + } \ No newline at end of file diff --git a/src/main/java/com/biutag/supervision/pojo/entity/Model.java b/src/main/java/com/biutag/supervision/pojo/entity/Model.java index af05575..e6b8f56 100644 --- a/src/main/java/com/biutag/supervision/pojo/entity/Model.java +++ b/src/main/java/com/biutag/supervision/pojo/entity/Model.java @@ -45,6 +45,9 @@ public class Model { // 分发周期 时间 private String distributionCycleTime; + // 分发周期,cron 表达式 + private String distributionCycleExpression; + // 限时 @TableField("time_limit") private String timeLimit; diff --git a/src/main/java/com/biutag/supervision/pojo/entity/ModelClue.java b/src/main/java/com/biutag/supervision/pojo/entity/ModelClue.java index 5879fda..985eae4 100644 --- a/src/main/java/com/biutag/supervision/pojo/entity/ModelClue.java +++ b/src/main/java/com/biutag/supervision/pojo/entity/ModelClue.java @@ -27,20 +27,25 @@ public class ModelClue { private String involveDepartId; // 涉及人员 - @TableField("involve_personnel_name") - private String involvePersonnelName; + private String involvePoliceName; + + // 涉及人员警号 + private String involvePoliceEmpNo; // 预警内容 @TableField("thing_desc") private String thingDesc; - // 分发状态 + // 分发状态 默认 0-未分发 1-已分发 @TableField("distribution_state") private String distributionState; @TableField("create_time") private LocalDateTime createTime; + private LocalDateTime updateTime; + + // 数据详情 JSON private String data; } \ No newline at end of file diff --git a/src/main/java/com/biutag/supervision/pojo/param/ModelClueQueryParam.java b/src/main/java/com/biutag/supervision/pojo/param/ModelClueQueryParam.java index 9899a71..7349f4a 100644 --- a/src/main/java/com/biutag/supervision/pojo/param/ModelClueQueryParam.java +++ b/src/main/java/com/biutag/supervision/pojo/param/ModelClueQueryParam.java @@ -1,9 +1,13 @@ package com.biutag.supervision.pojo.param; +import com.fasterxml.jackson.annotation.JsonFormat; import lombok.Getter; import lombok.Setter; +import org.springframework.format.annotation.DateTimeFormat; import java.time.LocalDateTime; +import java.util.ArrayList; +import java.util.Date; import java.util.List; /** @@ -16,7 +20,8 @@ public class ModelClueQueryParam extends BasePage { private Integer modelId; private String modelType; - private List createTime; + @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") + private List createTime = new ArrayList<>(); private Integer involveDepartId; private String thingDesc; private String distributionState; diff --git a/src/main/java/com/biutag/supervision/service/BaseAccountService.java b/src/main/java/com/biutag/supervision/service/BaseAccountService.java index 2a9af0b..39030f2 100644 --- a/src/main/java/com/biutag/supervision/service/BaseAccountService.java +++ b/src/main/java/com/biutag/supervision/service/BaseAccountService.java @@ -13,4 +13,8 @@ public class BaseAccountService extends ServiceImpl().eq(BaseAccount::getAccount, account)); } + public BaseAccount getByLogin(String account) { + return baseMapper.selectByLogin(account); + } + } diff --git a/src/main/java/com/biutag/supervision/service/ModelClueService.java b/src/main/java/com/biutag/supervision/service/ModelClueService.java index 1cdf481..0812582 100644 --- a/src/main/java/com/biutag/supervision/service/ModelClueService.java +++ b/src/main/java/com/biutag/supervision/service/ModelClueService.java @@ -5,14 +5,19 @@ import cn.hutool.core.util.StrUtil; import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; import com.baomidou.mybatisplus.extension.plugins.pagination.Page; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; +import com.biutag.supervision.constants.enums.*; +import com.biutag.supervision.mapper.ModelClueMapper; +import com.biutag.supervision.mapper.ModelMapper; +import com.biutag.supervision.pojo.dto.NegativeDto; import com.biutag.supervision.pojo.entity.Model; import com.biutag.supervision.pojo.entity.ModelClue; -import com.biutag.supervision.mapper.ModelClueMapper; +import com.biutag.supervision.pojo.entity.SupDepart; import com.biutag.supervision.pojo.model.ModelClueModel; import com.biutag.supervision.pojo.param.ModelClueQueryParam; import lombok.RequiredArgsConstructor; import org.springframework.stereotype.Service; +import java.time.LocalDateTime; import java.util.ArrayList; import java.util.List; import java.util.Objects; @@ -23,7 +28,11 @@ import java.util.stream.Collectors; @Service public class ModelClueService extends ServiceImpl { - private final ModelService modelService; + private final ModelMapper modelMapper; + + private final NegativeService negativeService; + + public final SupDepartService departService; public Page page(ModelClueQueryParam param) { LambdaQueryWrapper queryWrapper = new LambdaQueryWrapper() @@ -31,12 +40,15 @@ public class ModelClueService extends ServiceImpl { .eq(StrUtil.isNotBlank(param.getDistributionState()), ModelClue::getDistributionState, param.getDistributionState()) .like(StrUtil.isNotBlank(param.getThingDesc()), ModelClue::getThingDesc, param.getThingDesc()) .orderByDesc(ModelClue::getCreateTime); + if (Objects.nonNull(param.getCreateTime()) && param.getCreateTime().size() == 2) { + queryWrapper.between(ModelClue::getCreateTime, param.getCreateTime().get(0), param.getCreateTime().get(1)); + } Page page = page(Page.of(param.getCurrent(), param.getSize()), queryWrapper); if (page.getRecords().isEmpty()) { return new Page().setRecords(new ArrayList<>()).setTotal(0); } Set modelIds = page.getRecords().stream().map(ModelClue::getModelId).collect(Collectors.toSet()); - List models = modelService.listByIds(modelIds); + List models = modelMapper.selectBatchIds(modelIds); List list = page.getRecords().stream().map(item -> { ModelClueModel modelClueModel = new ModelClueModel(); BeanUtil.copyProperties(item, modelClueModel); @@ -47,4 +59,62 @@ public class ModelClueService extends ServiceImpl { return new Page().setRecords(list).setTotal(page.getTotal()); } + public List listByUnDistributed(Integer modelId) { + return list(new LambdaQueryWrapper().eq(ModelClue::getModelId, modelId).eq(ModelClue::getDistributionState, ModelDistributionStateEnum.UNDISTRIBUTED.getValue())); + } + + public boolean distribution(Integer modelId) { + List modelClues = listByUnDistributed(modelId); + Model model = modelMapper.selectById(modelId); + modelClues.forEach(item -> { + if (Objects.isNull(item.getInvolveDepartId())) { + throw new RuntimeException("涉及单位不能为空..."); + } + NegativeDto negative = new NegativeDto(); + // 问题来源 + negative.setProblemSourcesCode(ProblemSourcesEnum.MXCS.getValue()); + negative.setProblemSourcesCode(ProblemSourcesEnum.MXCS.getLabel()); + // TODO 业务类别 涉及问题 + // 涉及单位 + negative.setInvolveDepartId(item.getInvolveDepartId()); + negative.setInvolveDepartName(item.getInvolveDepartName()); + // 问题发生时间 + negative.setHappenTime(item.getCreateTime()); + // 问题发现时间 + negative.setDiscoveryTime(item.getCreateTime()); + // 事情简要描述 + negative.setThingDesc(item.getThingDesc()); + // 主办层级 + negative.setHostLevel(HostLevelEnums.THREE.getValue()); + // 办理单位 + SupDepart depart = departService.getById(item.getInvolveDepartId()); + if (depart.getLevel().equals(DepartLevelEnum.SECOND.getValue())) { + negative.setDepartId(depart.getId()); + negative.setDepartId(depart.getName()); + } + if (depart.getLevel().equals(DepartLevelEnum.THREE.getValue()) && Objects.equals(model.getDistributionFlow(), DistributionFlowEnum.SECOND.getValue())) { + SupDepart parentDepart = departService.getParentDepart(item.getInvolveDepartId()); + negative.setDepartId(parentDepart.getId()); + negative.setDepartId(parentDepart.getName()); + } + if (depart.getLevel().equals(DepartLevelEnum.THREE.getValue()) && Objects.equals(model.getDistributionFlow(), DistributionFlowEnum.THIRD.getValue())) { + negative.setDepartId(depart.getId()); + negative.setDepartId(depart.getName()); + } + negative.setTimeLimit(model.getTimeLimit()); + negative.setMaxSignDuration(model.getMaxSignDuration()); + negative.setMaxHandleDuration(model.getMaxHandleDuration()); + negative.setMaxExtensionDuration(model.getMaxExtensionDuration()); + // 审批流程 + negative.setApprovalFlow(model.getApprovalFlow()); + + negativeService.save(negative); + + item.setDistributionState(ModelDistributionStateEnum.DISTRIBUTED.getValue()); + item.setUpdateTime(LocalDateTime.now()); + updateById(item); + }); + return true; + } + } diff --git a/src/main/java/com/biutag/supervision/service/ModelService.java b/src/main/java/com/biutag/supervision/service/ModelService.java index 379715d..884d0e0 100644 --- a/src/main/java/com/biutag/supervision/service/ModelService.java +++ b/src/main/java/com/biutag/supervision/service/ModelService.java @@ -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 { + public List listByDistributionMethodOfNegative() { + return list(new LambdaQueryWrapper().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); + } + } diff --git a/src/main/java/com/biutag/supervision/support/ModelClueDistributionAware.java b/src/main/java/com/biutag/supervision/support/ModelClueDistributionAware.java new file mode 100644 index 0000000..854487c --- /dev/null +++ b/src/main/java/com/biutag/supervision/support/ModelClueDistributionAware.java @@ -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 taskMap = new ConcurrentHashMap<>(); + + private final ModelService modelService; + + private final ModelClueService modelClueService; + + @PostConstruct + public void init() { + log.info("初始化模型分发任务-------------------------------------------"); + List 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); + } + + } +} diff --git a/src/main/java/com/biutag/supervision/support/ModelEditAspect.java b/src/main/java/com/biutag/supervision/support/ModelEditAspect.java new file mode 100644 index 0000000..07ede38 --- /dev/null +++ b/src/main/java/com/biutag/supervision/support/ModelEditAspect.java @@ -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---------------"); + } + +} diff --git a/src/main/java/com/biutag/supervision/util/ExpressionBuilder.java b/src/main/java/com/biutag/supervision/util/ExpressionBuilder.java new file mode 100644 index 0000000..4519ba7 --- /dev/null +++ b/src/main/java/com/biutag/supervision/util/ExpressionBuilder.java @@ -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); + } + +} diff --git a/src/test/java/com/biutag/supervision/SupervisionApplicationTests.java b/src/test/java/com/biutag/supervision/SupervisionApplicationTests.java index edf9ba4..77c70dc 100644 --- a/src/test/java/com/biutag/supervision/SupervisionApplicationTests.java +++ b/src/test/java/com/biutag/supervision/SupervisionApplicationTests.java @@ -1,16 +1,17 @@ package com.biutag.supervision; -import com.github.tobato.fastdfs.domain.fdfs.StorePath; -import com.github.tobato.fastdfs.service.AppendFileStorageClient; import com.github.tobato.fastdfs.service.FastFileStorageClient; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; -import org.springframework.data.redis.core.RedisTemplate; +import org.springframework.scheduling.TaskScheduler; +import org.springframework.scheduling.Trigger; +import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler; +import org.springframework.scheduling.config.ScheduledTaskRegistrar; +import org.springframework.scheduling.support.CronTrigger; -import java.io.File; -import java.io.FileInputStream; -import java.io.FileNotFoundException; +import java.time.LocalDateTime; +import java.util.Date; @SpringBootTest class SupervisionApplicationTests { @@ -18,12 +19,24 @@ class SupervisionApplicationTests { @Autowired private FastFileStorageClient fastFileStorageClient; + @Autowired + private ScheduledTaskRegistrar taskRegistrar; + + @Autowired + private TaskScheduler taskScheduler; @Test - void contextLoads() throws FileNotFoundException { - File file = new File("D:\\deploy\\response.txt"); - StorePath storePath = fastFileStorageClient.uploadFile("group1", new FileInputStream(file), file.length(), "txt"); - System.out.println(storePath); + void contextLoads() { + ThreadPoolTaskScheduler scheduler = new ThreadPoolTaskScheduler(); + scheduler.setPoolSize(5); + scheduler.setThreadNamePrefix("scheduled-task-"); + scheduler.initialize(); + Runnable runnable = () -> { + System.out.println("Executing task at " + LocalDateTime.now()); + }; + taskRegistrar.setTaskScheduler(scheduler); + taskRegistrar.addTriggerTask(runnable, new CronTrigger("0 2 * * * ?")); + } }