feat: 定时任务更新阿里数据补偿功能

v3
chenhc 3 months ago
parent 378174251d
commit eed301b50f

@ -19,8 +19,13 @@ public interface ThirdAliDrugMapper extends BaseMapper<ThirdAliDrug> {
List<ThirdAliDrug> filterList(ThirdAliDrugRequest thirdAliDrug);
// @Select("SELECT cpmctymc FROM third_ali_drug GROUP BY cpmctymc")
// List<ThirdAliDrug> groupByCpmctymc();
@Select("SELECT a.cpmctymc FROM ( SELECT cpmctymc,max(createTime) as createTime FROM third_ali_drug GROUP BY cpmctymc) a WHERE a.createTime < '2025-01-06 09:00:00'")
@Select("SELECT cpmctymc FROM third_ali_drug_task GROUP BY cpmctymc")
List<ThirdAliDrug> groupByCpmctymcTask();
@Select("SELECT cpmctymc FROM third_ali_drug GROUP BY cpmctymc")
List<ThirdAliDrug> groupByCpmctymc();
boolean insertBatchTask(@Param("list") List<ThirdAliDrug> list);
boolean deleteCpmctymcTask(@Param("cpmctymc")String cpmctymc);
}

@ -7,12 +7,15 @@ import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.glxp.udidl.admin.dao.collect.ThirdAliDrugMapper;
import com.glxp.udidl.admin.entity.auth.AuthAliUser;
import com.glxp.udidl.admin.entity.collect.ThirdAliDrug;
import com.glxp.udidl.admin.exception.JsonException;
import com.glxp.udidl.admin.req.alihealth.AlihealthKytDrugrescodeReqeust;
import com.glxp.udidl.admin.req.alihealth.local.AlihealthThirdAliDrugInsertReqeust;
import com.glxp.udidl.admin.req.collect.ThirdAliDrugAddRequest;
import com.glxp.udidl.admin.req.collect.ThirdAliDrugRequest;
import com.glxp.udidl.admin.res.BaseResponse;
import com.glxp.udidl.admin.service.auth.AuthAliUserService;
import com.glxp.udidl.admin.util.alihealth.AlihealthUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
@ -74,12 +77,24 @@ public class ThirdAliDrugService extends ServiceImpl<ThirdAliDrugMapper, ThirdAl
}
@Resource
private AlihealthUtils alihealthUtils;
@Resource
private AuthAliUserService authAliUserService;
public void updateThirdAliDrugTask() {
List<ThirdAliDrug> groupedByCpmctymc = this.baseMapper.groupByCpmctymc();
if (CollUtil.isNotEmpty(groupedByCpmctymc)){
for (int i = 0; i < groupedByCpmctymc.size(); i++) {
ThirdAliDrug thirdAliDrug = groupedByCpmctymc.get(i);
//1、读取 任务待定表 如果有数据 继续执行 如果没有 读取数据 进行任务的插入
List<ThirdAliDrug> groupedByCpmctymcTask = this.baseMapper.groupByCpmctymcTask();
if (CollUtil.isEmpty(groupedByCpmctymcTask)){
List<ThirdAliDrug> groupedByCpmctymc = this.baseMapper.groupByCpmctymc();
if (CollUtil.isEmpty(groupedByCpmctymc))return;
this.insertBatchInChunks(groupedByCpmctymc);
groupedByCpmctymcTask = groupedByCpmctymc;
}
if (CollUtil.isNotEmpty(groupedByCpmctymcTask)){
for (int i = 0; i < groupedByCpmctymcTask.size(); i++) {
ThirdAliDrug thirdAliDrug = groupedByCpmctymcTask.get(i);
String cpmctymc = thirdAliDrug.getCpmctymc();
ThirdAliDrugAddRequest thirdAliDrugAddRequest = new ThirdAliDrugAddRequest();
@ -89,6 +104,20 @@ public class ThirdAliDrugService extends ServiceImpl<ThirdAliDrugMapper, ThirdAl
AlihealthKytDrugrescodeReqeust alihealthKytDrugrescodeReqeust = new AlihealthKytDrugrescodeReqeust();
alihealthKytDrugrescodeReqeust.setPhysic_name(cpmctymc);
request.setAlihealthKytDrugrescodeReqeust(alihealthKytDrugrescodeReqeust);
// key组装
AuthAliUser authAliUser = authAliUserService.getOne();
//增加次数限制
Integer drugResCodeCount = authAliUser.getDrugResCodeCount();
Integer drugResCodeCountMax = authAliUser.getDrugResCodeCountMax();
if (drugResCodeCount >= drugResCodeCountMax){
throw new JsonException("当前系统调用阿里药品数据查询接口最大次数已达到,禁止访问,如需访问请联系管理员增加访问次数!");
}
drugResCodeCount = drugResCodeCount + 1;
authAliUser.setDrugResCodeCount(drugResCodeCount);
authAliUserService.updateById(authAliUser);
try {
BaseResponse<List<ThirdAliDrug>> baseResponse = alihealthUtils.thirdAliDrugInsertAll(request);
if(baseResponse.getCode() == 20000){
@ -102,6 +131,10 @@ public class ThirdAliDrugService extends ServiceImpl<ThirdAliDrugMapper, ThirdAl
thirdAliDrugAddRequest.setList(add);
this.addThirdAliDrug(thirdAliDrugAddRequest);
//删除 已完成的任务
this.baseMapper.deleteCpmctymcTask(cpmctymc);
try {
Thread.sleep(15000);
} catch (InterruptedException e) {
@ -124,4 +157,31 @@ public class ThirdAliDrugService extends ServiceImpl<ThirdAliDrugMapper, ThirdAl
}
return new ThirdAliDrug();
}
// 方法用于分批插入数据
public void insertBatchInChunks(List<ThirdAliDrug> groupedByCpmctymc) {
int batchSize = 200; // 每批插入200条记录
int totalSize = groupedByCpmctymc.size();
// 计算需要多少个批次
int totalBatches = (totalSize + batchSize - 1) / batchSize;
for (int i = 0; i < totalBatches; i++) {
int fromIndex = i * batchSize;
int toIndex = Math.min(fromIndex + batchSize, totalSize);
List<ThirdAliDrug> batchList = groupedByCpmctymc.subList(fromIndex, toIndex);
// 调用批量插入方法
boolean result = this.baseMapper.insertBatchTask(batchList);
// 这里可以添加逻辑来处理每个批次插入的结果,例如记录日志或处理错误
if (!result) {
// 处理插入失败的情况,例如抛出异常或记录错误日志
System.err.println("Batch insertion failed for batch starting at index " + fromIndex);
// 根据需要决定是否继续执行后续的批次插入
}
}
}
}

@ -1,49 +1,49 @@
//package com.glxp.udidl.admin.thread;
//
//import com.glxp.udidl.admin.dao.info.ScheduledMapper;
//import com.glxp.udidl.admin.entity.info.ScheduledEntity;
//import com.glxp.udidl.admin.req.info.ScheduledRequest;
//import com.glxp.udidl.admin.service.chsapi.ChsYbHcxxService;
//import com.glxp.udidl.admin.service.collect.ThirdAliDrugService;
//import org.slf4j.Logger;
//import org.slf4j.LoggerFactory;
//import org.springframework.scheduling.annotation.EnableScheduling;
//import org.springframework.scheduling.annotation.SchedulingConfigurer;
//import org.springframework.scheduling.config.ScheduledTaskRegistrar;
//import org.springframework.scheduling.support.CronTrigger;
//import org.springframework.stereotype.Component;
//
//import javax.annotation.Resource;
//
//@Component
//@EnableScheduling
//public class ThirdAliDrugTask implements SchedulingConfigurer {
//
// @Resource
// private ScheduledMapper scheduledDao;
//
// @Resource
// private ThirdAliDrugService thirdAliDrugService;
//
// final Logger logger = LoggerFactory.getLogger(ThirdAliDrugTask.class);
//
// @Override
// public void configureTasks(ScheduledTaskRegistrar scheduledTaskRegistrar) {
// scheduledTaskRegistrar.addTriggerTask(() -> process(),
// triggerContext -> {
// ScheduledRequest scheduledRequest = new ScheduledRequest();
// scheduledRequest.setCronName("thirdAliDrugTask");
// ScheduledEntity scheduledEntity = scheduledDao.findScheduled(scheduledRequest);
// String cron = scheduledEntity.getCron();
// if (cron.isEmpty()) {
// logger.error("cron is null");
// }
// return new CronTrigger(cron).nextExecutionTime(triggerContext);
// });
// }
//
// private void process() {
// System.out.println("开始执行更新阿里健康数据");
// thirdAliDrugService.updateThirdAliDrugTask();
// }
//}
package com.glxp.udidl.admin.thread;
import com.glxp.udidl.admin.dao.info.ScheduledMapper;
import com.glxp.udidl.admin.entity.info.ScheduledEntity;
import com.glxp.udidl.admin.req.info.ScheduledRequest;
import com.glxp.udidl.admin.service.chsapi.ChsYbHcxxService;
import com.glxp.udidl.admin.service.collect.ThirdAliDrugService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.SchedulingConfigurer;
import org.springframework.scheduling.config.ScheduledTaskRegistrar;
import org.springframework.scheduling.support.CronTrigger;
import org.springframework.stereotype.Component;
import javax.annotation.Resource;
@Component
@EnableScheduling
public class ThirdAliDrugTask implements SchedulingConfigurer {
@Resource
private ScheduledMapper scheduledDao;
@Resource
private ThirdAliDrugService thirdAliDrugService;
final Logger logger = LoggerFactory.getLogger(ThirdAliDrugTask.class);
@Override
public void configureTasks(ScheduledTaskRegistrar scheduledTaskRegistrar) {
scheduledTaskRegistrar.addTriggerTask(() -> process(),
triggerContext -> {
ScheduledRequest scheduledRequest = new ScheduledRequest();
scheduledRequest.setCronName("thirdAliDrugTask");
ScheduledEntity scheduledEntity = scheduledDao.findScheduled(scheduledRequest);
String cron = scheduledEntity.getCron();
if (cron.isEmpty()) {
logger.error("cron is null");
}
return new CronTrigger(cron).nextExecutionTime(triggerContext);
});
}
private void process() {
System.out.println("开始执行更新阿里健康数据");
thirdAliDrugService.updateThirdAliDrugTask();
}
}

@ -149,16 +149,16 @@ public class AlihealthUtils {
AlihealthKytDrugrescodeReqeust alihealthKytDrugrescodeReqeust = alihealthThirdAliDrugInsertReqeust.getAlihealthKytDrugrescodeReqeust();
// key组装
AuthAliUser authAliUser = authAliUserService.getOne();
//增加次数限制
Integer drugResCodeCount = authAliUser.getDrugResCodeCount();
Integer drugResCodeCountMax = authAliUser.getDrugResCodeCountMax();
if (drugResCodeCount >= drugResCodeCountMax){
throw new JsonException("当前系统调用阿里药品数据查询接口最大次数已达到,禁止访问,如需访问请联系管理员增加访问次数!");
}
drugResCodeCount = drugResCodeCount + 1;
authAliUser.setDrugResCodeCount(drugResCodeCount);
authAliUserService.updateById(authAliUser);
//
// //增加次数限制
// Integer drugResCodeCount = authAliUser.getDrugResCodeCount();
// Integer drugResCodeCountMax = authAliUser.getDrugResCodeCountMax();
// if (drugResCodeCount >= drugResCodeCountMax){
// throw new JsonException("当前系统调用阿里药品数据查询接口最大次数已达到,禁止访问,如需访问请联系管理员增加访问次数!");
// }
// drugResCodeCount = drugResCodeCount + 1;
// authAliUser.setDrugResCodeCount(drugResCodeCount);
// authAliUserService.updateById(authAliUser);
alihealthThirdAliDrugInsertReqeust.setErpId(authAliUser.getErpid());
alihealthThirdAliDrugInsertReqeust.setAppSecret(authAliUser.getAppsecret());

@ -102,4 +102,21 @@
</if>
</where>
</select>
<insert id="insertBatchTask" parameterType="com.glxp.udidl.admin.entity.collect.ThirdAliDrug">
replace INTO third_ali_drug_task (cpmctymc)
VALUES
<foreach collection="list" item="item" separator="," index="index">
( #{item.cpmctymc})
</foreach>
</insert>
<delete id="deleteCpmctymcTask" parameterType="java.lang.String">
DELETE FROM third_ali_drug_task WHERE cpmctymc = #{cpmctymc}
</delete>
</mapper>

@ -364,3 +364,27 @@ CALL Pro_Temp_ColumnWork('code_rel', 'xjdw',
CALL Pro_Temp_ColumnWork('code_rel', 'ybBzgg',
' varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci DEFAULT NULL COMMENT ''医保包装规格''',
1);
CREATE TABLE IF NOT EXISTS `third_ali_drug_task` (
`id` int NOT NULL AUTO_INCREMENT,
`type` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci DEFAULT NULL COMMENT '药品类型',
`manufacturer` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci DEFAULT NULL COMMENT '生产企业',
`cpmctymc` varchar(255) DEFAULT NULL COMMENT '产品名称通用名称',
`form` varchar(255) DEFAULT NULL COMMENT '剂型',
`formSpec` varchar(255) DEFAULT NULL COMMENT '剂型规格',
`bzgg` varchar(255) DEFAULT NULL COMMENT '包装规格',
`spmc` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci DEFAULT NULL COMMENT '商品名称',
`nameCode` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci DEFAULT NULL COMMENT '层级标识',
`packRatio` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci DEFAULT NULL COMMENT '包装比例',
`packLevel` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci DEFAULT NULL COMMENT '包装级别',
`createTime` datetime DEFAULT NULL COMMENT '创建时间',
`updateTime` datetime DEFAULT NULL COMMENT '更新时间',
`erpId` varchar(255) DEFAULT NULL,
`approvalNum` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci DEFAULT NULL COMMENT '批准文号',
PRIMARY KEY (`id`),
UNIQUE KEY `nameCode` (`nameCode`),
KEY `approvalNum` (`approvalNum`) USING BTREE,
KEY `cpmctymc` (`cpmctymc`)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;
Loading…
Cancel
Save