
-- ======================================
-- 语句处理
-- ======================================

-- 月份数据是执行的
-- 增加计划是计划调整为正数的
-- 减少计划是计划调整为负数的

-- 计划调整
-- 订单执行
-- 订单预占
-- 合同约定


select
        a.year              -- 年度
       ,a.month           -- 月度
       ,a.deal_code         -- 主经销商编码
       ,a.deal_name         -- 主经销商名称
       ,a.src_deal_code     -- 子经销商编码
       ,a.src_deal_name     -- 子经销商名称
       ,d.fist_dept_code as bu_code -- 事业部编码
       ,d.secd_dept_code as region_code -- 大区编码
       ,d.secd_dept_name as region_name  -- 大区名称
       ,a.plan_type  -- 计划类型
       ,a.plan_item  -- 计划项目
       ,a.plan_pi_name -- 计划品项
         -- 月份的
       ,a.m_plan_in_num  AS m_plan_in_num -- 计划内瓶数
       ,a.m_plan_in_pack AS m_plan_in_pack -- 计划内件数
       ,a.m_plan_in_ton  AS m_plan_in_ton -- 计划内吨数
       ,a.m_plan_in_amt / 10000 AS m_plan_in_amt  -- 计划内金额
       ,a.m_plan_out_num AS m_plan_out_num -- 计划外瓶数
       ,a.m_plan_out_pack AS m_plan_out_pack   -- 计划外件数
       ,a.m_plan_out_ton  AS m_plan_out_ton-- 计划外吨数
       ,a.m_plan_out_amt  / 10000 AS m_plan_out_amt -- 计划外金额
     -- 合同约定
       ,a.ct_plan_in_num   AS ct_plan_in_num  -- 合同约定计划内瓶数
       ,a.ct_plan_in_pack  AS ct_plan_in_pack -- 合同约定计划内件数
       ,a.ct_plan_in_ton   AS ct_plan_in_ton  -- 合同约定计划内吨数
       ,a.ct_plan_in_amt   / 10000 AS ct_plan_in_amt -- 合同约定计划内金额
       ,a.ct_plan_out_num  AS ct_plan_out_num -- 合同约定计划外瓶数
       ,a.ct_plan_out_pack AS ct_plan_out_pack -- 合同约定计划外件数
       ,a.ct_plan_out_ton  AS ct_plan_out_ton -- 合同约定计划外吨数
       ,a.ct_plan_out_amt  / 10000 AS ct_plan_out_amt -- 合同约定计划外金额

     -- 增加计划
       ,a.add_plan_in_num AS add_plan_in_num    -- 增加计划内瓶数
       ,a.add_plan_in_pack AS add_plan_in_pack  -- 增加计划内件数
       ,a.add_plan_in_ton  AS add_plan_in_ton   -- 增加计划内吨数
       ,a.add_plan_in_amt / 10000 AS add_plan_in_amt-- 增加计划内金额
       ,a.add_plan_out_num   AS add_plan_out_num  -- 增加计划外瓶数
       ,a.add_plan_out_pack   AS add_plan_out_pack -- 增加计划外件数
       ,a.add_plan_out_ton    AS add_plan_out_ton  -- 增加计划外吨数
       ,a.add_plan_out_amt   / 10000 ASadd_plan_out_amt -- 增加计划外金额

     -- 减少计划
       ,a.cut_plan_in_num   AS cut_plan_in_num  -- 减少计划内瓶数
       ,a.cut_plan_in_pack  AS cut_plan_in_pack -- 减少计划内件数
       ,a.cut_plan_in_ton   AS cut_plan_in_ton  -- 减少计划内吨数
       ,a.cut_plan_in_amt   / 10000 AS cut_plan_in_amt-- 减少计划内金额
       ,a.cut_plan_out_num    AS cut_plan_out_num  -- 减少计划外瓶数
       ,a.cut_plan_out_park   AS cut_plan_out_park -- 减少计划外件数
       ,a.cut_plan_out_ton    AS cut_plan_out_ton  -- 减少计划外吨数
       ,a.cut_plan_out_amt / 10000 AS cut_plan_out_amt -- 减少加计划外金额
