<?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.financialSupervision.modular.rotationCostSubsidySettle.mapper.RotationCostSubsidySettleMapper">
    <select id="getList" parameterType="com.baomidou.mybatisplus.core.conditions.Wrapper" resultType="com.unis.financialSupervision.modular.rotationCostSubsidySettle.entity.RotationCostSubsidySettle">
        SELECT
            frcs.*, oi.org_name orgName
        FROM
            biz_fs_rotation_cost_subsidy_settle frcs
        LEFT JOIN depot_qh.org_info oi ON frcs.org_id = oi.org_id
        ${ew.customSqlSegment}
    </select>
    <select id="selectChaosData" resultType="com.unis.financialSupervision.modular.rotationCostSubsidySettle.entity.RotationCostSubsidySettle">
        SELECT
            t1.orgId,
            t1.orgName,
            t1.outPlanNumber staticRawGrainPlanQuantity,
            (t1.sealing_quantity + t2.quantity)/(t1.outPlanNumber + t2.inPlanNumber) staticRawGrainCompletionRate,
            t1.standard_value staticRawGrainCostStandard,
            (t1.outPlanNumber + t2.inPlanNumber) * t1.standard_value staticRawGrainSubsidy,
            t3.outPlanNumber dynamicRawGrainPlanQuantity,
            (t3.sealing_quantity + t4.quantity)/(t3.outPlanNumber + t4.inPlanNumber) dynamicRawGrainCompletionRate,
            t3.standard_value dynamicRawGrainCostStandard,
            (t3.outPlanNumber + t4.inPlanNumber) * t3.standard_value dynamicRawGrainSubsidy,
            t1.planYear,
            t1.remarks remark
        FROM
            (
                SELECT
                    bro.org_id orgId,
                    oi.org_name orgName,
                    bri.plan_number outPlanNumber,
                    bri.plan_year planYear,
                    bro.plan_quantity,
                    bro.sealing_quantity,
                    bfcs.standard_value,
                    bro.remarks
                FROM
                    biz_rp_issuance bri
                INNER JOIN depot_qh.business_rp_report brr ON bri.rp_report_id = brr.id
                INNER JOIN depot_qh.business_rp_outbound bro ON brr.id = bro.rp_report_id
                INNER JOIN depot_qh.org_info oi ON oi.org_id = bro.org_id
                LEFT JOIN depot_qh.business_fs_cost_standard bfcs ON bfcs.org_id = oi.org_id
                WHERE
                    brr.plan_type = 1
                AND brr.type = 1
                AND brr.rotation_year = YEAR (NOW())
                AND bri.plan_year = YEAR (NOW())
                AND bfcs.standard_type = 1
            ) t1
        LEFT JOIN (
            SELECT
                brin.org_id orgId,
                oi.org_name orgName,
                bri.plan_number inPlanNumber,
                bfcs.standard_value,
                bri.plan_year planYear,
                brin.plan_quantity,
                brin.quantity
            FROM
                biz_rp_issuance bri
            INNER JOIN depot_qh.business_rp_report brr ON bri.rp_report_id = brr.id
            INNER JOIN depot_qh.business_rp_inbound brin ON brr.id = brin.rp_report_id
            INNER JOIN depot_qh.org_info oi ON oi.org_id = brin.org_id
            LEFT JOIN depot_qh.business_fs_cost_standard bfcs ON bfcs.org_id = oi.org_id
            WHERE
                brr.plan_type = 1
            AND brr.type = 1
            AND brr.rotation_year = YEAR (NOW())
            AND bri.plan_year = YEAR (NOW())
            AND bfcs.standard_type = 1
        ) t2 ON t1.orgId = t2.orgId
        LEFT JOIN (
                SELECT
                    bro.org_id orgId,
                    oi.org_name orgName,
                    bri.plan_number outPlanNumber,
                    bri.plan_year planYear,
                    bro.plan_quantity,
                    bro.sealing_quantity,
                    bfcs.standard_value
                FROM
                    biz_rp_issuance bri
                INNER JOIN depot_qh.business_rp_report brr ON bri.rp_report_id = brr.id
                INNER JOIN depot_qh.business_rp_outbound bro ON brr.id = bro.rp_report_id
                INNER JOIN depot_qh.org_info oi ON oi.org_id = bro.org_id
                LEFT JOIN depot_qh.business_fs_cost_standard bfcs ON bfcs.org_id = oi.org_id
                WHERE
                    brr.plan_type = 1
                AND brr.type = 2
                AND brr.rotation_year = YEAR (NOW())
                AND bri.plan_year = YEAR (NOW())
                AND bfcs.standard_type = 1
        ) t3 ON t1.orgId = t2.orgId AND t2.orgId = t3.orgId
        LEFT JOIN (
            SELECT
                brin.org_id orgId,
                oi.org_name orgName,
                bri.plan_number inPlanNumber,
                bfcs.standard_value,
                bri.plan_year planYear,
                brin.plan_quantity,
                brin.quantity
            FROM
                biz_rp_issuance bri
            INNER JOIN depot_qh.business_rp_report brr ON bri.rp_report_id = brr.id
            INNER JOIN depot_qh.business_rp_inbound brin ON brr.id = brin.rp_report_id
            INNER JOIN depot_qh.org_info oi ON oi.org_id = brin.org_id
            LEFT JOIN depot_qh.business_fs_cost_standard bfcs ON bfcs.org_id = oi.org_id
            WHERE
                brr.plan_type = 1
            AND brr.type = 2
            AND brr.rotation_year = YEAR (NOW())
            AND bri.plan_year = YEAR (NOW())
            AND bfcs.standard_type = 1
        ) t4 ON t3.orgId = t4.orgId
    </select>
</mapper>