<?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.ElectricClientInfoDAO">
    <resultMap id="ElectricClientInfoMap" type="com.jpsoft.shinestar.modules.base.entity.ElectricClientInfo">
        <id property="id" column="id_"/>
        <result property="customerNo" column="customer_no"/>
        <result property="roomId" column="room_id"/>
        <result property="roomName" column="room_name"/>
        <result property="meterId" column="meter_id"/>
        <result property="meterName" column="meter_name"/>
        <result property="electricityTotal" column="electricity_total"/>
        <result property="amountTotal" column="amount_total"/>
        <result property="buyNum" column="buy_num"/>
        <result property="electricityRemaining" column="electricity_remaining"/>
        <result property="amountRemaining" column="amount_remaining"/>
        <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="rechargeTimes" column="recharge_times"/>
        <result property="active" column="active_"/>
        <result property="warningValue" column="warning_value"/>
    </resultMap>
    <insert id="insert" parameterType="com.jpsoft.shinestar.modules.base.entity.ElectricClientInfo">
        <!--
        <selectKey resultType="java.lang.String" order="BEFORE" keyProperty="id">
            select sys_guid() from dual
        </selectKey>
        -->
        <![CDATA[
		insert into base_electric_client_info
	    (id_,customer_no,room_id,meter_id,electricity_total,amount_total,buy_num,
	    electricity_remaining,amount_remaining,create_by,create_time,update_by,
	    update_time,del_flag,recharge_times,active_,warning_value)
		values
		(
			#{id,jdbcType=VARCHAR}
			,#{customerNo,jdbcType=VARCHAR}
			,#{roomId,jdbcType=VARCHAR}
			,#{meterId,jdbcType=VARCHAR}
			,#{electricityTotal,jdbcType= NUMERIC }
			,#{amountTotal,jdbcType= NUMERIC }
			,#{buyNum,jdbcType= NUMERIC }
			,#{electricityRemaining,jdbcType= NUMERIC }
			,#{amountRemaining,jdbcType= NUMERIC }
			,#{createBy,jdbcType=VARCHAR}
			,#{createTime,jdbcType= TIMESTAMP }
			,#{updateBy,jdbcType=VARCHAR}
			,#{updateTime,jdbcType= TIMESTAMP }
			,#{delFlag,jdbcType= NUMERIC }
			,#{rechargeTimes,jdbcType= NUMERIC }
			,#{active,jdbcType= NUMERIC }
			,#{warningValue,jdbcType= NUMERIC }
		)
	]]>
    </insert>
    <delete id="delete" parameterType="string">
        delete from base_electric_client_info where id_=#{id,jdbcType=VARCHAR}
    </delete>
    <update id="update" parameterType="com.jpsoft.shinestar.modules.base.entity.ElectricClientInfo">
        update base_electric_client_info
        <set>
            <if test="customerNo!=null">
                customer_no=#{customerNo,jdbcType=VARCHAR},
            </if>
            <if test="roomId!=null">
                room_id=#{roomId,jdbcType=VARCHAR},
            </if>
            <if test="meterId!=null">
                meter_id=#{meterId,jdbcType=VARCHAR},
            </if>
            <if test="electricityTotal!=null">
                electricity_total=#{electricityTotal,jdbcType= NUMERIC },
            </if>
            <if test="amountTotal!=null">
                amount_total=#{amountTotal,jdbcType= NUMERIC },
            </if>
            <if test="buyNum!=null">
                buy_num=#{buyNum,jdbcType= NUMERIC },
            </if>
            <if test="electricityRemaining!=null">
                electricity_remaining=#{electricityRemaining,jdbcType= NUMERIC },
            </if>
            <if test="amountRemaining!=null">
                amount_remaining=#{amountRemaining,jdbcType= NUMERIC },
            </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="rechargeTimes!=null">
                recharge_times=#{rechargeTimes,jdbcType= NUMERIC },
            </if>
            <if test="active!=null">
                active_=#{active,jdbcType= NUMERIC },
            </if>
            <if test="warningValue!=null">
                warning_value=#{warningValue,jdbcType= NUMERIC },
            </if>
        </set>
        where id_=#{id}
    </update>
    <select id="get" parameterType="string" resultMap="ElectricClientInfoMap">
        select a.* from base_electric_client_info a where id_=#{0} and del_flag = 0
    </select>
    <select id="exist" parameterType="string" resultType="int">
        select count(*) from base_electric_client_info where id_=#{0} and del_flag = 0
    </select>
    <select id="list" resultMap="ElectricClientInfoMap">
        select * from base_electric_client_info where del_flag = 0
    </select>
    <select id="search" parameterType="hashmap" resultMap="ElectricClientInfoMap">
        <![CDATA[
			select a.*,
			b.name_ as room_name,
			b.use_type as room_use_type,
			c.name_ as meter_name
			from
			base_electric_client_info a
			left join base_room_info b on a.room_id = b.id_
			left join base_electric_meter_info c on a.meter_id = c.id_
		]]>
        <where>
            a.del_flag = 0
            <if test="searchParams.id != null">
                and a.ID_ like #{searchParams.id}
            </if>
            <if test="searchParams.customerNo != null">
                and a.customer_no like #{searchParams.customerNo}
            </if>
            <if test="searchParams.meterName != null">
                and c.name_ like #{searchParams.meterName}
            </if>
            <if test="searchParams.roomsIds != null and searchParams.roomsIds.size() > 0">
                and a.room_id in
                <foreach item="rId" index="index" collection="searchParams.roomsIds" open="("  close=")" separator=",">
                    #{rId}
                </foreach>
            </if>
        </where>
        order by b.name_ asc, b.sort_no asc,a.create_time desc
    </select>
    <select id="findOneByRoomId" resultMap="ElectricClientInfoMap">
        select * from base_electric_client_info where room_id=#{roomId} and del_flag=0 limit 1
    </select>
    <select id="getRoomMessage" parameterType="string" resultMap="ElectricClientInfoMap">
        select a.*,
        b.name_ as room_name,
        b.use_type as room_use_type,
        c.name_ as meter_name
        from
        base_electric_client_info a
        left join base_room_info b on a.room_id = b.id_
        left join base_electric_meter_info c on a.meter_id = c.id_ where a.id_=#{0} and a.del_flag = 0
    </select>

    <select id="getCountByCNo" parameterType="string" resultType="int">
			select count(a.id_)
			from
			base_electric_client_info a
			where
			a.customer_no = #{customerNo}
            and a.del_flag = 0
			<if test="excludeId != null">
                and a.Id_ != #{excludeId}
            </if>
    </select>
</mapper>