<?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.yanzhu.jh.wxsetting.mapper.SurMenuConfigMapper">
    
    <resultMap type="SurMenuConfig" id="SurMenuConfigResult">
        <result property="id"    column="id"    />
        <result property="menuName"    column="menu_name"    />
        <result property="menuIdenti"    column="menu_identi"    />
        <result property="menuImg"    column="menu_img"    />
        <result property="menuUrl"    column="menu_url"    />
        <result property="delFlag"    column="del_flag"    />
        <result property="createTime"    column="create_time"    />
        <result property="menuSort"    column="menu_sort"    />
        <result property="menuType"    column="menu_type"    />
    </resultMap>

    <resultMap id="SurMenuConfigSurMenuConfigRoleResult" type="SurMenuConfig" extends="SurMenuConfigResult">
        <collection property="surMenuConfigRoleList" notNullColumn="sub_smcid" javaType="java.util.List" resultMap="SurMenuConfigRoleResult" />
    </resultMap>

    <resultMap type="SurMenuConfigRole" id="SurMenuConfigRoleResult">
        <result property="smcid"    column="sub_smcid"    />
        <result property="roleId"    column="sub_role_id"    />
    </resultMap>

    <sql id="selectSurMenuConfigVo">
        select id, menu_name, menu_identi, menu_img, menu_url, del_flag, create_time, menu_type, menu_sort from sur_menu_config
    </sql>

    <select id="selectSurMenuConfigList" parameterType="SurMenuConfig" resultMap="SurMenuConfigResult">
        <include refid="selectSurMenuConfigVo"/>
        <where>
            <if test="menuName != null  and menuName != ''"> and menu_name like concat('%', #{menuName}, '%')</if>
            <if test="menuIdenti != null  and menuIdenti != ''"> and menu_identi = #{menuIdenti}</if>
            <if test="menuImg != null  and menuImg != ''"> and menu_img = #{menuImg}</if>
            <if test="menuUrl != null  and menuUrl != ''"> and menu_url = #{menuUrl}</if>
            <if test="delFlag != null "> and del_flag = #{delFlag}</if>
            <if test="menuType != null "> and menu_type = #{menuType}</if>
        </where>
    </select>
    
    <select id="selectSurMenuConfigById" parameterType="Long" resultMap="SurMenuConfigSurMenuConfigRoleResult">
        select a.id, a.menu_name, a.menu_identi, a.menu_img, a.menu_url, a.del_flag, a.create_time,menu_type,menu_sort,
 b.smcid as sub_smcid, b.role_id as sub_role_id
        from sur_menu_config a
        left join sur_menu_config_role b on b.smcid = a.id
        where a.id = #{id}
    </select>
        
    <insert id="insertSurMenuConfig" parameterType="SurMenuConfig" useGeneratedKeys="true" keyProperty="id">
        insert into sur_menu_config
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="menuName != null">menu_name,</if>
            <if test="menuIdenti != null">menu_identi,</if>
            <if test="menuImg != null">menu_img,</if>
            <if test="menuUrl != null">menu_url,</if>
            <if test="delFlag != null">del_flag,</if>
            <if test="createTime != null">create_time,</if>
            <if test="menuSort != null">menu_sort,</if>
            <if test="menuType != null">menu_type,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="menuName != null">#{menuName},</if>
            <if test="menuIdenti != null">#{menuIdenti},</if>
            <if test="menuImg != null">#{menuImg},</if>
            <if test="menuUrl != null">#{menuUrl},</if>
            <if test="delFlag != null">#{delFlag},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="menuSort != null">#{menuSort},</if>
            <if test="menuType != null">#{menuType},</if>
         </trim>
    </insert>

    <update id="updateSurMenuConfig" parameterType="SurMenuConfig">
        update sur_menu_config
        <trim prefix="SET" suffixOverrides=",">
            <if test="menuName != null">menu_name = #{menuName},</if>
            <if test="menuIdenti != null">menu_identi = #{menuIdenti},</if>
            <if test="menuImg != null">menu_img = #{menuImg},</if>
            <if test="menuUrl != null">menu_url = #{menuUrl},</if>
            <if test="delFlag != null">del_flag = #{delFlag},</if>
            <if test="createTime != null">create_time = #{createTime},</if>
            <if test="menuSort != null">menu_sort = #{menuSort},</if>
            <if test="menuType != null">menu_type = #{menuType},</if>
        </trim>
        where id = #{id}
    </update>

    <delete id="deleteSurMenuConfigById" parameterType="Long">
        delete from sur_menu_config where id = #{id}
    </delete>

    <delete id="deleteSurMenuConfigByIds" parameterType="String">
        delete from sur_menu_config where id in 
        <foreach item="id" collection="array" open="(" separator="," close=")">
            #{id}
        </foreach>
    </delete>
    
    <delete id="deleteSurMenuConfigRoleBySmcids" parameterType="String">
        delete from sur_menu_config_role where smcid in 
        <foreach item="smcid" collection="array" open="(" separator="," close=")">
            #{smcid}
        </foreach>
    </delete>

    <delete id="deleteSurMenuConfigRoleBySmcid" parameterType="Long">
        delete from sur_menu_config_role where smcid = #{smcid}
    </delete>

    <insert id="batchSurMenuConfigRole">
        insert into sur_menu_config_role( smcid, role_id) values
		<foreach item="item" index="index" collection="list" separator=",">
            ( #{item.smcid}, #{item.roleId})
        </foreach>
    </insert>
</mapper>