From faf499a4c6eb65e5fd9d8071c9cb6aec669b5303 Mon Sep 17 00:00:00 2001 From: x_z Date: Wed, 19 Apr 2023 17:35:44 +0800 Subject: [PATCH] =?UTF-8?q?1.=E5=90=88=E5=B9=B6=E7=AC=AC=E4=B8=89=E6=96=B9?= =?UTF-8?q?=E6=8E=A5=E5=8F=A3=E8=AF=B7=E6=B1=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../api/dao/thrsys/ThrSystemDetailDao.java | 7 + .../thrsys/ThirdSysInterfaceExecuteVo.java | 28 +++ .../thrsys/ThrInvWarehouseService.java | 12 +- .../impl/ThrInvWarehouseServiceImpl.java | 6 + .../glxp/api/task/ThirdSysInterfaceTask.java | 237 ++++++++++++++++++ .../java/com/glxp/api/util/RedisUtil.java | 18 ++ .../mapper/thrsys/ThrSystemDetailDao.xml | 10 + 7 files changed, 317 insertions(+), 1 deletion(-) create mode 100644 src/main/java/com/glxp/api/res/thrsys/ThirdSysInterfaceExecuteVo.java create mode 100644 src/main/java/com/glxp/api/task/ThirdSysInterfaceTask.java diff --git a/src/main/java/com/glxp/api/dao/thrsys/ThrSystemDetailDao.java b/src/main/java/com/glxp/api/dao/thrsys/ThrSystemDetailDao.java index 25cac9758..836d227b5 100644 --- a/src/main/java/com/glxp/api/dao/thrsys/ThrSystemDetailDao.java +++ b/src/main/java/com/glxp/api/dao/thrsys/ThrSystemDetailDao.java @@ -17,4 +17,11 @@ public interface ThrSystemDetailDao { List filterBasicThirdSysDetail(FilterBasicThirdSysDetailRequest filterBasicThirdSysDetailRequest); + /** + * 查询自动执行的接口列表 + * + * @return + */ + List selectAutoExecuteList(); + } diff --git a/src/main/java/com/glxp/api/res/thrsys/ThirdSysInterfaceExecuteVo.java b/src/main/java/com/glxp/api/res/thrsys/ThirdSysInterfaceExecuteVo.java new file mode 100644 index 000000000..2a3e66406 --- /dev/null +++ b/src/main/java/com/glxp/api/res/thrsys/ThirdSysInterfaceExecuteVo.java @@ -0,0 +1,28 @@ +package com.glxp.api.res.thrsys; + +import lombok.Data; + +/** + * 第三方系统接口执行状态VO + */ +@Data +public class ThirdSysInterfaceExecuteVo { + + /** + * 接口任务Key + * 规则:thirdI: + thirdId: + 接口名 + * 例子:thirdI:thirdId1:orderSubmitUrl + */ + private String key; + + /** + * 下次执行的时间戳 + */ + private long nextTime; + + /** + * 任务是否执行完毕 + */ + private boolean isFinished; + +} diff --git a/src/main/java/com/glxp/api/service/thrsys/ThrInvWarehouseService.java b/src/main/java/com/glxp/api/service/thrsys/ThrInvWarehouseService.java index 7ab3d0864..5572240dd 100644 --- a/src/main/java/com/glxp/api/service/thrsys/ThrInvWarehouseService.java +++ b/src/main/java/com/glxp/api/service/thrsys/ThrInvWarehouseService.java @@ -2,6 +2,7 @@ package com.glxp.api.service.thrsys; import com.glxp.api.entity.thrsys.ThrInvWarehouseEntity; +import com.glxp.api.entity.thrsys.ThrSystemDetailEntity; import com.glxp.api.req.thrsys.FilterThrSubInvWarehouseRequest; import com.glxp.api.res.thrsys.ThrInvWarehouseResponse; @@ -15,8 +16,10 @@ public interface ThrInvWarehouseService { ThrInvWarehouseEntity selectByThrCode(String thirdSys, String thirdId); List filterThrInvWarehouse(FilterThrSubInvWarehouseRequest filterThrSubInvWarehouseRequest); + List selectByPid(String pid); - List selectByThrDeptCode( FilterThrSubInvWarehouseRequest filterThrSubInvWarehouseRequest); + + List selectByThrDeptCode(FilterThrSubInvWarehouseRequest filterThrSubInvWarehouseRequest); boolean insertThrInvWarehouse(ThrInvWarehouseEntity thrInvWarehouseEntity); @@ -35,4 +38,11 @@ public interface ThrInvWarehouseService { List filterThrInvWarehouseResponse(FilterThrSubInvWarehouseRequest filterThrSubInvWarehouseRequest); List findByLastTime(Date lastUpdateTime); + + /** + * 下载第三方系统仓库/科室信息 + * + * @param thrSystemDetailEntity + */ + void downloadThrInv(ThrSystemDetailEntity thrSystemDetailEntity); } diff --git a/src/main/java/com/glxp/api/service/thrsys/impl/ThrInvWarehouseServiceImpl.java b/src/main/java/com/glxp/api/service/thrsys/impl/ThrInvWarehouseServiceImpl.java index af51cc40a..1debd0960 100644 --- a/src/main/java/com/glxp/api/service/thrsys/impl/ThrInvWarehouseServiceImpl.java +++ b/src/main/java/com/glxp/api/service/thrsys/impl/ThrInvWarehouseServiceImpl.java @@ -5,6 +5,7 @@ import cn.hutool.core.util.StrUtil; import com.github.pagehelper.PageHelper; import com.glxp.api.dao.thrsys.ThrInvWarehouseDao; import com.glxp.api.entity.thrsys.ThrInvWarehouseEntity; +import com.glxp.api.entity.thrsys.ThrSystemDetailEntity; import com.glxp.api.req.thrsys.FilterThrSubInvWarehouseRequest; import com.glxp.api.res.thrsys.ThrInvWarehouseResponse; import com.glxp.api.service.thrsys.ThrInvWarehouseService; @@ -118,4 +119,9 @@ public class ThrInvWarehouseServiceImpl implements ThrInvWarehouseService { return thrInvWarehouseDao.selectByLastTime(lastUpdateTime); } + @Override + public void downloadThrInv(ThrSystemDetailEntity thrSystemDetailEntity) { + + } + } diff --git a/src/main/java/com/glxp/api/task/ThirdSysInterfaceTask.java b/src/main/java/com/glxp/api/task/ThirdSysInterfaceTask.java new file mode 100644 index 000000000..f78c5f595 --- /dev/null +++ b/src/main/java/com/glxp/api/task/ThirdSysInterfaceTask.java @@ -0,0 +1,237 @@ +package com.glxp.api.task; + +import cn.hutool.core.collection.CollUtil; +import cn.hutool.core.date.DateUtil; +import cn.hutool.core.thread.ThreadUtil; +import cn.hutool.core.util.StrUtil; +import cn.hutool.json.JSONUtil; +import com.glxp.api.constant.ThirdSysConstant; +import com.glxp.api.dao.thrsys.ThrSystemDetailDao; +import com.glxp.api.entity.thrsys.ThrSystemDetailEntity; +import com.glxp.api.res.thrsys.ThirdSysInterfaceExecuteVo; +import com.glxp.api.service.inout.IoOrderService; +import com.glxp.api.service.thrsys.IThrBusTypeOriginService; +import com.glxp.api.service.thrsys.ThrCorpService; +import com.glxp.api.service.thrsys.ThrInvWarehouseService; +import com.glxp.api.service.thrsys.ThrProductsService; +import com.glxp.api.util.RedisUtil; +import lombok.extern.slf4j.Slf4j; +import org.springframework.scheduling.annotation.Scheduled; +import org.springframework.stereotype.Component; + +import javax.annotation.Resource; +import java.util.Date; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.concurrent.ConcurrentHashMap; +import java.util.concurrent.ExecutorService; + +/** + * 第三方系统接口定时任务 + * 此任务负责校验第三方接口设置里的所有接口列表,并生成任务到线程池中异步执行 + */ +@Slf4j +@Component +public class ThirdSysInterfaceTask { + + private ExecutorService executor; + + /** + * redis key 前缀 + */ + private static final Map> keyMap = new ConcurrentHashMap<>(5); + + /** + * 获取执行任务的线程池 + * + * @return + */ + private ExecutorService getExecutor() { + if (null == executor) { + log.info("初始化第三方系统接口执行线程池"); + executor = ThreadUtil.newExecutor(10, 100, Integer.MAX_VALUE); + } + return executor; + } + + @Resource + private RedisUtil redisUtil; + @Resource + private ThrSystemDetailDao thrSystemDetailDao; + @Resource + private ThrInvWarehouseService thrInvWarehouseService; + @Resource + private ThrCorpService thrCorpService; + @Resource + private ThrProductsService thrProductsService; + @Resource + private IThrBusTypeOriginService thrBusTypeOriginService; + @Resource + private IoOrderService orderService; + + @Scheduled(fixedRate = 60 * 1000, initialDelay = 60 * 1000) + public void scanInterface() { + log.info("开始扫描自动执行的第三方接口列表"); + List list = thrSystemDetailDao.selectAutoExecuteList(); + if (CollUtil.isNotEmpty(list)) { + log.info("本次查询到的接口列表数量:{}", list.size()); + list.parallelStream().forEach(thrSystemDetailEntity -> { + switch (thrSystemDetailEntity.getKey()) { + case ThirdSysConstant.WAREHOUSE_QUERY_URL: + //下载第三方仓库信息 + downloadThrInv(thrSystemDetailEntity); + break; + case ThirdSysConstant.CORP_URL: + //下载往来单位信息 + downloadThrCorp(thrSystemDetailEntity); + break; + case ThirdSysConstant.PI_QUERY_URL: + //下载第三方产品信息 + downloadThrPi(thrSystemDetailEntity); + break; + case ThirdSysConstant.BUS_TYPE_QUERY_URL: + //下载第三方单据类型 + downloadThrBusType(thrSystemDetailEntity); + break; + case ThirdSysConstant.ORDER_SUBMIT_URL: + //提交单据 + submitOrder(thrSystemDetailEntity); + break; + default: + //其他接口暂不处理 + break; + } + }); + } else { + log.info("未配置自动执行的第三方接口列表"); + } + } + + /** + * 提交单据到第三方系统 + * + * @param thrSystemDetailEntity + */ + private void submitOrder(ThrSystemDetailEntity thrSystemDetailEntity) { + + } + + /** + * 下载第三方单据类型 + * + * @param thrSystemDetailEntity + */ + private void downloadThrBusType(ThrSystemDetailEntity thrSystemDetailEntity) { + + } + + /** + * 下载第三方产品信息 + * + * @param thrSystemDetailEntity + */ + private void downloadThrPi(ThrSystemDetailEntity thrSystemDetailEntity) { + + } + + /** + * 下载第三方往来单位 + * + * @param thrSystemDetailEntity + */ + private void downloadThrCorp(ThrSystemDetailEntity thrSystemDetailEntity) { + + } + + /** + * 下载第三方系统仓库 + * + * @param thrSystemDetailEntity + */ + private void downloadThrInv(ThrSystemDetailEntity thrSystemDetailEntity) { + //校验任务并更新redis数据执行标识 + if (verifyTask(thrSystemDetailEntity)) { + getExecutor().submit(() -> { + log.info("开始下载第三方仓库信息"); + thrInvWarehouseService.downloadThrInv(thrSystemDetailEntity); + updateTask(getTaskKey(thrSystemDetailEntity)); + log.info("第三方仓库信息下载完成"); + }); + } + } + + /** + * 更新任务状态为已完成 + * + * @param taskKey + */ + private void updateTask(String taskKey) { + ThirdSysInterfaceExecuteVo vo = getLastResult(taskKey); + vo.setFinished(true); + redisUtil.set(taskKey, vo); + } + + /** + * 校验当前任务是否可以执行 + * + * @param thrSystemDetailEntity + * @return + */ + private boolean verifyTask(ThrSystemDetailEntity thrSystemDetailEntity) { + String taskKey = getTaskKey(thrSystemDetailEntity); + ThirdSysInterfaceExecuteVo vo = getLastResult(taskKey); + if (null != vo && !vo.isFinished()) { + log.info("有任务尚未执行完成,当前任务key:{}", taskKey); + return false; + } else { + if (vo == null) { + vo = new ThirdSysInterfaceExecuteVo(); + vo.setKey(taskKey); + } + vo.setNextTime(DateUtil.offsetMonth(new Date(), thrSystemDetailEntity.getTime()).getTime()); + vo.setFinished(false); + + redisUtil.set(taskKey, vo); + return true; + } + } + + /** + * 获取上一次执行结果 + * + * @param taskKey + * @return + */ + private ThirdSysInterfaceExecuteVo getLastResult(String taskKey) { + String json = redisUtil.getJSON(taskKey); + return StrUtil.isBlank(json) ? null : JSONUtil.toBean(json, ThirdSysInterfaceExecuteVo.class); + } + + /** + * 拼接redis Key + * + * @param thrSystemDetailEntity + * @return + */ + private String getTaskKey(ThrSystemDetailEntity thrSystemDetailEntity) { + String key = ""; + Map keys = keyMap.get(thrSystemDetailEntity.getThirdSysFk()); + if (CollUtil.isEmpty(keys)) { + Map map = new HashMap<>(1); + key = "thirdI:" + thrSystemDetailEntity.getThirdSysFk() + ":" + thrSystemDetailEntity.getKey(); + map.put(thrSystemDetailEntity.getKey(), key); + keyMap.put(thrSystemDetailEntity.getThirdSysFk(), map); + } else { + key = keys.get(thrSystemDetailEntity.getKey()); + if (StrUtil.isBlank(key)) { + key = "thirdI:" + thrSystemDetailEntity.getThirdSysFk() + ":" + thrSystemDetailEntity.getKey(); + Map map = new HashMap<>(1); + map.put(thrSystemDetailEntity.getKey(), key); + keyMap.put(thrSystemDetailEntity.getThirdSysFk(), map); + } + } + return key; + } + +} diff --git a/src/main/java/com/glxp/api/util/RedisUtil.java b/src/main/java/com/glxp/api/util/RedisUtil.java index 02f4f0cde..bb399da61 100644 --- a/src/main/java/com/glxp/api/util/RedisUtil.java +++ b/src/main/java/com/glxp/api/util/RedisUtil.java @@ -2,6 +2,8 @@ package com.glxp.api.util; import cn.hutool.core.collection.CollUtil; +import cn.hutool.core.util.StrUtil; +import cn.hutool.json.JSONUtil; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.redis.core.RedisTemplate; import org.springframework.stereotype.Component; @@ -99,6 +101,22 @@ public class RedisUtil { return key == null ? null : redisTemplate.opsForValue().get(key); } + /** + * 获取指定类型的结果的json数据 + * + * @param key + * @param t + * @return + */ + public String getJSON(String key) { + if (StrUtil.isBlank(key)) { + return null; + } + Object o = redisTemplate.opsForValue().get(key); + return null == o ? null : JSONUtil.toJsonStr(o); + } + + /** * 普通缓存放入 * diff --git a/src/main/resources/mybatis/mapper/thrsys/ThrSystemDetailDao.xml b/src/main/resources/mybatis/mapper/thrsys/ThrSystemDetailDao.xml index dc79e9075..ce9872754 100644 --- a/src/main/resources/mybatis/mapper/thrsys/ThrSystemDetailDao.xml +++ b/src/main/resources/mybatis/mapper/thrsys/ThrSystemDetailDao.xml @@ -45,4 +45,14 @@ and thr_system_detail.thirdSysFk = #{thirdSys} and thr_system.enabled = 1 + + \ No newline at end of file