with base_data as (
    select
        to_char(a.stat_date,'yyyy') as stat_year,
        to_char(a.stat_date,'mm') as stat_mon,
        a.stat_date,
        case when a.indi_type='现金分润'
                 then (case when dmp.pl_name='兼香龙马' and dmp.product_name like '%郎牌·%马特%'
                                then '郎牌·马特2.0版终端月度奖励'
                            when dmp.pl_name='兼香龙马'
                                then '兼香龙马终端月度奖励'
                            when dmp.pl_name='兼香顺品'
                                then '兼香顺品终端分润活动奖励'
                end
                )
             when a.indi_type='消费者红包'
                 then (case when dmp.pl_name='兼香龙马' and dmp.product_name like '%郎牌·%马特%'
                                then '郎牌·马特2.0版消费者红包奖励'
                            when dmp.pl_name='红花郎'
                                then '红花郎消费者红包奖励'
                            else dmp.pl_name||'消费者红包月度奖励'
                 end
                 )
             else '终端销售奖月度销售奖励' end as indi_type,
        d.fist_dept_code as bu_code,
        max(d.fist_dept_name) as bu_name,
        d.secd_dept_code as region_code,
        max(d.secd_dept_name) as region_name,
        d.thrd_dept_code as office_code,
        max(d.thrd_dept_name) as office_name,
        cust.deal_code,
        cust.deal_name,
        dmp.pl_name,
        nvl(p.pi_name,dmp.pi_name)  as pi_name,
        a.product_code,
        dmp.product_name,
        sum(a.bottle_num) as bottle_num,
        sum(a.amt) as amt
    from dm.dm_mak_indicator_day a
             left join dwr.dim_product dmp on a.product_code = dmp.product_code
             left join sdi_oth.product_pi_special p on a.product_code=p.product_code
             left join dwr.dim_department d on d.dept_code = nvl(a.city_unit_code,dmp.bu_code)
             left join dwr.dim_dealer cust on a.src_deal_code = cust.deal_code
    where indi_type in ('终端销售奖','现金分润','消费者红包')
      and a.stat_date >=  timestamp'${CONCATENATE(sStart_date," 00:00:00.000")}'
      AND a.stat_date <  timestamp '${CONCATENATE(sEnd_date," 00:00:00.000")}'

    group by
        to_char(a.stat_date,'yyyy'),
        to_char(a.stat_date,'mm'),
        a.stat_date,
        case when a.indi_type='现金分润'
        then (case when dmp.pl_name='兼香龙马' and dmp.product_name like '%郎牌·%马特%'
        then '郎牌·马特2.0版终端月度奖励'
        when dmp.pl_name='兼香龙马'
        then '兼香龙马终端月度奖励'
        when dmp.pl_name='兼香顺品'
        then '兼香顺品终端分润活动奖励'
        end
        )
        when a.indi_type='消费者红包'
        then (case when dmp.pl_name='兼香龙马' and dmp.product_name like '%郎牌·%马特%'
        then '郎牌·马特2.0版消费者红包奖励'
        when dmp.pl_name='红花郎'
        then '红花郎消费者红包奖励'
        else dmp.pl_name||'消费者红包月度奖励'
        end
        )
        else '终端销售奖月度销售奖励' end,
        d.fist_dept_code,
        d.secd_dept_code,
        d.thrd_dept_code,
        cust.deal_code,
        cust.deal_name,
        dmp.pl_name,
        nvl(p.pi_name,dmp.pi_name),
        a.product_code,
        dmp.product_name
    union all
    select
        to_char(a.rebate_time,'yyyy') as stat_year,
        to_char(a.rebate_time,'mm') as stat_mon,
        date_trunc('day',a.rebate_time) as stat_date,
        '终端销售奖月度销售奖励' as indi_type,
        d.fist_dept_code as bu_code,
        max(d.fist_dept_name) as bu_name,
        d.secd_dept_code as region_code,
        max(d.secd_dept_name) as region_name,
        d.thrd_dept_code as office_code,
        max(d.thrd_dept_name) as office_name,
        cust.deal_code,
        cust.deal_name,
        dmp.pl_name,
        nvl(p.pi_name,dmp.pi_name)  as pi_name,
        a.product_code,
        dmp.product_name,
        count(a.bottle_code) as bottle_num,
        sum(a.amt) as amt
    from dwi.dwi_mak_rebate_person a
        left join dwr.dim_product dmp on a.product_code=dmp.product_code
        left join sdi_oth.product_pi_special p on a.product_code=p.product_code
        left join (select
        deal_code,
        pl_name,
        max(city_unit_code) as city_unit_code
        from dwr.dim_dealer_administer
        where enabled_status=1
        group by deal_code,pl_name) dda on a.deal_code=dda.deal_code and dmp.pl_name=dda.pl_name
        left join dwr.dim_department d on d.dept_code = nvl(dda.city_unit_code,a.bu_code)
        left join dwr.dim_dealer cust on a.deal_code = cust.deal_code
    where a.trade_direction='收入'
      and a.bill_type='分润'
      and rebate_time >= timestamp '${CONCATENATE(sStart_date," 00:00:00.000")}'
      AND rebate_time < timestamp '${CONCATENATE(sEnd_date," 00:00:00.000")}'
    group by
        to_char(a.rebate_time,'yyyy'),
        to_char(a.rebate_time,'mm'),
        date_trunc('day',a.rebate_time),
        d.fist_dept_code,
        d.secd_dept_code,
        d.thrd_dept_code,
        cust.deal_code,
        cust.deal_name,
        dmp.pl_name,
        nvl(p.pi_name,dmp.pi_name) ,
        a.product_code,
        dmp.product_name
)
SELECT
stat_year       -- 统计年
,stat_mon       -- 统计月
,stat_date      -- 统计日期
,indi_type      -- 账单类型
,bu_code        -- 事业部编码
,bu_name        -- 事业部
,region_code    -- 大区编码
,region_name    -- 大区名称
,office_code    -- 办事处编码
,office_name    -- 办事处名称
,deal_code      -- 经销商编码
,deal_name      -- 经销商名称
,pl_name        -- 产品线名称
,pi_name        -- 品项
,product_code   -- 产品编码
,product_name   -- 产品名称
,bottle_num     -- 瓶数
,amt            -- 金额
FROM base_data a
-- 排序
left join sdi_xdata.mdm_dim_sorted st
on a.region_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 = a.bu_code  -- 事业部
            OR per_code = a.region_code  -- 大区
            OR per_code = a.office_code  -- 办事处
       --     OR per_code = a.city_unit_code -- 城市单元
        )
)

-- 事业部
${if(len(sBu) == 0,"","and bu_code IN ('"+sBu+"')")}
-- 大区
${if(len(sRegion) == 0,"","and region_code IN ('"+sRegion+"')")}
-- 办事处
${if(len(sOffice) == 0,"","and office_code IN ('"+ sOffice+"')")}
-- 产品线
${if(len(sPl_name) == 0,"","and pl_name IN ('"+ sPl_name+"')")}
-- 经销商
${if(len(sDeal) == 0,"",if(REGEXP(sDeal,"^[0-9]+$"),"and deal_code = '"+sDeal+"'","and deal_name like '%"+sDeal+"%'"))}
-- 账单类型
${if(len(sIndi_type) == 0,"","and indi_type IN ('"+ sIndi_type +"')")}
ORDER BY nvl(st.sorted_no,99)

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




-- 数据更新时间:
--

SELECT
    CONCAT('数据更新时间:',LEFT(MAX(T.update_time),19)) AS update_time
FROM
(
SELECT MAX(update_time) AS update_time FROM dm.dm_mak_indicator_day
UNION ALL
SELECT MAX(update_time) AS update_time FROM dwi.dwi_mak_rebate_person
)T
;