<?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.chinaitop.depot.intelligent.grainsituation.mapper.TQtdevinfoValueMapper" >
  <resultMap id="BaseResultMap" type="com.chinaitop.depot.intelligent.grainsituation.model.TQtdevinfoValue" >
    <result column="id" property="id" jdbcType="VARCHAR" />
    <result column="org_id" property="orgId" jdbcType="VARCHAR" />
    <result column="v_cf_code" property="vCfCode" jdbcType="VARCHAR" />
    <result column="v_dev_code" property="vDevCode" jdbcType="VARCHAR" />
    <result column="v_value_time" property="vValueTime" jdbcType="TIMESTAMP" />
    <result column="cpo_avg" property="cpoAvg" jdbcType="DECIMAL" />
    <result column="v_dev_kind_code" property="vDevKindCode" jdbcType="VARCHAR" />
    <result column="data_source" property="dataSource" jdbcType="VARCHAR" />
  </resultMap>
  <sql id="Example_Where_Clause" >
    <where >
      <foreach collection="oredCriteria" item="criteria" separator="or" >
        <if test="criteria.valid" >
          <trim prefix="(" suffix=")" prefixOverrides="and" >
            <foreach collection="criteria.criteria" item="criterion" >
              <choose >
                <when test="criterion.noValue" >
                  and ${criterion.condition}
                </when>
                <when test="criterion.singleValue" >
                  and ${criterion.condition} #{criterion.value}
                </when>
                <when test="criterion.betweenValue" >
                  and ${criterion.condition} #{criterion.value} and #{criterion.secondValue}
                </when>
                <when test="criterion.listValue" >
                  and ${criterion.condition}
                  <foreach collection="criterion.value" item="listItem" open="(" close=")" separator="," >
                    #{listItem}
                  </foreach>
                </when>
              </choose>
            </foreach>
          </trim>
        </if>
      </foreach>
    </where>
  </sql>
  <sql id="Update_By_Example_Where_Clause" >
    <where >
      <foreach collection="example.oredCriteria" item="criteria" separator="or" >
        <if test="criteria.valid" >
          <trim prefix="(" suffix=")" prefixOverrides="and" >
            <foreach collection="criteria.criteria" item="criterion" >
              <choose >
                <when test="criterion.noValue" >
                  and ${criterion.condition}
                </when>
                <when test="criterion.singleValue" >
                  and ${criterion.condition} #{criterion.value}
                </when>
                <when test="criterion.betweenValue" >
                  and ${criterion.condition} #{criterion.value} and #{criterion.secondValue}
                </when>
                <when test="criterion.listValue" >
                  and ${criterion.condition}
                  <foreach collection="criterion.value" item="listItem" open="(" close=")" separator="," >
                    #{listItem}
                  </foreach>
                </when>
              </choose>
            </foreach>
          </trim>
        </if>
      </foreach>
    </where>
  </sql>
  <sql id="Base_Column_List" >
    id, org_id, v_cf_code, v_dev_code, v_value_time, cpo_avg, v_dev_kind_code, data_source
  </sql>
  <select id="selectByExample" resultMap="BaseResultMap" parameterType="com.chinaitop.depot.intelligent.grainsituation.model.TQtdevinfoValueExample" >
    select
    <if test="distinct" >
      distinct
    </if>
    <include refid="Base_Column_List" />
    from t_qtdevinfo_value
    <if test="_parameter != null" >
      <include refid="Example_Where_Clause" />
    </if>
    <if test="orderByClause != null" >
      order by ${orderByClause}
    </if>
  </select>
  <delete id="deleteByExample" parameterType="com.chinaitop.depot.intelligent.grainsituation.model.TQtdevinfoValueExample" >
    delete from t_qtdevinfo_value
    <if test="_parameter != null" >
      <include refid="Example_Where_Clause" />
    </if>
  </delete>
  <insert id="insert" parameterType="com.chinaitop.depot.intelligent.grainsituation.model.TQtdevinfoValue" >
    insert into t_qtdevinfo_value (id, org_id, v_cf_code, 
      v_dev_code, v_value_time, cpo_avg, 
      v_dev_kind_code, data_source)
    values (#{id,jdbcType=VARCHAR}, #{orgId,jdbcType=VARCHAR}, #{vCfCode,jdbcType=VARCHAR}, 
      #{vDevCode,jdbcType=VARCHAR}, #{vValueTime,jdbcType=TIMESTAMP}, #{cpoAvg,jdbcType=DECIMAL}, 
      #{vDevKindCode,jdbcType=VARCHAR}, #{dataSource,jdbcType=VARCHAR})
  </insert>
  <insert id="insertSelective" parameterType="com.chinaitop.depot.intelligent.grainsituation.model.TQtdevinfoValue" >
    insert into t_qtdevinfo_value
    <trim prefix="(" suffix=")" suffixOverrides="," >
      <if test="id != null" >
        id,
      </if>
      <if test="orgId != null" >
        org_id,
      </if>
      <if test="vCfCode != null" >
        v_cf_code,
      </if>
      <if test="vDevCode != null" >
        v_dev_code,
      </if>
      <if test="vValueTime != null" >
        v_value_time,
      </if>
      <if test="cpoAvg != null" >
        cpo_avg,
      </if>
      <if test="vDevKindCode != null" >
        v_dev_kind_code,
      </if>
      <if test="dataSource != null" >
        data_source,
      </if>
    </trim>
    <trim prefix="values (" suffix=")" suffixOverrides="," >
      <if test="id != null" >
        #{id,jdbcType=VARCHAR},
      </if>
      <if test="orgId != null" >
        #{orgId,jdbcType=VARCHAR},
      </if>
      <if test="vCfCode != null" >
        #{vCfCode,jdbcType=VARCHAR},
      </if>
      <if test="vDevCode != null" >
        #{vDevCode,jdbcType=VARCHAR},
      </if>
      <if test="vValueTime != null" >
        #{vValueTime,jdbcType=TIMESTAMP},
      </if>
      <if test="cpoAvg != null" >
        #{cpoAvg,jdbcType=DECIMAL},
      </if>
      <if test="vDevKindCode != null" >
        #{vDevKindCode,jdbcType=VARCHAR},
      </if>
      <if test="dataSource != null" >
        #{dataSource,jdbcType=VARCHAR},
      </if>
    </trim>
  </insert>
  <select id="countByExample" parameterType="com.chinaitop.depot.intelligent.grainsituation.model.TQtdevinfoValueExample" resultType="java.lang.Integer" >
    select count(*) from t_qtdevinfo_value
    <if test="_parameter != null" >
      <include refid="Example_Where_Clause" />
    </if>
  </select>
  <update id="updateByExampleSelective" parameterType="map" >
    update t_qtdevinfo_value
    <set >
      <if test="record.id != null" >
        id = #{record.id,jdbcType=VARCHAR},
      </if>
      <if test="record.orgId != null" >
        org_id = #{record.orgId,jdbcType=VARCHAR},
      </if>
      <if test="record.vCfCode != null" >
        v_cf_code = #{record.vCfCode,jdbcType=VARCHAR},
      </if>
      <if test="record.vDevCode != null" >
        v_dev_code = #{record.vDevCode,jdbcType=VARCHAR},
      </if>
      <if test="record.vValueTime != null" >
        v_value_time = #{record.vValueTime,jdbcType=TIMESTAMP},
      </if>
      <if test="record.cpoAvg != null" >
        cpo_avg = #{record.cpoAvg,jdbcType=DECIMAL},
      </if>
      <if test="record.vDevKindCode != null" >
        v_dev_kind_code = #{record.vDevKindCode,jdbcType=VARCHAR},
      </if>
      <if test="record.dataSource != null" >
        data_source = #{record.dataSource,jdbcType=VARCHAR},
      </if>
    </set>
    <if test="_parameter != null" >
      <include refid="Update_By_Example_Where_Clause" />
    </if>
  </update>
  <update id="updateByExample" parameterType="map" >
    update t_qtdevinfo_value
    set id = #{record.id,jdbcType=VARCHAR},
      org_id = #{record.orgId,jdbcType=VARCHAR},
      v_cf_code = #{record.vCfCode,jdbcType=VARCHAR},
      v_dev_code = #{record.vDevCode,jdbcType=VARCHAR},
      v_value_time = #{record.vValueTime,jdbcType=TIMESTAMP},
      cpo_avg = #{record.cpoAvg,jdbcType=DECIMAL},
      v_dev_kind_code = #{record.vDevKindCode,jdbcType=VARCHAR},
      data_source = #{record.dataSource,jdbcType=VARCHAR}
    <if test="_parameter != null" >
      <include refid="Update_By_Example_Where_Clause" />
    </if>
  </update>

  <insert id="insertBatch" parameterType="java.util.List">
    insert into t_qtdevinfo_value (id, org_id, v_cf_code,
    v_dev_code, v_value_time, cpo_avg,
    v_dev_kind_code, data_source)
    values
    <foreach collection="list" item="qt" index="index" separator=",">
      (#{qt.id,jdbcType=VARCHAR}, #{qt.orgId,jdbcType=VARCHAR}, #{qt.vCfCode,jdbcType=VARCHAR},
      #{qt.vDevCode,jdbcType=VARCHAR}, #{qt.vValueTime,jdbcType=TIMESTAMP}, #{qt.cpoAvg,jdbcType=DECIMAL},
      #{qt.vDevKindCode,jdbcType=VARCHAR}, #{qt.dataSource,jdbcType=VARCHAR})
    </foreach>
  </insert>

  <select id="getByCfCdoe" resultType="java.util.Map" parameterType="java.util.Map">
    SELECT
    id,
    v_cf_code AS "vCfCode",
    v_value_time AS "vValueTime",
    MAX(
    CASE v_dev_kind_code
    WHEN "6964" THEN
    cpo_avg
    ELSE
    0
    END
    ) AS "co2avg",
    MAX(
    CASE v_dev_kind_code
    WHEN "6965" THEN
    cpo_avg
    ELSE
    0
    END
    ) AS "ph3avg",
    MAX(
    CASE v_dev_kind_code
    WHEN "6962" THEN
    cpo_avg
    ELSE
    0
    END
    ) AS "o2avg"
    FROM
    t_qtdevinfo_value
    <where>
      <if test="vCfCode != null">
        v_cf_code = #{vCfCode}
      </if>
      <if test="searchStartDate != null and test=searchEndDate != null">
        AND v_value_time BETWEEN #{searchStartDate} AND #{searchEndDate}
      </if>
      <if test="id != null">
        AND id IN ${id}
      </if>
    </where>
    GROUP BY
    v_cf_code,
    v_value_time
    ORDER BY v_value_time DESC
  </select>

  <select id="getByCfCdoes" parameterType="java.util.Map" resultType="java.util.Map">
    SELECT
    a.vCfCode,
    a.vValueTime,
    MAX(
    CASE a.v_dev_kind_code
    WHEN "6964" THEN
    a.cpo_avg
    ELSE
    0
    END
    ) AS "co2avg",
    MAX(
    CASE a.v_dev_kind_code
    WHEN "6965" THEN
    a.cpo_avg
    ELSE
    0
    END
    ) AS "ph3avg",
    MAX(
    CASE a.v_dev_kind_code
    WHEN "6962" THEN
    a.cpo_avg
    ELSE
    0
    END
    ) AS "o2avg"
    FROM
    (
    SELECT
    v_cf_code AS "vCfCode",
    v_value_time AS "vValueTime",
    v_dev_kind_code,
    cpo_avg
    FROM
    t_qtdevinfo_value
    ORDER BY
    vValueTime DESC
    ) a
    WHERE 1=1
    <if test="orgId != null">
      AND a.orgId = #{orgId}
    </if>
    GROUP BY
    a.v_value_time DESC
  </select>

  <select id="getByCfCdoeGroup" parameterType="java.lang.String" resultType="java.util.Map">
    SELECT
    *
    FROM
    (
    SELECT
    id,
    v_cf_code AS "vCfCode",
    v_value_time AS "vValueTime",
    MAX(
    CASE v_dev_kind_code
    WHEN "6964" THEN
    cpo_avg
    ELSE
    0
    END
    ) AS "co2avg",
    MAX(
    CASE v_dev_kind_code
    WHEN "6965" THEN
    cpo_avg
    ELSE
    0
    END
    ) AS "ph3avg",
    MAX(
    CASE v_dev_kind_code
    WHEN "6962" THEN
    cpo_avg
    ELSE
    0
    END
    ) AS "o2avg"
    FROM
    t_qtdevinfo_value
    GROUP BY
    v_cf_code,
    v_value_time
    ORDER BY
    v_value_time DESC
    ) a
    WHERE 1 = 1
    <if test="vCfCode != null and vCfCode!= ''">
      AND a.vCfCode = #{vCfCode}
    </if>
    GROUP BY
    a.vCfCode
    ORDER BY
    a.vValueTime DESC
  </select>
</mapper>