<?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.supervise.mapper.TDepotStyleMapper">
    <resultMap id="BaseResultMap" type="com.chinaitop.depot.intelligent.supervise.model.TDepotStyle">
        <id column="id" property="id" jdbcType="INTEGER"/>
        <result column="org_id" property="orgId" jdbcType="INTEGER"/>
        <result column="t_type" property="type" jdbcType="VARCHAR"/>
        <result column="indexid" property="indexid" jdbcType="INTEGER"/>
        <result column="styles" property="styles" jdbcType="VARCHAR"/>
    </resultMap>
    <resultMap id="ResultMapWithBLOBs" type="com.chinaitop.depot.intelligent.supervise.model.TDepotStyle"
               extends="BaseResultMap">
        <result column="imgdata" property="imgdata" jdbcType="LONGVARBINARY"/>
    </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, t_type, indexid, styles
  </sql>
    <sql id="Blob_Column_List">
    imgdata
  </sql>
    <select id="selectByExampleWithBLOBs" resultMap="ResultMapWithBLOBs"
            parameterType="com.chinaitop.depot.intelligent.supervise.model.TDepotStyleExample">
        select
        <if test="distinct">
            distinct
        </if>
        <include refid="Base_Column_List"/>
        ,
        <include refid="Blob_Column_List"/>
        from t_depot_style
        <if test="_parameter != null">
            <include refid="Example_Where_Clause"/>
        </if>
        <if test="orderByClause != null">
            order by ${orderByClause}
        </if>
    </select>
    <select id="selectByExample" resultMap="BaseResultMap"
            parameterType="com.chinaitop.depot.intelligent.supervise.model.TDepotStyleExample">
        select
        <if test="distinct">
            distinct
        </if>
        <include refid="Base_Column_List"/>
        from t_depot_style
        <if test="_parameter != null">
            <include refid="Example_Where_Clause"/>
        </if>
        <if test="orderByClause != null">
            order by ${orderByClause}
        </if>
    </select>
    <select id="selectByPrimaryKey" resultMap="ResultMapWithBLOBs" parameterType="java.lang.Integer">
        select
        <include refid="Base_Column_List"/>
        ,
        <include refid="Blob_Column_List"/>
        from t_depot_style
        where id = #{id,jdbcType=INTEGER}
    </select>
    <delete id="deleteByPrimaryKey" parameterType="java.lang.Integer">
    delete from t_depot_style
    where id = #{id,jdbcType=INTEGER}
  </delete>
    <delete id="deleteByExample" parameterType="com.chinaitop.depot.intelligent.supervise.model.TDepotStyleExample">
        delete from t_depot_style
        <if test="_parameter != null">
            <include refid="Example_Where_Clause"/>
        </if>
    </delete>
    <insert id="insert" parameterType="com.chinaitop.depot.intelligent.supervise.model.TDepotStyle">
    insert into t_depot_style (id, org_id, t_type,
      indexid, styles, imgdata
      )
    values (SEQ_T_DEPOT_STYLE.nextVal, #{orgId,jdbcType=INTEGER}, #{type,jdbcType=VARCHAR},
      #{indexid,jdbcType=INTEGER}, #{styles,jdbcType=VARCHAR}, #{imgdata,jdbcType=LONGVARBINARY}
      )
  </insert>
    <insert id="insertSelective" parameterType="com.chinaitop.depot.intelligent.supervise.model.TDepotStyle">
        insert into t_depot_style
        <trim prefix="(" suffix=")" suffixOverrides=",">
                id,
            <if test="orgId != null">
                org_id,
            </if>
            <if test="type != null">
                t_type,
            </if>
            <if test="indexid != null">
                indexid,
            </if>
            <if test="styles != null">
                styles,
            </if>
            <if test="imgdata != null">
                imgdata,
            </if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            SEQ_T_DEPOT_STYLE.nextVal,
            <if test="orgId != null">
                #{orgId,jdbcType=INTEGER},
            </if>
            <if test="type != null">
                #{type,jdbcType=VARCHAR},
            </if>
            <if test="indexid != null">
                #{indexid,jdbcType=INTEGER},
            </if>
            <if test="styles != null">
                #{styles,jdbcType=VARCHAR},
            </if>
            <if test="imgdata != null">
                #{imgdata,jdbcType=LONGVARBINARY},
            </if>
        </trim>
    </insert>
    <select id="countByExample" parameterType="com.chinaitop.depot.intelligent.supervise.model.TDepotStyleExample"
            resultType="java.lang.Integer">
        select count(*) from t_depot_style
        <if test="_parameter != null">
            <include refid="Example_Where_Clause"/>
        </if>
    </select>
    <update id="updateByExampleSelective" parameterType="map">
        update t_depot_style
        <set>
            <if test="record.id != null">
                id = #{record.id,jdbcType=INTEGER},
            </if>
            <if test="record.orgId != null">
                org_id = #{record.orgId,jdbcType=INTEGER},
            </if>
            <if test="record.type != null">
                t_type = #{record.type,jdbcType=VARCHAR},
            </if>
            <if test="record.indexid != null">
                indexid = #{record.indexid,jdbcType=INTEGER},
            </if>
            <if test="record.styles != null">
                styles = #{record.styles,jdbcType=VARCHAR},
            </if>
            <if test="record.imgdata != null">
                imgdata = #{record.imgdata,jdbcType=LONGVARBINARY},
            </if>
        </set>
        <if test="_parameter != null">
            <include refid="Update_By_Example_Where_Clause"/>
        </if>
    </update>
    <update id="updateByExampleWithBLOBs" parameterType="map">
        update t_depot_style
        set id = #{record.id,jdbcType=INTEGER},
        org_id = #{record.orgId,jdbcType=INTEGER},
        t_type = #{record.type,jdbcType=VARCHAR},
        indexid = #{record.indexid,jdbcType=INTEGER},
        styles = #{record.styles,jdbcType=VARCHAR},
        imgdata = #{record.imgdata,jdbcType=LONGVARBINARY}
        <if test="_parameter != null">
            <include refid="Update_By_Example_Where_Clause"/>
        </if>
    </update>
    <update id="updateByExample" parameterType="map">
        update t_depot_style
        set id = #{record.id,jdbcType=INTEGER},
        org_id = #{record.orgId,jdbcType=INTEGER},
        t_type = #{record.type,jdbcType=VARCHAR},
        indexid = #{record.indexid,jdbcType=INTEGER},
        styles = #{record.styles,jdbcType=VARCHAR}
        <if test="_parameter != null">
            <include refid="Update_By_Example_Where_Clause"/>
        </if>
    </update>
    <update id="updateByPrimaryKeySelective"
            parameterType="com.chinaitop.depot.intelligent.supervise.model.TDepotStyle">
        update t_depot_style
        <set>
            <if test="orgId != null">
                org_id = #{orgId,jdbcType=INTEGER},
            </if>
            <if test="type != null">
                t_type = #{type,jdbcType=VARCHAR},
            </if>
            <if test="indexid != null">
                indexid = #{indexid,jdbcType=INTEGER},
            </if>
            <if test="styles != null">
                styles = #{styles,jdbcType=VARCHAR},
            </if>
            <if test="imgdata != null">
                imgdata = #{imgdata,jdbcType=LONGVARBINARY},
            </if>
        </set>
        where id = #{id,jdbcType=INTEGER}
    </update>
    <update id="updateByPrimaryKeyWithBLOBs"
            parameterType="com.chinaitop.depot.intelligent.supervise.model.TDepotStyle">
    update t_depot_style
    set org_id = #{orgId,jdbcType=INTEGER},
      t_type = #{type,jdbcType=VARCHAR},
      indexid = #{indexid,jdbcType=INTEGER},
      styles = #{styles,jdbcType=VARCHAR},
      imgdata = #{imgdata,jdbcType=LONGVARBINARY}
    where id = #{id,jdbcType=INTEGER}
  </update>
    <update id="updateByPrimaryKey" parameterType="com.chinaitop.depot.intelligent.supervise.model.TDepotStyle">
    update t_depot_style
    set org_id = #{orgId,jdbcType=INTEGER},
      t_type = #{type,jdbcType=VARCHAR},
      indexid = #{indexid,jdbcType=INTEGER},
      styles = #{styles,jdbcType=VARCHAR}
    where id = #{id,jdbcType=INTEGER}
  </update>

    <select id="selectByGrainSituaiton" parameterType="java.util.Map" resultType="java.util.HashMap">
   SELECT
        a.*
    FROM
        (
            SELECT
                row_number () over (
                    PARTITION BY house.storehouse_id
                ) rn,
                st.styles,
                st.imgdata,
                hw.input_year TestDate,
                house.storehouse_name barnName,
                house.storehouse_code vCf,
                t3.inTemp InT,
                t3.outTemp OutT,
                t3.inH InH,
                t3.outH OutH,
                t3.t_avg AvgT,
                t3.t_min MinT,
                t3.t_max MaxT,
                t3.storehouse vCfCode,
                t3.t_time time,
                house.storehouse_id houseid,
                house.storehouse_name,
                house.storehouse_type,
                hw.sub_type
            FROM
                basic_storehouse house
            LEFT JOIN t_depot_style st ON st.indexid = house.storehouse_id
            LEFT JOIN storage_foodbasicinfo hw ON house.storehouse_id = hw.house_id
            LEFT JOIN (
                SELECT
                    *
                FROM
                    t_testdata t
                WHERE
                    t.org_id = #{orgId}
                AND (
                    t.storehouse IN (
                        SELECT
                            t.storehouse
                        FROM
                            t_testdata t
                        WHERE
                            org_id = #{orgId}
                        GROUP BY
                            t.storehouse
                    )
                )
                AND t.t_time IN (
                    SELECT
                        max(t.t_time) timemax
                    FROM
                        t_testdata t
                    WHERE
                        org_id = #{orgId}
                    GROUP BY
                        t.storehouse
                )
                ORDER BY
                    t.t_time DESC
            ) t3 ON house.storehouse_code = t3.storehouse
            WHERE
                st.t_type = 2
            AND house.org_id = #{orgId}
        ) a
    WHERE
        a.rn = 1
  </select>

    <select id="selectByQT" parameterType="java.util.Map" resultType="java.util.HashMap">
   SELECT DISTINCT
	style.styles,
	style.imgdata,
	hw.input_year TestDate,
	house.storehouse_name barnName,
	house.storehouse_code vCf,
	t3.co2avg co2avg,
	t3.ph3avg ph3avg,
	t3.o2avg o2avg,
  t3.vValueTime time,
  t3.vCfCode vCfCode,
	house.storehouse_id house_id,
	house.storehouse_name,
	house.storehouse_type,
	hw.sub_type
