<?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.SysMenuColumnMapper">

    <!-- 通用查询映射结果 -->
    <resultMap id="BaseResultMap" type="com.hzs.common.domain.system.sys.SysMenuColumn">
        <id column="PK_ID" property="pkId"/>
        <result column="PK_MENU" property="pkMenu"/>
        <result column="FIELD_NAME" property="fieldName"/>
        <result column="FIELD_PERMS" property="fieldPerms"/>
        <result column="FIELD_SORT" property="fieldSort"/>
        <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"/>
    </resultMap>

    <!-- 批量插入或更新 -->
    <insert id="insertOrUpdateBatch">
        merge into SYS_MENU_COLUMN smc
        using (
        <foreach collection="list" item="item" separator=" union all ">
            select #{item.pkId, jdbcType=NUMERIC} PK_ID,
            #{item.fieldName} FIELD_NAME,
            #{item.fieldPerms} FIELD_PERMS,
            #{item.fieldSort} FIELD_SORT
            from dual
        </foreach>
        ) tmp
        on (tmp.pk_id is not null and tmp.pk_id = smc.pk_id and smc.del_flag = 0)
        WHEN MATCHED THEN
        update
        set smc.FIELD_NAME = tmp.field_name,
        smc.FIELD_PERMS = tmp.field_perms,
        smc.FIELD_SORT = tmp.field_sort,
        smc.PK_MODIFIED = #{userId},
        smc.MODIFIED_TIME = sysdate
        WHEN NOT MATCHED THEN
        insert (
        PK_ID, PK_MENU, FIELD_NAME, FIELD_PERMS, FIELD_SORT, PK_CREATOR
        )
        values (
        SYS_MENU_COLUMN_SEQ.Nextval,
        #{menuId},
        tmp.field_name,
        tmp.field_perms,
        tmp.field_sort,
        #{userId}
        )
    </insert>

</mapper>
