<?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="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, 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, type, 
      indexid, styles, imgdata
      )
    values (#{id,jdbcType=INTEGER}, #{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=",">
            <if test="id != null">
                id,
            </if>
            <if test="orgId != null">
                org_id,
            </if>
            <if test="type != null">
                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=",">
            <if test="id != null">
                #{id,jdbcType=INTEGER},
            </if>
            <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">
                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},
        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},
        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">
                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},
      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},
      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 DISTINCT
	style.styles,
	style.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.avg AvgT,
	t3.min MinT,
	t3.max MaxT,
    t3.storehouse vCfCode,
    t3.time time,
	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
		*
	FROM
		(
			SELECT
				*
			FROM
				t_testdata
			WHERE iLq_yq = 0
			 AND org_id =#{orgId}
			ORDER BY
				time DESC
				limit 10000000000
		) a
	GROUP BY
		a.storehouse
) t3 ON house.storehouse_code = t3.storehouse
WHERE
	1 = 1
AND style.type = 2
GROUP BY
	house.storehouse_id
  </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.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.type = 2
GROUP BY
	house.storehouse_id
  </select>


    <select id="selectByWarning" 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.wThreshold wThreshold,
        t3.threshold threshold,
        t3.wRemark wRemark,
        t3.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 style ON style.indexid = house.storehouse_id
        LEFT JOIN storage_foodbasicinfo hw ON house.storehouse_id = hw.house_id
        LEFT JOIN (
        SELECT
        a.v_cf_code vCfCode,
        a.w_time time,
        a.w_threshold wThreshold,
        a.threshold threshold,
        a.w_remark wRemark
        FROM
        (
        SELECT
        *
        FROM
        t_warning_threshold_history
        WHERE w_warning = 0
        AND org_id =#{orgId}
        ORDER BY
        w_time DESC
        ) a
        WHERE 1=1
        <if test="times != null">
            AND a.w_time in
            <foreach item="time" index="index" collection="times" open="(" separator="," close=")">
                #{time}
            </foreach>
        </if>
        <if test="cfCodes != null">
            AND a.v_cf_code in
            <foreach item="cfCode" index="index" collection="cfCodes" open="(" separator="," close=")">
                #{cfCode}
            </foreach>
        </if>
        GROUP BY
        a.v_cf_code
        ) t3 ON house.storehouse_code = t3.vCfCode
        WHERE
        1 = 1
        AND style.type = 2
        GROUP BY
        house.storehouse_id
    </select>
</mapper>