<?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.grainOilPurchaseSales.mapper.GrainOilPurchaseSalesMapper">

    <select id="getRotationPalnVarietyChartDatas"  resultType="com.unis.visualization.modular.grainOilPurchaseSales.entity.VO.RotationPlanVarietyChart">
     SELECT
        e.grain_variety_id,g.enumName as grain_variety_name,
        ROUND(IFNULL(SUM(f.plan_quantity),0.0)/10000,3) as "outPlanQuantity",
        ROUND(IFNULL(SUM(e.plan_quantity),0.0)/10000,3) as "inPlanQuantity"
      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
        LEFT JOIN depot_qh.basic_enum g on g.enumid = e.grain_variety_id
        WHERE CAST(c.rotation_year as char)  = DATE_FORMAT(NOW(),'%Y')
        GROUP BY e.grain_variety_id,g.enumName
      ORDER BY e.grain_variety_id
    </select>
    <select id="getRotationPalnMapChartDatas" resultType="com.unis.visualization.modular.grainOilPurchaseSales.entity.VO.RotationPlanMapChart">
      SELECT
		org_id,
		 org_name,
		 longitude,
		 latitude,
		planQuantity,
		outPlanQuantity,
	   ROUND(ckslSum/1000,3) as ckslSum,
		inPlanQuantity,
		 ROUND(rkslSum/1000,3) as rkslSum,
		IFNULL((ckslSum+rkslSum)/(planQuantity*1000)*100,0) planRate
 FROM (SELECT
            g.org_id,
            g.org_name,
            g.longitude,
            g.latitude,
         (IFNULL(SUM(f.plan_quantity)+ SUM(e.plan_quantity),0.0)) 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
    right JOIN  depot_qh.org_info  g on g.org_id = e.org_id
 where g.org_id != '9'
    GROUP BY g.org_id
) x
	ORDER BY x.org_id
    </select>
    <select id="getCurrentCrkWorkList" resultType="com.unis.visualization.modular.grainOilPurchaseSales.entity.VO.CurrentCrkWork">
        <choose>
            <when  test=" crkType !='' and crkType == '1'.toString()" >
                SELECT DISTINCT
                e.org_name,d.storehouse_name
                FROM
                depot_qh.data_crkyw_ckrmdj_default a
                inner JOIN  depot_qh.data_crkyw_ckcmz_default b ON a.BizNo = b.BizNo
                LEFT JOIN depot_qh.basic_storehouse d on b.sjckch = d.storehouse_id
                LEFT JOIN depot_qh.org_info e on a.UnitID = e.org_id
                where DATE_FORMAT(b.jmsj,'%Y-%m-%d') = DATE_FORMAT(NOW(),'%Y-%m-%d')
                ORDER BY e.org_name,d.storehouse_name
            </when >
            <otherwise >
                SELECT DISTINCT
                e.org_name,d.storehouse_name
                FROM
                depot_qh.data_crkyw_rmdj_default a
                inner JOIN  depot_qh.data_crkyw_cpz_default b ON a.BizNo = b.BizNo
                LEFT JOIN depot_qh.basic_storehouse d on b.sjchID = d.storehouse_id
                LEFT JOIN depot_qh.org_info e on a.UnitID = e.org_id
                where DATE_FORMAT(b.cpzsj,'%Y-%m-%d') = DATE_FORMAT(NOW(),'%Y-%m-%d')
                ORDER BY e.org_name,d.storehouse_name
            </otherwise>
        </choose>
    </select>
    <select id="getCurrentCrkWorkStock" resultType="com.unis.visualization.modular.grainOilPurchaseSales.entity.VO.CurrentCrkStock">
      SELECT
        MAX(CASE WHEN ywlx = '3' THEN slSum ELSE 0 END) AS 'rkslSum',
        MAX(CASE WHEN ywlx = '1' THEN slSum ELSE 0 END) AS 'ckslSum'

    FROM  (SELECT
            '3' as ywlx,
            IFNULL(SUM(b.xzklhjz),0) as slSum
            FROM
            depot_qh.data_crkyw_rmdj_default a
            inner JOIN  depot_qh.data_crkyw_cpz_default b ON a.BizNo = b.BizNo
             where DATE_FORMAT(a.ywdjsj,'%Y-%m-%d') = DATE_FORMAT(NOW(),'%Y-%m-%d')

    union all
    SELECT
            '1' as ywlx,
            IFNULL(SUM(b.klhjz),0) as slSum
            FROM
            depot_qh.data_crkyw_ckrmdj_default a
            inner JOIN  depot_qh.data_crkyw_ckcmz_default b ON a.BizNo = b.BizNo
            where DATE_FORMAT(a.rmsj,'%Y-%m-%d') = DATE_FORMAT(NOW(),'%Y-%m-%d')
    ) x
    </select>
    <select id="getGopsPalnSumList" resultType="com.unis.visualization.modular.grainOilPurchaseSales.entity.VO.GopsPlanSum">
        <choose>
            <when  test=" planType !='' and planType == '1'.toString()" >
                SELECT
                b.id,
                c.org_name,
                b.plan_order_no,
                b.plan_name,
                b.plan_type,
                SUM(a.plan_quantity) AS plan_quantity
                FROM
                biz_gops_plan_sale a
                INNER JOIN biz_gops_plan b ON a.gops_plan_id = b.id
                LEFT JOIN depot_qh.org_info c ON b.org_id = c.org_id
                GROUP BY
                b.id
                ORDER BY
                b.plan_order_no
            </when >
            <otherwise >
                SELECT
                b.id,
                c.org_name,
                b.plan_order_no,
                b.plan_name,
                b.plan_type,
                SUM(a.plan_quantity) AS plan_quantity
                FROM
                biz_gops_plan_buy a
                INNER JOIN biz_gops_plan b ON a.gops_plan_id = b.id
                LEFT JOIN depot_qh.org_info c ON b.org_id = c.org_id
                GROUP BY
                b.id
                ORDER BY
                b.plan_order_no
            </otherwise>
        </choose>
    </select>
    <select id="getDeoptRotationPalnDetail" resultType="com.unis.visualization.modular.grainOilPurchaseSales.entity.VO.DeoptRotationPalnDetail">
          SELECT
		 c.dwmc as unitName,
		x.org_id,
		x.org_name,
		d.dict_label cityName,
		x.address,
		planQuantity,
		outPlanQuantity,
	   ROUND(ckslSum/1000,3) as ckslSum,
		inPlanQuantity,
		 ROUND(rkslSum/1000,3) as rkslSum,
		IFNULL((ckslSum+rkslSum)/(planQuantity*1000)*100,0) completeRate
 FROM (SELECT
            g.org_id,
            g.org_name,
            g.address,
			g.unit_info_id,
             (IFNULL(SUM(f.plan_quantity)+ SUM(e.plan_quantity),0.0)) 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
        right JOIN  depot_qh.org_info  g on g.org_id = e.org_id
     where g.org_id = #{orgId}
        GROUP BY g.org_id
    ) x
     LEFT JOIN province_all.biz_unit_info c ON c.id = x.unit_info_id
     LEFT JOIN province_all.dev_dict d ON d.id = c.zcdz_city
    </select>
</mapper>