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

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

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


SELECT

         a.deal_code         -- 主经销商编码
          ,a.deal_name         -- 主经销商名称
          ,a.src_deal_code     -- 子经销商编码
          ,a.src_deal_name     -- 子经销商名称
          ,a.plan_type  -- 计划类型
          ,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 -- 计划外金额

          -- 月度
-- 1月
          ,SUM(CASE WHEN  month = '01' THEN a.m_plan_in_pack ELSE 0 END) AS m01_plan_in_pack -- 1月计划内件数
          ,SUM(CASE WHEN  month = '01' THEN a.m_plan_in_ton  ELSE 0 END) AS m01_plan_in_ton -- 1月计划内吨数
          ,SUM(CASE WHEN  month = '01' THEN a.m_plan_in_amt ELSE 0 END) / 10000 AS m01_plan_in_amt  -- 1月计划内金额
          ,SUM(CASE WHEN  month = '01' THEN a.m_plan_out_pack ELSE 0 END) AS m01_plan_out_pack   -- 1月计划外件数
          ,SUM(CASE WHEN  month = '01' THEN a.m_plan_out_ton  ELSE 0 END) AS m01_plan_out_ton-- 1月计划外吨数
          ,SUM(CASE WHEN  month = '01' THEN a.m_plan_out_amt  ELSE 0 END) / 10000 AS m01_plan_out_amt -- 1月计划外金额
-- 2月
          ,SUM(CASE WHEN month = '02' THEN a.m_plan_in_pack ELSE 0 END) AS m02_plan_in_pack -- 2月计划内件数
          ,SUM(CASE WHEN month = '02' THEN a.m_plan_in_ton  ELSE 0 END) AS m02_plan_in_ton -- 2月计划内吨数
          ,SUM(CASE WHEN month = '02' THEN a.m_plan_in_amt ELSE 0 END) / 10000 AS m02_plan_in_amt  --2月计划内金额
          ,SUM(CASE WHEN month = '02' THEN a.m_plan_out_pack ELSE 0 END) AS m02_plan_out_pack   -- 2月计划外件数
          ,SUM(CASE WHEN month = '02' THEN a.m_plan_out_ton  ELSE 0 END) AS m02_plan_out_ton-- 2月计划外吨数
          ,SUM(CASE WHEN month = '02' THEN a.m_plan_out_amt  ELSE 0 END) / 10000 AS m02_plan_out_amt -- 2月计划外金额
-- 3月
          ,SUM(CASE WHEN month = '03' THEN a.m_plan_in_pack ELSE 0 END) AS m03_plan_in_pack -- 3月计划内件数
          ,SUM(CASE WHEN month = '03' THEN a.m_plan_in_ton  ELSE 0 END) AS m03_plan_in_ton -- 3月计划内吨数
          ,SUM(CASE WHEN month = '03' THEN a.m_plan_in_amt ELSE 0 END) / 10000 AS m03_plan_in_amt  -- 3月计划内金额
          ,SUM(CASE WHEN month = '03' THEN a.m_plan_out_pack ELSE 0 END) AS m03_plan_out_pack   -- 3月计划外件数
          ,SUM(CASE WHEN month = '03' THEN a.m_plan_out_ton  ELSE 0 END) AS m03_plan_out_ton-- 3月计划外吨数
          ,SUM(CASE WHEN month = '03' THEN a.m_plan_out_amt  ELSE 0 END) / 10000 AS m03_plan_out_amt -- 3月计划外金额
-- 4月
          ,SUM(CASE WHEN month = '04' THEN a.m_plan_in_pack ELSE 0 END) AS m04_plan_in_pack -- 4月计划内件数
          ,SUM(CASE WHEN month = '04' THEN a.m_plan_in_ton  ELSE 0 END) AS m04_plan_in_ton -- 4月计划内吨数
          ,SUM(CASE WHEN month = '04' THEN a.m_plan_in_amt ELSE 0 END) / 10000 AS m04_plan_in_amt  -- 4月计划内金额
          ,SUM(CASE WHEN month = '04' THEN a.m_plan_out_pack ELSE 0 END) AS m04_plan_out_pack   -- 4月计划外件数
          ,SUM(CASE WHEN month = '04' THEN a.m_plan_out_ton  ELSE 0 END) AS m04_plan_out_ton-- 4月计划外吨数
          ,SUM(CASE WHEN month = '04' THEN a.m_plan_out_amt  ELSE 0 END) / 10000 AS m04_plan_out_amt -- 4月计划外金额
