Commit 36c5912fc8f6e0b8c07553e8efa2d96508fd8895

Authored by zichun
1 parent 06d4c5a4

feat(usr): mapper#deleteByUserId for permission rebuild REQ-USR-002

backend/src/main/java/com/xly/erp/module/usr/mapper/UserPermissionMapper.java
@@ -2,6 +2,11 @@ package com.xly.erp.module.usr.mapper; @@ -2,6 +2,11 @@ package com.xly.erp.module.usr.mapper;
2 2
3 import com.baomidou.mybatisplus.core.mapper.BaseMapper; 3 import com.baomidou.mybatisplus.core.mapper.BaseMapper;
4 import com.xly.erp.module.usr.entity.UserPermission; 4 import com.xly.erp.module.usr.entity.UserPermission;
  5 +import org.apache.ibatis.annotations.Delete;
  6 +import org.apache.ibatis.annotations.Param;
5 7
6 public interface UserPermissionMapper extends BaseMapper<UserPermission> { 8 public interface UserPermissionMapper extends BaseMapper<UserPermission> {
  9 +
  10 + @Delete("DELETE FROM tUserPermission WHERE iUserId = #{userId}")
  11 + int deleteByUserId(@Param("userId") Integer userId);
7 } 12 }
backend/src/test/java/com/xly/erp/module/usr/mapper/UserMapperIT.java
@@ -93,6 +93,49 @@ class UserMapperIT { @@ -93,6 +93,49 @@ class UserMapperIT {
93 assertThat(count).isEqualTo(1); 93 assertThat(count).isEqualTo(1);
94 } 94 }
95 95
  96 + @Test
  97 + void userPermissionMapper_deleteByUserId_removesAllRowsForGivenUser() {
  98 + User user1 = newUser("sp_test_del_u1", "用户1");
  99 + userMapper.insert(user1);
  100 + User user2 = newUser("sp_test_del_u2", "用户2");
  101 + userMapper.insert(user2);
  102 +
  103 + jdbcTemplate.update(
  104 + "INSERT INTO tPermissionCategory (sBrandsId, sSubsidiaryId, tCreateDate, sCategoryCode, sCategoryName, "
  105 + + "iSortOrder, sCreatedBy, bDeleted) VALUES "
  106 + + "('XLY','XLY', NOW(), 'sp_test_del_c1', '权限C1', 0, 'STUB', 0),"
  107 + + "('XLY','XLY', NOW(), 'sp_test_del_c2', '权限C2', 0, 'STUB', 0)");
  108 + Integer cat1 = jdbcTemplate.queryForObject(
  109 + "SELECT iIncrement FROM tPermissionCategory WHERE sCategoryCode = 'sp_test_del_c1'", Integer.class);
  110 + Integer cat2 = jdbcTemplate.queryForObject(
  111 + "SELECT iIncrement FROM tPermissionCategory WHERE sCategoryCode = 'sp_test_del_c2'", Integer.class);
  112 +
  113 + insertUserPermission(user1.getIIncrement(), cat1);
  114 + insertUserPermission(user1.getIIncrement(), cat2);
  115 + insertUserPermission(user2.getIIncrement(), cat1);
  116 +
  117 + int affected = userPermissionMapper.deleteByUserId(user1.getIIncrement());
  118 + assertThat(affected).isEqualTo(2);
  119 +
  120 + Integer u1Count = jdbcTemplate.queryForObject(
  121 + "SELECT COUNT(1) FROM tUserPermission WHERE iUserId = ?", Integer.class, user1.getIIncrement());
  122 + Integer u2Count = jdbcTemplate.queryForObject(
  123 + "SELECT COUNT(1) FROM tUserPermission WHERE iUserId = ?", Integer.class, user2.getIIncrement());
  124 + assertThat(u1Count).isZero();
  125 + assertThat(u2Count).isEqualTo(1);
  126 + }
  127 +
  128 + private void insertUserPermission(Integer userId, Integer catId) {
  129 + UserPermission rel = new UserPermission();
  130 + rel.setSBrandsId("XLY");
  131 + rel.setSSubsidiaryId("XLY");
  132 + rel.setTCreateDate(LocalDateTime.now());
  133 + rel.setIUserId(userId);
  134 + rel.setICategoryId(catId);
  135 + rel.setSCreatedBy("STUB_ADMIN");
  136 + userPermissionMapper.insert(rel);
  137 + }
  138 +
96 private User newUser(String userNo, String userName) { 139 private User newUser(String userNo, String userName) {
97 User u = new User(); 140 User u = new User();
98 u.setSBrandsId("XLY"); 141 u.setSBrandsId("XLY");