<?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.foldthevalues.mapper.FoldTheValuesMapper">
  <resultMap id="BaseResultMap" type="com.steerinfo.ems.foldthevalues.model.FoldTheValues">
    <id column="ID" jdbcType="VARCHAR" property="id" />
    <result column="MATERIALSOURCE" jdbcType="VARCHAR" property="materialsource" />
    <result column="CLOCK" jdbcType="VARCHAR" property="clock" />
    <result column="CODE" jdbcType="VARCHAR" property="code" />
    <result column="DESCRIPTION" jdbcType="VARCHAR" property="description" />
    <result column="ISAVAILABLE" jdbcType="VARCHAR" property="isavailable" />
    <result column="REVISOR" jdbcType="VARCHAR" property="revisor" />
    <result column="REVISETIME" jdbcType="VARCHAR" property="revisetime" />
    <result column="UNITID" jdbcType="VARCHAR" property="unitid" />
    <result column="BZ" jdbcType="VARCHAR" property="bz" />
    <result column="MATERIALNO" jdbcType="VARCHAR" property="materialno"/>
    <result column="TIMEGRANID" jdbcType="VARCHAR" property="timegranid"/>
    <result column="ESTIMATEDVALUE" jdbcType="VARCHAR" property="estimatedvalue"/>
  </resultMap>
  <sql id="columns">
    ID, MATERIALSOURCE, CLOCK, CODE, DESCRIPTION, ISAVAILABLE, REVISOR, REVISETIME,
    UNITID, BZ,MATERIALNO,TIMEGRANID,ESTIMATEDVALUE
  </sql>
  <sql id="columns_alias">
    t.ID, t.MATERIALSOURCE, t.CLOCK, t.CODE, t.DESCRIPTION, t.ISAVAILABLE, t.REVISOR,
    t.REVISETIME, t.UNITID, t.BZ,t.MATERIALNO
  </sql>
  <sql id="select">
    SELECT <include refid="columns"/> FROM FOLD_THE_VALUES
  </sql>
  <sql id="select_alias">
    SELECT <include refid="columns_alias"/> FROM FOLD_THE_VALUES t
  </sql>
  <sql id="where">
    <where>
      <if test="id != null and id != ''">
        and ID = #{id}
      </if>
      <if test="materialsource != null and materialsource != ''">
        and MATERIALSOURCE = #{materialsource}
      </if>
      <if test="clock != null and clock != ''">
        and CLOCK = #{clock}
      </if>
      <if test="code != null and code != ''">
        and CODE = #{code}
      </if>
      <if test="description != null and description != ''">
        and DESCRIPTION = #{description}
      </if>
      <if test="isavailable != null and isavailable != ''">
        and ISAVAILABLE = #{isavailable}
      </if>
      <if test="revisor != null and revisor != ''">
        and REVISOR = #{revisor}
      </if>
      <if test="revisetime != null and revisetime != ''">
        and REVISETIME = #{revisetime}
      </if>
      <if test="unitid != null and unitid != ''">
        and UNITID = #{unitid}
      </if>
      <if test="bz != null and bz != ''">
        and BZ = #{bz}
      </if>
    </where>
  </sql>
  <sql id="whereLike">
    <where>
      <if test="id != null and id != ''">
        and ID LIKE '%${id}%'
      </if>
      <if test="materialsource != null and materialsource != ''">
        and MATERIALSOURCE LIKE '%${materialsource}%'
      </if>
      <if test="clock != null and clock != ''">
        and CLOCK LIKE '%${clock}%'
      </if>
      <if test="code != null and code != ''">
        and CODE LIKE '%${code}%'
      </if>
      <if test="description != null and description != ''">
        and DESCRIPTION LIKE '%${description}%'
      </if>
      <if test="isavailable != null and isavailable != ''">
        and ISAVAILABLE LIKE '%${isavailable}%'
      </if>
      <if test="revisor != null and revisor != ''">
        and REVISOR LIKE '%${revisor}%'
      </if>
      <if test="revisetime != null and revisetime != ''">
        and REVISETIME LIKE '%${revisetime}%'
      </if>
      <if test="unitid != null and unitid != ''">
        and UNITID LIKE '%${unitid}%'
      </if>
      <if test="bz != null and bz != ''">
        and BZ LIKE '%${bz}%'
      </if>
    </where>
  </sql>
  <delete id="deleteByPrimaryKey" parameterType="java.lang.String">
    delete from FOLD_THE_VALUES
    where ID = #{id,jdbcType=VARCHAR}
  </delete>
  <delete id="deleteBySelectiveElement" parameterType="java.util.HashMap">
    delete from FOLD_THE_VALUES
    where 1!=1
      <if test="materialsource != null and materialsource != ''">
        or MATERIALSOURCE = #{materialsource}
      </if>
      <if test="clock != null and clock != ''">
        or CLOCK = #{clock}
      </if>
      <if test="code != null and code != ''">
        or CODE = #{code}
      </if>
      <if test="description != null and description != ''">
        or DESCRIPTION = #{description}
      </if>
      <if test="isavailable != null and isavailable != ''">
        or ISAVAILABLE = #{isavailable}
      </if>
      <if test="revisor != null and revisor != ''">
        or REVISOR = #{revisor}
      </if>
      <if test="revisetime != null and revisetime != ''">
        or REVISETIME = #{revisetime}
      </if>
      <if test="unitid != null and unitid != ''">
        or UNITID = #{unitid}
      </if>
      <if test="bz != null and bz != ''">
        or BZ = #{bz}
      </if>
  </delete>
  <insert id="insert" parameterType="com.steerinfo.ems.foldthevalues.model.FoldTheValues">
    insert into FOLD_THE_VALUES (ID, MATERIALSOURCE, CLOCK,
      CODE, DESCRIPTION, ISAVAILABLE,
      REVISOR, REVISETIME,MATERIALNO,
      UNITID, BZ,TIMEGRANID,ESTIMATEDVALUE)
    values (#{id,jdbcType=VARCHAR}, #{materialsource,jdbcType=VARCHAR}, #{clock,jdbcType=VARCHAR},
      #{code,jdbcType=VARCHAR}, #{description,jdbcType=VARCHAR}, #{isavailable,jdbcType=VARCHAR},
      #{revisor,jdbcType=VARCHAR}, #{revisetime,jdbcType=VARCHAR},#{materialno,jdbcType=VARCHAR},
      #{unitid,jdbcType=VARCHAR}, #{bz,jdbcType=VARCHAR},#{timegranid,jdbcType=VARCHAR},#{estimatedvalue,jdbcType=VARCHAR})
  </insert>
  <insert id="insertSelective" parameterType="com.steerinfo.ems.foldthevalues.model.FoldTheValues">
    insert into FOLD_THE_VALUES
    <trim prefix="(" suffix=")" suffixOverrides=",">
      <if test="id != null">
        ID,
      </if>
      <if test="materialsource != null">
        MATERIALSOURCE,
      </if>
      <if test="clock != null">
        CLOCK,
      </if>
      <if test="code != null">
        CODE,
      </if>
      <if test="description != null">
        DESCRIPTION,
      </if>
      <if test="isavailable != null">
        ISAVAILABLE,
      </if>
      <if test="revisor != null">
        REVISOR,
      </if>
      <if test="revisetime != null">
        REVISETIME,
      </if>
      <if test="unitid != null">
        UNITID,
      </if>
      <if test="bz != null">
        BZ,
      </if>
    </trim>
    <trim prefix="values (" suffix=")" suffixOverrides=",">
      <if test="id != null">
        #{id,jdbcType=VARCHAR},
      </if>
      <if test="materialsource != null">
        #{materialsource,jdbcType=VARCHAR},
      </if>
      <if test="clock != null">
        #{clock,jdbcType=VARCHAR},
      </if>
      <if test="code != null">
        #{code,jdbcType=VARCHAR},
      </if>
      <if test="description != null">
        #{description,jdbcType=VARCHAR},
      </if>
      <if test="isavailable != null">
        #{isavailable,jdbcType=VARCHAR},
      </if>
      <if test="revisor != null">
        #{revisor,jdbcType=VARCHAR},
      </if>
      <if test="revisetime != null">
        #{revisetime,jdbcType=VARCHAR},
      </if>
      <if test="unitid != null">
        #{unitid,jdbcType=VARCHAR},
      </if>
      <if test="bz != null">
        #{bz,jdbcType=VARCHAR},
      </if>
    </trim>
  </insert>
  <update id="updateByPrimaryKey" parameterType="com.steerinfo.ems.foldthevalues.model.FoldTheValues">
    update FOLD_THE_VALUES
    set
      CLOCK = #{clock,jdbcType=VARCHAR},
      CODE = #{code,jdbcType=VARCHAR},
      DESCRIPTION = #{description,jdbcType=VARCHAR},
      ISAVAILABLE = #{isavailable,jdbcType=VARCHAR},
      REVISOR = #{revisor,jdbcType=VARCHAR},
      REVISETIME = #{revisetime,jdbcType=VARCHAR},
      UNITID = #{unitid,jdbcType=VARCHAR},
      BZ = #{bz,jdbcType=VARCHAR},
      TIMEGRANID = #{timegranid,jdbcType=VARCHAR},
      ESTIMATEDVALUE = #{estimatedvalue,jdbcType=VARCHAR}
    where ID = #{id,jdbcType=VARCHAR}
  </update>
  <update id="updateByPrimaryKeySelective" parameterType="com.steerinfo.ems.foldthevalues.model.FoldTheValues">
    update FOLD_THE_VALUES
    <set>
      <if test="materialsource != null">
        MATERIALSOURCE = #{materialsource,jdbcType=VARCHAR},
      </if>
      <if test="clock != null">
        CLOCK = #{clock,jdbcType=VARCHAR},
      </if>
      <if test="code != null">
        CODE = #{code,jdbcType=VARCHAR},
      </if>
      <if test="description != null">
        DESCRIPTION = #{description,jdbcType=VARCHAR},
      </if>
      <if test="isavailable != null">
        ISAVAILABLE = #{isavailable,jdbcType=VARCHAR},
      </if>
      <if test="revisor != null">
        REVISOR = #{revisor,jdbcType=VARCHAR},
      </if>
      <if test="revisetime != null">
        REVISETIME = #{revisetime,jdbcType=VARCHAR},
      </if>
      <if test="unitid != null">
        UNITID = #{unitid,jdbcType=VARCHAR},
      </if>
      <if test="bz != null">
        BZ = #{bz,jdbcType=VARCHAR},
      </if>
    </set>
    where ID = #{id,jdbcType=VARCHAR}
  </update>
  <select id="selectByPrimaryKey" parameterType="java.lang.String" resultMap="BaseResultMap">
    <include refid="select"/>
    where ID = #{id,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 FOLD_THE_VALUES
      (ID,
      MATERIALSOURCE, CLOCK, CODE,
      DESCRIPTION, ISAVAILABLE, REVISOR,
      REVISETIME, UNITID,
      BZ)
    ( <foreach collection="list" item="item" separator="union all">
   select
      #{item.id,jdbcType=VARCHAR},
      #{item.materialsource,jdbcType=VARCHAR}, #{item.clock,jdbcType=VARCHAR}, #{item.code,jdbcType=VARCHAR},
      #{item.description,jdbcType=VARCHAR}, #{item.isavailable,jdbcType=VARCHAR}, #{item.revisor,jdbcType=VARCHAR},
      #{item.revisetime,jdbcType=VARCHAR}, #{item.unitid,jdbcType=VARCHAR},
      #{item.bz,jdbcType=VARCHAR} from dual
   </foreach> )
  </insert>
  <update id="batchUpdate" parameterType="java.util.List">
     update FOLD_THE_VALUES
     set
       ID=
       <foreach collection="list" item="item" index="index" separator=" " open="case ID" close="end">
          when #{item.id,jdbcType=VARCHAR} then #{item.id,jdbcType=VARCHAR}
       </foreach>
       ,MATERIALSOURCE=
       <foreach collection="list" item="item" index="index" separator=" " open="case ID" close="end">
          when #{item.id,jdbcType=VARCHAR} then #{item.materialsource,jdbcType=VARCHAR}
       </foreach>
       ,CLOCK=
       <foreach collection="list" item="item" index="index" separator=" " open="case ID" close="end">
          when #{item.id,jdbcType=VARCHAR} then #{item.clock,jdbcType=VARCHAR}
       </foreach>
       ,CODE=
       <foreach collection="list" item="item" index="index" separator=" " open="case ID" close="end">
          when #{item.id,jdbcType=VARCHAR} then #{item.code,jdbcType=VARCHAR}
       </foreach>
       ,DESCRIPTION=
       <foreach collection="list" item="item" index="index" separator=" " open="case ID" close="end">
          when #{item.id,jdbcType=VARCHAR} then #{item.description,jdbcType=VARCHAR}
       </foreach>
       ,ISAVAILABLE=
       <foreach collection="list" item="item" index="index" separator=" " open="case ID" close="end">
          when #{item.id,jdbcType=VARCHAR} then #{item.isavailable,jdbcType=VARCHAR}
       </foreach>
       ,REVISOR=
       <foreach collection="list" item="item" index="index" separator=" " open="case ID" close="end">
          when #{item.id,jdbcType=VARCHAR} then #{item.revisor,jdbcType=VARCHAR}
       </foreach>
       ,REVISETIME=
       <foreach collection="list" item="item" index="index" separator=" " open="case ID" close="end">
          when #{item.id,jdbcType=VARCHAR} then #{item.revisetime,jdbcType=VARCHAR}
       </foreach>
       ,UNITID=
       <foreach collection="list" item="item" index="index" separator=" " open="case ID" close="end">
          when #{item.id,jdbcType=VARCHAR} then #{item.unitid,jdbcType=VARCHAR}
       </foreach>
       ,BZ=
       <foreach collection="list" item="item" index="index" separator=" " open="case ID" close="end">
          when #{item.id,jdbcType=VARCHAR} then #{item.bz,jdbcType=VARCHAR}
       </foreach>
     where ID in
     <foreach collection="list" index="index" item="item" separator="," open="(" close=")">
    #{item.id,jdbcType=VARCHAR}
     </foreach>
  </update>
  <delete id="batchDelete" parameterType="java.util.List">
    delete from FOLD_THE_VALUES
    where ID in
    <foreach collection="list" item="id" open="(" close=")" separator=",">
      #{id}
    </foreach>
  </delete>
  <!-- 友情提示！！！-->
  <!-- 请将自己写的代码放在此标签之下，方便以后粘贴复制。-->
  <select id="getDateForPage" parameterType="java.util.HashMap" resultType="Map">
    select t.*, t1.id as energyid, t1.name as energyname
    from Fold_the_values t
    left join t_rm_energy t1
    on t.materialno = t1.id
    <where>
     <if test = "startTime != null and startTime != '' and endTime != null and endTime != '' " >
      and t.clock between #{startTime} and #{endTime}
      </if >
      <if test = "energyid != null and energyid != '' " >
      and t.materialno in  (${energyid})
      </if >
      <if test="timegranid != null and timegranid != ''">
      and t.timegranid = #{timegranid}
      </if>
    </where>
    order by t.clock desc
  </select>
  <!---->
  <select id="getNewData" resultMap="BaseResultMap">
  select t.batchno as id,
          '三电厂' as materialsource,
          to_char(to_date(t.monitordate, 'yyyy-mm-dd hh24:mi:ss'),'yyyy-mm-dd') as clock,
          t.monitoringvalue as estimatedvalue,
          (t.MONITORINGVALUE * 0.03412 / 100) as code,
          t.powersystemname as materialno
     from t_cm_0348 t
    where t.SITEM = 'BFHYD_05'
      and t.powersystemname in ('P00006', 'P00007', 'P00008')
      and t.powermonitorname in ('007', '008', '009')
      and t.batchno not in (select distinct ID from FOLD_THE_VALUES)
  </select>
  <!--获取最近最新的数据-->
  <select id="getNewOne" parameterType="java.lang.String" resultMap="BaseResultMap">
        select *
      from (select *
              from FOLD_THE_VALUES
              <where>
                <if test="materialno != null and materialno !=''">
                  materialno = #{materialno}
                </if>
              </where>
             order by CLOCK desc)
     where rownum = 1
  </select>
  <!--查询出昨天没有折标值的数据-->
  <select id="getNozbzData" parameterType="java.lang.String"  resultMap="BaseResultMap">
    select distinct t.materialno
      from FOLD_THE_VALUES t
     where t.materialno not in
           (select materialno from FOLD_THE_VALUES
            <where>
              <if test="clock != null and clock != ''">
                clock = #{clock}
              </if>
            </where>
            )
  </select>
  <update id="updateByClockAndMaterialno" parameterType="com.steerinfo.ems.foldthevalues.model.FoldTheValues">
    update FOLD_THE_VALUES
    set
      CLOCK = #{clock,jdbcType=VARCHAR},
      CODE = #{code,jdbcType=VARCHAR},
      ISAVAILABLE = #{isavailable,jdbcType=VARCHAR},
      UNITID = #{unitid,jdbcType=VARCHAR},
      TIMEGRANID = #{timegranid,jdbcType=VARCHAR},
      MATERIALNO = #{materialno,jdbcType=VARCHAR},
      ESTIMATEDVALUE = #{estimatedvalue,jdbcType=VARCHAR},
      ID = #{id,jdbcType=VARCHAR}
      <where>
        <if test="clock != null and clock != ''">
         and CLOCK = #{clock,jdbcType=VARCHAR}
        </if>
        <if test="materialno != null and materialno != ''">
          and MATERIALNO = #{materialno,jdbcType=VARCHAR}
        </if>
      </where>
  </update>
  <select id="getByClockAndMaterialno" parameterType="com.steerinfo.ems.foldthevalues.model.FoldTheValues" resultMap="BaseResultMap">
    select * from FOLD_THE_VALUES
    <where>
      <if test="clock != null and clock != ''">
        and CLOCK = #{clock,jdbcType=VARCHAR}
      </if>
      <if test="materialno != null and materialno != ''">
        and MATERIALNO = #{materialno,jdbcType=VARCHAR}
      </if>
    </where>
  </select>
  <!--获取月指标数据-->
  <select id="getDataForMoth" parameterType="java.util.HashMap" resultMap="BaseResultMap">
       select sum(t.ESTIMATEDVALUE) as estimatedvalue, t.MATERIALSOURCE,t.MATERIALNO
         from FOLD_THE_VALUES t
        <where>
          <if test="startTime != null and startTime != '' and endTime != null and endTime !=''">
            T.clock between #{startTime} AND
            #{endTime}
          </if>
        </where>
        group by t.MATERIALNO,MATERIALSOURCE
  </select>
  <!--获取物料日折标数据-->
  <select id="getDateForwl" parameterType="java.util.HashMap" resultMap="BaseResultMap">
select t2.*
  from (select  to_char(t.id, 'yyyymmdd') || t.MATERIALNO as id,
               t.code as ESTIMATEDVALUE,
                to_char(ROUND(((29727 + (10 - t.code) * 334) / 29307.6), 3),
                '0.999') as code,
               t.MATERIALNO,
               MATERIALSOURCE,
               to_char(t.id, 'yyyy-mm-dd') as clock
          from (select Dates as id,
                       code,
                       MATERIALNO,
                       '机焦厂' as MATERIALSOURCE
                  from EMS_SCQK_JJ unpivot(code for MATERIALNO in(A as
                                                                  'X010'))
                union all
                select Dates as id,
                       code,
                       MATERIALNO,
                       '机焦厂' as MATERIALSOURCE
                  from EMS_SCQK_JJ unpivot(code for MATERIALNO in(JM_HF as
                                                                  'X005'))
                union all
                select Dates as id,
                       code,
                       MATERIALNO,
                       '机焦厂' as MATERIALSOURCE
                  from EMS_SCQK_JJ unpivot(code for MATERIALNO in(A as
                                                                  'X018'))) t
               <where>
               <if test="clock != null and clock != ''">
                 and  t.id = to_date(#{clock}, 'yyyy-mm-dd')
               </if>
              </where>) t2
            where t2.id not in
             (select distinct t.id
            from FOLD_THE_VALUES t
            where
            t.materialno in ('X005', 'X018', 'X010'))
  </select>
  <select id="getavgDate" parameterType="java.util.HashMap" resultType="String">
    select  listagg(t.ESTIMATEDVALUE,',') as avg  from
    (select distinct t.estimatedvalue
          from FOLD_THE_VALUES t
          <where>
            <if test="startTime != null and startTime != '' and endTime != null and endTime !=''">
              and t.clock between #{startTime} and #{endTime}
            </if>
            <if test="materialno != null and materialno != ''">
              and t.materialno = #{materialno}
            </if>
          </where>) t
  </select>

  <update id="updateByClock" parameterType="com.steerinfo.ems.foldthevalues.model.FoldTheValues">
    UPDATE FOLD_THE_VALUES
    SET
    CODE = #{code,jdbcType=VARCHAR},
    ESTIMATEDVALUE = #{estimatedvalue,jdbcType=VARCHAR}
    <where>
      <if test="clock != null and clock != ''">
        and CLOCK > #{clock,jdbcType=VARCHAR}
      </if>
      <if test="materialno != null and materialno != ''">
        and MATERIALNO = #{materialno,jdbcType=VARCHAR}
      </if>
    </where>
  </update>
</mapper>
