package jnpf.dmsMapper; import com.baomidou.mybatisplus.core.mapper.BaseMapper; import jnpf.dmsEntity.permission.entity.DmsFilePermissionEntity; 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 DmsFilePermissionMapper extends BaseMapper { String COLUMNS = "id, tenant_id, resource_type, resource_id, resource_scope, principal_type, " + "principal_id, include_child_orgs, action_code, source_type, source_id, valid_from, " + "valid_to, max_use_count, used_count, grant_reason, granted_by_type, granted_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_file_permissions " + "WHERE tenant_id = #{tenantId} AND source_type = #{sourceType} " + "AND source_id = #{sourceId} ORDER BY id") List selectBySource(@Param("tenantId") String tenantId, @Param("sourceType") String sourceType, @Param("sourceId") String sourceId); @Select("INSERT INTO public.dms_file_permissions (id, tenant_id, resource_type, resource_id, " + "resource_scope, principal_type, principal_id, include_child_orgs, action_code, " + "source_type, source_id, valid_from, valid_to, max_use_count, grant_reason, " + "granted_by_type, granted_by_id) VALUES (uuidv7(), #{p.tenantId}, #{p.resourceType}, " + "#{p.resourceId}, #{p.resourceScope}, #{p.principalType}, #{p.principalId}, " + "#{p.includeChildOrgs}, #{p.actionCode}, #{p.sourceType}, #{p.sourceId}, " + "#{p.validFrom}, #{p.validTo}, #{p.maxUseCount}, #{p.grantReason}, " + "#{p.grantedByType}, #{p.grantedById}) RETURNING " + COLUMNS) DmsFilePermissionEntity insertReturning(@Param("p") DmsFilePermissionEntity permission); @Select({""}) List selectCandidates(@Param("tenantId") String tenantId, @Param("actionCodes") List actionCodes, @Param("resourceIds") List resourceIds); @Select({""}) List selectForResourceIds(@Param("tenantId") String tenantId, @Param("resourceIds") List resourceIds, @Param("limit") int limit); @Select("SELECT " + COLUMNS + " FROM public.dms_file_permissions " + "WHERE tenant_id = #{tenantId} AND id = #{id}") DmsFilePermissionEntity selectByTenantAndId(@Param("tenantId") String tenantId, @Param("id") UUID id); @Select("SELECT " + COLUMNS + " FROM public.dms_file_permissions " + "WHERE tenant_id = #{tenantId} AND id = #{id} FOR UPDATE") DmsFilePermissionEntity selectByTenantAndIdForUpdate(@Param("tenantId") String tenantId, @Param("id") UUID id); @Update("UPDATE public.dms_file_permissions SET revoked_at = CURRENT_TIMESTAMP, " + "revoked_by_type = #{actorType}, revoked_by_id = #{actorId}, revoke_reason = #{reason}, " + "revision = revision + 1 WHERE tenant_id = #{tenantId} AND id = #{id} " + "AND revoked_at IS NULL") int revoke(@Param("tenantId") String tenantId, @Param("id") UUID id, @Param("actorType") String actorType, @Param("actorId") String actorId, @Param("reason") String reason); @Select({""}) List selectPage(@Param("tenantId") String tenantId, @Param("resourceId") UUID resourceId, @Param("principalType") String principalType, @Param("principalId") String principalId, @Param("cursorTime") Date cursorTime, @Param("cursorId") UUID cursorId, @Param("limit") int limit); }