<?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.steerinfo.ems.zghbjkbzlog.mapper.ZghbjkbzLogMapper">
  <resultMap id="BaseResultMap" type="com.steerinfo.ems.zghbjkbzlog.model.ZghbjkbzLog">
    <id column="ID" jdbcType="VARCHAR" property="id" />
    <result column="ZID" jdbcType="VARCHAR" property="zid" />
    <result column="TIM" jdbcType="TIMESTAMP" property="tim" />
    <result column="MIN_VALUE" jdbcType="DECIMAL" property="minValue" />
    <result column="MAX_VALUE" jdbcType="DECIMAL" property="maxValue" />
    <result column="METER_VALUE" jdbcType="DECIMAL" property="meterValue" />
    <result column="INTERVALS" jdbcType="DECIMAL" property="intervals" />
    <result column="STATE" jdbcType="VARCHAR" property="state" />
  </resultMap>

  <delete id="deleteByPrimaryKey" parameterType="java.lang.String">
    delete from ZGHBJKBZ_LOG
    where id = #{id,jdbcType=VARCHAR}
  </delete>

  <select id="selectByPrimaryKey" parameterType="java.lang.String" resultMap="BaseResultMap">
    select ID, ZID, MIN_VALUE, MAX_VALUE, METER_VALUE, INTERVALS, STATE, TIM
    from ZGHBJKBZ_LOG
    where id = #{id,jdbcType=VARCHAR}
  </select>
  <select id="selectByParameters" parameterType="java.util.HashMap" resultType="Map">
    select t.id, t.zid, m.wr_type, m.drain_name, m.source_name,m.location, m.check_ele, t.min_value, t.max_value, t.meter_value, t.state, t.intervals, t.tim  
    from ZGHBJKBZ_LOG t left join ZGHBJKBZ m on t.zid = m.id
    <where>
      <if test="wrType != null and wrType != ''">
        ${wrType}
      </if>
    </where>
    <where>
      <if test="sourceName != null and sourceName != ''">
        ${sourceName}
      </if>
    </where>
    <where>
      <if test="drainName != null and drainName != ''">
        ${drainName}
      </if>
    </where>
    <where>
      <if test="location != null and location != ''">
        ${location}
      </if>
    </where>
    <where>
      <if test="time != null and time != ''">
        ${time}
      </if>
    </where>
    order by t.TIM
  </select>

  <insert id="insert" parameterType="com.steerinfo.ems.zghbjkbzlog.model.ZghbjkbzLog">
    insert into ZGHBJKBZ_LOG (ID, ZID, MIN_VALUE, MAX_VALUE, METER_VALUE, INTERVALS, STATE)
    values (#{id,jdbcType=VARCHAR}, #{zid,jdbcType=VARCHAR},
      #{minValue,jdbcType=DECIMAL}, #{maxValue,jdbcType=DECIMAL}, #{meterValue,jdbcType=DECIMAL}, 
      #{intervals,jdbcType=DECIMAL}, #{state,jdbcType=VARCHAR})
  </insert>

  <update id="updateByPrimaryKeySelective" parameterType="com.steerinfo.ems.zghbjkbzlog.model.ZghbjkbzLog">
    update ZGHBJKBZ_LOG
    <set>
      <if test="state != null">
        STATE = #{state,jdbcType=VARCHAR},
      </if>
      <if test="meterValue != null">
        METER_VALUE = #{meterValue,jdbcType=DECIMAL},
      </if>
      TIM = SYSDATE
    </set>
    where id = #{id,jdbcType=VARCHAR}
  </update>
  
  <select id="getHourZghbjkbzLog" parameterType="java.lang.String" resultMap="BaseResultMap">
  	select ID, ZID, MIN_VALUE, MAX_VALUE, METER_VALUE, INTERVALS, STATE, TIM from
	(select t.*,rownum r from ZGHBJKBZ_LOG t where 
	t.zid = #{zid,jdbcType=VARCHAR} and tim > sysdate - 1/24 order by tim desc) where r=1
  </select>
  
  <select id="createZghbjkbzLog" parameterType="java.util.HashMap" resultMap="BaseResultMap">
  	select '' as ID,id ZID,${cname} as METER_VALUE,t.MIN_VALUE,t.MAX_VALUE from ZGHBJKBZ t, EMS_HBSJ e
	where (${cname} &gt; max_value or ${cname} &lt; min_value) and ${cname} is not null and t.id = #{zid,jdbcType=VARCHAR}
  </select>
  
  
</mapper>