<?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.fsrotatedgrainstorage.mapper.BizFsRotatedGrainStorageMapper">

    <select id="getKcyjdPz" resultType="java.util.Map" parameterType="java.util.Map">
        SELECT
            UnitID orgId,pz
        FROM
            depot_qh.data_kcgl_kcyjd_default
        where nf = #{year} and UnitID = #{orgId}
        GROUP BY UnitID,pz
    </select>

    <select id="getYmswkc" resultType="java.util.Map" parameterType="java.util.Map">
        SELECT
            UnitID orgId,
            yf,
            pz,
            SUM(qmkc) qmkc
        FROM
            depot_qh.data_kcgl_kcyjd_default
        WHERE
            UnitID = #{orgId}
          AND nf = #{year}
          AND pz = #{pz}
        GROUP BY
            UnitID,yf,pz
    </select>

    <select id="getCbgm" resultType="java.util.Map" parameterType="java.util.Map">
        SELECT
            a.org_info_id orgId,
            a.scale_year scaleYear,
            a.scale_month scaleMonth,
            a.grain_variety_id pz,
            b.enumname pzmc,
            a.grain_nature_id xz,
            c.enumname xzmc,
            a.reserve_plan_quantity cbgmsl
        FROM
            biz_gr_reserve_scale a
        LEFT JOIN depot_qh.basic_enum b ON a.grain_variety_id = b.enumId
        LEFT JOIN depot_qh.basic_enum c ON a.grain_nature_id = c.enumId
        WHERE a.delete_flag = 'NOT_DELETE' AND a. STATUS = 2 -- 下发的储备规模
        AND a.grain_nature_id = 6877 -- 省(自治区、直辖市)级地方储备粮
        AND a.scale_year =  #{year}
        AND a.org_info_id = #{orgId}
        AND a.grain_variety_id = #{pz}
        AND a.scale_month = #{yf}
    </select>

    <select id="getRpOrgInfo" resultType="com.unis.basic.info.modular.orgInfo.entity.OrgInfo" parameterType="java.lang.Integer">
        select * from depot_qh.org_info where parent_id != 0 and org_id in (
            select c.org_id orgId from province_all.biz_rp_issuance a -- 轮换计划下发表
            INNER JOIN depot_qh.business_rp_report b on a.rp_report_id = b.id
            INNER JOIN depot_qh.business_rp_inbound c on c.rp_report_id = b.id
            where a.plan_year = #{year}
            GROUP BY orgId
            UNION
            select d.org_id orgId from province_all.biz_rp_issuance a --  轮换计划下发表
            INNER JOIN depot_qh.business_rp_report b on a.rp_report_id = b.id
            INNER JOIN depot_qh.business_rp_outbound d on d.rp_report_id = b.id
            where a.plan_year = #{year}
            GROUP BY orgId
            )
    </select>

    <select id="getJkqSj" parameterType="java.util.Map" resultType="java.util.Map">
        SELECT
            rotation_start rotationStart,
            rotation_end rotationEnd
        FROM
            biz_pss_storage_empty_period
        where org_id = #{orgId}
          and plan_year = #{year}
    </select>


    <select id="getZclkqsl" parameterType="java.util.Map" resultType="java.util.Map">
        select zclkqnlksl, yf
        from (
                 SELECT IFNULL(sum(m1.zcsl), 0.0) as zclkqnlksl,
                        DATE_FORMAT(m1.rq, '%m')  yf
                 FROM province_all.biz_rp_issuance a
                          INNER JOIN depot_qh.business_rp_report c ON c.id = a.rp_report_id
                          INNER JOIN depot_qh.business_rp_inbound e ON c.id = e.rp_report_id
                          INNER JOIN depot_qh.business_rp_outbound f ON c.id = f.rp_report_id
                          INNER JOIN depot_qh.org_info g on g.org_id = e.org_id
                          INNER JOIN depot_qh.data_crkyw_ckrmdj_default m3 on m3.jhdID = a.rp_report_id
                          INNER JOIN depot_qh.data_crkyw_ckcmz_default m2 on m3.bizno = m2.bizno
                          INNER JOIN depot_qh.data_kcgl_fcbgz_default m1 on m1.DataID = m2.DataID
                 where g.org_id = #{orgId}
                   and CAST(c.plan_year as char) = #{year}
                   AND e.grain_variety_id = #{pz}
                   AND m3.lspz = #{pz}
                   And m1.rq &gt;= #{rotationStart}
                   And m1.rq &lt;= #{rotationEnd}
                 GROUP BY DATE_FORMAT(m1.rq, '%m')
             ) a where yf = #{yf}
    </select>

    <select id="getClkqsl" parameterType="java.util.Map" resultType="java.util.Map">
        select clkqsl, yf
        from (
                 SELECT IFNULL(sum(m1.zcsl), 0.0) as clkqsl,
                        DATE_FORMAT(m1.rq, '%m')  yf
                 FROM province_all.biz_rp_issuance a
                          INNER JOIN depot_qh.business_rp_report c ON c.id = a.rp_report_id
                          INNER JOIN depot_qh.business_rp_inbound e ON c.id = e.rp_report_id
                          INNER JOIN depot_qh.business_rp_outbound f ON c.id = f.rp_report_id
                          INNER JOIN depot_qh.org_info g on g.org_id = e.org_id
                          INNER JOIN depot_qh.data_crkyw_ckrmdj_default m3 on m3.jhdID = a.rp_report_id
                          INNER JOIN depot_qh.data_crkyw_ckcmz_default m2 on m3.bizno = m2.bizno
                          INNER JOIN depot_qh.data_kcgl_fcbgz_default m1 on m1.DataID = m2.DataID
                 where g.org_id = #{orgId}
                   and CAST(c.plan_year as char) = #{year}
                   AND e.grain_variety_id = #{pz}
                   AND m3.lspz = #{pz}
                   And m1.rq &lt; #{rotationStart}
                   And m1.rq &gt; #{rotationEnd}
                 GROUP BY DATE_FORMAT(m1.rq, '%m')
             ) a where yf = #{yf}
    </select>
</mapper>