-- Databricks notebook source
-- MAGIC %run ../../../Common/config

-- COMMAND ----------

-- MAGIC %python
-- MAGIC spark.read.table(f'`{CDW_CATALOG}`.`dwd`.`dim_product_wide`').createOrReplaceTempView('cdw_dwd_dim_product_wide')
-- MAGIC spark.read.table(f'`{CDW_CATALOG}`.`dwd`.`dwd_hospital_classification`').createOrReplaceTempView('cdw_dwd_dwd_hospital_classification')

-- COMMAND ----------

---------------------------------------------------------------------------
--修改时间：20241108
--修改人：Fanxujia
--修改内容：
--for AIA Dashboard
--增加sub_channel,dragon_flag,ka_flag,target_ins_level字段
---------------------------------------------------------------------------
---------------------------------------------------------------------------
--修改时间：20241209
--修改人：Fanxujia
--修改内容：
--增加sub channel的排序
---------------------------------------------------------------------------
--目标医院
insert overwrite table dm.dm_aia_targethp_flag --获取全部的机构 + 市场
with sales as (
  select distinct t1.inst_code,t2.MARKET
  from dm.dm_ext_aia_sales t1
  left join DM.DM_TD_EXT_AIA_MARKET_PACK_MAPPING t2
  on t1.pack_cod = t2.PACK_CODE
)
--获取产品与市场的映射关系
,pack_market as (
select distinct 
PACK_CODE as PACK_COD,
MARKET
from DM.DM_TD_EXT_AIA_MARKET_PACK_MAPPING
)
--辖区表，获取到KA_Flag、target_ins_level以及目标机构Flag(在辖区表里存在的就是目标机构)
,td_territory_inst as (
select distinct 
inst_code,
trty_code,
family_code,
ka_flag,
TARGET_LEVEL as target_ins_level
from dm.dm_td_sd_territory_indication --eagle的非重点医院同样加为目标机构
where yyyymm in (
    select
      key_value
    from
      dm.dm_td_date_config
    where
      key_code = 'pfmc_cvh'
  )
and bu not in ('CUBE','KA')
and key_hp<>3 --chenwu 20250904 排除双考影响
)
--汇报线表，获取到Channel，后续用于判断出sub_channel
,td_org as (
select distinct 
t1.mr_trty_code,
case when t1.bu_code in ('PC14','PC16','PC18','PC19') then 'BU'
     when t1.bu_code = 'SC01' and t1.sub_bu_code = 'NA29' then sub_bu_name
     when t1.bu_code = 'SC01' then 'BU'
     when t1.bu_code <> 'SC01' and t2.bm_name = 'BCBH' then 'BU'
     when t1.bu_code <> 'SC01' then t1.bu_name
end as channel
from dm.dm_td_org t1
left join dws.dws_td_bu_active t2
on t1.bu_name = t2.bu_name
where t1.yyyymm in (
    select
      key_value
    from
      dm.dm_td_date_config
    where
      key_code = 'pfmc_cvh'
  )
)
--Family粒度和Brand粒度的映射表
,product_wide as (
select distinct 
prd_family_cd,
prd_brand_cd
from cdw_dwd_dim_product_wide
)
--内部Brand粒度与外部pack_cod的映射表
,imscode_azcode as (
select distinct 
brandcode,
case when length(ims_pack_cod) < 12 and ims_pack_cod REGEXP '^[0-9]' then right(concat('00000000000',ims_pack_cod),12) else ims_pack_cod end as ims_pack_cod
from dwd.dwd_td_imscode_azcode
)
--综合上面的几个表，得到KA_flag、target_ins_level、channel、目标机构Flag
,target_hp_all as (
select
  distinct 
  t1.inst_code,
  t1.ka_flag,
  t1.target_ins_level,
  t2.channel,
  t5.MARKET
from
  td_territory_inst t1
  inner join td_org t2 on t1.trty_code = t2.mr_trty_code
  left join product_wide t3 on t3.prd_family_cd = t1.family_code
  left join imscode_azcode t4 on t3.prd_brand_cd = t4.brandcode
  left join pack_market t5 on t4.ims_pack_cod = t5.pack_cod
where t5.MARKET is not null
)
--target_hp_all里一个机构一个市场对应了多个属性，这里按优先级依次取唯一值
--ka_flag
,ka_flag1 as (
select distinct
inst_code,
MARKET,
ka_flag
from target_hp_all
)
,ka_flag2 as (
select
inst_code,
MARKET,
ka_flag,
row_number() over(partition by inst_code,market
                  order by case when ka_flag = 'KA' then 1 
                                ELSE 2 end asc) as RN
from ka_flag1
)
,ka_flag_final as (
select
inst_code,
MARKET,
ka_flag
from ka_flag2
where RN = 1
)
--target_ins_level不取唯一值
--加个hardcoding，ONC Market和ALL Market设置成NA
,target_ins_level1 as (
select distinct
inst_code,
MARKET,
case when MARKET in ('ALL Market','ONC Market') then 'NA'
     else target_ins_level
end as target_ins_level
from target_hp_all
)
,target_ins_level_final as (
select distinct
inst_code,
MARKET,
target_ins_level
from target_ins_level1
)
--target_hp_all里一个机构一个市场对应了多个属性，这里按优先级依次取唯一值
--Channel
,Channel1 as (
select distinct
inst_code,
MARKET,
channel
from target_hp_all
)
,channel2 as (
select
inst_code,
MARKET,
channel,
row_number() over(partition by inst_code,market
                  order by case when channel = 'BU' then 1
                                when channel = 'BBU_County' then 2
                                when channel = 'CHC' then 3
                                when channel = 'Eagle' then 4
                                ELSE 5 end asc) as RN
from channel1
)
,channel_final as (
select
inst_code,
MARKET,
channel
from channel2
where RN = 1
)
--确定唯一值后重新放一起
,target_hp1 as (
select distinct
inst_code,
MARKET
from target_hp_all
)
,target_hp as (
select t1.inst_code,
t1.market,
t2.ka_flag,
t3.target_ins_level,
t4.channel
from target_hp1 t1
left join ka_flag_final t2
on t1.inst_code = t2.inst_code
and t1.market = t2.market
left join target_ins_level_final t3
on t1.inst_code = t3.inst_code
and t1.market = t3.market
left join channel_final t4
on t1.inst_code = t4.inst_code
and t1.market = t4.market
)

