<?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.rotationPlan.info.modular.rpBiweeklyReport.mapper.RpBiweeklyReportMapper">
    <select id="selectMyMapsPage" resultType="java.util.Map">
        SELECT
        a.id as "id",
        CAST(c.plan_year as char) as "planYear",
        d.org_name as "orgName",
        DATE_FORMAT(a.report_time,'%Y-%m-%d %T') as "reportTime",
        SUM(e.plan_quantity) as "inPlanQuantity",
        SUM(f.plan_quantity) as "outPlanQuantity"
        FROM
        depot_qh.business_rp_biweekly_report a
        LEFT JOIN depot_qh.business_rp_report c ON c.id = a.rp_report_id
        LEFT JOIN depot_qh.org_info d on a.org_id = d.org_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
        <if test="ew.sqlSegment != null and ew.sqlSegment != ''">
            where ${ew.sqlSegment}
        </if>
        GROUP BY a.id,c.plan_year,d.org_name,a.report_time
        ORDER BY a.report_time desc
    </select>
    <select id="getDetail" resultType="java.util.Map">
        SELECT
        a.id,
        c.org_name,
        a.grain_variety_id,
        d.enumName as grainVarietyName,
        SUM(f.plan_quantity) as "planQuantity",
        a.out_current_quantity as "outCurrentQuantity",
        a.out_current_avg_price as "outCurrentAvgPrice",
        a.out_sum_quantity as "outSumQuantity",
        a.out_sum_avg_price as "outSumAvgPrice",
        a.out_sum_ratio  as "outSumRatio",
        a.in_current_quantity as "inCurrentQuantity",
        a.in_current_avg_price as "inCurrentAvgPrice",
        a.in_sum_quantity as "inSumQuantity",
        a.in_sum_avg_price as "inSumAvgPrice",
        a.in_sum_ratio as "inSumRatio",
        a.remark
        FROM
            depot_qh.business_rp_biweekly_report_grain a
        INNER JOIN depot_qh.business_rp_biweekly_report b ON a.rp_biweekly_report_id = b.id
        LEFT JOIN  depot_qh.org_info c on b.org_id = c.org_id
        LEFT JOIN depot_qh.basic_enum d on a.grain_variety_id = d.enumId
        LEFT JOIN depot_qh.business_rp_outbound f ON b.rp_report_id = f.rp_report_id AND a.grain_variety_id = f.grain_variety_id
        WHERE
            b.id = #{id}
        GROUP BY
        a.id,
        c.org_name,
        a.grain_variety_id,
        d.enumName,
        a.out_current_quantity,
        a.out_current_avg_price,
        a.out_sum_quantity,
        a.out_sum_avg_price,
        a.out_sum_ratio,
        a.in_current_quantity,
        a.in_current_avg_price,
        a.in_sum_quantity,
        a.in_sum_avg_price,
        a.in_sum_ratio,
        a.remark
    </select>
    <select id="selectMyPlanMapsPage" resultType="java.util.Map">
         SELECT
        cast(c.rotation_year as char) as "rotationYear",
            SUM(f.plan_quantity) 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
                INNER JOIN depot_qh.data_crkyw_tzdxx_default m4 on m3.tzdbh = m4.tzdbh
                where m4.jhdID = a.rp_report_id

            )  as "ckslSum",
            SUM(e.plan_quantity) 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
                INNER JOIN depot_qh.data_crkyw_tzdxx_default m4 on m3.tzdbh = m4.tzdbh
                where m4.jhdID = a.rp_report_id
            )  as "rkslSum"
        FROM
        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
        <if test="ew.sqlSegment != null and ew.sqlSegment != ''">
            where ${ew.sqlSegment}
        </if>
        GROUP BY c.rotation_year
    </select>
    <select id="getPlanDetail" resultType="java.util.Map">
         SELECT

       cast(c.rotation_year as char) as "rotationYear",
				b.dwmc as "dwmc",
        d.org_name as "orgName",
				SUM(f.plan_quantity) 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
					INNER JOIN depot_qh.data_crkyw_tzdxx_default m4 on m3.tzdbh = m4.tzdbh
					where m4.jhdID = a.rp_report_id

				)  as "ckslSum",
				SUM(e.plan_quantity) 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
					INNER JOIN depot_qh.data_crkyw_tzdxx_default m4 on m3.tzdbh = m4.tzdbh
					where m4.jhdID = a.rp_report_id
				)  as "rkslSum"
        FROM
        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.org_info d on f.org_id = d.org_id
        LEFT JOIN  biz_unit_info b on c.unit_info_id = b.id
        where  cast(c.rotation_year as char) = #{rotationYear}
        GROUP BY
        c.rotation_year,
        b.dwmc ,
    d.org_name
    </select>
    <select id="selectMyPlanDetailMapsPage" resultType="java.util.Map">
         SELECT

       cast(c.rotation_year as char) as "rotationYear",
				b.dwmc as "dwmc",
        d.org_name as "orgName",
				SUM(f.plan_quantity) 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
					INNER JOIN depot_qh.data_crkyw_tzdxx_default m4 on m3.tzdbh = m4.tzdbh
					where m4.jhdID = a.rp_report_id

				)  as "ckslSum",
				SUM(e.plan_quantity) 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
					INNER JOIN depot_qh.data_crkyw_tzdxx_default m4 on m3.tzdbh = m4.tzdbh
					where m4.jhdID = a.rp_report_id
				)  as "rkslSum"
        FROM
        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.org_info d on f.org_id = d.org_id
        LEFT JOIN  biz_unit_info b on c.unit_info_id = b.id
        <if test="ew.sqlSegment != null and ew.sqlSegment != ''">
            where ${ew.sqlSegment}
        </if>
        GROUP BY
        c.rotation_year,
        b.dwmc ,
    d.org_name
    </select>
</mapper>