<?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.business.reportdoc.dal.mapper.ReportDocumentTypeMapper">

    <!--
        一般情况下，尽可能使用 Mapper 进行 CRUD 增删改查即可。
        无法满足的场景，例如说多表关联查询，才使用 XML 编写 SQL。
        代码生成器暂时只生成 Mapper XML 文件本身，更多推荐 MybatisX 快速开发插件来生成查询。
        文档可见：https://www.iocoder.cn/MyBatis/x-plugins/
     -->

    <select id="selectListWithPermission" resultType="com.zt.plat.module.qms.business.reportdoc.dal.dataobject.ReportDocumentTypeDO">
        select
            t.id as id,
            t.CFG_RPT_TP_ID as configReportTypeId,
            t.NAME as name,
            t.FLW_KY as flowKey,
            t.RPT_KY as reportKey,
            t.CD_RUL as codeRule,
            t.DOC_TP as documentType,
            t.PERM as permission,
            t.SMP_CNT as sampleCount,
            t.CST_CFG as customConfig,
            t.SYS_DEPT_CD as systemDepartmentCode,
            t.RMK as remark,
            t.VER as version,
            t.SRT_NO as sortNo,
            t.CRNT_FLG as currentFlag,
            t.MAIN_ID as mainId,
            t.PG_CPNT as pageComponent,
            t.PG_CPNT_MOB as pageComponentMobile
        from T_RPT_DOC_TP t
        <where>
            <if test="param.configReportTypeId != null and param.configReportTypeId != ''">
                and t.CFG_RPT_TP_ID = #{param.configReportTypeId}
            </if>
            <if test="param.name != null and param.name != ''">
                and t.NAME like concat('%',#{param.name},'%')
            </if>
            <if test="param.mainId != null">
                and t.MAIN_ID = #{param.mainId}
            </if>
            <if test="param.documentType != null and param.documentType != ''">
                and t.DOC_TP = #{param.documentType}
            </if>
            <if test="param.currentFlag != null">
                and t.CRNT_FLG = #{param.currentFlag}
            </if>
            <if test="param.permissionFilterFlag != null and param.permissionFilterFlag != ''">
                and (
                    <if test="param.roleIds != null and param.roleIds.size > 0">
                        exists(
                        select 1
                        from T_CFG_PERM p
                        where t.MAIN_ID = p.SRC_ID
                        and p.SRC_TP = #{param.srcPermissionType}
                        and p.TGT_ID in (
                        <foreach item="item" collection="param.roleIds" separator=",">
                            #{item}
                        </foreach>
                        )
                        and p.TGT_TP = 'role'
                        and p.DELETED = 0
                        )
                        or
                    </if>
                    not exists(
                        select 1 from T_CFG_PERM p where t.MAIN_ID = p.SRC_ID
                        and p.SRC_TP = #{param.srcPermissionType}
                        and p.DELETED = 0
                    )
                )
            </if>
            and t.DELETED = 0

        </where>
    </select>


</mapper>