<?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.config.dal.mapper.ConfigWarehouseLocationMapper">

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

    <select id="selectListWithPermission" resultType="com.zt.plat.module.qms.business.config.dal.dataobject.ConfigWarehouseLocationDO">
        select
        t.id as id,
        t.PRN_ID as parentId,
        t.NAME as name,
        t.CD as code,
        t.SRT_NO as sortNo,
        t.CPY as capacity,
        t.MAIN_DEPT_ID as mainDepartmentId,
        t.MAIN_DEPT_NAME as mainDepartmentName,
        t.DAT_TP as dataType,
        t.WRH_TP as warehouseType,
        t.SYS_DEPT_CD as systemDepartmentCode,
        t.RMK as remark,
        t.PRNT_TMPL as printTemplate
        from T_CFG_WRH_LOC t
        <where>
            <if test="param.dataType != null and param.dataType != ''">
                and t.DAT_TP = #{param.dataType}
            </if>
            <if test="param.warehouseType != null and param.warehouseType != ''">
                and t.WRH_TP = #{param.warehouseType}
            </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.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.ID = p.SRC_ID
                and p.SRC_TP = #{param.srcPermissionType}
                and p.DELETED = 0
                )
                )
            </if>
            and t.DELETED = 0

        </where>
        order by t.SRT_NO asc
    </select>

</mapper>