<?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.emscalpointvalue.mapper.EmsCalpointValueMapper">
  <resultMap id="BaseResultMap" type="com.steerinfo.ems.emscalpointvalue.model.EmsCalpointValue">
    <id column="CLOCK" jdbcType="VARCHAR" property="clock" />
    <id column="ITEMID" jdbcType="VARCHAR" property="itemid" />
    <result column="MINTVALUE" jdbcType="DECIMAL" property="mintvalue" />
    <result column="MENDVALUE" jdbcType="DECIMAL" property="mendvalue" />
    <result column="APPORTVALUE" jdbcType="DECIMAL" property="apportvalue" />
    <result column="OPER_USER" jdbcType="VARCHAR" property="operUser" />
    <result column="OPER_DATE" jdbcType="TIMESTAMP" property="operDate" />
    <result column="BZ" jdbcType="VARCHAR" property="bz" />
  </resultMap>
  <sql id="columns">
    CLOCK, ITEMID, MINTVALUE, MENDVALUE, APPORTVALUE, OPER_USER, OPER_DATE, BZ
  </sql>
  <sql id="columns_alias">
    t.CLOCK, t.ITEMID, t.MINTVALUE, t.MENDVALUE, t.APPORTVALUE, t.OPER_USER, t.OPER_DATE, 
    t.BZ
  </sql>
  <sql id="select">
    SELECT <include refid="columns"/> FROM EMS_CALPOINT_VALUE
  </sql>
  <sql id="select_alias">
    SELECT <include refid="columns_alias"/> FROM EMS_CALPOINT_VALUE t
  </sql>
  <sql id="where">
    <where> 
      <if test="itemid != null and itemid != ''">
        and ITEMID = #{itemid}
      </if>
      <if test="mintvalue != null">
        and MINTVALUE = #{mintvalue}
      </if>
      <if test="mendvalue != null">
        and MENDVALUE = #{mendvalue}
      </if>
      <if test="apportvalue != null">
        and APPORTVALUE = #{apportvalue}
      </if>
      <if test="operUser != null and operUser != ''">
        and OPER_USER = #{operUser}
      </if>
      <if test="operDate != null">
        and TO_CHAR(OPER_DATE,'yyyy-MM-dd') = #{operDate}
      </if>
      <if test="bz != null and bz != ''">
        and BZ = #{bz}
      </if>
    </where>
  </sql>
  <sql id="whereLike">
    <where> 
      <if test="itemid != null and itemid != ''">
        and ITEMID LIKE '%${itemid}%'
      </if>
      <if test="mintvalue != null">
        and MINTVALUE = #{mintvalue}
      </if>
      <if test="mendvalue != null">
        and MENDVALUE = #{mendvalue}
      </if>
      <if test="apportvalue != null">
        and APPORTVALUE = #{apportvalue}
      </if>
      <if test="operUser != null and operUser != ''">
        and OPER_USER LIKE '%${operUser}%'
      </if>
      <if test="operDate != null">
        and TO_CHAR(OPER_DATE,'yyyy-MM-dd') = #{operDate}
      </if>
      <if test="bz != null and bz != ''">
        and BZ LIKE '%${bz}%'
      </if>
    </where>
  </sql>
  <delete id="deleteByPrimaryKey" parameterType="map">
    delete from EMS_CALPOINT_VALUE
    where CLOCK = #{clock,jdbcType=VARCHAR}
      and ITEMID = #{itemid,jdbcType=VARCHAR}
  </delete>
  <delete id="deleteBySelectiveElement" parameterType="java.util.HashMap">
    delete from EMS_CALPOINT_VALUE
    where 1!=1 
      <if test="itemid != null and itemid != ''">
        or ITEMID = #{itemid}
      </if>
      <if test="mintvalue != null">
        or MINTVALUE = #{mintvalue}
      </if>
      <if test="mendvalue != null">
        or MENDVALUE = #{mendvalue}
      </if>
      <if test="apportvalue != null">
        or APPORTVALUE = #{apportvalue}
      </if>
      <if test="operUser != null and operUser != ''">
        or OPER_USER = #{operUser}
      </if>
      <if test="operDate != null">
        or TO_CHAR(OPER_DATE,'yyyy-MM-dd') = '#{operDate}'
      </if>
      <if test="bz != null and bz != ''">
        or BZ = #{bz}
      </if>
  </delete>
  <insert id="insert" parameterType="com.steerinfo.ems.emscalpointvalue.model.EmsCalpointValue">
    insert into EMS_CALPOINT_VALUE (CLOCK, ITEMID, MINTVALUE, 
      MENDVALUE, APPORTVALUE, OPER_USER, 
      OPER_DATE, BZ)
    values (#{clock,jdbcType=VARCHAR}, #{itemid,jdbcType=VARCHAR}, #{mintvalue,jdbcType=DECIMAL}, 
      #{mendvalue,jdbcType=DECIMAL}, #{apportvalue,jdbcType=DECIMAL}, #{operUser,jdbcType=VARCHAR}, 
      #{operDate,jdbcType=TIMESTAMP}, #{bz,jdbcType=VARCHAR})
  </insert>
  <insert id="insertSelective" parameterType="com.steerinfo.ems.emscalpointvalue.model.EmsCalpointValue">
    insert into EMS_CALPOINT_VALUE
    <trim prefix="(" suffix=")" suffixOverrides=",">
      <if test="clock != null">
        CLOCK,
      </if>
      <if test="itemid != null">
        ITEMID,
      </if>
      <if test="mintvalue != null">
        MINTVALUE,
      </if>
      <if test="mendvalue != null">
        MENDVALUE,
      </if>
      <if test="apportvalue != null">
        APPORTVALUE,
      </if>
      <if test="operUser != null">
        OPER_USER,
      </if>
      <if test="operDate != null">
        OPER_DATE,
      </if>
      <if test="bz != null">
        BZ,
      </if>
    </trim>
    <trim prefix="values (" suffix=")" suffixOverrides=",">
      <if test="clock != null">
        #{clock,jdbcType=VARCHAR},
      </if>
      <if test="itemid != null">
        #{itemid,jdbcType=VARCHAR},
      </if>
      <if test="mintvalue != null">
        #{mintvalue,jdbcType=DECIMAL},
      </if>
      <if test="mendvalue != null">
        #{mendvalue,jdbcType=DECIMAL},
      </if>
      <if test="apportvalue != null">
        #{apportvalue,jdbcType=DECIMAL},
      </if>
      <if test="operUser != null">
        #{operUser,jdbcType=VARCHAR},
      </if>
      <if test="operDate != null">
        #{operDate,jdbcType=TIMESTAMP},
      </if>
      <if test="bz != null">
        #{bz,jdbcType=VARCHAR},
      </if>
    </trim>
  </insert>
  <update id="updateByPrimaryKey" parameterType="com.steerinfo.ems.emscalpointvalue.model.EmsCalpointValue">
    update EMS_CALPOINT_VALUE
    set MINTVALUE = #{mintvalue,jdbcType=DECIMAL},
      MENDVALUE = #{mendvalue,jdbcType=DECIMAL},
      APPORTVALUE = #{apportvalue,jdbcType=DECIMAL},
      OPER_USER = #{operUser,jdbcType=VARCHAR},
      OPER_DATE = #{operDate,jdbcType=TIMESTAMP},
      BZ = #{bz,jdbcType=VARCHAR}
    where CLOCK = #{clock,jdbcType=VARCHAR}
      and ITEMID = #{itemid,jdbcType=VARCHAR}
  </update>
  <update id="updateByPrimaryKeySelective" parameterType="com.steerinfo.ems.emscalpointvalue.model.EmsCalpointValue">
    update EMS_CALPOINT_VALUE
    <set>
      <if test="mintvalue != null">
        MINTVALUE = #{mintvalue,jdbcType=DECIMAL},
      </if>
      <if test="mendvalue != null">
        MENDVALUE = #{mendvalue,jdbcType=DECIMAL},
      </if>
      <if test="apportvalue != null">
        APPORTVALUE = #{apportvalue,jdbcType=DECIMAL},
      </if>
      <if test="operUser != null">
        OPER_USER = #{operUser,jdbcType=VARCHAR},
      </if>
      <if test="operDate != null">
        OPER_DATE = #{operDate,jdbcType=TIMESTAMP},
      </if>
      <if test="bz != null">
        BZ = #{bz,jdbcType=VARCHAR},
      </if>
    </set>
    where CLOCK = #{clock,jdbcType=VARCHAR}
      and ITEMID = #{itemid,jdbcType=VARCHAR}
  </update>
  <select id="selectByPrimaryKey" parameterType="map" resultMap="BaseResultMap">
    <include refid="select"/>
    where CLOCK = #{clock,jdbcType=VARCHAR}
      and ITEMID = #{itemid,jdbcType=VARCHAR}
  </select>
  <select id="selectByParameters" parameterType="java.util.HashMap" resultMap="BaseResultMap">
    <include refid="select"/>
    <include refid="where"/>
  </select>
  <select id="selectLikeByParameters" parameterType="java.util.HashMap" resultMap="BaseResultMap">
    <include refid="select"/>
    <include refid="whereLike"/>
  </select>
  <insert id="batchInsert" parameterType="java.util.List">
    insert into EMS_CALPOINT_VALUE 
      (CLOCK, 
      ITEMID, MINTVALUE, MENDVALUE, 
      APPORTVALUE, OPER_USER, OPER_DATE, 
      BZ)
    ( <foreach collection="list" item="item" separator="union all"> 
   select  
      #{item.clock,jdbcType=VARCHAR}, 
      #{item.itemid,jdbcType=VARCHAR}, #{item.mintvalue,jdbcType=DECIMAL}, #{item.mendvalue,jdbcType=DECIMAL}, 
      #{item.apportvalue,jdbcType=DECIMAL}, #{item.operUser,jdbcType=VARCHAR}, #{item.operDate,jdbcType=TIMESTAMP}, 
      #{item.bz,jdbcType=VARCHAR} from dual  
   </foreach> )
  </insert>
  <update id="batchUpdate" parameterType="java.util.List">
     update EMS_CALPOINT_VALUE
     set
       CLOCK=
       <foreach collection="list" item="item" index="index" separator=" " open="case CLOCK" close="end">
          when #{item.clock,jdbcType=VARCHAR} then #{item.clock,jdbcType=VARCHAR}
       </foreach>
       ,ITEMID=
       <foreach collection="list" item="item" index="index" separator=" " open="case CLOCK" close="end">
          when #{item.clock,jdbcType=VARCHAR} then #{item.itemid,jdbcType=VARCHAR}
       </foreach>
       ,MINTVALUE=
       <foreach collection="list" item="item" index="index" separator=" " open="case CLOCK" close="end">
          when #{item.clock,jdbcType=VARCHAR} then #{item.mintvalue,jdbcType=DECIMAL}
       </foreach>
       ,MENDVALUE=
       <foreach collection="list" item="item" index="index" separator=" " open="case CLOCK" close="end">
          when #{item.clock,jdbcType=VARCHAR} then #{item.mendvalue,jdbcType=DECIMAL}
       </foreach>
       ,APPORTVALUE=
       <foreach collection="list" item="item" index="index" separator=" " open="case CLOCK" close="end">
          when #{item.clock,jdbcType=VARCHAR} then #{item.apportvalue,jdbcType=DECIMAL}
       </foreach>
       ,OPER_USER=
       <foreach collection="list" item="item" index="index" separator=" " open="case CLOCK" close="end">
          when #{item.clock,jdbcType=VARCHAR} then #{item.operUser,jdbcType=VARCHAR}
       </foreach>
       ,OPER_DATE=
       <foreach collection="list" item="item" index="index" separator=" " open="case CLOCK" close="end">
          when #{item.clock,jdbcType=VARCHAR} then #{item.operDate,jdbcType=TIMESTAMP}
       </foreach>
       ,BZ=
       <foreach collection="list" item="item" index="index" separator=" " open="case CLOCK" close="end">
          when #{item.clock,jdbcType=VARCHAR} then #{item.bz,jdbcType=VARCHAR}
       </foreach>
     where CLOCK in 
     <foreach collection="list" index="index" item="item" separator="," open="(" close=")">
    #{item.clock,jdbcType=VARCHAR}
     </foreach> 
  </update>
  <delete id="batchDelete" parameterType="java.util.List">
    delete from EMS_CALPOINT_VALUE
    where CLOCK in 
    <foreach collection="list" item="id" open="(" close=")" separator=",">
      #{id}
    </foreach>
  </delete>
  <!-- 友情提示！！！-->
  <!-- 请将自己写的代码放在此标签之下，方便以后粘贴复制。-->

  <!-- 查询人工录入计量点数据，分页显示 -->
  <select id="getDateEdit" parameterType="java.util.HashMap" resultType = "Map">
    select clock,c.itemid,c.itemname,c.itemdesc,c.locateid,c.energyid,c.tag_modulus,mintvalue,mendvalue,apportvalue,bz,
    case when clock = #{clockt} then 1 else 0 end ed,oper_user,to_char(oper_date,'yyyy-mm-dd hh24:mi:ss') oper_date,
    c.unitid,c.mintimegranid as timegranid,to_number(REGEXP_REPLACE(bdesc2,'[^0-9]','')) as xh
    from EMS_CALPOINT_VALUE t inner join T_RM_CALPOINT c on t.itemid=c.itemid
    where c.mintimegranid = 'DAY' and c.itemid in (${itemid})
    <if test="itemid2 != null and itemid2 != ''">
      and c.itemid = #{itemid2}
    </if>
    <if test="clock != null and clock != ''">
        and CLOCK &gt;= #{clock}
    </if>
    <if test="clocke != null and clocke != ''">
        and CLOCK &lt;= #{clocke}
    </if>
    <if test="itemname != null and itemname != ''">
      and c.ITEMNAME LIKE '%${itemname}%'
    </if>
    <if test="itemdesc != null and itemdesc != ''">
      and c.itemdesc LIKE '%${itemdesc}%'
    </if>
    <if test="locateid != null and locateid != ''">
      ${locateid}
    </if>
    union all
    select #{clockt} clock,itemid,itemname,itemdesc,locateid,energyid,tag_modulus,
    nvl((select mendvalue from EMS_CALPOINT_VALUE where itemid =t.itemid and clock =
    (select max(clock) from EMS_CALPOINT_VALUE where itemid = t.itemid and clock &lt; #{clockt})), 0) mintvalue,0 mendvalue,
    0 apportvalue,'' bz,1 ed,'' oper_user,'' oper_date,unitid,mintimegranid as timegranid,to_number(REGEXP_REPLACE(bdesc2,'[^0-9]','')) as xh
    from T_RM_CALPOINT t 
    where itemid in (${itemid}) and mintimegranid = 'DAY'
    <if test="itemid2 != null and itemid2 != ''">
      and itemid = #{itemid2}
    </if>
    and itemid not in 
    (select itemid from EMS_CALPOINT_VALUE where clock = #{clockt})
    <if test="clock != null and clock != ''">
        and #{clockt} &gt;= #{clock}
    </if>
    <if test="clocke != null and clocke != ''">
        and #{clockt} &lt;= #{clocke}
    </if>
    <if test="itemname != null and itemname != ''">
      and ITEMNAME LIKE '%${itemname}%'
    </if>
    <if test="itemdesc != null and itemdesc != ''">
      and itemdesc LIKE '%${itemdesc}%'
    </if>
    <if test="locateid != null and locateid != ''">
      ${locateid}
    </if>
    union all
    select clock,c.itemid,c.itemname,c.itemdesc,c.locateid,c.energyid,c.tag_modulus,mintvalue,mendvalue,apportvalue,bz,
    case when clock = #{clocktm} then 1 else 0 end ed,oper_user,to_char(oper_date,'yyyy-mm-dd hh24:mi:ss') oper_date,
    c.unitid,c.mintimegranid as timegranid,to_number(REGEXP_REPLACE(bdesc2,'[^0-9]','')) as xh
    from EMS_CALPOINT_VALUE t inner join T_RM_CALPOINT c on t.itemid=c.itemid
    where c.mintimegranid = 'MONTH' and c.itemid in (${itemid})
    <if test="itemid2 != null and itemid2 != ''">
      and c.itemid = #{itemid2}
    </if>
    <if test="clock != null and clock != ''">
        and CLOCK &gt;= substr(#{clock}, 0, 7)
    </if>
    <if test="clocke != null and clocke != ''">
        and CLOCK &lt;= substr(#{clocke}, 0, 7)
    </if>
    <if test="itemname != null and itemname != ''">
      and c.ITEMNAME LIKE '%${itemname}%'
    </if>
    <if test="itemdesc != null and itemdesc != ''">
      and c.itemdesc LIKE '%${itemdesc}%'
    </if>
    <if test="locateid != null and locateid != ''">
      ${locateid}
    </if>
    union all
    select #{clocktm} clock,itemid,itemname,itemdesc,locateid,energyid,tag_modulus,
    nvl((select mendvalue from EMS_CALPOINT_VALUE where itemid =t.itemid and clock =
    (select max(clock) from EMS_CALPOINT_VALUE where itemid = t.itemid and clock &lt; #{clocktm})), 0) mintvalue,0 mendvalue,
    0 apportvalue,'' bz,1 ed,'' oper_user,'' oper_date,unitid,mintimegranid as timegranid,to_number(REGEXP_REPLACE(bdesc2,'[^0-9]','')) as xh
    from T_RM_CALPOINT t 
    where itemid in (${itemid}) and mintimegranid = 'MONTH'
    <if test="itemid2 != null and itemid2 != ''">
      and itemid = #{itemid2}
    </if>
    and itemid not in 
    (select itemid from EMS_CALPOINT_VALUE where clock = #{clocktm})
    <if test="clock != null and clock != ''">
        and #{clocktm} &gt;= substr(#{clock}, 0, 7)
    </if>
    <if test="clocke != null and clocke != ''">
        and #{clocktm} &lt;= substr(#{clocke}, 0, 7)
    </if>
    <if test="itemname != null and itemname != ''">
      and ITEMNAME LIKE '%${itemname}%'
    </if>
    <if test="itemdesc != null and itemdesc != ''">
      and itemdesc LIKE '%${itemdesc}%'
    </if>
    <if test="locateid != null and locateid != ''">
      ${locateid}
    </if>
    order by clock desc,xh
  </select>
</mapper>