FROM
	basic_storehouse house
LEFT JOIN t_depot_style style ON style.indexid = house.storehouse_id
LEFT JOIN storage_foodbasicinfo hw ON house.storehouse_id = hw.house_id
LEFT JOIN (
	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
    WHERE org_id =#{orgId}
    ORDER BY
    vValueTime DESC
    limit 10000000000
    ) a
    GROUP BY
    a.vCfCode
) t3 ON house.storehouse_code = t3.vCfCode
WHERE
	1 = 1
AND style.t_type = 2
GROUP BY
	house.storehouse_id
  </select>

    <select id="selectByInsect" parameterType="java.util.Map" resultType="java.util.HashMap">
    SELECT DISTINCT
	style.styles,
	style.imgdata,
	hw.input_year TestDate,
	house.storehouse_name barnName,
	house.storehouse_code vCf,
	t3.max max,
	t3.min min,
	t3.avg avg,
  t3.vUpdateTime time,
  t3.vCfCode vCfCode,
	house.storehouse_id house_id,
	house.storehouse_name,
	house.storehouse_type,
	hw.sub_type
FROM
	basic_storehouse house
LEFT JOIN t_depot_style style ON style.indexid = house.storehouse_id
LEFT JOIN storage_foodbasicinfo hw ON house.storehouse_id = hw.house_id
LEFT JOIN (
	 SELECT
    a.*
    FROM
    (
    SELECT
    v_cf_code AS vCfCode,
    v_max AS max,
    v_min AS min,
    v_avg AS avg,
    v_update_time AS vUpdateTime
    FROM
    t_ccvalue
     WHERE org_id =#{orgId}
    ORDER BY
    vUpdateTime DESC
    limit 10000000000
    ) a
    GROUP BY
    a.vCfCode
) t3 ON house.storehouse_code = t3.vCfCode
WHERE
	1 = 1
