diff --git a/epmet-commons/epmet-commons-dynamic-datasource/src/main/java/com/epmet/commons/dynamic/datasource/annotation/DataSource.java b/epmet-commons/epmet-commons-dynamic-datasource/src/main/java/com/epmet/commons/dynamic/datasource/annotation/DataSource.java index 3a8ac1ae8f..e0675092c8 100644 --- a/epmet-commons/epmet-commons-dynamic-datasource/src/main/java/com/epmet/commons/dynamic/datasource/annotation/DataSource.java +++ b/epmet-commons/epmet-commons-dynamic-datasource/src/main/java/com/epmet/commons/dynamic/datasource/annotation/DataSource.java @@ -21,5 +21,15 @@ import java.lang.annotation.*; @Documented @Inherited public @interface DataSource { + /** + * 直接指定数据源名称 + * @return + */ String value() default ""; + + /** + * 是否从参数中获取数据源名称,优先级高于value + * @return + */ + boolean datasourceNameFromArg() default false; } diff --git a/epmet-commons/epmet-commons-dynamic-datasource/src/main/java/com/epmet/commons/dynamic/datasource/aspect/DataSourceAspect.java b/epmet-commons/epmet-commons-dynamic-datasource/src/main/java/com/epmet/commons/dynamic/datasource/aspect/DataSourceAspect.java index d636568425..45113847c6 100644 --- a/epmet-commons/epmet-commons-dynamic-datasource/src/main/java/com/epmet/commons/dynamic/datasource/aspect/DataSourceAspect.java +++ b/epmet-commons/epmet-commons-dynamic-datasource/src/main/java/com/epmet/commons/dynamic/datasource/aspect/DataSourceAspect.java @@ -9,7 +9,9 @@ package com.epmet.commons.dynamic.datasource.aspect; import com.epmet.commons.dynamic.datasource.annotation.DataSource; +import com.epmet.commons.dynamic.datasource.bean.DataSourceParam; import com.epmet.commons.dynamic.datasource.config.DynamicContextHolder; +import org.apache.commons.lang3.StringUtils; import org.aspectj.lang.ProceedingJoinPoint; import org.aspectj.lang.annotation.Around; import org.aspectj.lang.annotation.Aspect; @@ -22,6 +24,7 @@ import org.springframework.core.annotation.Order; import org.springframework.stereotype.Component; import java.lang.reflect.Method; +import java.lang.reflect.Parameter; /** * 多数据源,切面处理类 @@ -52,9 +55,9 @@ public class DataSourceAspect { if(targetDataSource != null || methodDataSource != null){ String value; if(methodDataSource != null){ - value = methodDataSource.value(); + value = getDatasourceName(methodDataSource, signature.getMethod().getParameters(), point.getArgs()); }else { - value = targetDataSource.value(); + value = getDatasourceName(targetDataSource, signature.getMethod().getParameters(), point.getArgs()); } DynamicContextHolder.push(value); @@ -68,4 +71,39 @@ public class DataSourceAspect { logger.debug("clean datasource"); } } + + /** + * 获取要用到的数据源名称 + * @param dataSource + * @return + */ + public String getDatasourceName(DataSource dataSource, Parameter[] methodParameters, Object[] methodArgValues) { + if (dataSource.datasourceNameFromArg()) { + // 1.从参数中动态获取数据源名称 + String datasourceNameFromParam = getDatasourceNameFromArg(methodParameters, methodArgValues); + if (StringUtils.isNotBlank(datasourceNameFromParam)) { + // 如果有DatasourceParam类型的参数并且设置了datasourceName值,那么返回这个值,否则使用硬编码的 + return datasourceNameFromParam; + } + } + // 2.硬编码指定数据源名称 + return dataSource.value(); + } + + /** + * 从参数中取数据源名称 + * @param parameters + * @param argsObject + * @return + */ + public String getDatasourceNameFromArg(Parameter[] parameters, Object[] argsObject) { + for (int i = 0; i < parameters.length; i++) { + if (parameters[i].getType() == DataSourceParam.class) { + DataSourceParam param = (DataSourceParam) argsObject[i]; + return param.getDatasourceName(); + } + } + + return null; + } } diff --git a/epmet-commons/epmet-commons-dynamic-datasource/src/main/java/com/epmet/commons/dynamic/datasource/bean/DataSourceParam.java b/epmet-commons/epmet-commons-dynamic-datasource/src/main/java/com/epmet/commons/dynamic/datasource/bean/DataSourceParam.java new file mode 100644 index 0000000000..19b5905588 --- /dev/null +++ b/epmet-commons/epmet-commons-dynamic-datasource/src/main/java/com/epmet/commons/dynamic/datasource/bean/DataSourceParam.java @@ -0,0 +1,12 @@ +package com.epmet.commons.dynamic.datasource.bean; + +import lombok.AllArgsConstructor; +import lombok.Data; + +@Data +@AllArgsConstructor +public class DataSourceParam { + + private String datasourceName; + +} diff --git a/epmet-module/epmet-common-service/common-service-client/src/main/java/com/epmet/dto/result/ExternalCustomerResultDTO.java b/epmet-module/epmet-common-service/common-service-client/src/main/java/com/epmet/dto/result/ExternalCustomerResultDTO.java index 79e58686fe..177b49575b 100644 --- a/epmet-module/epmet-common-service/common-service-client/src/main/java/com/epmet/dto/result/ExternalCustomerResultDTO.java +++ b/epmet-module/epmet-common-service/common-service-client/src/main/java/com/epmet/dto/result/ExternalCustomerResultDTO.java @@ -36,7 +36,7 @@ public class ExternalCustomerResultDTO implements Serializable { /** * 客户ID */ - private String id; + private String customerId; /** * 客户名称 diff --git a/epmet-module/epmet-common-service/common-service-server/src/main/java/com/epmet/controller/ExternalCustomerController.java b/epmet-module/epmet-common-service/common-service-server/src/main/java/com/epmet/controller/ExternalCustomerController.java index 2817a2fae5..becc72c341 100644 --- a/epmet-module/epmet-common-service/common-service-server/src/main/java/com/epmet/controller/ExternalCustomerController.java +++ b/epmet-module/epmet-common-service/common-service-server/src/main/java/com/epmet/controller/ExternalCustomerController.java @@ -5,7 +5,6 @@ import com.epmet.commons.tools.utils.Result; import com.epmet.commons.tools.validator.ValidatorUtils; import com.epmet.dto.form.ExternalCustomerFormDTO; import com.epmet.dto.result.ExternalCustomerResultDTO; -import com.epmet.entity.ExternalCustomerEntity; import com.epmet.service.ExternalCustomerService; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -15,8 +14,6 @@ import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; -import javax.validation.constraints.NotBlank; - /** * 外部客户管理 */ @@ -48,11 +45,11 @@ public class ExternalCustomerController { * @return */ @PostMapping("add") - public Result add(@RequestBody ExternalCustomerFormDTO formDTO) { + public Result add(@RequestBody ExternalCustomerFormDTO formDTO) { ValidatorUtils.validateEntity(formDTO, ExternalCustomerFormDTO.AddExternalCustomerGroup.class); String customerName = formDTO.getCustomerName(); - ExternalCustomerEntity result = externalCustomerService.add(customerName); - return new Result().ok(result); + ExternalCustomerResultDTO result = externalCustomerService.add(customerName); + return new Result().ok(result); } /** @@ -61,12 +58,12 @@ public class ExternalCustomerController { * @return */ @PostMapping("update") - public Result update(@RequestBody ExternalCustomerFormDTO formDTO) { + public Result update(@RequestBody ExternalCustomerFormDTO formDTO) { ValidatorUtils.validateEntity(formDTO, ExternalCustomerFormDTO.UpdateExternalCustomerGroup.class); String customerId = formDTO.getCustomerId(); String customerName = formDTO.getCustomerName(); - ExternalCustomerEntity result = externalCustomerService.update(customerId, customerName); - return new Result().ok(result); + ExternalCustomerResultDTO result = externalCustomerService.update(customerId, customerName); + return new Result().ok(result); } } diff --git a/epmet-module/epmet-common-service/common-service-server/src/main/java/com/epmet/service/ExternalCustomerService.java b/epmet-module/epmet-common-service/common-service-server/src/main/java/com/epmet/service/ExternalCustomerService.java index 6af116be98..25645bea0b 100644 --- a/epmet-module/epmet-common-service/common-service-server/src/main/java/com/epmet/service/ExternalCustomerService.java +++ b/epmet-module/epmet-common-service/common-service-server/src/main/java/com/epmet/service/ExternalCustomerService.java @@ -8,7 +8,7 @@ public interface ExternalCustomerService { PageData listPage(Integer pageNo, Integer pageSize); - ExternalCustomerEntity add(String customerName); + ExternalCustomerResultDTO add(String customerName); - ExternalCustomerEntity update(String customerId, String customerName); + ExternalCustomerResultDTO update(String customerId, String customerName); } diff --git a/epmet-module/epmet-common-service/common-service-server/src/main/java/com/epmet/service/impl/ExternalCustomerServiceImpl.java b/epmet-module/epmet-common-service/common-service-server/src/main/java/com/epmet/service/impl/ExternalCustomerServiceImpl.java index 8cfe32cf96..728e588e80 100644 --- a/epmet-module/epmet-common-service/common-service-server/src/main/java/com/epmet/service/impl/ExternalCustomerServiceImpl.java +++ b/epmet-module/epmet-common-service/common-service-server/src/main/java/com/epmet/service/impl/ExternalCustomerServiceImpl.java @@ -31,7 +31,7 @@ public class ExternalCustomerServiceImpl implements ExternalCustomerService { } @Override - public ExternalCustomerEntity add(String customerName) { + public ExternalCustomerResultDTO add(String customerName) { Integer exitsCustomerCount = externalCustomerDao.countByCustomerName(customerName); if (exitsCustomerCount > 0) { throw new RenException(EpmetErrorCode.OPER_CUSTOMER_EXISTS.getCode(), "客户已存在"); @@ -39,11 +39,15 @@ public class ExternalCustomerServiceImpl implements ExternalCustomerService { ExternalCustomerEntity entity = new ExternalCustomerEntity(); entity.setCustomerName(customerName); externalCustomerDao.insert(entity); - return entity; + + ExternalCustomerResultDTO resultDTO = new ExternalCustomerResultDTO(); + resultDTO.setCustomerId(entity.getId()); + resultDTO.setCustomerName(customerName); + return resultDTO; } @Override - public ExternalCustomerEntity update(String customerId, String customerName) { + public ExternalCustomerResultDTO update(String customerId, String customerName) { ExternalCustomerEntity existsCustomer = externalCustomerDao.selectById(customerId); if (existsCustomer == null) { throw new RenException(EpmetErrorCode.OPER_EXTERNAL_CUSTOMER_NOT_EXISTS.getCode(), @@ -51,6 +55,10 @@ public class ExternalCustomerServiceImpl implements ExternalCustomerService { } existsCustomer.setCustomerName(customerName); externalCustomerDao.updateById(existsCustomer); - return existsCustomer; + + ExternalCustomerResultDTO resultDTO = new ExternalCustomerResultDTO(); + resultDTO.setCustomerId(customerId); + resultDTO.setCustomerName(customerName); + return resultDTO; } } diff --git a/epmet-module/epmet-common-service/common-service-server/src/main/resources/mapper/ExternalCustomerDao.xml b/epmet-module/epmet-common-service/common-service-server/src/main/resources/mapper/ExternalCustomerDao.xml index 0e4d03d0cc..3950f8d014 100644 --- a/epmet-module/epmet-common-service/common-service-server/src/main/resources/mapper/ExternalCustomerDao.xml +++ b/epmet-module/epmet-common-service/common-service-server/src/main/resources/mapper/ExternalCustomerDao.xml @@ -17,7 +17,7 @@