<?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.ElectricMeterInfoDAO">
	<resultMap id="ElectricMeterInfoMap" type="ElectricMeterInfo">
		<id property="id" column="id_" />
			<result property="meterNo" column="meter_no" />
			<result property="unitNo" column="unit_no" />
			<result property="name" column="name_" />
			<result property="terminalId" column="terminal_id" />
			<result property="terminalName" column="terminal_name" />
			<result property="terminalRemark" column="terminal_remark" />
			<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" />
			</resultMap>
	<insert id="insert" parameterType="ElectricMeterInfo">
	<!--
	<selectKey resultType="java.lang.String" order="BEFORE" keyProperty="id">
		select sys_guid() from dual
	</selectKey>
	-->
	<![CDATA[
		insert into base_electric_meter_info
	    (id_,meter_no,unit_no,name_,terminal_id,create_by,create_time,update_by,update_time,del_flag)
		values
		(
#{id,jdbcType=VARCHAR}
,#{meterNo,jdbcType=VARCHAR}
,#{unitNo,jdbcType=VARCHAR}
,#{name,jdbcType=VARCHAR}
,#{terminalId,jdbcType=VARCHAR}
,#{createBy,jdbcType=VARCHAR}
,#{createTime,jdbcType= TIMESTAMP }
,#{updateBy,jdbcType=VARCHAR}
,#{updateTime,jdbcType= TIMESTAMP }
,#{delFlag,jdbcType= NUMERIC }
		)
	]]>
	</insert>
	<delete id="delete" parameterType="string">
		delete from base_electric_meter_info where id_=#{id,jdbcType=VARCHAR}
	</delete>
	<update id="update" parameterType="ElectricMeterInfo">
		update base_electric_meter_info
		<set>
				<if test="meterNo!=null">
		meter_no=#{meterNo,jdbcType=VARCHAR},
		</if>
				<if test="unitNo!=null">
		unit_no=#{unitNo,jdbcType=VARCHAR},
		</if>
				<if test="name!=null">
		name_=#{name,jdbcType=VARCHAR},
		</if>
				<if test="terminalId!=null">
		terminal_id=#{terminalId,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>
		</set>
	where id_=#{id}
	</update>
	<select id="get" parameterType="string" resultMap="ElectricMeterInfoMap">
		select a.* from base_electric_meter_info a where id_=#{0} and del_flag = 0
	</select>
	<select id="exist" parameterType="string" resultType="int">
		select count(*) from base_electric_meter_info where id_=#{0} and del_flag = 0
	</select>
	<select id="list" resultMap="ElectricMeterInfoMap">
		select * from base_electric_meter_info where del_flag = 0
	</select>
	<select id="search" parameterType="hashmap" resultMap="ElectricMeterInfoMap">
		<![CDATA[
			select a.*,b.name_ as terminal_name,b.remark_ as terminal_remark
			from base_electric_meter_info a
			left join base_terminal_info b on a.terminal_id = b.id_
		]]>
		<where>
			a.del_flag = 0
			<if test="searchParams.id != null">
				and a.ID_ like #{searchParams.id}
			</if>
			<if test="searchParams.excludeId != null">
				<![CDATA[
				and a.ID_ <> #{searchParams.excludeId}
				]]>
			</if>
			<if test="searchParams.meterNo != null">
				and a.meter_no = #{searchParams.meterNo}
			</if>
			<if test="searchParams.terminalId != null">
				and a.terminal_id = #{searchParams.terminalId}
			</if>
		</where>
		<foreach item="sort" collection="sortList"  open="order by" separator=",">
	        ${sort.name} ${sort.order}
	 	</foreach>
	</select>

	<select id="getCountByMNoAndUNo" parameterType="string" resultType="int">
		select count(*)
		from
		base_electric_meter_info
        where
			meter_no = #{meterNo}
			and unit_no = #{unitNo}
			and del_flag = 0
			<if test="excludeId != null">
				and id_ != #{excludeId}
			</if>
	</select>
</mapper>