AND style.t_type = 2
GROUP BY
	house.storehouse_id
  </select>


    <select id="selectByWarning" parameterType="java.util.Map" resultType="java.util.HashMap">
select
 *
from
 (
  select
   row_number() over(
    partition by house.storehouse_code
   ) rn,
   st.styles,
   st.imgdata,
   hw.input_year TestDate,
   house.storehouse_name barnName,
   house.storehouse_code vCf,
   t3.wThreshold wThreshold,
   t3.threshold threshold,
   t3.wRemark wRemark,
   t3.w_time time,
   t3.vCfCode vCfCode,
   house.storehouse_id house_id,
   house.storehouse_name,
   house.storehouse_type,
   hw.sub_type
  from
   basic_storehouse house left join t_depot_style st on
   st.indexid = house.storehouse_id left join storage_foodbasicinfo hw on
   house.storehouse_id = hw.house_id left join(
    select
     th.v_cf_code vCfCode,
     th.w_time,
     th.w_threshold wThreshold,
     th.threshold threshold,
     th.w_remark wRemark
    from
     t_warning_threshold_history th
    where
     th.org_id =#{orgId}
     and(
      th.v_cf_code in(
       select
        v_cf_code
       from
        t_warning_threshold_history
       where
        org_id =#{orgId}
       group by
        v_cf_code
      )
     )
     and th.w_time in(
      select
       max( w_time ) w_time
      from
       t_warning_threshold_history
      where
       org_id =#{orgId}
      group by
       w_time
     )
    order by
     th.w_time desc
   ) t3 on
   house.storehouse_code = t3.vCfCode
  where
   1 = 1
   and st.t_type = 2
 ) a
where
 a.rn = 1
    </select>
</mapper>