From 56ce70a82ce84f3cbed28c176cf0d836ced35cba Mon Sep 17 00:00:00 2001 From: CaptainB Date: Thu, 1 Jun 2023 10:36:15 +0800 Subject: [PATCH] =?UTF-8?q?perf:=20=E4=BC=98=E5=8C=96=E6=89=B9=E9=87=8F?= =?UTF-8?q?=E4=BF=9D=E5=AD=98=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../system/controller/UserController.java | 9 +++- .../system/service/UserService.java | 25 +++++++---- .../controller/UserControllerTests.java | 42 ++++--------------- 3 files changed, 31 insertions(+), 45 deletions(-) diff --git a/backend/services/system-setting/src/main/java/io/metersphere/system/controller/UserController.java b/backend/services/system-setting/src/main/java/io/metersphere/system/controller/UserController.java index ce85669744..8821e0f27b 100644 --- a/backend/services/system-setting/src/main/java/io/metersphere/system/controller/UserController.java +++ b/backend/services/system-setting/src/main/java/io/metersphere/system/controller/UserController.java @@ -29,15 +29,20 @@ public class UserController { } @PostMapping("/add") - public boolean addUser(@Validated({Created.class}) @RequestBody UserDTO user) { + public UserDTO addUser(@Validated({Created.class}) @RequestBody UserDTO user) { return userService.add(user); } @PostMapping("/update") - public boolean updateUser(@Validated({Updated.class}) @RequestBody UserDTO user) { + public UserDTO updateUser(@Validated({Updated.class}) @RequestBody UserDTO user) { return userService.update(user); } + @GetMapping("/delete/{userId}") + public UserDTO deleteUser(@PathVariable String userId) { + return userService.delete(userId); + } + @PostMapping("/batch-add2") public boolean batchSaveUser2(@Validated({Created.class}) @RequestBody List user) { return userService.batchSave2(user); diff --git a/backend/services/system-setting/src/main/java/io/metersphere/system/service/UserService.java b/backend/services/system-setting/src/main/java/io/metersphere/system/service/UserService.java index 13bc5a0f29..be1692a425 100644 --- a/backend/services/system-setting/src/main/java/io/metersphere/system/service/UserService.java +++ b/backend/services/system-setting/src/main/java/io/metersphere/system/service/UserService.java @@ -29,7 +29,7 @@ public class UserService { @Resource private SqlSessionFactory sqlSessionFactory; - public boolean add(UserDTO entity) { + public UserDTO add(UserDTO entity) { // todo 后台直接获取在线用户 entity.setCreateUser("admin"); entity.setCreateTime(System.currentTimeMillis()); @@ -39,10 +39,10 @@ public class UserService { UserExtend userExtend = new UserExtend(); BeanUtils.copyBean(userExtend, entity); userExtendMapper.insert(userExtend); - return true; + return entity; } - public boolean update(UserDTO entity) { + public UserDTO update(UserDTO entity) { entity.setCreateUser(null); entity.setCreateTime(null); entity.setUpdateTime(System.currentTimeMillis()); @@ -53,7 +53,7 @@ public class UserService { BeanUtils.copyBean(userExtend, entity); userExtendMapper.updateByPrimaryKeySelective(userExtend); } - return true; + return baseUserMapper.selectById(entity.getId()); } public UserDTO getById(String id) { @@ -87,11 +87,12 @@ public class UserService { int batchSize = 100; int size = users.size(); int pageSize = size / batchSize; - if (pageSize == 0) { - baseUserMapper.batchSave(users); - System.out.println("batch save cost: " + (System.currentTimeMillis() - start) + "ms"); - return true; - } + + users.forEach(user -> { + user.setCreateUser("admin"); + user.setCreateTime(System.currentTimeMillis()); + user.setUpdateTime(System.currentTimeMillis()); + }); for (int i = 0; i < pageSize; i++) { int startIndex = i * batchSize; @@ -112,4 +113,10 @@ public class UserService { return userMapper.countByExample(new UserExample()); } + public UserDTO delete(String userId) { + UserDTO userDTO = baseUserMapper.selectById(userId); + userMapper.deleteByPrimaryKey(userId); + userExtendMapper.deleteByPrimaryKey(userId); + return userDTO; + } } diff --git a/backend/services/system-setting/src/test/java/io/metersphere/system/controller/UserControllerTests.java b/backend/services/system-setting/src/test/java/io/metersphere/system/controller/UserControllerTests.java index 8ca608f117..cf66f99498 100644 --- a/backend/services/system-setting/src/test/java/io/metersphere/system/controller/UserControllerTests.java +++ b/backend/services/system-setting/src/test/java/io/metersphere/system/controller/UserControllerTests.java @@ -85,43 +85,22 @@ public class UserControllerTests { .andExpect(content().contentType(MediaType.APPLICATION_JSON)); } - @Test @Order(5) - public void testSelectAll() throws Exception { - mockMvc.perform(MockMvcRequestBuilders.get("/user/list-all")) + public void testDeleteUser() throws Exception { + + mockMvc.perform(MockMvcRequestBuilders.get("/user/delete/admin")) .andExpect(status().isOk()) .andExpect(content().contentType(MediaType.APPLICATION_JSON)) - .andExpect(jsonPath("$.data[0].id").value("admin")); + .andExpect(jsonPath("$.data.id").value("admin")); } - - // @Test - @Order(6) - public void testBatchAddUser2() throws Exception { - var users = new ArrayList(); - for (int i = 0; i < 1000; i++) { - User user = new User(); - user.setId("batch2_" + i); - user.setName("batch2_" + i); - user.setSource("LOCAL"); - user.setEmail("bin@fit2cloud.com"); - user.setStatus("enabled"); - users.add(user); - } - - mockMvc.perform(MockMvcRequestBuilders.post("/user/batch-add2") - .content(JSON.toJSONString(users)) - .contentType(MediaType.APPLICATION_JSON)) - .andExpect(status().isOk()) - .andExpect(content().contentType(MediaType.APPLICATION_JSON)); - } - - // @Test + @Test @Order(7) public void testBatchAddUser3() throws Exception { var users = new ArrayList(); - for (int i = 0; i < 1000; i++) { + int size = 123; + for (int i = 0; i < size; i++) { User user = new User(); user.setId("batch3_" + i); user.setName("batch3_" + i); @@ -136,16 +115,11 @@ public class UserControllerTests { .contentType(MediaType.APPLICATION_JSON)) .andExpect(status().isOk()) .andExpect(content().contentType(MediaType.APPLICATION_JSON)); - } - // @Test - @Order(8) - public void testCount() throws Exception { mockMvc.perform(MockMvcRequestBuilders.get("/user/count") .contentType(MediaType.APPLICATION_JSON)) .andExpect(status().isOk()) .andExpect(content().contentType(MediaType.APPLICATION_JSON)) - .andExpect(jsonPath("$.data").value(3001)); + .andExpect(jsonPath("$.data").value(size)); } - }