<?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.dk.mdm.mapper.sys.ModuleMapper">

    <!-- 通用设置 -->
    <!-- 通用查询列 -->
    <sql id="Base_Column_List">
        module_code
        , sys.f_get_name_i18n_lang(
        module_name_i18n,#{i18n}
        )
        as
        moduleName,
        parent_code,
        display_no,
        level_no,
        level_code,
        sys_id,
        flg_valid,
        remarks
    </sql>

    <!-- 通用查询映射结果 -->
    <resultMap id="BaseResultMap" type="com.dk.mdm.model.pojo.sys.Module">
        <id column="module_code" property="moduleCode"/>
        <result column="module_name_i18n" property="moduleNameI18n"
                typeHandler="JsonTypeHandler"/>
        <result column="parent_code" property="parentCode"/>
        <result column="display_no" property="displayNo"/>
        <result column="level_no" property="levelNo"/>
        <result column="level_code" property="levelCode"/>
        <result column="sys_id" property="sysId"/>
        <result column="flg_valid" property="flgValid"/>
        <result column="remarks" property="remarks"/>
    </resultMap>

    <!-- 通用条件列 -->
    <sql id="Condition">
        <where>
            <if test="moduleNameI18n != null and moduleNameI18n != ''">
                AND module_name_i18n = #{moduleNameI18n}
            </if>
            <if test="parentCode != null and parentCode != ''">
                AND parent_code = #{parentCode}
            </if>
            <if test="displayNo != null">
                AND display_no = #{displayNo}
            </if>
            <if test="levelNo != null">
                AND level_no = #{levelNo}
            </if>
            <if test="levelCode != null and levelCode != ''">
                AND level_code = #{levelCode}
            </if>
            <if test="sysId != null">
                AND sys_id = #{sysId}
            </if>
            <if test="flgValid != null">
                AND flg_valid = #{flgValid}
            </if>
            <if test="remarks != null and remarks != ''">
                AND remarks = #{remarks}
            </if>
        </where>
    </sql>

    <sql id="idsForeach">
        <!-- 根据主键moduleCode批量操作 -->
        WHERE module_code in
        <foreach collection="ids" index="index" item="item" separator="," open="(" close=")">
            #{item}
        </foreach>
    </sql>

    <!-- 查询表t_s_module，（条件查询+分页）列表 -->
    <select id="selectByCond" resultMap="BaseResultMap">
        SELECT
        <include refid="Base_Column_List"/>
        FROM t_s_module
        <include refid="Condition"/>
        <if test="pageSize != null and currentPage != null and pageSize != 0 and currentPage != 0">
            limit #{end} offset #{start}
        </if>
    </select>

    <!-- 查询表t_s_module，（条件查询）个数 -->
    <select id="countByCond" resultType="Long">
        SELECT
        count(1)
        FROM t_s_module
        <include refid="Condition"/>
    </select>


    <!-- 根据主键锁定表t_s_module的一行数据 -->
    <select id="selectByIdForUpdate" resultMap="BaseResultMap">
        SELECT
        <include refid="Base_Column_List"/>
        FROM t_s_module
        WHERE module_code = #{moduleCode}
        for update
    </select>

    <!-- 根据主键锁定表t_s_module的多行数据 -->
    <select id="selectByIdsForUpdate" resultMap="BaseResultMap">
        SELECT
        <include refid="Base_Column_List"/>
        FROM t_s_module
        <include refid="idsForeach"/>
        for update
    </select>

    <insert id="insertBatch">
        insert into t_s_module
        (
        <trim suffixOverrides=",">
            module_name_i18n,
            parent_code,
            display_no,
            level_no,
            level_code,
            sys_id,
            remarks,
        </trim>
        )
        values
        <foreach collection="list" index="index" item="item" separator=",">
            (
            <trim suffixOverrides=",">
                #{item.moduleNameI18n},
                #{item.parentCode},
                #{item.displayNo},
                #{item.levelNo},
                #{item.levelCode},
                #{item.sysId},
                #{item.remarks},
            </trim>
            )
        </foreach>
    </insert>

    <!--    查询功能模块列表按序号升序-->
    <select id="selectModuleList" resultMap="BaseResultMap">
        select *
        from (
                 select 0                                                flgNum,
                        m.module_code                                    ,
                        sys.f_get_name_i18n_lang(m.module_name_i18n,#{i18n}) moduleName,
                        m.parent_code,
                        m.level_code,
                        null                                             pageCodes,
                        null                                             pageNames
                 from sys.t_s_module m
                 where m.flg_valid
                 <if test="moduleCode != null and moduleCode !=''">
                     m.module_code=#{moduleCode}
                 </if>
                <if test="sysId != null and sysId !=''">
                    and m.sys_id=#{sysId}
                </if>
                 union all
                 select 1                                                                        flgNum,
                        d.doc_code,
                        sys.f_get_name_i18n_lang(d.doc_name_i18n,#{i18n})                            docName,
                        d.module_code,
                        m.level_code || sys.f_to_str36(COALESCE(min(p.display_no), 0), 3) || '#' levelCode,
                        array_agg(p.page_code)                                                   pageCodes,
                        array_agg(sys.f_get_name_i18n_lang(p.page_name_i18n,#{i18n}))                pageNames
                 from sys.t_s_doc d
                          inner join sys.t_s_module m on d.module_code = m.module_code
                          left join sys.t_s_page p on p.doc_code = d.doc_code
                 where d.flg_valid
                   and d.flg_sensitive
                    <if test="moduleCode != null and moduleCode !=''">
                        d.module_code=#{moduleCode}
                    </if>
                    <if test="sysId != null and sysId !=''">
                        and m.sys_id=#{sysId}
                    </if>
                 group by d.module_code, d.doc_code, m.level_code
                 union all
                 select 2                                                      flgNum,
                        p.page_code,
                        sys.f_get_name_i18n_lang(p.page_name_i18n,#{i18n})         pageName,
                        p.module_code,
                        m.level_code || sys.f_to_str36(p.display_no, 3) || '#' levelCode,
                        null                                                   pageCodes,
                        null                                                   pageNames
                 from sys.t_s_page p
                          inner join sys.t_s_module m on p.module_code = m.module_code
                 where p.flg_valid
                   and p.flg_sensitive
                   and p.doc_code is null
                    <if test="moduleCode != null and moduleCode !=''">
                        p.module_code=#{moduleCode}
                    </if>
                    <if test="sysId != null and sysId !=''">
                       and m.sys_id=#{sysId}
                    </if>
             ) t
        order by t.level_code
    </select>


</mapper>
