<?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">
<!-- namespace必须指向DAO接口 -->
<mapper namespace="com.jpsoft.shinestar.modules.sys.dao.MenuDAO">
    <resultMap id="MenuMap" type="com.jpsoft.shinestar.modules.sys.entity.Menu">
        <id property="id" column="id_"/>
        <result property="menuName" column="menu_name"/>
        <result property="parentId" column="parent_id"/>
        <result property="parentName" column="parent_name"/>
        <result property="sortNo" column="sort_no"/>
        <result property="menuUrl" column="menu_url"/>
        <result property="icon" column="icon_"/>
        <result property="menuType" column="menu_type"/>
        <result property="createTime" column="create_time"/>
        <result property="createBy" column="create_by"/>
        <result property="updateTime" column="update_time"/>
        <result property="updateBy" column="update_by"/>
        <result property="delFlag" column="del_flag"/>
    </resultMap>
    <insert id="insert" parameterType="com.jpsoft.shinestar.modules.sys.entity.Menu">
        <!--
        <selectKey resultType="java.lang.String" order="BEFORE" keyProperty="id">
            select sys_guid() from dual
        </selectKey>
        -->
        <![CDATA[
		insert into sys_menu
	    (id_,menu_name,parent_id,sort_no,menu_url,menu_type,create_time,create_by,update_time,update_by,del_flag,icon_)
		values
		(
            #{id,jdbcType=VARCHAR}
            ,#{menuName,jdbcType=VARCHAR}
            ,#{parentId,jdbcType=VARCHAR}
            ,#{sortNo,jdbcType= NUMERIC }
            ,#{menuUrl,jdbcType=VARCHAR}
            ,#{menuType,jdbcType=VARCHAR}
            ,#{createTime,jdbcType= TIMESTAMP }
            ,#{createBy,jdbcType=VARCHAR}
            ,#{updateTime,jdbcType= TIMESTAMP }
            ,#{updateBy,jdbcType=VARCHAR}
            ,#{delFlag,jdbcType= NUMERIC }
            ,#{icon,jdbcType=VARCHAR}
		)
	]]>
    </insert>
    <delete id="delete" parameterType="string">
        delete from sys_menu where id_=#{id,jdbcType=VARCHAR}
    </delete>
    <update id="update" parameterType="com.jpsoft.shinestar.modules.sys.entity.Menu">
        update sys_menu
        <set>
            <if test="menuName!=null">
                menu_name=#{menuName,jdbcType=VARCHAR},
            </if>
            <if test="parentId!=null">
                parent_id=#{parentId,jdbcType=VARCHAR},
            </if>
            <if test="sortNo!=null">
                sort_no=#{sortNo,jdbcType= NUMERIC },
            </if>
            <if test="menuUrl!=null">
                menu_url=#{menuUrl,jdbcType=VARCHAR},
            </if>
            <if test="menuType!=null">
                menu_type=#{menuType,jdbcType=VARCHAR},
            </if>
            <if test="createTime!=null">
                create_time=#{createTime,jdbcType= TIMESTAMP },
            </if>
            <if test="createBy!=null">
                create_by=#{createBy,jdbcType=VARCHAR},
            </if>
            <if test="updateTime!=null">
                update_time=#{updateTime,jdbcType= TIMESTAMP },
            </if>
            <if test="updateBy!=null">
                update_by=#{updateBy,jdbcType=VARCHAR},
            </if>
            <if test="delFlag!=null">
                del_flag=#{delFlag,jdbcType= NUMERIC },
            </if>
            <if test="icon!=null">
                icon_=#{icon,jdbcType=VARCHAR},
            </if>
        </set>
        where id_=#{id}
    </update>
    <select id="get" parameterType="string" resultMap="MenuMap">
        select a.*,b.menu_name as parent_name
        from sys_menu a left join sys_menu b on a.parent_id = b.id_
        where a.id_=#{0}
    </select>
    <select id="exist" parameterType="string" resultType="int">
        select count(*) from sys_menu where id_=#{0}
    </select>
    <select id="list" resultMap="MenuMap">
        select * from sys_menu
    </select>
    <select id="search" parameterType="hashmap" resultMap="MenuMap">
        <![CDATA[
			select a.*,b.menu_name as parent_name
			from sys_menu a left join sys_menu b on a.parent_id = b.id_
		]]>
        where a.del_flag=0
        <if test="searchParams.menuName != null">
            and a.menu_name like #{searchParams.menuName}
        </if>
        <if test="searchParams.menuType != null">
            and a.menu_type = #{searchParams.menuType}
        </if>
        <if test="searchParams.parentId != null">
            and a.parent_id = #{searchParams.parentId}
        </if>
        <if test="searchParams.excludeId != null">
            <![CDATA[
                and a.id_ <> #{searchParams.excludeId}
            ]]>
        </if>
        <foreach item="sort" collection="sortList" open="order by" separator=",">
            ${sort.name} ${sort.order}
        </foreach>
    </select>
    <select id="findAllocMenu"  parameterType="string" resultMap="MenuMap">
        select * from sys_menu m
        where m.menu_type = 2
        and m.del_flag=0
        and m.id_ in(
        select rm.menu_id from
        sys_role_menu rm,sys_user_role ur
        where ur.user_id=#{userId}
        and ur.role_id = rm.role_id
        and rm.menu_id = m.id_
        )
        <if test="parentId==null">
            and (m.parent_id is null or m.parent_id='')
        </if>
        <if test="parentId!=null">
            and m.parent_id = #{parentId}
        </if>
        order by m.sort_no asc
    </select>
</mapper>