<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.zt.plat.module.qms.resource.record.dal.mapper.RecordPermissionMapper">

    <!--
        一般情况下，尽可能使用 Mapper 进行 CRUD 增删改查即可。
        无法满足的场景，例如说多表关联查询，才使用 XML 编写 SQL。
        代码生成器暂时只生成 Mapper XML 文件本身，更多推荐 MybatisX 快速开发插件来生成查询。
        文档可见：https://www.iocoder.cn/MyBatis/x-plugins/
     -->
    <sql id="permissionColumn">
        rp.id as id,
               rp.src_id as sourceId,
               rp.src_tp as sourceType,
               rp.tgt_id as targetId,
               rp.tgt_tp as targetType,
               rp.tgt_name as targetName,
               rp.perm as permission,
               rp.CNF_LVL as confidentialLevel,
               rp.PMNT as permanently,
               rp.EXPR_DT as expirationDate,
               rp.EXPR_STS as expirationStatus,
               rp.CNL_FLG as cancelFlag,
               rp.SVE_FLG as saveFlag
    </sql>

<!--    分类权限查询（查询管理员，以及处管理员以外满足条件的权限）-->
    <select id="selectPermissionList" resultType="com.zt.plat.module.qms.resource.record.dal.dataobject.recordpermission.RecordPermissionDO">
        SELECT
            <include refid="permissionColumn"/>
        FROM t_rcd_ctgr rc
        INNER JOIN t_rcd_perm rp ON rc.id = rp.src_id
        WHERE rp.deleted = 0 and rc.deleted = 0 AND rc.id = #{recordP.sourceId}
        <if test="recordP.permission != null and recordP.permission != ''">
            and rp.perm = #{recordP.permission}
        </if>
        AND (
            ( rp.tgt_id = #{userId} AND rp.tgt_tp = '用户')
            <if test="userDepts != null and userDepts.size() > 0">
                OR
                (rp.tgt_tp = '部门' AND rp.tgt_id IN
                <foreach collection="userDepts" item="deptId" open="(" separator="," close=")">
                    #{deptId}
                </foreach>
                )
            </if>
            <if test="userRoles != null and userRoles.size() > 0">
                OR
                (rp.tgt_tp = '角色' AND rp.tgt_id IN
                <foreach collection="userRoles" item="roleId" open="(" separator="," close=")">
                    #{roleId}
                </foreach>
                )
            </if>
        )
    </select>

    <!--    记录权限查询 SRC_ID = 记录Id 通过 记录Id 过滤查询重复数据-->
    <select id="selectRecordPermissionList"
            resultType="com.zt.plat.module.qms.resource.record.dal.dataobject.recordpermission.RecordPermissionDO">
        SELECT
        <include refid="permissionColumn"/>
        FROM T_RCD_PERM rp
        WHERE
        rp.deleted = 0

        AND rp.SRC_TP = #{recordP.sourceType}
        AND rp.PERM = #{recordP.permission}

        <!--    权限保留 和 截止日期-->
        and (rp.SVE_FLG = 1 and rp.SVE_END_DT > now())

        <if test="recordP.cancelFlag != null and recordP.expirationStatus != null">
            <!--    作废 和 过期-->
           OR (rp.CNL_FLG = #{recordP.cancelFlag} AND rp.EXPR_STS = #{recordP.expirationStatus})
        </if>

        AND (
        ( rp.tgt_id = #{userId} AND rp.tgt_tp = '用户')
        <if test="userDepts != null and userDepts.size() > 0">
            OR
            (rp.tgt_tp = '部门' AND rp.tgt_id IN
            <foreach collection="userDepts" item="deptId" open="(" separator="," close=")">
                #{deptId}
            </foreach>
            )
        </if>
        )
    </select>

</mapper>