<?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.rpIssuance.mapper.RpIssuanceMapper">
    <update id="updateForOut">
       update  depot_qh.business_rp_outbound
       set plan_quantity=#{planQuantity}

           where id = #{id,jdbcType=INTEGER}
    </update>
    <update id="updateForIn">
        update  depot_qh.business_rp_inbound
        set plan_quantity=#{planNum}

        where id = #{id,jdbcType=INTEGER}

    </update>

    <select id="selectRIPage" resultType="com.unis.rotationPlan.info.modular.rpIssuance.entity.vo.RpIssuanceVO">
        SELECT DISTINCT
        c.id as "rpReportId",
        c.rotation_year as "rotationYear",
        a.plan_number,
        a.start_date,
        a.end_date
        FROM
        province_all.biz_rp_issuance a
        inner JOIN depot_qh.business_rp_report c  ON c.id = a.rp_report_id
        where c.status = 1

        <if test="PageParam.rotationYear !=null and PageParam.rotationYear!=''">
            and c.rotation_year =#{PageParam.rotationYear}
        </if>

        <if test="PageParam.planNumber !=null and PageParam.planNumber!=''">
            and a.plan_number like concat("%", #{PageParam.planNumber},"%")
        </if>
        GROUP BY c.rotation_year
    </select>
    <select id="getRpIssuanceByrpReportId"
            resultType="com.unis.rotationPlan.info.modular.rpIssuance.entity.vo.RpIssuanceVO">
        SELECT DISTINCT
        c.id rpReportId,
        bri.plan_number planNumber,
        bri.plan_name planName,
        bri.plan_year rotationYear,
        bri.start_date startDate,
        bri.end_date endDate,
        bri.issuance_date issuanceDate,
        SUM(bro.plan_quantity) planQuantity
        FROM
        province_all.biz_rp_issuance bri
        LEFT JOIN depot_qh.business_rp_report c ON c.id = bri.rp_report_id
        LEFT JOIN depot_qh.business_rp_outbound bro ON bro.rp_report_id = c.id
        where c.status =1
        <if test="PageParam.rotationYear !=null and PageParam.rotationYear!=''">
            and  cast(c.rotation_year as char) =#{PageParam.rotationYear}
        </if>


    </select>
    <select id="getOutIssuanceByrpReportId"
            resultType="com.unis.rotationPlan.info.modular.businessrpreport.entity.BusinessRpOutbound">
        SELECT DISTINCT
        d.id,
        d.rp_report_id,
        d.org_id,
        o.org_name orgName,
        d.basic_storehouse_id,
        d.warehouse_id,
        d.grain_variety_id,
        d.grain_nature_id,
        d.grain_grade_id,
        d.storage_period storagePeriod,
        d.purchase_price purchasePrice,
        d.product_year,
        d.remarks,
        d.inspection_report_id,
        d.regular_inspection_report_ids,
        d.plan_quantity sealingQuantity,
        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 d.org_id =o.org_id
        where c.status =1
        <if test="PageParam.rotationYear !=null and PageParam.rotationYear!=''">
            and  cast(c.rotation_year as char) =#{PageParam.rotationYear}
        </if>


    </select>
    <select id="getRpIssuanceByadd"
            resultType="com.unis.rotationPlan.info.modular.rpIssuance.entity.vo.RpIssuanceVO">
        SELECT DISTINCT
            c.id rpReportId,
            SUM(bro.sealing_quantity) planQuantity
        FROM depot_qh.business_rp_report c
                 LEFT JOIN depot_qh.business_rp_outbound bro ON bro.rp_report_id = c.id
        where
        c.status =1
        <if test="PageParam.rotationYear !=null and PageParam.rotationYear!=''">
            and  cast(c.rotation_year as char) =#{PageParam.rotationYear}
        </if>
    </select>
    <select id="getNewOutIssuanceByrpReportId"
            resultType="com.unis.rotationPlan.info.modular.businessrpreport.entity.BusinessRpOutbound">
        SELECT DISTINCT
        d.id,
        d.rp_report_id,
        d.org_id,
        o.org_name orgName,
        d.basic_storehouse_id,
        d.warehouse_id,
        d.grain_variety_id,
        d.grain_nature_id,
        d.grain_grade_id,
        d.sealing_quantity planQuantity,
        d.storage_period storagePeriod,
        d.purchase_price purchasePrice,
        d.product_year,
        d.remarks,
        d.inspection_report_id,
        d.regular_inspection_report_ids,
        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 d.org_id =o.org_id
        where c.status =1
        <if test="PageParam.rotationYear !=null and PageParam.rotationYear!=''">
            and  cast(c.rotation_year as char) =#{PageParam.rotationYear}
        </if>





    </select>
    <select id="selectRpportInbound"
            resultType="com.unis.rotationPlan.info.modular.businessrpreport.entity.BusinessRpInbound">
        select   id,org_id,grain_variety_id,grain_nature_id,plan_quantity  from depot_qh.business_rp_inbound
        where org_id=#{orgId} and rp_report_id=#{rpReportId} and grain_nature_id=#{grainNatureId} and grain_variety_id=#{grainVarietyId}

    </select>


</mapper>