<?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.pj.project4sp.menu.SpMenuMapper">

	<!-- 增 -->
	<insert id="add">
		insert into 
		sp_menu (id, title, icon, info, type, path, component_path, url, is_blank, `show`, auth, parent_id, sort, create_time) 
		values (#{id}, #{title}, #{icon}, #{info}, #{type}, #{path}, #{componentPath}, #{url}, #{isBlank}, #{show}, #{auth}, #{parentId}, #{sort}, now()) 
	</insert>

	<!-- 删 -->
	<delete id="delete">
		delete from sp_menu 
		where id = #{id}
	</delete>

	<!-- 改 -->
	<update id="update">
		update sp_menu set
		title = #{title}, 
		icon = #{icon}, 
		info = #{info}, 
		type = #{type}, 
		path = #{path}, 
		component_path = #{componentPath}, 
		url = #{url}, 
		is_blank = #{isBlank}, 
		`show` = #{show}, 
		auth = #{auth}, 
		parent_id = #{parentId}, 
		sort = #{sort}, 
		update_time = now()
		where id = #{id}
	</update>


	<!-- ================================== 查询相关 ================================== -->
	<!-- select id, title, icon, info, path, component_path, url, is_blank, show, auth, parent_id, sort, create_time, update_time from sp_menu  -->
	
	<!-- 通用映射：手动模式 -->
	<resultMap id="model" type="com.pj.project4sp.menu.SpMenu">
		<result property="aid" column="aid" />
		<result property="id" column="id" />
		<result property="title" column="title" />
		<result property="icon" column="icon" />
		<result property="info" column="info" />
		<result property="type" column="type" />
		<result property="path" column="path" />
		<result property="componentPath" column="component_path" />
		<result property="url" column="url" />
		<result property="isBlank" column="is_blank" />
		<result property="show" column="show" />
		<result property="auth" column="auth" />
		<result property="parentId" column="parent_id" />
		<result property="sort" column="sort" />
		<result property="createTime" column="create_time" />
		<result property="updateTime" column="update_time" />
	</resultMap>
	
	<!-- 公共查询sql片段 -->
	<sql id="select_sql">
		select * 
		from sp_menu 
	</sql>
	
	<!-- 查 - 根据id -->
	<select id="getById" resultMap="model">
		<include refid="select_sql"></include>
		where id = #{id}
	</select>
	
	<!-- 查集合 - 根据条件（参数为空时代表忽略指定条件） -->
	<select id="getList" resultMap="model">
		<include refid="select_sql"></include>
		<where>
			<if test=' this.has("id") '> and id = #{id} </if>
			<if test=' this.has("title") '> and name like concat('%', #{title}, '%') </if>
			<if test=' this.has("type") '> and type = #{type} </if>
			<if test=' this.has("show") '> and show = #{show} </if>
			<if test=' this.has("auth") '> and auth = #{auth} </if>
			<if test=' this.has("parentId") '> and parent_id = #{parentId} </if>
		</where>
		order by sort, aid
	</select>
	
	<!-- 查集合 - 根据角色id -->
	<select id="getListByRoleId" resultMap="model">
		<include refid="select_sql"></include>
		where id in (
			select permission_code from sp_role_permission where role_id = #{roleId}
		)
		or `show` = 'yes'
		order by sort, aid
	</select>
	
	
	
	
	
	
	
	
	

</mapper>
