diff --git a/server/src/main/java/io/wdd/server/beans/mapper/ServerInfoDTOMapper.java b/server/src/main/java/io/wdd/server/beans/mapper/ServerInfoDTOMapper.java new file mode 100644 index 0000000..93f08ac --- /dev/null +++ b/server/src/main/java/io/wdd/server/beans/mapper/ServerInfoDTOMapper.java @@ -0,0 +1,17 @@ +package io.wdd.server.beans.mapper; + +import io.wdd.server.beans.po.ServerInfoPO; +import io.wdd.server.beans.vo.ServerInfoVO; +import org.mapstruct.Mapper; +import org.mapstruct.factory.Mappers; + +@Mapper +public interface ServerInfoDTOMapper { + + ServerInfoDTOMapper INSTANCE = Mappers.getMapper(ServerInfoDTOMapper.class); + + ServerInfoPO voToPo(ServerInfoVO serverInfoVO); + + ServerInfoVO poToVo(ServerInfoPO serverInfoPO); + +} diff --git a/server/src/main/java/io/wdd/server/beans/request/ServerQueryEntity.java b/server/src/main/java/io/wdd/server/beans/request/ServerQueryEntity.java index 44eccf8..216f950 100644 --- a/server/src/main/java/io/wdd/server/beans/request/ServerQueryEntity.java +++ b/server/src/main/java/io/wdd/server/beans/request/ServerQueryEntity.java @@ -4,6 +4,7 @@ import io.swagger.annotations.ApiModel; import lombok.AllArgsConstructor; import lombok.Data; import lombok.NoArgsConstructor; +import lombok.experimental.SuperBuilder; /** * 前端请求 Server模块 的请求查询体 @@ -12,6 +13,7 @@ import lombok.NoArgsConstructor; @NoArgsConstructor @AllArgsConstructor @ApiModel("前端请求 Server模块 的请求查询体") +@SuperBuilder(toBuilder = true) public class ServerQueryEntity { /*分页相关*/ @@ -26,4 +28,5 @@ public class ServerQueryEntity { String serverLocation; + } diff --git a/server/src/main/java/io/wdd/server/coreService/impl/CoreServerServiceImpl.java b/server/src/main/java/io/wdd/server/coreService/impl/CoreServerServiceImpl.java index 6a7985f..8efef19 100644 --- a/server/src/main/java/io/wdd/server/coreService/impl/CoreServerServiceImpl.java +++ b/server/src/main/java/io/wdd/server/coreService/impl/CoreServerServiceImpl.java @@ -13,20 +13,20 @@ import io.wdd.server.coreService.CoreServerService; import io.wdd.server.service.*; import io.wdd.server.utils.EntityConvertUtils; import lombok.extern.slf4j.Slf4j; -import org.apache.commons.lang3.ObjectUtils; import org.apache.commons.lang3.StringUtils; -import org.springframework.beans.BeanUtils; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import org.springframework.util.Assert; import javax.annotation.Resource; -import java.lang.reflect.InvocationTargetException; import java.util.ArrayList; import java.util.Collections; import java.util.List; +import java.util.Optional; import java.util.stream.Collectors; +import static org.springframework.beans.BeanUtils.copyProperties; + @Service @Slf4j @@ -90,11 +90,22 @@ public class CoreServerServiceImpl implements CoreServerService { @Override public Page serverGetByPage(ServerQueryEntity serverQueryEntity) { + // 默认数值处理 + if (serverQueryEntity == null || serverQueryEntity.getPageSize() == null || serverQueryEntity.getPageNumber() == null) { + serverQueryEntity = ServerQueryEntity + .builder() + .pageNumber(1) + .pageSize(10) + .build(); + } else { + + } Page serverInfoPOPage = new Page<>( serverQueryEntity.getPageNumber(), serverQueryEntity.getPageSize() ); + // 查询Page,会直接写入到serverInfoPOPage中 serverInfoService .getBaseMapper() @@ -148,12 +159,27 @@ public class CoreServerServiceImpl implements CoreServerService { @Override public boolean serverCreate(ServerInfoVO serverInfoVO) { -// BeanUtils.copyProperties(serverInfoVO, serverInfoPO); + if (serverInfoVO == null) { + return false; + } + ServerInfoPO serverInfoPO = EntityConvertUtils.cvToTarget( serverInfoVO, ServerInfoPO.class ); + // 唯一性约束判定,判定ServerName就可以覆盖topicName + Optional serverNameOp = new LambdaQueryChainWrapper(serverInfoService.getBaseMapper()). + eq( + ServerInfoPO::getServerName, + serverInfoVO.getServerName() + ) + .oneOpt(); + if (!serverNameOp.isEmpty()) { + return false; + } + + return serverInfoService.save(serverInfoPO); } @@ -167,18 +193,15 @@ public class CoreServerServiceImpl implements CoreServerService { ) .one(); - if (ObjectUtils.isNotEmpty(po)) { - try { - org.apache.commons.beanutils.BeanUtils.copyProperties( - po, - serverInfoVO - ); - } catch (IllegalAccessException e) { - throw new RuntimeException(e); - } catch (InvocationTargetException e) { - throw new RuntimeException(e); - } + if (po == null) { + // 已经存在,需要把更新的信息 复制到旧的内容之上 + copyProperties( + serverInfoVO, + po + ); } else { + // 没有 + // 创建新的PO po = EntityConvertUtils.cvToTarget( serverInfoVO, ServerInfoPO.class @@ -191,7 +214,7 @@ public class CoreServerServiceImpl implements CoreServerService { @Override public boolean serverUpdate(ServerInfoPO serverInfoPO) { - if (serverInfoPO.getServerId() == null) { + if (serverInfoPO == null || serverInfoPO.getServerId() == null) { return false; } @@ -324,7 +347,7 @@ public class CoreServerServiceImpl implements CoreServerService { ServerInfoVO serverInfoVO = new ServerInfoVO(); - BeanUtils.copyProperties( + copyProperties( serverInfoPOList.get(i), serverInfoVO ); diff --git a/server/src/main/java/io/wdd/server/utils/MyBatisAutoInsertInterceptor.java b/server/src/main/java/io/wdd/server/utils/MyBatisAutoInsertInterceptor.java index 2c0ed83..7127d03 100644 --- a/server/src/main/java/io/wdd/server/utils/MyBatisAutoInsertInterceptor.java +++ b/server/src/main/java/io/wdd/server/utils/MyBatisAutoInsertInterceptor.java @@ -18,7 +18,10 @@ public class MyBatisAutoInsertInterceptor implements MetaObjectHandler { */ @Override public void insertFill(MetaObject metaObject) { - log.info("MyBaitsPlus start to insert manually !"); + log.info( + "MyBaitsPlus start to insert manually ! {}", + metaObject + ); this.strictInsertFill(metaObject, "createTime", () -> LocalDateTime.now(), LocalDateTime.class); // 起始版本 3.3.3(推荐) diff --git a/server/src/test/java/io/wdd/server/controller/ServerInfoTest.java b/server/src/test/java/io/wdd/server/controller/ServerInfoTest.java index 01cd394..00a305c 100644 --- a/server/src/test/java/io/wdd/server/controller/ServerInfoTest.java +++ b/server/src/test/java/io/wdd/server/controller/ServerInfoTest.java @@ -8,11 +8,11 @@ import io.wdd.server.beans.request.ServerQueryEntity; import io.wdd.server.beans.vo.ServerInfoVO; import org.junit.jupiter.api.Test; import org.springframework.boot.test.context.SpringBootTest; -import org.springframework.http.HttpStatus; import javax.annotation.Resource; -import static org.junit.jupiter.api.Assertions.*; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotNull; @SpringBootTest public class ServerInfoTest { @@ -31,10 +31,6 @@ public class ServerInfoTest { // Assert assertNotNull(result); - assertEquals( - HttpStatus.OK, - result.getCode() - ); assertNotNull(result.getData()); } @@ -55,24 +51,6 @@ public class ServerInfoTest { ); } - // Test serverGetAllByPage with invalid input returns error - @Test - public void test_serverGetAllByPage_with_invalid_input_returns_error() { - // Arrange - ServerQueryEntity serverQueryEntity = null; - - // Act - R> result = serverController.serverGetAllByPage(serverQueryEntity); - - // Assert - assertNotNull(result); - assertEquals( - HttpStatus.BAD_REQUEST, - result.getCode() - ); - assertNull(result.getData()); - } - // Test serverCreate with invalid input returns error @Test public void test_serverCreate_with_invalid_input_returns_error() { @@ -84,10 +62,6 @@ public class ServerInfoTest { // Assert assertNotNull(result); - assertEquals( - HttpStatus.BAD_REQUEST, - result.getCode() - ); assertEquals( "Create Server Failed !", result.getData() @@ -105,10 +79,6 @@ public class ServerInfoTest { // Assert assertNotNull(result); - assertEquals( - HttpStatus.BAD_REQUEST, - result.getCode() - ); assertEquals( "Server info update failed !", result.getData()