-- 5月
          ,SUM(CASE WHEN month = '05' THEN a.m_plan_in_pack ELSE 0 END) AS m05_plan_in_pack -- 5月计划内件数
          ,SUM(CASE WHEN month = '05' THEN a.m_plan_in_ton  ELSE 0 END) AS m05_plan_in_ton --  5月计划内吨数
          ,SUM(CASE WHEN month = '05' THEN a.m_plan_in_amt ELSE 0 END) / 10000 AS m05_plan_in_amt  -- 5月计划内金额
          ,SUM(CASE WHEN month = '05' THEN a.m_plan_out_pack ELSE 0 END) AS m05_plan_out_pack   -- 5月计划外件数
          ,SUM(CASE WHEN month = '05' THEN a.m_plan_out_ton  ELSE 0 END) AS m05_plan_out_ton-- 5月计划外吨数
          ,SUM(CASE WHEN month = '05' THEN a.m_plan_out_amt  ELSE 0 END) / 10000 AS m05_plan_out_amt -- 5月计划外金额
-- 6月
          ,SUM(CASE WHEN month = '06' THEN a.m_plan_in_pack ELSE 0 END) AS m06_plan_in_pack -- 6月计划内件数
          ,SUM(CASE WHEN month = '06' THEN a.m_plan_in_ton  ELSE 0 END) AS m06_plan_in_ton -- 6月计划内吨数
          ,SUM(CASE WHEN month = '06' THEN a.m_plan_in_amt ELSE 0 END) / 10000 AS m06_plan_in_amt  -- 6月计划内金额
          ,SUM(CASE WHEN month = '06' THEN a.m_plan_out_pack ELSE 0 END) AS m06_plan_out_pack   -- 6月计划外件数
          ,SUM(CASE WHEN month = '06' THEN a.m_plan_out_ton  ELSE 0 END) AS m06_plan_out_ton-- 6月计划外吨数
          ,SUM(CASE WHEN month = '06' THEN a.m_plan_out_amt  ELSE 0 END) / 10000 AS m06_plan_out_amt -- 6月计划外金额
-- 7月
          ,SUM(CASE WHEN month = '07' THEN a.m_plan_in_pack ELSE 0 END) AS m07_plan_in_pack -- 7月计划内件数
          ,SUM(CASE WHEN month = '07' THEN a.m_plan_in_ton  ELSE 0 END) AS m07_plan_in_ton -- 7月计划内吨数
          ,SUM(CASE WHEN month = '07' THEN a.m_plan_in_amt ELSE 0 END) / 10000 AS m07_plan_in_amt  -- 7月计划内金额
          ,SUM(CASE WHEN month = '07' THEN a.m_plan_out_pack ELSE 0 END) AS m07_plan_out_pack   -- 7月计划外件数
          ,SUM(CASE WHEN month = '07' THEN a.m_plan_out_ton  ELSE 0 END) AS m07_plan_out_ton-- 7月计划外吨数
          ,SUM(CASE WHEN month = '07' THEN a.m_plan_out_amt  ELSE 0 END) / 10000 AS m07_plan_out_amt -- 7月计划外金额
-- 8月
          ,SUM(CASE WHEN month = '08' THEN a.m_plan_in_pack ELSE 0 END) AS m08_plan_in_pack -- 8月计划内件数
          ,SUM(CASE WHEN month = '08' THEN a.m_plan_in_ton  ELSE 0 END) AS m08_plan_in_ton -- 8月计划内吨数
          ,SUM(CASE WHEN month = '08' THEN a.m_plan_in_amt ELSE 0 END) / 10000 AS m08_plan_in_amt  -- 8月计划内金额
          ,SUM(CASE WHEN month = '08' THEN a.m_plan_out_pack ELSE 0 END) AS m08_plan_out_pack   -- 8月计划外件数
          ,SUM(CASE WHEN month = '08' THEN a.m_plan_out_ton  ELSE 0 END) AS m08_plan_out_ton-- 8月计划外吨数
          ,SUM(CASE WHEN month = '08' THEN a.m_plan_out_amt  ELSE 0 END) / 10000 AS m08_plan_out_amt -- 8月计划外金额
