给所有多数据源切换 加上try finally处理

This commit is contained in:
1378012178@qq.com 2025-10-16 15:08:10 +08:00
parent bcbcf55ea0
commit a068975b98
14 changed files with 261 additions and 108 deletions

View File

@ -15,6 +15,7 @@ import com.nu.modules.elder.api.IElderInfoApi;
import com.nu.modules.nubaseelder.api.INuBaseElderApi;
import com.nu.modules.orgapplyinfo.api.IOrgApplyInfoApi;
import com.nu.modules.orgapplyinfo.entity.OrgAllInfoApiEntity;
import com.nu.utils.NuRedisUtils;
import com.nu.utils.RabbitMQUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
@ -131,6 +132,7 @@ public class ElderApi {
//切换数据源整理好数据放到返回结果中
DynamicDataSourceContextHolder.push(orgCode);
try {
ElderInfoMQDto mqdto = new ElderInfoMQDto();
mqdto.setGuardianId(nuBizAdvisoryInfo.getId());//家属用户id
mqdto.setGuardianOpenId(nuBizAdvisoryInfo.getOpenId());//联系人openId
@ -140,9 +142,11 @@ public class ElderApi {
mqdto.setGuardianHomeAddress(nuBizAdvisoryInfo.getHomeAddress());//联系人家庭住址
mqdto.setGuardianWorkUnit(nuBizAdvisoryInfo.getWorkUnit());//联系人工作单位
rabbitMQUtil.sendToExchange("hldy.elder", orgCode + ".elder.updateguardianinfo", mqdto);
} finally {
DynamicDataSourceContextHolder.clear();
}
}
}
return Result.OK("编辑成功!");
}
@ -247,9 +251,13 @@ public class ElderApi {
// for (Map.Entry<String, String> entry : orgCodeToNuIdMap.entrySet()) {
//切换数据源整理好数据放到返回结果中
DynamicDataSourceContextHolder.push(orgCode);
List<ElderInfoEntity> list = elderInfoApi.queryElders(openId, elderId);
result.addAll(list);
List<ElderInfoEntity> list = Lists.newArrayList();
try {
list = elderInfoApi.queryElders(openId, elderId);
} finally {
DynamicDataSourceContextHolder.clear();
}
result.addAll(list);
// }
result.stream().forEach(d -> {
d.setOrgCode(orgCode);
@ -265,7 +273,10 @@ public class ElderApi {
* @return
*/
@PostMapping("/addElder")
public Result<?> save(@RequestBody ElderInfoEntity elderInfoEntity) {
public Result<?> save(@RequestParam("ukey") String ukey, @RequestBody ElderInfoEntity elderInfoEntity) {
if (NuRedisUtils.existKey(ukey)) {
return Result.ok("重复提交");
}
//长者id
String id = elderInfoEntity.getSysOrgCode() + IdUtil.simpleUUID();
@ -273,6 +284,7 @@ public class ElderApi {
{
//切换数据源
DynamicDataSourceContextHolder.push(elderInfoEntity.getSysOrgCode());
try {
NuBaseInfoEntity judgeResult = nuBaseInfoApi.queryByElderIdNum(elderInfoEntity.getIdCard());
if (judgeResult != null) {
//这个是老人id跟护理单元还绑定中认为是还在住
@ -285,9 +297,11 @@ public class ElderApi {
id = judgeResult.getElderTableId();
}
}
} finally {
//清理数据源
DynamicDataSourceContextHolder.clear();
}
}
elderInfoEntity.setOrgCode(elderInfoEntity.getSysOrgCode());
@ -327,7 +341,10 @@ public class ElderApi {
* @return
*/
@PostMapping("/updateElderInfo")
public Result<?> updateElderInfo(@RequestBody ElderInfoEntity elderInfoEntity) {
public Result<?> updateElderInfo(@RequestParam("ukey") String ukey, @RequestBody ElderInfoEntity elderInfoEntity) {
if (NuRedisUtils.existKey(ukey)) {
return Result.ok("重复提交");
}
elderInfoEntity.setOrgCode(elderInfoEntity.getSysOrgCode());
ElderInfoMQDto mqdto = new ElderInfoMQDto();
BeanUtils.copyProperties(elderInfoEntity, mqdto);

View File

@ -1,7 +1,8 @@
package com.nu.modules.wechat.employess.api;
import com.nu.modules.EmployessInfo.api.IEmployessAdvisioryInfoApi;
import com.nu.entity.EmployeesAdvisoryInfoEntity;
import com.nu.modules.EmployessInfo.api.IEmployessAdvisioryInfoApi;
import com.nu.utils.NuRedisUtils;
import org.apache.commons.lang.StringUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
@ -17,15 +18,20 @@ public class EmployessInfoApi {
@Autowired
private IEmployessAdvisioryInfoApi employessAdvisioryInfoApi;
/**
* 新增/编辑员工入驻申请信息
*
* @param ukey 幂等性
* @param EmployeesAdvisoryInfoEntity
* @return
*/
@AutoLog(value = "员工咨询信息-新增或修改")
@PostMapping("/saveApiEmployess")
public Result<Object> saveApiEmployess(@RequestBody EmployeesAdvisoryInfoEntity EmployeesAdvisoryInfoEntity) {
public Result<Object> saveApiEmployess(@RequestParam("ukey") String ukey, @RequestBody EmployeesAdvisoryInfoEntity EmployeesAdvisoryInfoEntity) {
if(NuRedisUtils.existKey(ukey)){
return Result.ok("重复提交");
}
//新增员工信息
// if (StringUtils.isNotBlank(EmployeesAdvisoryInfoEntity.getOpenId())) {
// EmployeesAdvisoryInfoEntity employeesAdvisoryInfoEntity = new EmployeesAdvisoryInfoEntity();
@ -39,7 +45,8 @@ public class EmployessInfoApi {
employessAdvisioryInfoApi.updateById(EmployeesAdvisoryInfoEntity);
return Result.ok("保存成功!");
} else {
return Result.ok(employessAdvisioryInfoApi.insert(EmployeesAdvisoryInfoEntity));
Result.ok(employessAdvisioryInfoApi.insert(EmployeesAdvisoryInfoEntity));
return Result.ok("添加成功!");
}
}

View File

@ -91,14 +91,15 @@ public class NuBaseInfoApi {
//切换数据源整理好数据放到返回结果中
DynamicDataSourceContextHolder.push(orgCode);
List<NuBillEntity> list;
try {
NuBillEntity params = new NuBillEntity();
params.setOpenId(openId);
params.setNuId(nuId);
List<NuBillEntity> list = nuBillApi.queryByOpenId(params);
// if (list == null || list.size() == 0) {
// continue;
// }
list = nuBillApi.queryByOpenId(params);
} finally {
DynamicDataSourceContextHolder.clear();
}
result.addAll(list);
// }
@ -150,7 +151,9 @@ public class NuBaseInfoApi {
//切换数据源整理好数据放到返回结果中
DynamicDataSourceContextHolder.push(orgCode);
List<NuBaseInfoEntity> baseList = nuBaseInfoApi.queryByOpenId(openId);
List<NuBaseInfoEntity> baseList;
try {
baseList = nuBaseInfoApi.queryByOpenId(openId);
if (baseList == null || baseList.size() == 0) {
continue;
}
@ -198,7 +201,9 @@ public class NuBaseInfoApi {
});
}
}
} finally {
DynamicDataSourceContextHolder.clear();
}
result.addAll(baseList);
}

View File

@ -4,6 +4,7 @@ import com.nu.modules.EmployessInfo.api.IEmployessAdvisioryInfoApi;
import com.nu.entity.EmployeesAdvisoryInfoEntity;
import com.nu.modules.orgapplyinfo.api.IOrgApplyInfoApi;
import com.nu.modules.orgapplyinfo.entity.OrgApplyInfoApiEntity;
import com.nu.utils.NuRedisUtils;
import org.apache.commons.lang.StringUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
@ -30,7 +31,10 @@ public class OrgApplyInfoApi {
*/
@AutoLog(value = "新增/编辑机构加盟申请信息")
@PostMapping("/save")
public Result<Object> save(@RequestBody OrgApplyInfoApiEntity orgApplyInfoApiEntity) {
public Result<Object> save(@RequestParam("ukey") String ukey,@RequestBody OrgApplyInfoApiEntity orgApplyInfoApiEntity) {
if(NuRedisUtils.existKey(ukey)){
return Result.ok("重复提交");
}
System.out.println("-----------新增/编辑机构加盟申请信息--------tel------------" + orgApplyInfoApiEntity.getTel());
if (StringUtils.isNotBlank(orgApplyInfoApiEntity.getTel())) {
EmployeesAdvisoryInfoEntity employeesAdvisoryInfoEntity = new EmployeesAdvisoryInfoEntity();

View File

@ -1,5 +1,6 @@
package com.nu.dto;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import java.io.Serializable;
@ -42,4 +43,12 @@ public class CategoryMQDto implements Serializable {
* 是否删除 0未删除 1删除
*/
private String delFlag;
/**
* 网络标记0为用pad本地静态图片1为用网络图
*/
private java.lang.String netFlag;
/**
* 动画图片
*/
private java.lang.String animationPath;
}

View File

@ -58,4 +58,12 @@ public class DirectiveTypeMQDto implements Serializable {
* 是否删除 0未删除 1删除
*/
private String delFlag;
/**
* 网络标记0为用pad本地静态图片1为用网络图
*/
private java.lang.String netFlag;
/**
* 动画图片
*/
private java.lang.String animationPath;
}

View File

@ -15,37 +15,73 @@ import java.util.Date;
@Data
public class ElderTagAsyncMQDto implements Serializable {
/**id*/
/**
* id
*/
private String id;
/**标签类型*/
/**
* 标签类型
*/
private String type;
/**标签名称*/
/**
* 标签名称
*/
private String tagName;
/**说明*/
/**
* 说明
*/
private java.lang.String describ;
/**价格*/
/**
* 价格
*/
private BigDecimal price;
/**图标*/
/**
* 图标
*/
private String pic;
/**图标md5值*/
/**
* 图标md5值
*/
private String picMd5;
/**排序*/
/**
* 焦点图标
*/
private java.lang.String picFocus;
/**
* 排序
*/
private Integer sort;
/**是否启用*/
/**
* 是否启用
*/
private String izEnabled;
/**是否删除*/
/**
* 是否删除
*/
private String delFlag;
/**创建人*/
/**
* 创建人
*/
private String createBy;
/**创建日期*/
/**
* 创建日期
*/
private Date createTime;
/**更新人*/
/**
* 更新人
*/
private String updateBy;
/**更新日期*/
/**
* 更新日期
*/
private Date updateTime;
/**所属机构*/
/**
* 所属机构
*/
private String sysOrgCode;
/**资源请求接口域名+项目上下文路径 */
/**
* 资源请求接口域名+项目上下文路径
*/
private String api;
}

View File

@ -0,0 +1,36 @@
package com.nu.utils;
import org.jeecg.common.util.RedisUtil;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;
@Component
public class NuRedisUtils implements ApplicationContextAware {
private static RedisUtil redisUtil;
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
// Spring 容器中获取 RedisUtil确保 RedisUtil Spring 管理的 Bean
redisUtil = applicationContext.getBean(RedisUtil.class);
}
/**
* 判断 key 是否存在如果不存在则设置30秒过期并返回 false
* 如果已存在返回 true
*/
public static boolean existKey(String key) {
if (redisUtil == null) {
throw new IllegalStateException("RedisUtil 尚未初始化,请确保 Spring 容器已启动");
}
if (!redisUtil.hasKey(key)) {
redisUtil.set(key, key, 15); // 过期时间
return false;
} else {
return true;
}
}
}

View File

@ -123,4 +123,5 @@ public class SafetyUtil {
// 计算 MD5 并返回
return calculateMD5(mixedKey);
}
}

View File

@ -39,6 +39,7 @@ public class ServiceElderBkJob implements Job {
/**
* 备份各机构长者标签
*
* @param jobExecutionContext
* @throws JobExecutionException
*/
@ -51,8 +52,12 @@ public class ServiceElderBkJob implements Job {
currentOrgCode = codes.get(i);
try {
DynamicDataSourceContextHolder.push(codes.get(i));
List<ElderTag> list = elderTagService.lambdaQuery().list();
List<ElderTag> list;
try {
list = elderTagService.lambdaQuery().list();
} finally {
DynamicDataSourceContextHolder.clear();
}
if (list != null && list.size() > 0) {
//增加长者标签主备份记录
ElderBkMain elderBkMain = new ElderBkMain();

View File

@ -60,6 +60,13 @@ public class ElderTag implements Serializable {
/**图标md5值*/
@ApiModelProperty(value = "图标md5值")
private java.lang.String picMd5;
/**焦点图标*/
@Excel(name = "焦点图标", width = 15)
@ApiModelProperty(value = "焦点图标")
private java.lang.String picFocus;
/**图标md5值*/
@ApiModelProperty(value = "图标md5值")
private java.lang.String picFocusMd5;
/**排序*/
@Excel(name = "排序", width = 15)
@ApiModelProperty(value = "排序")

View File

@ -39,6 +39,7 @@ public class ServiceDirectiveBkJob implements Job {
/**
* 备份各机构服务指令
*
* @param jobExecutionContext
* @throws JobExecutionException
*/
@ -50,9 +51,14 @@ public class ServiceDirectiveBkJob implements Job {
for (int i = 0; i < codes.size(); i++) {
currentOrgCode = codes.get(i);
try {
//定时任务 无法通过获取本身或注入方式使用@DS注解切换
DynamicDataSourceContextHolder.push(codes.get(i));
List<ConfigServiceDirective> directives = directiveService.queryAndTranslate(null);
List<ConfigServiceDirective> directives;
try {
directives = directiveService.queryAndTranslate(null);
} finally {
DynamicDataSourceContextHolder.clear();
}
if (directives != null && directives.size() > 0) {
//增加服务指令主备份记录
DirectiveBkMain directiveBkMain = new DirectiveBkMain();

View File

@ -81,4 +81,10 @@ public class ConfigServiceCategory implements Serializable {
/**所属部门*/
@ApiModelProperty(value = "所属部门")
private java.lang.String sysOrgCode;
/**网络标记0为用pad本地静态图片1为用网络图*/
@ApiModelProperty(value = "网络标记")
private java.lang.String netFlag;
/**动画图片*/
@ApiModelProperty(value = "动画图片")
private java.lang.String animationPath;
}

View File

@ -86,4 +86,10 @@ public class ConfigServiceType implements Serializable {
/**所属部门*/
@ApiModelProperty(value = "所属部门")
private java.lang.String sysOrgCode;
/**网络标记0为用pad本地静态图片1为用网络图*/
@ApiModelProperty(value = "网络标记")
private java.lang.String netFlag;
/**动画图片*/
@ApiModelProperty(value = "动画图片")
private java.lang.String animationPath;
}