刘光辉
昨天 bb638871a7fb692d80f1b7a758f991dc0879002c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
package jnpf.dmsMapper;
 
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import jnpf.dmsEntity.permission.entity.DmsAccessExclusionEntity;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
 
import java.util.Date;
import java.util.List;
import java.util.UUID;
 
@Mapper
public interface DmsAccessExclusionMapper extends BaseMapper<DmsAccessExclusionEntity> {
    String COLUMNS = "id, tenant_id, resource_type, resource_id, user_id, action_code, source_type, "
            + "source_id, valid_from, valid_to, exclude_reason, excluded_by_type, excluded_by_id, "
            + "revoked_at, revoked_by_type, revoked_by_id, revoke_reason, revision, created_at, updated_at";
 
    @Select("SELECT 1 FROM (SELECT pg_advisory_xact_lock(hashtextextended(#{tenantId} || ':' "
            + "|| #{sourceType} || ':' || #{sourceId}, 0))) advisory_lock")
    Integer lockSource(@Param("tenantId") String tenantId,
                       @Param("sourceType") String sourceType,
                       @Param("sourceId") String sourceId);
 
    @Select("SELECT " + COLUMNS + " FROM public.dms_access_exclusions "
            + "WHERE tenant_id = #{tenantId} AND source_type = #{sourceType} "
            + "AND source_id = #{sourceId} ORDER BY id")
    List<DmsAccessExclusionEntity> selectBySource(@Param("tenantId") String tenantId,
                                                   @Param("sourceType") String sourceType,
                                                   @Param("sourceId") String sourceId);
 
    @Insert({"<script>",
            "INSERT INTO public.dms_access_exclusions (id, tenant_id, resource_type, resource_id, user_id,",
            "action_code, source_type, source_id, valid_from, valid_to, exclude_reason, excluded_by_type,",
            "excluded_by_id) VALUES",
            "<foreach collection='items' item='item' separator=','>",
            "(uuidv7(), #{item.tenantId}, #{item.resourceType}, #{item.resourceId}, #{item.userId},",
            "#{item.actionCode}, #{item.sourceType}, #{item.sourceId}, #{item.validFrom}, #{item.validTo},",
            "#{item.excludeReason}, #{item.excludedByType}, #{item.excludedById})",
            "</foreach>",
            "</script>"})
    int insertBatch(@Param("items") List<DmsAccessExclusionEntity> items);
 
    @Select({"<script>",
            "SELECT ", COLUMNS, " FROM public.dms_access_exclusions",
            "WHERE tenant_id = #{tenantId} AND id IN",
            "<foreach collection='ids' item='id' open='(' separator=',' close=')'>#{id}</foreach>",
            "ORDER BY id FOR UPDATE",
            "</script>"})
    List<DmsAccessExclusionEntity> selectByTenantAndIdsForUpdate(@Param("tenantId") String tenantId,
                                                                  @Param("ids") List<UUID> ids);
 
    @Select({"<script>",
            "SELECT ", COLUMNS, " FROM public.dms_access_exclusions",
            "WHERE tenant_id = #{tenantId} AND id IN",
            "<foreach collection='ids' item='id' open='(' separator=',' close=')'>#{id}</foreach>",
            "ORDER BY id",
            "</script>"})
    List<DmsAccessExclusionEntity> selectByTenantAndIds(@Param("tenantId") String tenantId,
                                                         @Param("ids") List<UUID> ids);
 
    @Update({"<script>",
            "UPDATE public.dms_access_exclusions SET revoked_at = CURRENT_TIMESTAMP,",
            "revoked_by_type = #{actorType}, revoked_by_id = #{actorId}, revoke_reason = #{reason},",
            "revision = revision + 1 WHERE tenant_id = #{tenantId} AND revoked_at IS NULL AND id IN",
            "<foreach collection='ids' item='id' open='(' separator=',' close=')'>#{id}</foreach>",
            "</script>"})
    int revokeBatch(@Param("tenantId") String tenantId,
                    @Param("ids") List<UUID> ids,
                    @Param("actorType") String actorType,
                    @Param("actorId") String actorId,
                    @Param("reason") String reason);
 
    @Select({"<script>",
            "SELECT ", COLUMNS, " FROM public.dms_access_exclusions",
            "WHERE tenant_id = #{tenantId}",
            "<if test='resourceId != null'>AND resource_id = #{resourceId}</if>",
            "<if test='userId != null and userId != \"\"'>AND user_id = #{userId}</if>",
            "<if test='cursorTime != null'>",
            "AND (created_at, id) &lt; (#{cursorTime}, #{cursorId})",
            "</if>",
            "ORDER BY created_at DESC, id DESC LIMIT #{limit}",
            "</script>"})
    List<DmsAccessExclusionEntity> selectPage(@Param("tenantId") String tenantId,
                                               @Param("resourceId") UUID resourceId,
                                               @Param("userId") String userId,
                                               @Param("cursorTime") Date cursorTime,
                                               @Param("cursorId") UUID cursorId,
                                               @Param("limit") int limit);
 
    @Select({"<script>",
            "SELECT ", COLUMNS, " FROM public.dms_access_exclusions",
            "WHERE tenant_id = #{tenantId} AND user_id = #{userId} AND action_code = 'READ'",
            "AND revoked_at IS NULL AND valid_from &lt;= CURRENT_TIMESTAMP",
            "AND (valid_to IS NULL OR CURRENT_TIMESTAMP &lt; valid_to)",
            "AND resource_id IN",
            "<foreach collection='resourceIds' item='id' open='(' separator=',' close=')'>#{id}</foreach>",
            "ORDER BY resource_type, resource_id, created_at, id",
            "</script>"})
    List<DmsAccessExclusionEntity> selectActiveForResourceIds(@Param("tenantId") String tenantId,
                                                               @Param("userId") String userId,
                                                               @Param("resourceIds") List<UUID> resourceIds);
}