Commit 56294ee0a2b8b3b4467c530de079a6d77430d5bf

Authored by zichun
1 parent 5ecface9

feat(usr): staff + permission-category existence mappers REQ-USR-001

backend/src/main/java/com/xly/erp/module/usr/mapper/PermissionCategoryMapper.java 0 → 100644
  1 +package com.xly.erp.module.usr.mapper;
  2 +
  3 +import org.apache.ibatis.annotations.Mapper;
  4 +import org.apache.ibatis.annotations.Param;
  5 +import org.apache.ibatis.annotations.Select;
  6 +
  7 +import java.util.List;
  8 +
  9 +@Mapper
  10 +public interface PermissionCategoryMapper {
  11 +
  12 + @Select("<script>"
  13 + + "SELECT COUNT(1) FROM tPermissionCategory "
  14 + + "WHERE bDeleted = 0 AND iIncrement IN "
  15 + + "<foreach collection='ids' item='id' open='(' separator=',' close=')'>#{id}</foreach>"
  16 + + "</script>")
  17 + int countActiveByIds(@Param("ids") List<Integer> ids);
  18 +}
backend/src/main/java/com/xly/erp/module/usr/mapper/StaffMapper.java 0 → 100644
  1 +package com.xly.erp.module.usr.mapper;
  2 +
  3 +import org.apache.ibatis.annotations.Mapper;
  4 +import org.apache.ibatis.annotations.Param;
  5 +import org.apache.ibatis.annotations.Select;
  6 +
  7 +@Mapper
  8 +public interface StaffMapper {
  9 +
  10 + @Select("SELECT 1 FROM tStaff WHERE iIncrement = #{id} AND bDeleted = 0 LIMIT 1")
  11 + Integer findActiveStaffFlag(@Param("id") Integer id);
  12 +
  13 + default boolean existsActiveById(Integer id) {
  14 + return findActiveStaffFlag(id) != null;
  15 + }
  16 +}
backend/src/test/java/com/xly/erp/module/usr/mapper/PermissionCategoryMapperIT.java 0 → 100644
  1 +package com.xly.erp.module.usr.mapper;
  2 +
  3 +import org.junit.jupiter.api.AfterEach;
  4 +import org.junit.jupiter.api.BeforeEach;
  5 +import org.junit.jupiter.api.Test;
  6 +import org.springframework.beans.factory.annotation.Autowired;
  7 +import org.springframework.boot.test.context.SpringBootTest;
  8 +import org.springframework.jdbc.core.JdbcTemplate;
  9 +import org.springframework.test.context.ActiveProfiles;
  10 +
  11 +import java.util.List;
  12 +
  13 +import static org.assertj.core.api.Assertions.assertThat;
  14 +
  15 +@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.NONE)
  16 +@ActiveProfiles("test")
  17 +class PermissionCategoryMapperIT {
  18 +
  19 + @Autowired
  20 + private PermissionCategoryMapper permissionCategoryMapper;
  21 +
  22 + @Autowired
  23 + private JdbcTemplate jdbcTemplate;
  24 +
  25 + @BeforeEach
  26 + @AfterEach
  27 + void cleanup() {
  28 + jdbcTemplate.update("DELETE FROM tPermissionCategory WHERE sCategoryCode LIKE 'sp_test_%'");
  29 + }
  30 +
  31 + @Test
  32 + void countActiveByIds_returnsCorrectCount() {
  33 + Integer cat1 = insertCategory("sp_test_c1", "权限1", false);
  34 + Integer cat2 = insertCategory("sp_test_c2", "权限2", false);
  35 + Integer cat3 = insertCategory("sp_test_c3", "权限3", true);
  36 +
  37 + assertThat(permissionCategoryMapper.countActiveByIds(List.of(cat1, cat2, cat3))).isEqualTo(2);
  38 + assertThat(permissionCategoryMapper.countActiveByIds(List.of(cat1, 99999991))).isEqualTo(1);
  39 + assertThat(permissionCategoryMapper.countActiveByIds(List.of(99999991))).isZero();
  40 + }
  41 +
  42 + private Integer insertCategory(String code, String name, boolean deleted) {
  43 + jdbcTemplate.update(
  44 + "INSERT INTO tPermissionCategory (sBrandsId, sSubsidiaryId, tCreateDate, sCategoryCode, sCategoryName, "
  45 + + "iSortOrder, sCreatedBy, bDeleted) "
  46 + + "VALUES ('XLY','XLY', NOW(), ?, ?, 0, 'STUB_ADMIN', ?)",
  47 + code, name, deleted ? 1 : 0);
  48 + return jdbcTemplate.queryForObject(
  49 + "SELECT iIncrement FROM tPermissionCategory WHERE sCategoryCode = ?", Integer.class, code);
  50 + }
  51 +}
backend/src/test/java/com/xly/erp/module/usr/mapper/StaffMapperIT.java 0 → 100644
  1 +package com.xly.erp.module.usr.mapper;
  2 +
  3 +import org.junit.jupiter.api.AfterEach;
  4 +import org.junit.jupiter.api.BeforeEach;
  5 +import org.junit.jupiter.api.Test;
  6 +import org.springframework.beans.factory.annotation.Autowired;
  7 +import org.springframework.boot.test.context.SpringBootTest;
  8 +import org.springframework.jdbc.core.JdbcTemplate;
  9 +import org.springframework.test.context.ActiveProfiles;
  10 +
  11 +import static org.assertj.core.api.Assertions.assertThat;
  12 +
  13 +@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.NONE)
  14 +@ActiveProfiles("test")
  15 +class StaffMapperIT {
  16 +
  17 + @Autowired
  18 + private StaffMapper staffMapper;
  19 +
  20 + @Autowired
  21 + private JdbcTemplate jdbcTemplate;
  22 +
  23 + @BeforeEach
  24 + @AfterEach
  25 + void cleanup() {
  26 + jdbcTemplate.update("DELETE FROM tStaff WHERE sStaffNo LIKE 'sp_test_%'");
  27 + }
  28 +
  29 + @Test
  30 + void existsActiveById_handlesAliveDeletedMissing() {
  31 + Integer aliveId = insertStaff("sp_test_st_alive", "活的", false);
  32 + Integer deletedId = insertStaff("sp_test_st_dead", "死的", true);
  33 +
  34 + assertThat(staffMapper.existsActiveById(aliveId)).isTrue();
  35 + assertThat(staffMapper.existsActiveById(deletedId)).isFalse();
  36 + assertThat(staffMapper.existsActiveById(99999990)).isFalse();
  37 + }
  38 +
  39 + private Integer insertStaff(String staffNo, String staffName, boolean deleted) {
  40 + jdbcTemplate.update(
  41 + "INSERT INTO tStaff (sBrandsId, sSubsidiaryId, tCreateDate, sStaffNo, sStaffName, sCreatedBy, bDeleted) "
  42 + + "VALUES ('XLY','XLY', NOW(), ?, ?, 'STUB_ADMIN', ?)",
  43 + staffNo, staffName, deleted ? 1 : 0);
  44 + return jdbcTemplate.queryForObject(
  45 + "SELECT iIncrement FROM tStaff WHERE sStaffNo = ?", Integer.class, staffNo);
  46 + }
  47 +}