-- ======================================
-- 主语句
-- ======================================
select a.period_id,   -- 时间ID
       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.plan_in_num,  -- 计划内瓶数
       a.plan_in_pack, -- 计划内件数
       a.plan_in_ton,  -- 计划内吨数
       a.plan_in_amt,  -- 计划内金额
       a.plan_out_num,  -- 计划外瓶数
       a.plan_out_pack,  -- 计划外件数
       a.plan_out_ton,  -- 计划外吨数
       a.plan_out_amt, -- 计划外金额
       a.create_by,   -- 创建人
       a.create_time, -- 创建时间
       a.update_by,   -- 更新人
       a.update_time,  -- 更新时间
       a.plan_pi_code,  -- 计划品相编码
       nvl(stp.is_suspend,'0') as is_suspend
from
(
    SELECT
    *
    FROM dm.dm_cus_flavour_plan_executed
    WHERE year = '${SYear}'
    -- 计划类型
    ${if(len(sPlantype) == 0,"","and plan_type IN ('"+sPlantype+"')")}
    -- 计划品项
    ${if(len(sPlanpi) == 0,"","and plan_pi_name IN ('"+sPlanpi+"')")}
    -- 子主经销商
    ${if(sDeal == 1,if(len(sDealName) == 0,"","and deal_code IN ('"+ sDealName+"')"),if(len(sDealName) == 0,"","and src_deal_code IN ('"+ sDealName +"')"))}
)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
WHERE 1=1
-- 事业部
${if(len(sBu) == 0,"","and stp.bu_code IN ('"+sBu+"')")}
-- 经销商是否终止
${if(len(SIsstop) == 0,"","and nvl(stp.is_suspend,'0') = '"+SIsstop+"'")}
ORDER BY  a.month
;

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

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

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


select
        a.year              -- 年度
       ,CONCAT(a.month,'月达成')  AS  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 -- 计划品项
         -- 月份的
       ,SUM(a.m_plan_in_num) AS m_plan_in_num -- 计划内瓶数
       ,SUM(a.m_plan_in_pack) AS m_plan_in_pack -- 计划内件数
       ,SUM(a.m_plan_in_ton)  AS m_plan_in_ton -- 计划内吨数
       ,SUM(a.m_plan_in_amt) / 10000 AS m_plan_in_amt  -- 计划内金额
       ,SUM(a.m_plan_out_num) AS m_plan_out_num -- 计划外瓶数
       ,SUM(a.m_plan_out_pack) AS m_plan_out_pack   -- 计划外件数
       ,SUM(a.m_plan_out_ton)  AS m_plan_out_ton-- 计划外吨数
       ,SUM(a.m_plan_out_amt)  / 10000 AS m_plan_out_amt -- 计划外金额
     -- 合同约定
       ,SUM(a.ct_plan_in_num  ) AS ct_plan_in_num  -- 合同约定计划内瓶数
       ,SUM(a.ct_plan_in_pack ) AS ct_plan_in_pack -- 合同约定计划内件数
       ,SUM(a.ct_plan_in_ton  ) AS ct_plan_in_ton  -- 合同约定计划内吨数
       ,SUM(a.ct_plan_in_amt)  / 10000 AS ct_plan_in_amt -- 合同约定计划内金额
       ,SUM(a.ct_plan_out_num)  AS ct_plan_out_num -- 合同约定计划外瓶数
       ,SUM(a.ct_plan_out_pack) AS ct_plan_out_pack -- 合同约定计划外件数
       ,SUM(a.ct_plan_out_ton)  AS ct_plan_out_ton -- 合同约定计划外吨数
       ,SUM(a.ct_plan_out_amt)   / 10000 AS ct_plan_out_amt -- 合同约定计划外金额

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

     -- 减少计划
       ,SUM(a.cut_plan_in_num  ) AS cut_plan_in_num  -- 减少计划内瓶数
       ,SUM(a.cut_plan_in_pack ) AS cut_plan_in_pack -- 减少计划内件数
       ,SUM(a.cut_plan_in_ton  ) AS cut_plan_in_ton  -- 减少计划内吨数
       ,SUM(a.cut_plan_in_amt  ) / 10000 AS cut_plan_in_amt-- 减少计划内金额
       ,SUM(a.cut_plan_out_num  )  AS cut_plan_out_num  -- 减少计划外瓶数
       ,SUM(a.cut_plan_out_park )  AS cut_plan_out_park -- 减少计划外件数
       ,SUM(a.cut_plan_out_ton  )  AS cut_plan_out_ton  -- 减少计划外吨数
       ,SUM(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 = '${SYear}'
              -- 计划类型
            ${if(len(sPlantype) == 0,"","and plan_type IN ('"+sPlantype+"')")}
    -- 计划品项
              ${if(len(sPlanpi) == 0,"","and plan_pi_name IN ('"+sPlanpi+"')")}
              -- 子主经销商
              ${if(sDeal == 1,if(len(sDealName) == 0,"","and deal_code IN ('"+ sDealName +"')"),if(len(sDealName) == 0,"","and src_deal_code IN ('"+ sDealName +"')"))}

)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
WHERE 1=1
-- 事业部
    ${if(len(sBu) == 0,"","and stp.bu_code IN ('"+sBu+"')")}
