<?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.hzs.system.sys.mapper.SysRoleMenuColumnMapper">

    <!-- 通用查询映射结果 -->
    <resultMap id="BaseResultMap" type="com.hzs.common.domain.system.sys.SysRoleMenuColumn">
        <id column="PK_ID" property="pkId"/>
        <result column="PK_ROLE" property="pkRole"/>
        <result column="PK_MENU" property="pkMenu"/>
        <result column="MENU_COLUMN" property="menuColumn"/>
        <result column="DEL_FLAG" property="delFlag"/>
        <result column="PK_CREATOR" property="pkCreator"/>
        <result column="CREATION_TIME" property="creationTime"/>
        <result column="PK_MODIFIED" property="pkModified"/>
        <result column="MODIFIED_TIME" property="modifiedTime"/>
        <result column="SHOW_FLAG" property="showFlag"/>
    </resultMap>

    <!-- 批量插入或更新 -->
    <insert id="insertBatch">
        merge into sys_role_menu_column srmc
        using (
        <foreach collection="saveList" item="item" separator=" union all ">
            select #{item.pkRole} as pk_role,
            #{item.pkMenu} as pk_menu,
            #{item.menuColumn} as menu_column,
            #{item.showFlag} as show_flag
            from dual
        </foreach>
        ) tmp
        on (srmc.pk_role = tmp.pk_role and srmc.pk_menu = tmp.pk_menu and srmc.menu_column = tmp.menu_column and srmc.del_flag = 0)
        WHEN MATCHED THEN
        update
        set srmc.show_flag = tmp.show_flag,
        srmc.pk_modified = #{userId},
        srmc.modified_time = sysdate
        WHEN NOT MATCHED THEN
        insert
        (pk_id, pk_role, pk_menu, menu_column, pk_creator, show_flag)
        values
        (SYS_ROLE_MENU_COLUMN_SEQ.Nextval,
        tmp.pk_role,
        tmp.pk_menu,
        tmp.menu_column,
        #{userId},
        tmp.show_flag)
    </insert>

    <!-- 根据角色查询菜单列 -->
    <select id="queryMenuColumn" resultMap="BaseResultMap">
        select srmc.menu_column, srmc.show_flag
        from SYS_MENU sm
        left join SYS_ROLE_MENU_COLUMN srmc
        on srmc.pk_menu = sm.menu_id
        where sm.del_flag = 0
        and sm.menu_type = 'C'
        and sm.field_perms = 0
        and sm.perms = #{perms}
        and srmc.del_flag = 0
        and srmc.pk_role in
        <foreach collection="roleIdList" item="item" open="(" close=")" separator=",">
            #{item}
        </foreach>
        order by srmc.menu_column
    </select>

</mapper>