from
(
        SELECT
        -- 维度
            year              -- 年度
         ,month             -- 月度
         ,deal_code         -- 主经销商编码
         ,deal_name         -- 主经销商名称
         ,src_deal_code     -- 子经销商编码
         ,src_deal_name     -- 子经销商名称
         ,plan_type         -- 计划类型
         ,plan_item         -- 计划项目
         ,plan_pi_name      -- 计划品项
         ,city_unit_code
         ,plan_pi_code  -- 计划品相编码
        -- 月份的
        ,CASE WHEN plan_item = '订单执行' THEN plan_in_num ELSE 0 END AS m_plan_in_num  -- 计划内瓶数
        ,CASE WHEN plan_item = '订单执行' THEN plan_in_pack ELSE 0 END AS m_plan_in_pack  -- 计划内件数
        ,CASE WHEN plan_item = '订单执行' THEN plan_in_ton ELSE 0 END AS m_plan_in_ton  -- 计划内吨数
        ,CASE WHEN plan_item = '订单执行' THEN plan_in_amt ELSE 0 END AS m_plan_in_amt  -- 计划内金额
        ,CASE WHEN plan_item = '订单执行' THEN plan_out_num ELSE 0 END AS m_plan_out_num  -- 计划外瓶数
        ,CASE WHEN plan_item = '订单执行' THEN plan_out_pack ELSE 0 END AS m_plan_out_pack  -- 计划外件数
        ,CASE WHEN plan_item = '订单执行' THEN plan_out_ton ELSE 0 END AS m_plan_out_ton  -- 计划外吨数
        ,CASE WHEN plan_item = '订单执行' THEN plan_out_amt ELSE 0 END AS m_plan_out_amt  -- 计划外金额
        -- 合同约定
        ,CASE WHEN plan_item = '合同约定' THEN plan_in_num ELSE 0 END AS ct_plan_in_num  -- 合同约定计划内瓶数
        ,CASE WHEN plan_item = '合同约定' THEN plan_in_pack ELSE 0 END AS ct_plan_in_pack  -- 合同约定计划内件数
        ,CASE WHEN plan_item = '合同约定' THEN plan_in_ton ELSE 0 END AS ct_plan_in_ton  -- 合同约定计划内吨数
        ,CASE WHEN plan_item = '合同约定' THEN plan_in_amt ELSE 0 END AS ct_plan_in_amt  -- 合同约定计划内金额
        ,CASE WHEN plan_item = '合同约定' THEN plan_out_num ELSE 0 END AS ct_plan_out_num  -- 合同约定计划外瓶数
        ,CASE WHEN plan_item = '合同约定' THEN plan_out_pack ELSE 0 END AS ct_plan_out_pack  -- 合同约定计划外件数
        ,CASE WHEN plan_item = '合同约定' THEN plan_out_ton ELSE 0 END AS ct_plan_out_ton  -- 合同约定计划外吨数
        ,CASE WHEN plan_item = '合同约定' THEN plan_out_amt ELSE 0 END AS ct_plan_out_amt  -- 合同约定计划外金额

        -- 增加计划
        ,CASE WHEN (plan_item = '计划调整' AND plan_in_num > 0) THEN plan_in_num ELSE 0 END AS add_plan_in_num  -- 增加计划内瓶数
        ,CASE WHEN (plan_item = '计划调整' AND plan_in_pack > 0) THEN plan_in_pack ELSE 0 END AS add_plan_in_pack  -- 增加计划内件数
        ,CASE WHEN (plan_item = '计划调整' AND plan_in_ton > 0) THEN plan_in_ton ELSE 0 END AS add_plan_in_ton  -- 增加计划内吨数
        ,CASE WHEN (plan_item = '计划调整' AND plan_in_amt > 0) THEN plan_in_amt ELSE 0 END AS add_plan_in_amt  -- 增加计划内金额
        ,CASE WHEN (plan_item = '计划调整' AND plan_out_num > 0) THEN plan_out_num ELSE 0 END AS add_plan_out_num  -- 增加计划外瓶数
        ,CASE WHEN (plan_item = '计划调整' AND plan_out_pack > 0) THEN plan_out_pack ELSE 0 END AS add_plan_out_pack  -- 增加计划外件数
        ,CASE WHEN (plan_item = '计划调整' AND plan_out_ton > 0) THEN plan_out_ton ELSE 0 END AS add_plan_out_ton  -- 增加计划外吨数
        ,CASE WHEN (plan_item = '计划调整' AND plan_out_amt > 0) THEN plan_out_amt ELSE 0 END AS add_plan_out_amt  -- 增加计划外金额

        -- 减少计划
        ,CASE WHEN (plan_item = '计划调整' AND plan_in_num < 0) THEN plan_in_num ELSE 0 END AS cut_plan_in_num  -- 减少计划内瓶数
        ,CASE WHEN (plan_item = '计划调整' AND plan_in_num < 0) THEN plan_in_pack ELSE 0 END AS cut_plan_in_pack  -- 减少计划内件数
        ,CASE WHEN (plan_item = '计划调整' AND plan_in_num < 0) THEN plan_in_ton ELSE 0 END AS cut_plan_in_ton  -- 减少计划内吨数
        ,CASE WHEN (plan_item = '计划调整' AND plan_in_num < 0) THEN plan_in_amt ELSE 0 END AS cut_plan_in_amt  -- 减少计划内金额
        ,CASE WHEN (plan_item = '计划调整' AND plan_in_num < 0) THEN plan_out_num ELSE 0 END AS cut_plan_out_num  -- 减少计划外瓶数
        ,CASE WHEN (plan_item = '计划调整' AND plan_in_num < 0) THEN plan_out_pack ELSE 0 END AS cut_plan_out_park  -- 减少计划外件数
        ,CASE WHEN (plan_item = '计划调整' AND plan_in_num < 0) THEN plan_out_ton ELSE 0 END AS cut_plan_out_ton  -- 减少计划外吨数
        ,CASE WHEN (plan_item = '计划调整' AND plan_in_num < 0) THEN plan_out_amt ELSE 0 END AS cut_plan_out_amt  -- 减少加计划外金额
        FROM dm.dm_cus_flavour_plan_executed
        WHERE year = '${sStart_year}'
        -- 计划类型
        ${if(len(sPlan_type) == 0,"","and plan_type IN ('"+sPlan_type+"')")}
        -- 计划品项
        ${if(len(sPlan_pi_name) == 0,"","and plan_pi_name IN ('"+sPlan_pi_name+"')")}
        -- 经销商
        ${if(sSub_main == '2',if(len(sDeal) == 0,"","and deal_code IN ('"+ sDeal+"')"),if(len(sDeal) == 0,"","and src_deal_code IN ('"+ sDeal +"')"))}
)a
left join dwr.dim_department d on a.city_unit_code=d.dept_code
left join
(
    select
            bu_code -- 事业部编码
             ,deal_code  -- 经销商编码
             ,'1' as is_suspend
        from dwi.dwi_mdm_customers_lifecycle -- 经销商生命周期
        where bill_type='终止'  -- 单据类型
        group by bu_code,deal_code
        having max(bill_date)<=sysdate   -- 单据时间
) stp
on a.src_deal_code=stp.deal_code
and d.fist_dept_code=stp.bu_code
-- 排序
left join sdi_xdata.mdm_dim_sorted st on d.secd_dept_code=st.sorted_value and st.sorted_type='大区'
-- 权限控制
where exists (
    select
        1
    from
        dm.dm_bi_user_permisson
    where
        bi_user = '${fine_username}'
      and per_model IN ('all','sal')
      and (
        is_all = 1
            OR per_code = d.fist_dept_code  -- 事业部
            OR per_code = d.secd_dept_code  -- 大区
         --    OR per_code = a.office_code
            OR per_code = a.city_unit_code -- 城市单元
        )
)
-- 事业部
${if(len(sBu) == 0,"","and stp.bu_code IN ('"+sBu+"')")}
-- 经销商是否终止
 ${if(len(sIs_stop_deal) == 0,"","and nvl(stp.is_suspend,'0') = '"+sIs_stop_deal+"'")}
ORDER BY nvl(st.sorted_no,99)

limit ${sPage_rows} offset (${pJump_page}-1)*${sPage_rows}

;





-- 计划类型
SELECT DISTINCT plan_type FROM dm.dm_cus_flavour_plan_executed
SELECT DISTINCT plan_item FROM dm.dm_cus_flavour_plan_executed


-- 计划品项
SELECT
DISTINCT
plan_pi_name
FROM dm.dm_cus_flavour_plan_executed
WHERE 1=1
-- 计划类型
${if(len(sPlantype) == 0,"","and plan_type IN ('"+sPlantype+"')")}
-- 子主经销商
${if(sDeal == 1,if(len(sDealName) == 0,"","and deal_code IN ('"+ sDealName+"')"),if(len(sDealName) == 0,"","and src_deal_code IN ('"+ sDealName +"')"))}





SELECT CONCAT('数据更新时间:',LEFT(MAX(update_time),19)) AS update_time FROM dm.dm_cus_flavour_plan_executed