-- 经销商是否终止
      ${if(len(SIsstop) == 0,"","and nvl(stp.is_suspend,'0') = '"+SIsstop+"'")}
GROUP BY
    a.year              -- 年度
        ,CONCAT(a.month,'月达成')              -- 月度
        ,a.deal_code         -- 主经销商编码
        ,a.deal_name         -- 主经销商名称
        ,a.src_deal_code     -- 子经销商编码
        ,a.src_deal_name     -- 子经销商名称
        ,d.fist_dept_code -- 事业部编码
        ,d.secd_dept_code   -- 大区编码
        ,d.secd_dept_name    -- 大区名称
        ,a.plan_type  -- 计划类型
        ,a.plan_item  -- 计划项目
        ,a.plan_pi_name -- 计划品项
ORDER BY  CONCAT(a.month,'月达成')
;





-- 计划类型
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
    period_year,
    quarter,
    month,
    period_year||month as "年月"
from dwr.dim_date
where period_year<=to_char(sysdate-30,'yyyy') + 2
  and period_year>='2019'
group by period_year,quarter,month
order by period_year,quarter,month;


-- ======================================
-- 产品维度
-- ======================================

select product_code,
       product_name,
       pi_code,
       pi_name,
       case when product_name  like '%T3%' then 'T3'
            when product_name like '%T6%' then 'T6'
            when product_name like '%T8%' then 'T8'
            when product_name like '%T9%' then 'T9'
            when product_name like '%鉴赏%' then '鉴赏级'
            when product_name like '%黑马特%' and product_name not like '%郎牌·%黑马特%'
                then '黑马特1.0'
            when product_name like '%郎牌·%黑马特%'
                then '黑马特2.0'
            when product_name like '%白马特%' and product_name not like '%郎牌·%白马特%'
                then '白马特1.0'
            when  product_name like '%郎牌·%白马特%'
                then '白马特2.0'
            when product_name like '%新郎酒%' then '新郎酒'
            when product_name ~ '皇福特|鸿福特' then '福系列'
            else pi_name end as bu_pi_name,
       pl_code,
       pl_name,
       price,
       spec,
       base_unit,
       pack_unit,
       pack_base_num,
       pack_length,
       pack_width,
       pack_height,
       length_unit,
       pack_volume,
       volume_unit,
       gross_weight,
       weight_unit,
       box_number,
       bottle_number,
       sale_status,
       prod_status,
       pr_code,
       report_pi_name,
       pr_name,
       bu_code,
       d.dept_name as bu_name,
       aroma_type,
       plan_type,
       plan_pi_name,
       plan_pi_code,
       alc_degree,
       price_out
from dwr.dim_product a
         left join dwr.dim_department d on a.bu_code=d.dept_code

