<?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.businessrpreport.mapper.BusinessRpReportMapper">

    <select id="selectMyMapsPage" resultType="java.util.Map">
        SELECT
        brr.id,
        b.dwmc AS dwmc,
        SUM(bro.sealing_quantity) AS plan_quantity,
        SUM(bri.quantity) AS quantity,
        brr.rotation_year AS rotationYear,
        brr.report_time AS reportTime
        FROM
        depot_qh.business_rp_report brr
        LEFT JOIN depot_qh.business_rp_outbound bro
        ON bro.rp_report_id = brr.id
        LEFT JOIN depot_qh.business_rp_inbound bri
        ON bri.rp_report_id = brr.id
        LEFT JOIN depot_qh.org_info oi
        ON bro.org_id = oi.org_id
        LEFT JOIN province_all.biz_unit_info b
        ON brr.unit_info_id = b.id
        <if test="ew.sqlSegment != null and ew.sqlSegment != ''">
            where ${ew.sqlSegment}
        </if>
        GROUP BY
        oi.org_name,
        brr.rotation_year,
        brr.report_time
    </select>

    <select id="getDetail" resultType="java.util.Map">
        SELECT
            brr.unit_info_id,
            brr.plan_name,
            brr.rotation_year,
            brr.plan_type,
            brr.plan_year,
            brr.type,
            brr.applicant,
            brr.create_date,
            bro.*,
            oi.org_name kqmc,
            bri.*,
            bro.sealing_quantity,
            be1.enumName grainVarietyName,
            be2.enumName grainNatureName,
            be3.enumName grainGradeName,
            bs.storehouse_name storehouseName,
            bw.warehouse_name warehouseName

        FROM
            depot_qh.business_rp_report brr
                LEFT JOIN depot_qh.business_rp_outbound bro ON bro.rp_report_id = brr.id
                left JOIN depot_qh.org_info oi on bro.org_id=oi.org_id
                LEFT JOIN depot_qh.basic_storehouse bs on bro.basic_storehouse_id=bs.storehouse_id
                LEFT JOIN depot_qh.basic_warehouse bw on  bro.warehouse_id =bw.warehouse_id
                LEFT JOIN depot_qh.basic_enum be1 on bro.grain_variety_id =be1.enumId
                LEFT JOIN depot_qh.basic_enum be2 on bro.grain_nature_id =be2.enumId
                LEFT JOIN depot_qh.basic_enum be3 on bro.grain_grade_id =be3.enumId
                LEFT JOIN depot_qh.business_rp_inbound bri on bri.rp_report_id= brr.id
        where brr.id =#{id}
    </select>


    <select id="createReport" resultType="com.unis.rotationPlan.info.modular.businessrpreport.entity.vo.BusinessRpReportVO">
        SELECT
        bo.grain_variety_id grainVarietyId,
        be1.enumName grainVarietyName,
        bbo.rotation_year rotationYear,
        IFNULL( SUM( bo.sealing_quantity ), 0 ) AS outboundQuantity,
        IFNULL( SUM( bi.quantity ), 0 ) AS inboundQuantity
        FROM
        depot_qh.business_rp_outbound bo
        LEFT JOIN depot_qh.business_rp_inbound bi ON bo.org_id = bi.org_id AND bo.grain_variety_id = bi.grain_variety_id
        left join  business_rp_report bbo on bbo.id=bo.rp_report_id
        LEFT JOIN depot_qh.basic_enum be1 on bo.grain_variety_id =be1.enumId
        <if test="pageParam.rotationYear!= null" >
            where bbo.rotation_year=#{pageParam.rotationYear,jdbcType=INTEGER}
        </if>
        GROUP BY
        bo.grain_variety_id
    </select>

    <select id="getRpReportByReportId" resultType="com.unis.rotationPlan.info.modular.businessrpreport.entity.vo.BusinessRpReportVO">
        SELECT DISTINCT
        c.id AS "id",
        b.dwmc,
        c.plan_name planName,
        c.rotation_year rotationYear,
        c.plan_year planYear,
        c.type,
        c.plan_type
        FROM
        depot_qh.business_rp_report c
        LEFT JOIN depot_qh.business_rp_outbound d ON d.rp_report_id = c.id
        LEFT JOIN province_all.biz_unit_info b ON c.unit_info_id = b.id
        left join depot_qh.org_info o on d.org_id=o.org_id
        where
        c.id = #{rpReportId}
        <if test="orgId!= null" >
            and d.org_id = #{orgId}
        </if>

    </select>

    <select id="findOutByRpReportId"  resultType="com.unis.rotationPlan.info.modular.businessrpreport.entity.BusinessRpOutbound" parameterType="String">
        SELECT DISTINCT
            d.id,
            d.createdate,
            d.updatename,
            d.updatedate,
            d.rp_report_id,
            d.org_id,
            d.basic_storehouse_id,
            d.warehouse_id,
            d.grain_variety_id,
            d.grain_nature_id,
            d.grain_grade_id,
            d.sealing_quantity sealingQuantity,
            d.storage_period,
            d.purchase_price,
            d.product_year,
            d.remarks,
            d.inspection_report_id,
            d.regular_inspection_report_ids,
            d.plan_quantity,
            o.org_name orgName,
            st.storehouse_name as "basicStorehouseName",
            wa.warehouse_name  as "warehouseName",
            pz.enumname as "grainVarietyName",
            xz.enumname as "grainNatureName",
            dj.enumname as "grainGradeName"
        FROM
            depot_qh.business_rp_report c
                INNER JOIN depot_qh.business_rp_outbound d ON d.rp_report_id = c.id
                LEFT JOIN depot_qh.basic_storehouse st on st.storehouse_id = d.basic_storehouse_id
                LEFT JOIN depot_qh.basic_warehouse wa on wa.warehouse_id = d.warehouse_id
                LEFT JOIN depot_qh.basic_enum pz on pz.enumId = d.grain_variety_id
                LEFT JOIN depot_qh.basic_enum xz on xz.enumId = d.grain_nature_id
                LEFT JOIN depot_qh.basic_enum dj on dj.enumId = d.grain_grade_id
                left join depot_qh.org_info o on o.org_id=d.org_id
        where 1=1
        <if test="rpReportId !=null and rpReportId !=''">
            and c.id = #{rpReportId}
        </if>

        <if test="orgId !=null and orgId !=''">
            and o.org_Id = #{orgId}
        </if>
    </select>

    <select id="findInByRpReportId"  resultType="com.unis.rotationPlan.info.modular.businessrpreport.entity.BusinessRpInbound"  >
        SELECT DISTINCT
            d.id,
            d.createdate,
            d.updatename,
            d.updatedate,
            d.rp_report_id,
            d.org_id,
            d.grain_variety_id,
            d.grain_nature_id,
            d.quantity sealingQuantity,
            d.product_period,
            d.plan_quantity,
            o.org_name orgName,
            pz.enumname as "grainVarietyName",
            xz.enumname as "grainNatureName"
        FROM
            depot_qh.business_rp_report c
                INNER JOIN depot_qh.business_rp_inbound d ON d.rp_report_id = c.id
                LEFT JOIN depot_qh.basic_enum pz on pz.enumId = d.grain_variety_id
                LEFT JOIN depot_qh.basic_enum xz on xz.enumId = d.grain_nature_id
                left join depot_qh.org_info o on o.org_id=d.org_id
        where 1=1
        <if test="rpReportId !=null and rpReportId !=''">
            and c.id = #{rpReportId}
        </if>

        <if test="orgId !=null and orgId !=''">
            and o.org_Id = #{orgId}
        </if>
    </select>

    <select id="getQualityInspectionFindByWarehouseId"  resultType="com.unis.rotationPlan.info.modular.businessrpreport.entity.BusinessQcQualityInspection"  >
        SELECT
            bqqi.id,
            bqqi.zjbgdh,
            bqqi.warehouse_id,
            bqqi.jysj,
            bqqi.basic_storehouse_id,
            bqqi.grain_variety_id,
            be1.enumName grainVarietyName,
            bqqi.check_result
        FROM
            depot_qh.business_qc_quality_inspection bqqi
                LEFT JOIN depot_qh.basic_enum be1 ON bqqi.grain_variety_id = be1.enumId
        where bqqi.warehouse_id=#{warehouseId} and bqqi.jylb=#{jylb}
    </select>

    <select id="selectRecListByPageParam" resultType="java.util.Map" parameterType="com.unis.rotationPlan.info.modular.businessrpreport.param.BusinessRpReportPageParam">
        SELECT
        brr.id,
        bo.org_id,
        o.org_name orgName,
        brr.rotation_year,
        brr.plan_name,
        brr.report_time,
        SUM( bo.plan_quantity) AS outbound_quantity,
        SUM( bi.plan_quantity) AS inbound_quantity
        FROM
        province_all.biz_rp_issuance c
        LEFT JOIN
        depot_qh.business_rp_outbound bo ON c.rp_report_id = bo.rp_report_id
        LEFT JOIN
        depot_qh.business_rp_inbound bi ON c.rp_report_id = bi.rp_report_id

        left join depot_qh.org_info o on bo.org_id=o.org_id
        left join depot_qh.business_rp_report brr on c.rp_report_id=brr.id
        where 1=1
        <if test="pageParam.orgName!=null and pageParam.orgName!=''">
            and o.org_name like concat("%",#{pageParam.orgName},"%")
        </if>

        <if test="pageParam.orgId !=null and pageParam.orgId!=''">
            and d.org_Id = #{pageParam.orgId}
        </if>
        GROUP BY
        bo.org_id,
        o.org_name,
        brr.rotation_year

    </select>
    <select id="getPlanRpReportByReportId" resultType="com.unis.rotationPlan.info.modular.businessrpreport.entity.vo.BusinessRpReportVO">
        SELECT
        o.org_name orgName,
        c.plan_name planName
        FROM
        province_all.biz_rp_issuance c
        left join depot_qh.org_info o on c.org_id=o.org_id
        left join depot_qh.business_rp_report brr on c.rp_report_id=brr.id
        left join depot_qh.business_rp_outbound bo on bo.business_rp_report =c.rp_report_id

        where 1=1
        <if test="rpReportId !=null and rpReportId !=''">
            and c.rp_report_id = #{rpReportId}
        </if>

        <if test="orgId !=null and orgId !=''">
            and bo.org_Id = #{orgId}
        </if>

    </select>
    <select id="findPlanOutByRpReportId"
            resultType="com.unis.rotationPlan.info.modular.businessrpreport.entity.BusinessRpOutbound">
        SELECT

        st.storehouse_name as "basicStorehouseName",
        wa.warehouse_name  as "warehouseName",
        pz.enumname as "grainVarietyName",
        xz.enumname as "grainNatureName",
        dj.enumname as "grainGradeName",
        m1.kcsl "storagePeriod",
        DATE_FORMAT(m1.rq,'YYYY') as "zyrq"

        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
        LEFT JOIN depot_qh.basic_storehouse st on st.storehouse_id = m1.ch
        LEFT JOIN depot_qh.basic_warehouse wa on wa.warehouse_id = m1.hwh
        LEFT JOIN depot_qh.basic_enum pz on pz.enumId = m1.pz
        LEFT JOIN depot_qh.basic_enum xz on xz.enumId = m1.hwxz
        LEFT JOIN depot_qh.basic_enum dj on dj.enumId = m1.dj
        where 1=1
        <if test="rpReportId !=null and rpReportId !=''">
            and  m4.jhdID = #{rpReportId}
        </if>

        <if test="orgId !=null and orgId !=''">
            and m1.UnitID = #{orgId}
        </if>
    </select>
    <select id="findPlanInByRpReportId"
            resultType="com.unis.rotationPlan.info.modular.businessrpreport.entity.BusinessRpInbound">
        SELECT
        st.storehouse_name as "basicStorehouseName",
        wa.warehouse_name  as "warehouseName",
        pz.enumname as "grainVarietyName",
        xz.enumname as "grainNatureName",
        dj.enumname as "grainGradeName",
        m1.kcsl "storagePeriod",
        DATE_FORMAT(m1.rq,'YYYY') as "zyrq"
        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
        LEFT JOIN depot_qh.basic_storehouse st on st.storehouse_id = m1.ch
        LEFT JOIN depot_qh.basic_warehouse wa on wa.warehouse_id = m1.hwh
        LEFT JOIN depot_qh.basic_enum pz on pz.enumId = m1.pz
        LEFT JOIN depot_qh.basic_enum xz on xz.enumId = m1.hwxz
        LEFT JOIN depot_qh.basic_enum dj on dj.enumId = m1.dj
        where 1=1
        <if test="rpReportId !=null and rpReportId !=''">
            and  m4.jhdID = #{rpReportId}
        </if>

        <if test="orgId !=null and orgId !=''">
            and m1.UnitID = #{orgId}
        </if>
    </select>
    <select id="createReportDetail"
            resultType="com.unis.rotationPlan.info.modular.businessrpreport.entity.vo.BusinessRpReportVO">
        SELECT
        bo.grain_variety_id,
        A.dwmc,
        A.org_id,
        A.org_name orgName,
        be1.enumName grainVarietyName,
        bbo.rotation_year rotationYear,
        IFNULL(SUM(bo.sealing_quantity), 0) AS outboundQuantity,
        IFNULL(SUM(bi.quantity), 0) AS inboundQuantity
        FROM
        depot_qh.business_rp_outbound bo
        LEFT JOIN depot_qh.business_rp_inbound bi ON bo.org_id = bi.org_id AND bo.grain_variety_id = bi.grain_variety_id
        left join  business_rp_report bbo on bbo.id=bo.rp_report_id
        LEFT JOIN depot_qh.basic_enum be1 on bo.grain_variety_id =be1.enumId
        left join (select o.org_id,bui.dwmc,o.org_name from province_all.biz_unit_info bui LEFT JOIN depot_qh.org_info o on  o.unit_info_id =bui.id)A
        on bo.org_id=A.org_id
        <if test="pageParam.rotationYear!= null" >
            where bbo.rotation_year=#{pageParam.rotationYear,jdbcType=INTEGER}
        </if>
        GROUP BY
        bo.grain_variety_id



    </select>


</mapper>