Commit 7bb03a6b authored by honghy's avatar honghy

短信功能调整

parent b5d2fcc1
......@@ -18,7 +18,7 @@ public class SmsNodeBaseVO {
@ApiModelProperty(value = "运输方式ID", required = true)
@NotNull(message = "运输方式ID不能为空")
private Long transportId;
private Integer transportId;
@ApiModelProperty(value = "国家区号id", required = true)
@NotNull(message = "国家区号id不能为空")
......
......@@ -18,7 +18,7 @@ public class SmsNodeQueryVO extends PageParam{
private String nodeValue;
@ApiModelProperty(value = "运输方式ID")
private Long transportId;
private Integer transportId;
@ApiModelProperty(value = "国家区号id")
private String countryId;
......
......@@ -18,7 +18,6 @@ import cn.iocoder.yudao.module.system.dal.dataobject.sms.SmsNodeDO;
import cn.iocoder.yudao.module.system.dal.mysql.sms.SmsNodeMapper;
import com.alibaba.fastjson.JSON;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;
......@@ -162,23 +161,37 @@ public class SmsNodeServiceImpl extends AbstractService<SmsNodeMapper, SmsNodeDO
/**
* 创建短信节点
* <p>
* 此方法用于处理短信节点的创建请求它首先检查节点是否已存在于缓存中,
* 如果存在,则抛出异常表示节点重复如果不存在,则将节点信息插入数据库,
* 并在缓存中设置相应的键值对以备后续快速访问
* 此方法接收一个包含短信节点创建请求的VO对象,验证是否存在重复节点,然后将节点信息插入数据库,
* 并创建相应的缓存最后返回新创建节点的ID
*
* @param createReqVO 短信节点创建请求对象,包含需要创建的短信节点的相关信息
* @param createReqVO 短信节点创建请求的VO对象,包含节点的必要信息如节点值、传输ID、排序标志等
* @return 返回新创建的短信节点的ID
* @throws ServiceException 如果节点重复,则抛出此异常
* @throws ServiceException 如果检测到重复的节点,则抛出服务异常
*/
@Override
public Long createSmsNode(SmsNodeCreateReqVO createReqVO) {
// 将创建请求对象转换为短信节点对象
SmsNodeDO smsNode = SmsNodeConvert.INSTANCE.convert(createReqVO);
// 检查缓存中是否已存在相同节点
String node = redisHelper.get(buildCacheKey(smsNode));
if (StringUtils.isNotBlank(node)) {
// 检查是否已存在相同节点
SmsNodeQueryVO smsNodeDO = new SmsNodeQueryVO();
smsNodeDO.setNodeValue(createReqVO.getNodeValue());
smsNodeDO.setTransportId(createReqVO.getTransportId());
smsNodeDO.setIsOrders(createReqVO.getIsOrders());
List<SmsNodeDO> smsNodeDOList = smsNodeMapper.selectList(smsNodeDO);
if (smsNodeDOList != null && !smsNodeDOList.isEmpty()) {
Set<String> reqCountryIds = new HashSet<>(Arrays.asList(createReqVO.getCountryId().split(",")));
for (SmsNodeDO nodeDO : smsNodeDOList) {
String countryIds = nodeDO.getCountryId();
if ("0".equals(countryIds) || reqCountryIds.contains("0")) {
// 如果节点已存在,抛出异常
throw new ServiceException(500, "Node cannot be repeated");
throw new ServiceException(500, "不能重复添加");
}
Set<String> existingCountryIds = new HashSet<>(Arrays.asList(countryIds.split(",")));
if (!Collections.disjoint(reqCountryIds, existingCountryIds)) {
// 如果节点已存在,抛出异常
throw new ServiceException(500, "不能重复添加");
}
}
}
// 插入新的短信节点到数据库
smsNodeMapper.insert(smsNode);
......@@ -238,10 +251,22 @@ public class SmsNodeServiceImpl extends AbstractService<SmsNodeMapper, SmsNodeDO
// 将更新请求对象转换为需要更新的实体对象
SmsNodeDO updateObj = SmsNodeConvert.INSTANCE.convert(updateReqVO);
// 从缓存中获取当前节点信息
String node = redisHelper.get(buildCacheKey(updateObj));
SmsNodeQueryVO smsNode = new SmsNodeQueryVO();
smsNode.setNodeValue(updateReqVO.getNodeValue());
smsNode.setTransportId(updateReqVO.getTransportId());
smsNode.setIsOrders(updateReqVO.getIsOrders());
List<SmsNodeDO> smsNodeDOList = smsNodeMapper.selectList(smsNode);
// 检查节点是否重复,如果重复且ID不匹配,则抛出异常
if (StringUtils.isNotBlank(node) && !updateReqVO.getId().equals(JSON.parseObject(node, SmsNodeDO.class).getId())) {
throw new ServiceException(500, "Node cannot be repeated");
if (smsNodeDOList != null && !smsNodeDOList.isEmpty()) {
String[] targetCountries = updateObj.getCountryId().split(",");
for (SmsNodeDO nodeDO : smsNodeDOList) {
String[] existingCountries = nodeDO.getCountryId().split(",");
if (isDuplicateCountry(targetCountries, existingCountries, nodeDO.getId(), updateObj.getId())) {
throw new ServiceException(500, "不能重复添加");
}
}
}
// 更新数据库中的短信节点信息
smsNodeMapper.updateById(updateObj);
......@@ -251,6 +276,31 @@ public class SmsNodeServiceImpl extends AbstractService<SmsNodeMapper, SmsNodeDO
createCache(updateObj);
}
/**
* 检查目标国家和现有国家是否有重复
*
* @param targetCountries 目标国家数组
* @param existingCountries 现有国家数组
* @param existingNodeId 现有节点ID
* @param updateNodeId 更新节点ID
* @return 如果有重复且节点ID不匹配,则返回true,否则返回false
*/
private boolean isDuplicateCountry(String[] targetCountries, String[] existingCountries, Long existingNodeId, Long updateNodeId) {
for (String targetCountry : targetCountries) {
for (String existingCountry : existingCountries) {
// 检查是否有国家代码为"0"的情况,且节点ID不匹配
if (("0".equals(targetCountry) || "0".equals(existingCountry)) && existingNodeId != updateNodeId) {
return true;
}
// 检查国家代码是否匹配,且节点ID不匹配
if (targetCountry.equals(existingCountry) && existingNodeId != updateNodeId) {
return true;
}
}
}
return false;
}
@Override
public void deleteSmsNode(Long id) {
// 校验存在
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment