<?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.grainOilReserve.mapper.GrainOilReserveMapper">

    <select id="getStorehouseCapacity" resultType="com.unis.visualization.modular.grainOilReserve.entity.VO.StorehouseCapacityChart">
      SELECT ROUND(SUM(designCapacity)/10000,3) as designCapacity FROM (
        SELECT  IFNULL(a.design_capacity,0.0) as  designCapacity,org_id as orgId  FROM depot_qh.basic_storehouse a where a.del_flag =1
        UNION ALL
        SELECT  IFNULL(a.tank_capacity,0.0) as  designCapacity,org_id as orgId  FROM depot_qh.basic_tank a where a.del_flag =1
        ) x
    LEFT JOIN depot_qh.org_info b on x.orgId = b.org_id
    LEFT JOIN province_all.biz_unit_info c on b.unit_info_id = c.id
        <if test="cityId != null and cityId != ''">
            where c.zcdz_city = #{cityId}
        </if>
    </select>
    <select id="getStorehouseStock" resultType="com.unis.visualization.modular.grainOilReserve.entity.VO.StorehouseCapacityChart">
      SELECT ROUND(IFNULL(SUM(kcsl),0.0)/1000/10000,3) as kcsl  FROM depot_qh.data_kcgl_kcsw_default a
        LEFT JOIN depot_qh.org_info b on a.UnitID = b.org_id
        LEFT JOIN province_all.biz_unit_info c on b.unit_info_id = c.id
        <if test="cityId != null and cityId != ''">
            where c.zcdz_city = #{cityId}
        </if>
    </select>
    <select id="getYesteryearStorehouseStock" resultType="com.unis.visualization.modular.grainOilReserve.entity.VO.StorehouseCapacityChart">
         SELECT ROUND(IFNULL(SUM(kcsl),0.0)/1000/10000,3) yesterYearKcsl
        FROM (
        SELECT a.kcsl,
        ROW_NUMBER() OVER (PARTITION BY a.hwh ORDER BY a.rq DESC) AS rn
        FROM depot_qh.data_kcgl_fcbgz_default a
				LEFT JOIN depot_qh.org_info b on a.UnitID = b.org_id
				LEFT JOIN province_all.biz_unit_info c on b.unit_info_id = c.id
				where   DATE_FORMAT(DATE_ADD(NOW(),INTERVAL -1 YEAR),'%Y-%m-%d') >=  DATE_FORMAT(a.rq,'%Y-%m-%d')
                <if test="cityId != null and cityId != ''">
                   and c.zcdz_city = #{cityId}
                </if>
        ) t
        WHERE rn = 1;

    </select>
    <select id="getVarietyChartDatas" resultType="com.unis.visualization.modular.grainOilReserve.entity.VO.VarietyCapacityChart">
        SELECT
            a.pz ,
            e.enumname pzName,
            ROUND(IFNULL(SUM(kcsl),0.0)/1000/10000,3) as kcsl
        FROM depot_qh.data_kcgl_kcsw_default a
        LEFT JOIN depot_qh.basic_enum e on a.pz =  e.enumId
        LEFT JOIN depot_qh.org_info b on a.UnitID = b.org_id
        LEFT JOIN province_all.biz_unit_info c on b.unit_info_id = c.id
        <if test="cityId != null and cityId != ''">
            where  c.zcdz_city = #{cityId}
        </if>
        GROUP BY
        a.pz,
        e.enumname
        ORDER BY a.pz
    </select>
    <select id="getGradeChartDatas" resultType="com.unis.visualization.modular.grainOilReserve.entity.VO.GradeCapacityChart">
       SELECT
        pz,pzName,
            MAX(CASE WHEN djName = '一等' THEN sumZl ELSE 0.0 END) AS 'oneLevel',
             MAX(CASE WHEN djName = '二等' THEN sumZl ELSE 0.0 END) AS 'twoLevel',
             MAX(CASE WHEN djName = '三等' THEN sumZl ELSE 0.0 END) AS 'threeLevel'
         FROM (SELECT
        a.pz,
        b.enumname pzName,
        c.enumname djName,

        ROUND(IFNULL(SUM(kcsl),0.0)/1000/10000,3) as sumZl  FROM depot_qh.data_kcgl_kcsw_default a
        LEFT JOIN depot_qh.basic_enum b on a.pz =  b.enumId
        LEFT JOIN depot_qh.basic_enum c on a.dj =  c.enumId
				LEFT JOIN depot_qh.org_info d on a.UnitID = d.org_id
				LEFT JOIN province_all.biz_unit_info e on d.unit_info_id = e.id
        where a.dj in  (3271,3272,3273)
        <if test="cityId != null and cityId != ''">
            and  e.zcdz_city = #{cityId}
        </if>
        GROUP by
        a.dj,a.pz
        ) x
        GROUP BY pz,pzName