-- 9月
          ,SUM(CASE WHEN month = '09' THEN a.m_plan_in_pack ELSE 0 END) AS m09_plan_in_pack -- 9月计划内件数
          ,SUM(CASE WHEN month = '09' THEN a.m_plan_in_ton  ELSE 0 END) AS m09_plan_in_ton -- 9月计划内吨数
          ,SUM(CASE WHEN month = '09' THEN a.m_plan_in_amt ELSE 0 END) / 10000 AS m09_plan_in_amt  -- 9月计划内金额
          ,SUM(CASE WHEN month = '09' THEN a.m_plan_out_pack ELSE 0 END) AS m09_plan_out_pack   -- 9月计划外件数
          ,SUM(CASE WHEN month = '09' THEN a.m_plan_out_ton  ELSE 0 END) AS m09_plan_out_ton-- 9月计划外吨数
          ,SUM(CASE WHEN month = '09' THEN a.m_plan_out_amt  ELSE 0 END) / 10000 AS m09_plan_out_amt -- 9月计划外金额

-- 10月
          ,SUM(CASE WHEN month = '10' THEN a.m_plan_in_pack ELSE 0 END) AS m10_plan_in_pack -- 11月计划内件数
          ,SUM(CASE WHEN month = '10' THEN a.m_plan_in_ton  ELSE 0 END) AS m10_plan_in_ton -- 11月计划内吨数
          ,SUM(CASE WHEN month = '10' THEN a.m_plan_in_amt ELSE 0 END) / 10000 AS m10_plan_in_amt  -- 11月计划内金额
          ,SUM(CASE WHEN month = '10' THEN a.m_plan_out_pack ELSE 0 END) AS m10_plan_out_pack   -- 11月计划外件数
          ,SUM(CASE WHEN month = '10' THEN a.m_plan_out_ton  ELSE 0 END) AS m10_plan_out_ton-- 11月计划外吨数
          ,SUM(CASE WHEN month = '10' THEN a.m_plan_out_amt  ELSE 0 END) / 10000 AS m10_plan_out_amt -- 11月计划外金额
-- 11月
          ,SUM(CASE WHEN month = '11' THEN a.m_plan_in_pack ELSE 0 END) AS m11_plan_in_pack -- 11月计划内件数
          ,SUM(CASE WHEN month = '11' THEN a.m_plan_in_ton  ELSE 0 END) AS m11_plan_in_ton -- 11月计划内吨数
          ,SUM(CASE WHEN month = '11' THEN a.m_plan_in_amt ELSE 0 END) / 10000 AS m11_plan_in_amt  -- 11月计划内金额
          ,SUM(CASE WHEN month = '11' THEN a.m_plan_out_pack ELSE 0 END) AS m11_plan_out_pack   -- 11月计划外件数
          ,SUM(CASE WHEN month = '11' THEN a.m_plan_out_ton  ELSE 0 END) AS m11_plan_out_ton-- 11月计划外吨数
          ,SUM(CASE WHEN month = '11' THEN a.m_plan_out_amt  ELSE 0 END) / 10000 AS m11_plan_out_amt -- 1月计划外金额
-- 12月
          ,SUM(CASE WHEN month = '12' THEN a.m_plan_in_pack ELSE 0 END) AS m12_plan_in_pack -- 12月计划内件数
          ,SUM(CASE WHEN month = '12' THEN a.m_plan_in_ton  ELSE 0 END) AS m12_plan_in_ton -- 12月计划内吨数
          ,SUM(CASE WHEN month = '12' THEN a.m_plan_in_amt ELSE 0 END) / 10000 AS m12_plan_in_amt  -- 12月计划内金额
          ,SUM(CASE WHEN month = '12' THEN a.m_plan_out_pack ELSE 0 END) AS m12_plan_out_pack   -- 12月计划外件数
          ,SUM(CASE WHEN month = '12' THEN a.m_plan_out_ton  ELSE 0 END) AS m12_plan_out_ton-- 12月计划外吨数
          ,SUM(CASE WHEN month = '12' THEN a.m_plan_out_amt  ELSE 0 END) / 10000 AS m12_plan_out_amt -- 12月计划外金额
          -- 合同约定
          ,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 AS add_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 = '${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  -- 事业部
        )
)
-- 事业部
${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+"'")}
     GROUP BY a.deal_code         -- 主经销商编码
             ,a.deal_name         -- 主经销商名称
             ,a.src_deal_code     -- 子经销商编码
             ,a.src_deal_name     -- 子经销商名称
             ,a.plan_type  -- 计划类型
             ,a.plan_pi_name -- 计划品项

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


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