<?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.unis.visualization.modular.grainSituationDetection.mapper.GrainSituationDetectionMapper">

    <select id="getDepotStockLqChartDatas" resultType="com.unis.visualization.modular.grainSituationDetection.entity.VO.GrainSituationDetectionChart">
         SELECT
            a.org_id,
            a.org_name,
            a.longitude,
            a.latitude,
            b.temp_max,
            b.temp_min,
            b.temp_avg,
            ROUND(IFNULL(SUM(kcsl),0.0)/1000/10000,2) as kcsl
            FROM
            depot_qh.org_info a
            LEFT JOIN
            (SELECT org_id,temp_max,temp_min,temp_avg
                FROM (
                        SELECT org_id,temp_max,temp_min,temp_avg,
                                     ROW_NUMBER() OVER (PARTITION BY org_id ORDER BY gather_time DESC) AS rn
                        FROM depot_qh.t_lq_data
                ) t
                WHERE rn = 1
            )
             b on a.org_id = b.org_id
            LEFT JOIN depot_qh.data_kcgl_kcsw_default c on a.org_id = c.UnitID
            where a.org_id != '9'
            GROUP BY a.org_id
            ORDER BY a.org_id
    </select>
    <select id="getDepotLqWarnList" resultType="com.unis.visualization.modular.grainSituationDetection.entity.VO.GrainSituationDetectionWarn">
        SELECT
        a.org_id,
        a.org_name,
        c.storehouse_name,
        b.temp_max,
        b.temp_min,
        IF(b.temp_max > 25,'高温预警','低温预警') warnInfo,
        b.gather_time
        FROM
        depot_qh.org_info a
        inner JOIN   depot_qh.t_lq_data  b on a.org_id = b.org_id
        LEFT JOIN depot_qh.basic_storehouse c on b.org_id = c.org_id AND b.storehouse_code = c.storehouse_code
        where b.temp_max > 25 or   10 > b.temp_min
        ORDER BY b.gather_time DESC
        LIMIT 0,20
    </select>
    <select id="getDepotLqWarnCount" resultType="com.unis.visualization.modular.grainSituationDetection.entity.VO.GrainSituationDetectionWarnCount">
        SELECT
        d.ID as cityId,
        d.DICT_LABEL AS cityName,
        COUNT(e.is_accord_model) as count
        FROM

        province_all.dev_dict d
        LEFT JOIN province_all.biz_unit_info c ON  d.ID = c.zcdz_city
        LEFT JOIN depot_qh.org_info b ON c.id = b.unit_info_id

        LEFT JOIN depot_qh.t_lq_data e ON e.org_id = b.org_id AND IFNULL(e.is_accord_model, 0) = 1

        <choose>
            <when  test=" lqWarnCountQuery !='' and lqWarnCountQuery == '3'.toString()" >
                and DATE_FORMAT(e.createdate,'%Y-%m') = DATE_FORMAT(NOW(),'%Y-%m')
            </when >
            <when  test=" lqWarnCountQuery !='' and lqWarnCountQuery == '2'.toString()" >
                and   QUARTER(e.createdate) =  QUARTER(NOW())
            </when >
            <otherwise >
                and DATE_FORMAT(e.createdate,'%Y') = DATE_FORMAT(NOW(),'%Y')
            </otherwise>
        </choose>
        where
        d.PARENT_ID =  '1781535965870915586'
        GROUP BY
        d.ID,d.DICT_LABEL
        order by COUNT(e.is_accord_model) desc,d.ID
    </select>
    <select id="getDepotLqWarnAvg" resultType="com.unis.visualization.modular.grainSituationDetection.entity.VO.GrainSituationDetectionWarnAvg">
            SELECT
        d.ID as cityId,
        d.DICT_LABEL AS cityName,
        ROUND(IFNULL(avg(e.temp_avg),0.0),1) as temp_avg
        FROM
        province_all.dev_dict d
        LEFT JOIN province_all.biz_unit_info c ON  d.ID = c.zcdz_city
        LEFT JOIN depot_qh.org_info b ON c.id = b.unit_info_id
        LEFT JOIN depot_qh.t_lq_data e ON e.org_id = b.org_id
        where
        d.PARENT_ID =  '1781535965870915586'
        GROUP BY
        d.ID,d.DICT_LABEL
        order by d.ID
    </select>
    <select id="getTotalCount" resultType="com.unis.visualization.modular.grainSituationDetection.entity.VO.GrainSituationDetectionOnline">
         SELECT
       IFNULL(COUNT(DISTINCT c.storehouse_id),0) as totalCount
        FROM
          depot_qh.t_lq_data  b
        LEFT JOIN depot_qh.basic_storehouse c on b.org_id = c.org_id AND b.storehouse_code = c.storehouse_code

    </select>
    <select id="getOnlineCount" resultType="com.unis.visualization.modular.grainSituationDetection.entity.VO.GrainSituationDetectionOnline">
         SELECT
       IFNULL(COUNT(DISTINCT c.storehouse_id),0) as onlineCount
        FROM
          depot_qh.t_lq_data  b
        LEFT JOIN depot_qh.basic_storehouse c on b.org_id = c.org_id AND b.storehouse_code = c.storehouse_code
			WHERE  DATE_FORMAT(b.gather_time,'%Y-%m-%d') >= DATE_FORMAT(DATE_ADD(NOW(),INTERVAL -1 DAY),'%Y-%m-%d')
    </select>
    <select id="getLqDetail" resultType="com.unis.visualization.modular.grainSituationDetection.entity.VO.LqDetail">
    SELECT
        c.storehouse_id,
        c.storehouse_name,
        b.gather_time,
        IFNULL(b.temp_max,0.0) temp_max,
        IFNULL(b.temp_min,0.0) temp_min,
        IFNULL(b.temp_avg,0.0) temp_avg,
      ROUND(IFNULL(SUM(kcsl),0.0)/1000/10000,3) kcsl
    FROM
        depot_qh.data_kcgl_kcsw_default a
    inner JOIN depot_qh.basic_storehouse c ON a.UnitID = c.org_id AND a.ch = c.storehouse_id
    LEFT JOIN
        (SELECT org_id,storehouse_code,gather_time,temp_max,temp_min,temp_avg
                FROM (
                                SELECT org_id,storehouse_code,gather_time,temp_max,temp_min,temp_avg,
                                                         ROW_NUMBER() OVER (PARTITION BY org_id ORDER BY gather_time DESC) AS rn
                                FROM depot_qh.t_lq_data
                ) t
                WHERE rn = 1
        )
         b on c.org_id = b.org_id AND b.storehouse_code = c.storehouse_code
    WHERE
        a.UnitID = #{orgId}
    GROUP BY
        c.storehouse_id,
        c.storehouse_name
    ORDER BY c.storehouse_id
    </select>
    <select id="getDepotStockDetail"  resultType="com.unis.visualization.modular.grainSituationDetection.entity.VO.DeportLqDetail">
          SELECT
        c.dwmc as unitName,
        a.org_id,
        a.org_name,
        d.dict_label cityName,
        a.address,
        b.temp_max,
        b.temp_min,
        b.temp_avg,
        ROUND(IFNULL(SUM(kcsl),0.0)/1000/10000,2) as kcsl
        FROM
        depot_qh.org_info a
        LEFT JOIN
        (SELECT org_id,temp_max,temp_min,temp_avg
                FROM (
                                SELECT org_id,temp_max,temp_min,temp_avg,
                                                         ROW_NUMBER() OVER (PARTITION BY org_id ORDER BY gather_time DESC) AS rn
                                FROM depot_qh.t_lq_data
                ) t
                WHERE rn = 1
        )
         b on a.org_id = b.org_id
         LEFT JOIN province_all.biz_unit_info c ON c.id = a.unit_info_id
        LEFT JOIN province_all.dev_dict d ON d.id = c.zcdz_city
        LEFT JOIN depot_qh.data_kcgl_kcsw_default e on a.org_id = e.UnitID
        where a.org_id =#{orgId}

    </select>
</mapper>