--获取Dragon_type
--判断Dragon_type是否为County
,county as (
select distinct inst_code,county_tier_desc
from dm.dm_td_institution
)
--判断另外2种
-- 20250825修改
,dragon_type as (
select src_hcc_code as inst_code,
src_prod_code as family_code,
max(is_core) is_core
from cdw_dwd_dwd_hospital_classification
where active_status = 1
and src_sales_cycle = (select key_value from dm.dm_td_date_config where key_code = 'pfmc_cvh')
group by src_hcc_code,
src_prod_code
)
--将Family_code转为pack_cod
,dragon_type_market1 as (
select distinct
t1.inst_code,
t1.is_core,
t4.market
from dragon_type t1
left join cdw_dwd_dim_product_wide t2
on t1.family_code = t2.prd_family_cd
left join dwd.dwd_td_imscode_azcode t3
on t2.prd_brand_cd = t3.brandcode
left join pack_market t4
on t3.ims_pack_cod = t4.pack_cod
where t4.market is not null
)
--按优先级取唯一值
,dragon_type_market2 as (
select distinct
inst_code,
is_core,
market,
row_number() over(partition by inst_code,market
                  order by case when is_core = 'CORE' then 1
                                when is_core = 'EMERGING' then 2
                                else 3 end asc) as RN
from dragon_type_market1
)
,dragon_type_market as (
select distinct
inst_code,
is_core,
market
from dragon_type_market2
where RN = 1
)
--综合前面几个表，得出dragon_flag
,dragon_flag1 as (
select distinct
t0.inst_code,
t0.market,
case when t1.county_tier_desc in ('County','County level city') then 'County'
     when t2.is_core is null or length(t2.is_core) = 0 then 'Others'
     else t2.is_core
end as dragon_flag
from target_hp t0
left join county t1
on t0.inst_code = t1.inst_code
left join dragon_type_market t2
on t0.inst_code = t2.inst_code
and t0.MARKET = t2.MARKET
)
--按优先级取唯一值
,dragon_flag2 as (
select inst_code,
market,
dragon_flag,
row_number() over(partition by inst_code,market
                  order by case when dragon_flag = 'CORE' then 1 
                                when dragon_flag = 'EMERGING' then 2 
                                when dragon_flag = 'County' then 3 
                                else 4 end asc) as RN
from dragon_flag1
)
,dragon_flag as (
select inst_code,
market,
dragon_flag
from dragon_flag2
where RN = 1
)

--下面是为了把其他渠道并进来
,other_data_source as (
select explode(array('IQVIA-CHPA(Monthly)','XH Data(Quarterly)','EC(Monthly)','Retail(Quarterly)','CHC(Quarterly)','THC(Quarterly)','IQVIA-COUNTY(Quarterly)'))
)
,col as (
select '' as inst_code,
'' as MARKET,
'' as is_target_hp,
'' as sub_channel,
'' as dragon_flag,
'' as ka_flag,
'' as target_ins_level
)

,temp_1 as (
select distinct 
case when t1.inst_code is null or t1.inst_code = '' then 'ROC'
     else t1.inst_code
end as inst_code,
t1.MARKET,
case when t2.inst_code is null then 'N' else 'Y' end as is_target_hp,
case when t2.channel = 'BU' and geo.county_tier_desc in ('County','County level city') then 'BU_County'
     when t2.channel = 'BU' and geo.county_tier_desc in ('City-3b','City-4a','City-4b') then 'BU_Emerging'
     when t2.channel = 'BU' and t3.dragon_flag = 'CORE' then 'BU_Core'
     when t2.channel = 'BU' then 'BU_Emerging'
     when t2.channel in ('Eagle','CHC','BBU_County') then t2.channel
     when t2.inst_code is null then 'NON_TARGET'
     else 'NA'
end as sub_channel,
case when t3.dragon_flag is null then 'NON_TARGET'
     else t3.dragon_flag
end as dragon_flag,
case when t2.ka_flag is null then 'NON_TARGET'
     else t2.ka_flag
end as ka_flag,
case when t2.target_ins_level is null then 'NON_TARGET'
     else t2.target_ins_level
end as target_ins_level,
'AIA(Monthly)' as DATA_SOURCE
from sales t1
left join target_hp t2
on t1.inst_code = t2.inst_code
and t1.MARKET = t2.MARKET
left join dragon_flag t3
on t1.inst_code = t3.inst_code
and t1.MARKET = t3.MARKET
left join dm.dm_td_institution inst
on t1.inst_code =inst.inst_code
left join dm.dm_td_geography geo
on inst.county_code = geo.geo_key
union all
select *
from col
cross join other_data_source
)
,sub_channel_rank as (
select distinct SUB_CHANNELSTD,SUB_CHANNEL_ORDER
from dm.dm_td_sd_channel
)


select t1.*,NVL(t2.SUB_CHANNEL_ORDER,100) as SUB_CHANNEL_ORDER
from temp_1 t1
left join sub_channel_rank t2
on t1.sub_channel = t2.SUB_CHANNELSTD