ORDER  by pz
    </select>
    <select id="getCityVarietyChartDatas" resultType="com.unis.visualization.modular.grainOilReserve.entity.VO.CityVarietyCapacityChart">

 SELECT
		cityId,
    cityName,
		 MAX(CASE WHEN lspzName = '小麦及混合小麦' THEN sumZl ELSE 0 END) AS 'xm',
     MAX(CASE WHEN lspzName = '稻谷' THEN sumZl ELSE 0 END) AS 'dg',
			MAX(CASE WHEN lspzName = '玉米' THEN sumZl ELSE 0 END) AS 'ym',
			MAX(CASE WHEN lspzName = '原油' THEN sumZl ELSE 0 END) AS 'yy',
			MAX(CASE WHEN lspzName = '青稞' THEN sumZl ELSE 0 END) AS 'qk'
    FROM (
			SELECT
				d.ID as cityId,
        d.DICT_LABEL AS cityName,
				a.pz,
				e.enumname as lspzName,
				ROUND(IFNULL(SUM(kcsl),0.0)/1000/10000,3) as sumZl
    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.data_kcgl_kcsw_default a ON  a.UnitID = b.org_id  and  a.pz  in (3164,3165,3166,7144,8000) /*     -- 目前取小麦，玉米，稻谷，青稞，油*/
		LEFT JOIN depot_qh.basic_enum e on e.enumId = a.pz
    where
		d.PARENT_ID =  '1781535965870915586'
        <if test="cityId != null and cityId != ''">
            and  c.zcdz_city = #{cityId}
        </if>
    GROUP BY
        d.ID,d.DICT_LABEL,a.pz,e.enumname
) x
 GROUP BY
        cityId,cityName
    ORDER BY  cityId
    </select>
    <select id="getCurrentCrkChartDatas" resultType="com.unis.visualization.modular.grainOilReserve.entity.VO.CurrentCrkCapacityChart">
        <choose>
            <when  test=" crkType !='' and crkType == '1'.toString()" >
                SELECT
                a.lspz pz,
                d.enumname  as pzName,
                ROUND(IFNULL(SUM(b.klhjz),0.0)/1000,3)  as sumZl,
                sum(IF(IFNULL(c.BizNo,'') = '',1,0)) as carCount
                FROM
                depot_qh.data_crkyw_ckrmdj_default a
                inner JOIN depot_qh.data_crkyw_ckcpz_default e ON a.BizNo = e.BizNo
                left JOIN  depot_qh.data_crkyw_ckcmz_default b ON a.BizNo = b.BizNo
                LEFT JOIN depot_qh.data_crkyw_ckcm_default  c  on a.BizNo = c.BizNo
                LEFT JOIN depot_qh.basic_enum d on a.lspz = d.enumId
                LEFT JOIN depot_qh.org_info m1 on a.UnitID = m1.org_id
                LEFT JOIN province_all.biz_unit_info m2 on m1.unit_info_id = m2.id
                where DATE_FORMAT(a.rmsj,'%Y-%m-%d') = DATE_FORMAT(NOW(),'%Y-%m-%d')
                <if test="cityId != null and cityId != ''">
                    and  m2.zcdz_city = #{cityId}
                </if>
                GROUP BY a.lspz,d.enumname
            </when >
            <otherwise >
                SELECT
                a.lspz pz,
                d.enumname  as pzName,
                ROUND(IFNULL(SUM(b.xzklhjz),0.0)/1000,3)  as sumZl,
                sum(IF(IFNULL(c.BizNo,'') = '',1,0)) as carCount
                FROM
                depot_qh.data_crkyw_rmdj_default a
                INNER JOIN  depot_qh.data_crkyw_qyxx_default e ON a.BizNo = e.BizNo
                left JOIN  depot_qh.data_crkyw_cpz_default b ON a.BizNo = b.BizNo
                LEFT JOIN depot_qh.data_crkyw_rkcm_default  c  on a.BizNo = c.BizNo
                LEFT JOIN depot_qh.basic_enum d on a.lspz = d.enumId
                LEFT JOIN depot_qh.org_info m1 on a.UnitID = m1.org_id
                LEFT JOIN province_all.biz_unit_info m2 on m1.unit_info_id = m2.id
                where DATE_FORMAT(a.ywdjsj,'%Y-%m-%d') = DATE_FORMAT(NOW(),'%Y-%m-%d')
                <if test="cityId != null and cityId != ''">
                    and  m2.zcdz_city = #{cityId}
                </if>
                GROUP BY a.lspz,d.enumname
            </otherwise>
        </choose>
    </select>
    <select id="getRotationPalnChartDatas" resultType="com.unis.visualization.modular.grainOilReserve.entity.VO.RotationPlanChart">
       SELECT
            COUNT(DISTINCT a.id) countReport,
            ROUND((IFNULL(SUM(f.plan_quantity)+ SUM(e.plan_quantity),0.0))/10000,3) as planQuantity,
            IFNULL(SUM(f.plan_quantity),0.0) as "outPlanQuantity",
            (   SELECT
             IFNULL(sum(m1.zcsl),0.0) as "cksl"
                FROM
                        depot_qh.data_kcgl_fcbgz_default m1
                INNER JOIN depot_qh.data_crkyw_ckcmz_default m2 ON m1.DataID = m2.DataID
                INNER JOIN depot_qh.data_crkyw_ckrmdj_default m3 ON m3.bizno = m2.bizno
                where m3.jhdID = a.rp_report_id

            )  as "ckslSum",
             IFNULL(SUM(e.plan_quantity),0.0) as "inPlanQuantity",
            (   SELECT
              IFNULL(sum(m1.srsl),0.0) as "rksl"
                FROM
                        depot_qh.data_kcgl_fcbgz_default m1
                INNER JOIN depot_qh.data_crkyw_cpz_default m2 ON m1.DataID = m2.DataID
                INNER JOIN depot_qh.data_crkyw_rmdj_default m3 ON m3.bizno = m2.bizno
                where m3.jhdh = a.rp_report_id
            )  as "rkslSum"
        FROM
         province_all.biz_rp_issuance a
        inner JOIN depot_qh.business_rp_report c ON c.id = a.rp_report_id
      	LEFT JOIN depot_qh.business_rp_inbound e ON c.id = e.rp_report_id
        LEFT JOIN depot_qh.business_rp_outbound f ON c.id = f.rp_report_id

    </select>
    <select id="getDepotStockChartDatas" resultType="com.unis.visualization.modular.grainOilReserve.entity.VO.DeportStockChart">
        <choose>
            <when  test=" queryType !='' and queryType == '2'.toString()" >
                /*  等级分布 9是青海粮食局 */
                SELECT
                a.org_id,
                a.org_name,
                a.longitude,
                a.latitude,
                IFNULL(c.enumname,'') djName,
                ROUND(IFNULL(SUM(kcsl),0.0)/1000/10000,2) as kcsl
                FROM
                depot_qh.org_info a
                LEFT JOIN depot_qh.data_kcgl_kcsw_default b on a.org_id = b.UnitID
                LEFT JOIN depot_qh.basic_enum c on b.dj = c.enumId
                where a.org_id != '9'
                GROUP BY a.org_id,c.enumname
                ORDER BY a.org_id,c.enumname
            </when >
            <when  test=" queryType !='' and queryType == '3'.toString()" >
                /*  作业状态分布*/
                SELECT
                a.org_id,
                a.org_name,
                a.longitude,
                a.latitude,
                (SELECT IF(left(m2.bizno,4)='qcrk','3','1') FROM  data_kcgl_xckc_default m2  where m2.UnitID = a.org_id and m2.bizno LIKE 'qc%' ORDER BY rcrq desc LIMIT 1) as crkStatus
                FROM
                depot_qh.org_info a
                WHERE
                a.org_id != '9'
            </when >
            <otherwise >
                /*  库区分布*/
                SELECT
                a.org_id,
                a.org_name,
                a.longitude,
                a.latitude,
                ROUND(IFNULL(SUM(kcsl),0.0)/1000/10000,2) as kcsl,
                GROUP_CONCAT(DISTINCT b.hwxz) as hwxz,
                GROUP_CONCAT(DISTINCT c.enumname) as hwxzName
                FROM
                depot_qh.org_info a
                LEFT JOIN depot_qh.data_kcgl_kcsw_default b on a.org_id = b.UnitID and b.hwxz in (6877,6878,6879)
                LEFT JOIN depot_qh.basic_enum c on b.hwxz = c.enumId
                /*  只要省储，市储，县储*/
                where a.org_id != '9'
                GROUP BY a.org_id
                ORDER BY a.org_id
            </otherwise>
        </choose>
    </select>
    <select id="getDepotStockDetail" resultType="com.unis.visualization.modular.grainOilReserve.entity.VO.DeportStockDetail">
         SELECT
        c.dwmc as unitName,
        a.org_id,
        a.org_name,
        d.dict_label cityName,
        a.address,
				ROUND(IFNULL(SUM(kcsl),0.0)/1000/10000,2) as kcsl
    FROM
        depot_qh.org_info a
    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 b on a.org_id = b.UnitID
    WHERE
	a.org_id = #{orgId}
    </select>
    <select id="getStorehouseDetail" resultType="com.unis.visualization.modular.grainOilReserve.entity.VO.DeportStockDetail">
    SELECT  IFNULL(SUM(IFNULL(a.design_capacity,0.0)),0.0) as designCapacity,COUNT(org_id) as storehouseCount  FROM depot_qh.basic_storehouse a
    WHERE
	a.org_id = #{orgId} and a.del_flag =1
    </select>
    <select id="getTankDetail" resultType="com.unis.visualization.modular.grainOilReserve.entity.VO.DeportStockDetail">
     SELECT  IFNULL(SUM(IFNULL(a.tank_capacity,0.0)),0.0) as tankCapacity,COUNT(org_id) as tankCount  FROM depot_qh.basic_tank a
    WHERE
	a.org_id = #{orgId} and a.del_flag =1
    </select>
    <select id="getStockDetailList" resultType="com.unis.visualization.modular.grainOilReserve.entity.VO.StockDetail">
        SELECT
        a.pz,
        a.dj,
        a.hwxz,
        e1.enumname pzName,
        e2.enumname xzName,
        e3.enumname djName,
     ROUND(IFNULL(SUM(kcsl),0.0)/1000/10000,2) kcsl
    FROM
        depot_qh.data_kcgl_kcsw_default a
    INNER JOIN depot_qh.basic_enum e1 ON e1.enumId = a.pz
    INNER JOIN depot_qh.basic_enum e2 ON e2.enumId = a.hwxz
    INNER JOIN depot_qh.basic_enum e3 ON e3.enumId = a.dj
    WHERE
        a.UnitID = #{orgId}
    GROUP BY
        a.pz,
        a.dj,
        a.hwxz,
        e1.enumname,
        e2.enumname,
        e3.enumname
    ORDER BY
        a.pz,
        a.dj,
        a.hwxz
    </select>
</mapper>