diff --git a/sf-service/src/main/java/com/sf/service/gateway/domain/dto/InsertGatewayInterfaceInfoDTO.java b/sf-service/src/main/java/com/sf/service/gateway/domain/dto/InsertGatewayInterfaceInfoDTO.java index 7ed3d32..16d39c1 100644 --- a/sf-service/src/main/java/com/sf/service/gateway/domain/dto/InsertGatewayInterfaceInfoDTO.java +++ b/sf-service/src/main/java/com/sf/service/gateway/domain/dto/InsertGatewayInterfaceInfoDTO.java @@ -6,6 +6,7 @@ import io.swagger.annotations.ApiModel; import io.swagger.annotations.ApiModelProperty; import lombok.Data; import lombok.EqualsAndHashCode; +import org.hibernate.validator.constraints.Length; import javax.validation.constraints.NotBlank; import javax.validation.constraints.NotNull; @@ -24,10 +25,12 @@ import java.util.List; public class InsertGatewayInterfaceInfoDTO { private static final long serialVersionUID = 1L; + @Length(max = 50,message = "接口名称不能超过50个字符") @NotBlank(message = "接口名称不能为空") @ApiModelProperty("接口名称") private String interfaceName; + @Length(max = 200,message = "接口名称不能超过200个字符") @NotBlank(message = "接口路径不能为空") @ApiModelProperty("接口路径") private String interfacePath; @@ -36,15 +39,14 @@ public class InsertGatewayInterfaceInfoDTO { @ApiModelProperty("请求方式") private String requestMethod; + @Length(max = 200,message = "接口描述不能超过200个字符") @ApiModelProperty("接口描述") private String description; + @Length(max = 20,message = "接口版本不能超过20个字符") @ApiModelProperty("接口版本") private String version; - @ApiModelProperty("接口状态(0=停用,1=启用)") - private String status; - @ApiModelProperty("接口文档(文档地址)") private String document; diff --git a/sf-service/src/main/java/com/sf/service/gateway/domain/dto/UpdateGatewayInterfaceInfoDTO.java b/sf-service/src/main/java/com/sf/service/gateway/domain/dto/UpdateGatewayInterfaceInfoDTO.java index 2011004..a5f6738 100644 --- a/sf-service/src/main/java/com/sf/service/gateway/domain/dto/UpdateGatewayInterfaceInfoDTO.java +++ b/sf-service/src/main/java/com/sf/service/gateway/domain/dto/UpdateGatewayInterfaceInfoDTO.java @@ -4,6 +4,7 @@ import io.swagger.annotations.ApiModel; import io.swagger.annotations.ApiModelProperty; import lombok.Data; import lombok.EqualsAndHashCode; +import org.hibernate.validator.constraints.Length; import javax.validation.constraints.NotBlank; import javax.validation.constraints.NotNull; @@ -26,10 +27,12 @@ public class UpdateGatewayInterfaceInfoDTO { @ApiModelProperty("ID") private Long id; + @Length(max = 50,message = "接口名称不能超过50个字符") @NotBlank(message = "接口名称不能为空") @ApiModelProperty("接口名称") private String interfaceName; + @Length(max = 200,message = "接口名称不能超过200个字符") @NotBlank(message = "接口路径不能为空") @ApiModelProperty("接口路径") private String interfacePath; @@ -41,12 +44,11 @@ public class UpdateGatewayInterfaceInfoDTO { @ApiModelProperty("接口描述") private String description; + @Length(max = 20,message = "接口版本不能超过20个字符") + @NotBlank(message = "接口版本不能为空") @ApiModelProperty("接口版本") private String version; - @ApiModelProperty("接口状态(0=停用,1=启用)") - private String status; - @ApiModelProperty("接口文档(文档地址)") private String document; diff --git a/sf-service/src/main/java/com/sf/service/gateway/mapper/GatewayInterfaceInfoMapper.java b/sf-service/src/main/java/com/sf/service/gateway/mapper/GatewayInterfaceInfoMapper.java index d7b2240..594c7a7 100644 --- a/sf-service/src/main/java/com/sf/service/gateway/mapper/GatewayInterfaceInfoMapper.java +++ b/sf-service/src/main/java/com/sf/service/gateway/mapper/GatewayInterfaceInfoMapper.java @@ -65,4 +65,6 @@ public interface GatewayInterfaceInfoMapper int updateGatewayInterfaceInfoStatusByIds(@Param("ids") Collection ids, @Param("status") String status); List selectGatewayInterfaceInfoByServerIds(@Param("serverIds") Collection serverIds); + + List selectGatewayInterfaceInfoByIds(@Param("ids") Collection ids); } diff --git a/sf-service/src/main/java/com/sf/service/gateway/service/IGatewayInterfaceInfoService.java b/sf-service/src/main/java/com/sf/service/gateway/service/IGatewayInterfaceInfoService.java index 84bb34e..4560e32 100644 --- a/sf-service/src/main/java/com/sf/service/gateway/service/IGatewayInterfaceInfoService.java +++ b/sf-service/src/main/java/com/sf/service/gateway/service/IGatewayInterfaceInfoService.java @@ -8,7 +8,6 @@ import com.sf.service.gateway.enums.GatewayDataStatus; import java.util.Collection; import java.util.List; -import java.util.Set; /** * 接口管理Service接口 @@ -70,6 +69,8 @@ public interface IGatewayInterfaceInfoService List selectGatewayInterfaceInfoByServerIds(Collection serverIds); + List selectGatewayInterfaceInfoByIds(Collection ids); + /** * 添加接口信息及绑定的策略信息 * diff --git a/sf-service/src/main/java/com/sf/service/gateway/service/impl/GatewayInterfaceInfoServiceImpl.java b/sf-service/src/main/java/com/sf/service/gateway/service/impl/GatewayInterfaceInfoServiceImpl.java index 0f0123d..fe1fc01 100644 --- a/sf-service/src/main/java/com/sf/service/gateway/service/impl/GatewayInterfaceInfoServiceImpl.java +++ b/sf-service/src/main/java/com/sf/service/gateway/service/impl/GatewayInterfaceInfoServiceImpl.java @@ -25,6 +25,8 @@ import org.springframework.transaction.annotation.Transactional; import org.springframework.util.Assert; import org.springframework.util.CollectionUtils; +import javax.annotation.Resource; + /** * 接口管理Service业务层处理 * @@ -35,13 +37,13 @@ import org.springframework.util.CollectionUtils; @Service public class GatewayInterfaceInfoServiceImpl implements IGatewayInterfaceInfoService { - @Autowired + @Resource private GatewayInterfaceInfoMapper gatewayInterfaceInfoMapper; - @Autowired + @Resource private GatewayInterfaceLinkStrategyMapper gatewayInterfaceLinkStrategyMapper; - @Autowired + @Resource private IGatewayStrategyService gatewayStrategyService; /** @@ -74,24 +76,37 @@ public class GatewayInterfaceInfoServiceImpl implements IGatewayInterfaceInfoSer */ @Override public Long insertGatewayInterfaceInfo(GatewayInterfaceInfo gatewayInterfaceInfo) { - gatewayInterfaceInfo.setAppCode(AppUtils.getAppCodeFromRequestHeader()); + String appCode = AppUtils.getAppCodeFromRequestHeader(); + gatewayInterfaceInfo.setAppCode(appCode); String username = SecurityUtils.getUsername(); gatewayInterfaceInfo.setCreated(username); gatewayInterfaceInfo.setModified(username); Date nowDate = DateUtils.getNowDate(); gatewayInterfaceInfo.setCreateTime(nowDate); gatewayInterfaceInfo.setUpdateTime(nowDate); - Assert.hasText(gatewayInterfaceInfo.getInterfacePath(),"接口路径不能为空"); + Assert.hasText(gatewayInterfaceInfo.getInterfacePath(), "接口路径不能为空"); gatewayInterfaceInfo.setUriRegular(RegexUtils.isRegex(gatewayInterfaceInfo.getInterfacePath())); try { gatewayInterfaceInfoMapper.insertGatewayInterfaceInfo(gatewayInterfaceInfo); return gatewayInterfaceInfo.getId(); - }catch (DuplicateKeyException e){ - throw new IllegalArgumentException("接口名称不可重复!"); + } catch (DuplicateKeyException e) { + throw new IllegalArgumentException("同名接口,版本不可重复!"); } } + private void checkGatewayInterfaceInfoCanEnable(GatewayInterfaceInfo current) { + GatewayInterfaceInfo query = new GatewayInterfaceInfo(); + query.setStatus(GatewayDataStatus.ENABLE.getCode()); + query.setAppCode(current.getAppCode()); + query.setInterfacePath(current.getInterfacePath()); + List infoList = gatewayInterfaceInfoMapper.selectGatewayInterfaceInfoList(query); + if (CollUtil.isNotEmpty(infoList)) { + Assert.isTrue(infoList.size() == 1,"该请求路径已存在启用的记录"); + Assert.isTrue(infoList.get(0).getId().equals(current.getId()),"该请求路径已存在启用的记录"); + } + } + /** * 修改接口管理 * @@ -103,12 +118,12 @@ public class GatewayInterfaceInfoServiceImpl implements IGatewayInterfaceInfoSer gatewayInterfaceInfo.setModified(SecurityUtils.getUsername()); gatewayInterfaceInfo.setUpdateTime(DateUtils.getNowDate()); String interfacePath = gatewayInterfaceInfo.getInterfacePath(); - Assert.hasText(interfacePath,"接口路径不能为空"); + Assert.hasText(interfacePath, "接口路径不能为空"); gatewayInterfaceInfo.setUriRegular(RegexUtils.isRegex(interfacePath)); try { return gatewayInterfaceInfoMapper.updateGatewayInterfaceInfo(gatewayInterfaceInfo); - }catch (DuplicateKeyException e){ - throw new IllegalArgumentException("接口名称不可重复!"); + } catch (DuplicateKeyException e) { + throw new IllegalArgumentException("同名接口,版本不可重复!"); } } @@ -146,21 +161,33 @@ public class GatewayInterfaceInfoServiceImpl implements IGatewayInterfaceInfoSer @Override public int updateGatewayInterfaceInfoStatusByIds(Collection ids, GatewayDataStatus status) { - if (CollectionUtils.isEmpty(ids) || status == null){ + if (CollectionUtils.isEmpty(ids) || status == null) { log.warn("Insufficient update conditions"); return 0; } - return gatewayInterfaceInfoMapper.updateGatewayInterfaceInfoStatusByIds(ids,status.getCode()); + if (GatewayDataStatus.ENABLE.equals(status)) { + List infoList = selectGatewayInterfaceInfoByIds(ids); + infoList.forEach(this::checkGatewayInterfaceInfoCanEnable); + } + return gatewayInterfaceInfoMapper.updateGatewayInterfaceInfoStatusByIds(ids, status.getCode()); } @Override public List selectGatewayInterfaceInfoByServerIds(Collection serverIds) { - if (CollectionUtils.isEmpty(serverIds)){ + if (CollectionUtils.isEmpty(serverIds)) { return Collections.emptyList(); } return gatewayInterfaceInfoMapper.selectGatewayInterfaceInfoByServerIds(serverIds); } + @Override + public List selectGatewayInterfaceInfoByIds(Collection ids) { + if (CollectionUtils.isEmpty(ids)) { + return Collections.emptyList(); + } + return gatewayInterfaceInfoMapper.selectGatewayInterfaceInfoByIds(ids); + } + @Transactional(rollbackFor = Exception.class) @Override public Long insertGatewayInterfaceInfoAndBindStrategy(InsertGatewayInterfaceInfoDTO dto) { @@ -170,11 +197,11 @@ public class GatewayInterfaceInfoServiceImpl implements IGatewayInterfaceInfoSer gatewayInterfaceInfo.setRequestMethod(dto.getRequestMethod()); gatewayInterfaceInfo.setDescription(dto.getDescription()); gatewayInterfaceInfo.setVersion(dto.getVersion()); - gatewayInterfaceInfo.setStatus(dto.getStatus()); + gatewayInterfaceInfo.setStatus(GatewayDataStatus.DISABLE.getCode()); gatewayInterfaceInfo.setDocument(dto.getDocument()); gatewayInterfaceInfo.setServerId(dto.getServerId()); Long interfaceInfoId = insertGatewayInterfaceInfo(gatewayInterfaceInfo); - if (CollUtil.isNotEmpty(dto.getStrategyIds())){ + if (CollUtil.isNotEmpty(dto.getStrategyIds())) { checkGatewayStrategyTypeCannotBeRepeated(dto.getStrategyIds()); List linkStrategyList = dto.getStrategyIds() .stream().map(strategyId -> new GatewayInterfaceLinkStrategy(interfaceInfoId, strategyId)) @@ -194,13 +221,12 @@ public class GatewayInterfaceInfoServiceImpl implements IGatewayInterfaceInfoSer gatewayInterfaceInfo.setRequestMethod(dto.getRequestMethod()); gatewayInterfaceInfo.setDescription(dto.getDescription()); gatewayInterfaceInfo.setVersion(dto.getVersion()); - gatewayInterfaceInfo.setStatus(dto.getStatus()); gatewayInterfaceInfo.setDocument(dto.getDocument()); gatewayInterfaceInfo.setServerId(dto.getServerId()); updateGatewayInterfaceInfo(gatewayInterfaceInfo); // 先删除 gatewayInterfaceLinkStrategyMapper.deleteByInterfaceIds(Collections.singleton(dto.getId())); - if (CollUtil.isNotEmpty(dto.getStrategyIds())){ + if (CollUtil.isNotEmpty(dto.getStrategyIds())) { checkGatewayStrategyTypeCannotBeRepeated(dto.getStrategyIds()); List linkStrategyList = dto.getStrategyIds() .stream().map(strategyId -> new GatewayInterfaceLinkStrategy(dto.getId(), strategyId)) @@ -212,7 +238,7 @@ public class GatewayInterfaceInfoServiceImpl implements IGatewayInterfaceInfoSer @Override public List selectGatewayInterfaceLinkStrategyByInterfaceIds(Collection interfaceIds) { - if (CollectionUtils.isEmpty(interfaceIds)){ + if (CollectionUtils.isEmpty(interfaceIds)) { return Collections.emptyList(); } return gatewayInterfaceLinkStrategyMapper.selectGatewayInterfaceLinkStrategyByInterfaceIds(interfaceIds); @@ -221,7 +247,7 @@ public class GatewayInterfaceInfoServiceImpl implements IGatewayInterfaceInfoSer private void checkGatewayStrategyTypeCannotBeRepeated(List strategyIds) { List strategyList = gatewayStrategyService.selectGatewayStrategyByIds(strategyIds); Map> groupByStrategyTypeMap = strategyList.stream().collect(Collectors.groupingBy(GatewayStrategy::getStrategyType)); - groupByStrategyTypeMap.forEach((type,strategies) ->{ + groupByStrategyTypeMap.forEach((type, strategies) -> { Assert.isTrue(strategies.size() == 1, Objects.requireNonNull(GatewayStrategyType.getByCode(type)).getInfo() + "类型只能选择一个!"); }); } diff --git a/sf-service/src/main/java/com/sf/service/gateway/service/impl/GatewayStrategyServiceImpl.java b/sf-service/src/main/java/com/sf/service/gateway/service/impl/GatewayStrategyServiceImpl.java index 6267c63..b1ebadb 100644 --- a/sf-service/src/main/java/com/sf/service/gateway/service/impl/GatewayStrategyServiceImpl.java +++ b/sf-service/src/main/java/com/sf/service/gateway/service/impl/GatewayStrategyServiceImpl.java @@ -24,6 +24,8 @@ import org.springframework.util.Assert; import org.springframework.util.CollectionUtils; import org.springframework.util.StringUtils; +import javax.annotation.Resource; + /** * 策略管理Service业务层处理 * @@ -34,10 +36,10 @@ import org.springframework.util.StringUtils; @Service public class GatewayStrategyServiceImpl implements IGatewayStrategyService { - @Autowired + @Resource private GatewayStrategyMapper gatewayStrategyMapper; - @Autowired + @Resource private GatewayInterfaceLinkStrategyMapper gatewayInterfaceLinkStrategyMapper; /** @@ -76,6 +78,17 @@ public class GatewayStrategyServiceImpl implements IGatewayStrategyService Date nowDate = DateUtils.getNowDate(); gatewayStrategy.setCreateTime(nowDate); gatewayStrategy.setUpdateTime(nowDate); + checkStrategyConfig(gatewayStrategy); + + try { + return gatewayStrategyMapper.insertGatewayStrategy(gatewayStrategy); + }catch (DuplicateKeyException e){ + throw new IllegalArgumentException("策略名称不可重复!"); + } + + } + + private void checkStrategyConfig(GatewayStrategy gatewayStrategy) { GatewayStrategyType strategyType = GatewayStrategyType.getByCode(gatewayStrategy.getStrategyType()); Assert.notNull(strategyType,"不支持的策略类型!"); switch (strategyType){ @@ -94,12 +107,6 @@ public class GatewayStrategyServiceImpl implements IGatewayStrategyService default: break; } - try { - return gatewayStrategyMapper.insertGatewayStrategy(gatewayStrategy); - }catch (DuplicateKeyException e){ - throw new IllegalArgumentException("策略名称不可重复!"); - } - } /** @@ -166,6 +173,11 @@ public class GatewayStrategyServiceImpl implements IGatewayStrategyService @Override public int updateGatewayStrategy(GatewayStrategy gatewayStrategy) { + // 策略类型不支持调整 + GatewayStrategy inDbStrategy = selectGatewayStrategyById(gatewayStrategy.getId()); + Assert.notNull(inDbStrategy,"数据不存在"); + gatewayStrategy.setStrategyType(inDbStrategy.getStrategyType()); + checkStrategyConfig(gatewayStrategy); gatewayStrategy.setUpdateTime(DateUtils.getNowDate()); try { return gatewayStrategyMapper.updateGatewayStrategy(gatewayStrategy); diff --git a/sf-service/src/main/resources/mapper/gateway/GatewayInterfaceInfoMapper.xml b/sf-service/src/main/resources/mapper/gateway/GatewayInterfaceInfoMapper.xml index f019ff5..311dfcc 100644 --- a/sf-service/src/main/resources/mapper/gateway/GatewayInterfaceInfoMapper.xml +++ b/sf-service/src/main/resources/mapper/gateway/GatewayInterfaceInfoMapper.xml @@ -35,6 +35,7 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" and interface_name like concat('%', #{interfaceName}, '%') and status = #{status} and app_code = #{appCode} + and interface_path = #{interfacePath} and create_time between #{params.beginCreateTime} and #{params.endCreateTime} @@ -50,6 +51,13 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" #{serverId} + insert into Gateway_interface_info