<?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.fsEnterpriseBalanceStatistic.mapper.FsEnterpriseBalanceStatisticMapper">

    <select id="getReserveScaleList" resultType="com.unis.financialSupervision.modular.fsEnterpriseBalanceStatistic.entity.FsEnterpriseBalanceStatistic">
        SELECT
        a.org_info_id as org_id,
        a.scale_year as plan_year,
        a.scale_month  as month,
        a.grain_variety_id as grainVarietyId,
        SUM(a.reserve_plan_quantity*1000) as reservePlanQuantity,
        IFNULL(b.standard_value, 0.0) as storageCostStandard,
		SUM(a.reserve_plan_quantity*1000)*IFNULL(b.standard_value, 0.0)/10000 as storageCostAccrued
    FROM
        biz_gr_reserve_scale a
    LEFT JOIN depot_qh.business_fs_cost_standard b ON a.org_info_id = b.org_id
    AND a.grain_variety_id = b.grain_variety_id and b.standard_type = 2
    WHERE
        a.delete_flag = 'NOT_DELETE'
    and CAST(a.scale_year as char)  = #{year}
    and CAST(a.scale_month as char)  = #{month}
    GROUP BY
        a.org_info_id,
        a.scale_year,
        a.scale_month,
        a.grain_variety_id,
        b.standard_value
    ORDER BY
        a.org_info_id,
        a.scale_year,
        a.scale_month,
        a.grain_variety_id
    </select>
    <select id="getRotationPlan" resultType="com.unis.financialSupervision.modular.fsEnterpriseBalanceStatistic.entity.FsEnterpriseBalanceStatistic">
         SELECT

		planQuantity*1000 as rotationPlanQuantity,
		IFNULL((ckslSum+rkslSum)/(planQuantity*1000)*100,0) rotationCompletionRate,
		IFNULL(b.standard_value, 0.0) as rotationCostStandard
 FROM (SELECT
            g.org_id,

         (IFNULL(SUM(f.plan_quantity)+ SUM(e.plan_quantity),0.0)) as planQuantity,
            IFNULL(SUM(f.plan_quantity),0.0) 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
                    where m3.jhdID = a.rp_report_id AND m3.lspz = #{grainVarietyId}
            )  as "ckslSum",
             IFNULL(SUM(e.plan_quantity),0.0) 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
                    where m3.jhdh = a.rp_report_id  AND m3.lspz = #{grainVarietyId}
            )  as "rkslSum"
    FROM
     province_all.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
    right JOIN  depot_qh.org_info  g on g.org_id = e.org_id
 where g.org_id = #{orgId} and CAST(c.plan_year as char)  = #{year} AND e.grain_variety_id = #{grainVarietyId}
    GROUP BY g.org_id
) x
LEFT JOIN depot_qh.business_fs_cost_standard b ON x.org_id = b.org_id
AND b.grain_variety_id =#{grainVarietyId} and b.standard_type = 1
    </select>
    <select id="getList" resultType="com.unis.financialSupervision.modular.fsEnterpriseBalanceStatistic.entity.VO.FsEnterpriseBalanceStatisticVO">
        SELECT
        ROW_NUMBER() OVER (ORDER BY id DESC) AS rownum,
        a.id,
        a.org_id,
        b.org_name,
        a.plan_year,
        a.month,
        a.grain_variety_id,
        c.enumname as grain_variety_name,
        a.reserve_plan_quantity,
        a.storage_cost_standard,
        a.rotation_plan_quantity,
        a.rotation_completion_rate,
        a.rotation_cost_standard,
        a.storage_cost_accrued,
        a.storage_cost_expenditure,
        a.storage_cost_balance,
        a.storage_cost_overdraft_warning,
        a.storage_cost_overdraft_month,
        a.rotation_cost_accrued,
        a.rotation_cost_expenditure,
        a.rotation_cost_balance,
        a.rotation_cost_overdraft_warning,
        a.rotation_cost_overdraft_month,
        a.reason_for_accrual_overdraft,
        a.reason_for_expenditure_overdraft
        FROM biz_fs_enterprise_balance_statistic a
        LEFT JOIN depot_qh.org_info b on a.org_id = b.org_id
        LEFT JOIN depot_qh.basic_enum c on a.grain_variety_id = c.enumid
        where 1=1
        <if test="pageParam.orgId != null and pageParam.orgId != ''">
            and a.org_id = #{pageParam.orgId}
        </if>
        <if test="pageParam.year != null ">
            and CAST(a.plan_year as char) = #{pageParam.year}
        </if>
        <if test="pageParam.year != null ">
            and a.month = #{pageParam.month}
        </if>
    </select>
    <select id="getRootTree" resultType="com.unis.financialSupervision.modular.fsEnterpriseBalanceStatistic.entity.VO.UnitAndOrgTreeVO">
        SELECT
        CAST(org_id as char) AS id,
        org_name as name
        FROM
            depot_qh.org_info
        WHERE
	IFNULL(parent_id, 0) = 0
    </select>
    <select id="getUnitList" resultType="com.unis.financialSupervision.modular.fsEnterpriseBalanceStatistic.entity.VO.UnitAndOrgTreeVO">
        SELECT
            id,
            dwmc as name
        FROM
            biz_unit_info
        WHERE
             delete_flag  =  'NOT_DELETE'
        ORDER BY  id
    </select>
    <select id="getOrgList" resultType="com.unis.financialSupervision.modular.fsEnterpriseBalanceStatistic.entity.VO.UnitAndOrgTreeVO">
      SELECT
        CAST(org_id AS CHAR) AS id,
        org_name AS name,
        org_id
    FROM
        depot_qh.org_info
    WHERE  unit_info_id = #{unitId}
    </select>
    <select id="selectMyMapsPage" resultType="com.unis.financialSupervision.modular.fsEnterpriseBalanceStatistic.entity.VO.FsEnterpriseBalanceStatisticVO">
      SELECT
        a.id,
        c.dwmc AS unitName,
        b.org_name AS orgName,
        d.enumname AS grain_variety_name,
        a.storage_cost_balance,
        a.rotation_cost_balance,
        a.update_time AS warnDate
    FROM
        biz_fs_enterprise_balance_statistic a
    LEFT JOIN depot_qh.org_info b ON a.org_id = b.org_id
    LEFT JOIN biz_unit_info c ON b.unit_info_id = c.id
    LEFT JOIN depot_qh.basic_enum d ON a.grain_variety_id = d.enumid
    WHERE
    (
        storage_cost_overdraft_warning = 2
        OR rotation_cost_overdraft_warning = 2
    )
    <if test="ew.sqlSegment != null and ew.sqlSegment != ''">
        and  ${ew.sqlSegment}
    </if>
    </select>
    <select id="getCostBalanceList" resultType="com.unis.financialSupervision.modular.fsEnterpriseBalanceStatistic.entity.VO.CostBalanceVO">
        SELECT
        '1' as type,
        IFNULL(MAX(CASE WHEN month = 1 THEN storage_cost_balance ELSE 0.0 END),0.0) AS 'january',
        IFNULL(MAX(CASE WHEN month = 2 THEN storage_cost_balance ELSE 0.0 END),0.0) AS 'february',
        IFNULL(MAX(CASE WHEN month = 3 THEN storage_cost_balance ELSE 0.0 END),0.0) AS 'march',
        IFNULL(MAX(CASE WHEN month = 4 THEN storage_cost_balance ELSE 0.0 END),0.0) AS 'april',
        IFNULL(MAX(CASE WHEN month = 5 THEN storage_cost_balance ELSE 0.0 END),0.0) AS 'may',
        IFNULL(MAX(CASE WHEN month = 6 THEN storage_cost_balance ELSE 0.0 END),0.0) AS 'june',
        IFNULL(MAX(CASE WHEN month = 7 THEN storage_cost_balance ELSE 0.0 END),0.0) AS 'july',
        IFNULL(MAX(CASE WHEN month = 8 THEN storage_cost_balance ELSE 0.0 END),0.0) AS 'august',
        IFNULL(MAX(CASE WHEN month = 9 THEN storage_cost_balance ELSE 0.0 END),0.0) AS 'september',
        IFNULL(MAX(CASE WHEN month = 10 THEN storage_cost_balance ELSE 0.0 END),0.0) AS 'october',
        IFNULL(MAX(CASE WHEN month = 11 THEN storage_cost_balance ELSE 0.0 END),0.0) AS 'november',
        IFNULL(MAX(CASE WHEN month = 12 THEN storage_cost_balance ELSE 0.0 END),0.0) AS 'december'
     FROM (
        SELECT
            a.month as 'month',
            SUM(a.storage_cost_balance) storage_cost_balance
        FROM
            biz_fs_enterprise_balance_statistic a
        WHERE
             CAST(a.plan_year AS CHAR) = #{costBalanceParam.year}
        <if test="costBalanceParam.orgId != null and costBalanceParam.orgId != ''">
            and a.org_id = #{costBalanceParam.orgId}
        </if>
        GROUP BY
            a. MONTH
    ) x
    UNION ALL
    SELECT
        '2' as type,
        IFNULL(MAX(CASE WHEN month = 1 THEN rotation_cost_balance ELSE 0.0 END),0.0) AS 'january',
        IFNULL(MAX(CASE WHEN month = 2 THEN rotation_cost_balance ELSE 0.0 END),0.0) AS 'february',
        IFNULL(MAX(CASE WHEN month = 3 THEN rotation_cost_balance ELSE 0.0 END),0.0) AS 'march',
        IFNULL(MAX(CASE WHEN month = 4 THEN rotation_cost_balance ELSE 0.0 END),0.0) AS 'april',
        IFNULL(MAX(CASE WHEN month = 5 THEN rotation_cost_balance ELSE 0.0 END),0.0) AS 'may',
        IFNULL(MAX(CASE WHEN month = 6 THEN rotation_cost_balance ELSE 0.0 END),0.0) AS 'june',
        IFNULL(MAX(CASE WHEN month = 7 THEN rotation_cost_balance ELSE 0.0 END),0.0) AS 'july',
        IFNULL(MAX(CASE WHEN month = 8 THEN rotation_cost_balance ELSE 0.0 END),0.0) AS 'august',
        IFNULL(MAX(CASE WHEN month = 9 THEN rotation_cost_balance ELSE 0.0 END),0.0) AS 'september',
        IFNULL(MAX(CASE WHEN month = 10 THEN rotation_cost_balance ELSE 0.0 END),0.0) AS 'october',
        IFNULL(MAX(CASE WHEN month = 11 THEN rotation_cost_balance ELSE 0.0 END),0.0) AS 'november',
        IFNULL(MAX(CASE WHEN month = 12 THEN rotation_cost_balance ELSE 0.0 END),0.0) AS 'december'
     FROM (
        SELECT
             a.month as 'month',
            SUM(a.rotation_cost_balance) rotation_cost_balance
        FROM
            biz_fs_enterprise_balance_statistic a
        WHERE
             CAST(a.plan_year AS CHAR) = #{costBalanceParam.year}
            <if test="costBalanceParam.orgId != null and costBalanceParam.orgId != ''">
                and a.org_id = #{costBalanceParam.orgId}
            </if>
        GROUP BY
            a. MONTH
    ) x
    </select>
</mapper>