<?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.base.dao.RoomInfoDAO">
	<resultMap id="RoomInfoMap" type="RoomInfo">
		<id property="id" column="id_" />
		<result property="name" column="name_" />
		<result property="number" column="number_" />
		<result property="type" column="type_" />
		<result property="useType" column="use_type" />
		<result property="sortNo" column="sort_no" />
		<result property="parentId" column="parent_id" />
		<result property="parentName" column="parent_name"/>
		<result property="createBy" column="create_by" />
		<result property="createTime" column="create_time" />
		<result property="updateBy" column="update_by" />
		<result property="updateTime" column="update_time" />
		<result property="delFlag" column="del_flag" />
		<result property="hasChildren" column="has_children" />
		<association property="useTypeN" column="use_type" select="com.jpsoft.shinestar.modules.sys.dao.DataDictionaryDAO.getName"/>
		<!--<association property="children" column="id_" select="com.jpsoft.smartt.modules.base.dao.RoomInfoDAO.getChildren"/>-->
	</resultMap>
	<insert id="insert" parameterType="RoomInfo">
	<!--
	<selectKey resultType="java.lang.String" order="BEFORE" keyProperty="id">
		select sys_guid() from dual
	</selectKey>
	-->
	<![CDATA[
		insert into base_room_info
	    (id_,name_,number_,type_,sort_no,parent_id,create_by,create_time,update_by,update_time,del_flag,use_type)
		values
		(
			#{id,jdbcType=VARCHAR}
			,#{name,jdbcType=VARCHAR}
			,#{number,jdbcType= NUMERIC }
			,#{type,jdbcType=VARCHAR}
			,#{sortNo,jdbcType= NUMERIC }
			,#{parentId,jdbcType=VARCHAR}
			,#{createBy,jdbcType=VARCHAR}
			,#{createTime,jdbcType= TIMESTAMP }
			,#{updateBy,jdbcType=VARCHAR}
			,#{updateTime,jdbcType= TIMESTAMP }
			,#{delFlag,jdbcType= NUMERIC }
			,#{useType,jdbcType= VARCHAR }
		)
	]]>
	</insert>
	<delete id="delete" parameterType="string">
		delete from base_room_info where id_=#{id,jdbcType=VARCHAR}
	</delete>
	<update id="update" parameterType="RoomInfo">
		update base_room_info
		<set>
				<if test="name!=null">
		name_=#{name,jdbcType=VARCHAR},
		</if>
				<if test="number!=null">
		number_=#{number,jdbcType= NUMERIC },
		</if>
				<if test="type!=null">
		type_=#{type,jdbcType=VARCHAR},
		</if>
				<if test="sortNo!=null">
		sort_no=#{sortNo,jdbcType= NUMERIC },
		</if>
				<if test="parentId!=null">
		parent_id=#{parentId,jdbcType=VARCHAR},
		</if>
				<if test="createBy!=null">
		create_by=#{createBy,jdbcType=VARCHAR},
		</if>
				<if test="createTime!=null">
		create_time=#{createTime,jdbcType= TIMESTAMP },
		</if>
				<if test="updateBy!=null">
		update_by=#{updateBy,jdbcType=VARCHAR},
		</if>
				<if test="updateTime!=null">
		update_time=#{updateTime,jdbcType= TIMESTAMP },
		</if>
				<if test="delFlag!=null">
		del_flag=#{delFlag,jdbcType= NUMERIC },
		</if>
			<if test="useType!=null">
				use_type=#{useType,jdbcType= NUMERIC },
			</if>
		</set>
	where id_=#{id}
	</update>
	<select id="get" parameterType="string" resultMap="RoomInfoMap">
		select a.*,b.name_ as parent_name
		from base_room_info a left join base_room_info b on a.parent_id = b.id_
		where a.id_=#{0} and a.del_flag = 0
	</select>
	<select id="exist" parameterType="string" resultType="int">
		select count(*) from base_room_info where id_=#{0} and del_flag = 0
	</select>
	<select id="list" resultMap="RoomInfoMap">
		select * from base_room_info where del_flag = 0
	</select>
	<select id="search" parameterType="hashmap" resultMap="RoomInfoMap">
		<![CDATA[
			select a.*,
			b.name_ AS parent_name,
			IF(a.type_ = 3, 'false', 'true' ) AS has_children
			from base_room_info a
			left join base_room_info b on a.parent_id = b.id_
		]]>
		<where>
			a.del_flag = 0
			<if test="searchParams.id != null">
				and a.ID_ like #{searchParams.id}
			</if>
			<if test="searchParams.name != null">
				and a.name_ like #{searchParams.name}
			</if>
			<if test="searchParams.parentId != null">
				and a.parent_id like #{searchParams.parentId}
			</if>
			<if test="searchParams.type != null">
				and a.type_ = #{searchParams.type}
			</if>
			<if test="searchParams.excludeId != null">
				<![CDATA[
				and a.ID_ <> #{searchParams.excludeId}
				]]>
			</if>
		</where>
		<foreach item="sort" collection="sortList"  open="order by" separator=",">
	        ${sort.name} ${sort.order}
	 	</foreach>
	</select>

	<select id="getChildren" resultMap="RoomInfoMap">
		select * from base_room_info where parent_id=#{0} and del_flag = 0 order by sort_no asc
	</select>
	<select id="findByTypeAndParentId" resultMap="RoomInfoMap">
		select * from base_room_info
		<where>
			del_flag = 0
			<if test="parentId!=null">
				and parent_id=#{parentId}
			</if>
			<if test="type!=null">
				and type_=#{type}
			</if>
		</where>
		order by sort_no asc
	</select>
	<select id="findIdByTypeAndParentIds" resultType="string">
		select id_ from base_room_info
		<where>
			del_flag = 0
			<if test="type!=null">
				and type_=#{type}
			</if>
			<if test="parentIds != null and parentIds.size() > 0">
				and parent_id in
				<foreach item="pId" index="index" collection="parentIds" open="("  close=")" separator=",">
					#{pId}
				</foreach>
			</if>
		</where>
		order by sort_no asc
	</select>
</mapper>