-- 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 ----------

-----------------------------------------------------------------------------------
--修改时间：20241021
--修改人：FanXujia
--修改内容：
--只取最新年-1  ~  最新年范围内的，称为“2年共有”。增加
-----------------------------------------------------------------------------------
--共有机构
insert overwrite table dm.dm_aia_hp_flag 
with max_year as (
select
max(cast(year as int)) as max_year,
max(cast(year as int) - 1) as max_year_ly
from dwd.dwd_gnd_hospital_not_provided
)
,hp_flag as (
  select
    distinct concat(t1.year, '-Y') as hp_his_flag,
    case when length(t1.cpa_hospital_code) < 7 then right(concat('0000000',t1.cpa_hospital_code),7) else t1.cpa_hospital_code end as cpa_hospital_code
  from
    dwd.dwd_gnd_hospital_not_provided t1
    cross join max_year t2
    where cast(t1.year as int) >= t2.max_year_ly
)
,hp_flag2 as (
SELECT 
    cpa_hospital_code,
    CONCAT_WS(' ∩ ', SORT_ARRAY(COLLECT_LIST(hp_his_flag))) AS FLAG
FROM 
    hp_flag
GROUP BY 
    cpa_hospital_code
)
,inst_mapping as (
  -- select
  --   distinct right(concat('0000000', org_cd), 7) org_cd,
  --   ins_cd
  -- from
  --   dwd.dwd_gnd_hospitalmapping
select distinct 
case when length(cpa_hospital_code) < 7 then right(concat('0000000',cpa_hospital_code),7) else cpa_hospital_code end as cpa_hospital_code,
ins_cd_nl as inst_code
from dwd.dwd_gnd_hospital_not_provided
)

,ALL_INS as (
select distinct inst_code
from dm.dm_ext_aia_sales
)

,flag as (
  select distinct
    coalesce(t2.inst_code,t1.cpa_hospital_code) as inst_code,
    t1.flag as aia_hp_flag,
    'Y' as FLAG,
'AIA(Monthly)' as DATA_SOURCE
  from hp_flag2 t1
    left join inst_mapping t2 on t1.cpa_hospital_code = t2.cpa_hospital_code
  where
    length(t1.flag) > 6
union all
select '' as inst_code,'' as aia_hp_flag,'' as FLAG,'IQVIA-CHPA(Monthly)' as DATA_SOURCE
union all
select '' as inst_code,'' as aia_hp_flag,'' as FLAG,'XH Data(Quarterly)' as DATA_SOURCE
union all
select '' as inst_code,'' as aia_hp_flag,'' as FLAG,'EC(Monthly)' as DATA_SOURCE
union all
select '' as inst_code,'' as aia_hp_flag,'' as FLAG,'Retail(Quarterly)' as DATA_SOURCE
union all
select '' as inst_code,'' as aia_hp_flag,'' as FLAG,'CHC(Quarterly)' as DATA_SOURCE
union all
select '' as inst_code,'' as aia_hp_flag,'' as FLAG,'THC(Quarterly)' as DATA_SOURCE
union all
select '' as inst_code,'' as aia_hp_flag,'' as FLAG,'IQVIA-COUNTY(Quarterly)' as DATA_SOURCE
)

select t1.inst_code,
NVL(t2.aia_hp_flag,'') as aia_hp_flag,
NVL(t2.FLAG,'N') as FLAG,
'AIA(Monthly)' as DATA_SOURCE
from ALL_INS t1
left join flag t2
on t1.inst_code = t2.inst_code


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

-----------------------------------------------------------------------------------
--修改时间：20241021
--修改人：FanXujia
--修改内容：
--只取最新年-1  ~  最新年范围内的，称为“2年共有”。增加
-----------------------------------------------------------------------------------
--共有机构
insert overwrite table dm.dm_aia_hp_flag 
with max_year as (
select
max(cast(year as int)) as max_year,
max(cast(year as int) - 1) as max_year_ly
from dwd.dwd_gnd_hospital_not_provided
)
,hp_flag as (
  select
    distinct concat(t1.year, '-Y') as hp_his_flag,
    case when length(t1.cpa_hospital_code) < 7 then right(concat('0000000',t1.cpa_hospital_code),7) else t1.cpa_hospital_code end as cpa_hospital_code
  from
    dwd.dwd_gnd_hospital_not_provided t1
    cross join max_year t2
    where cast(t1.year as int) >= t2.max_year_ly
)
,hp_flag2 as (
SELECT 
    cpa_hospital_code,
    CONCAT_WS(' ∩ ', SORT_ARRAY(COLLECT_LIST(hp_his_flag))) AS FLAG
FROM 
    hp_flag
GROUP BY 
    cpa_hospital_code
)
,inst_mapping as (
  -- select
  --   distinct right(concat('0000000', org_cd), 7) org_cd,
  --   ins_cd
  -- from
  --   dwd.dwd_gnd_hospitalmapping
select distinct 
case when length(cpa_hospital_code) < 7 then right(concat('0000000',cpa_hospital_code),7) else cpa_hospital_code end as cpa_hospital_code,
ins_cd_nl as inst_code
from dwd.dwd_gnd_hospital_not_provided
)

,ALL_INS as (
select distinct inst_code
from dm.dm_ext_aia_sales
)

,flag as (
  select distinct
    coalesce(t2.inst_code,t1.cpa_hospital_code) as inst_code,
    t1.flag as aia_hp_flag,
    'Y' as FLAG,
'AIA(Monthly)' as DATA_SOURCE
  from hp_flag2 t1
    left join inst_mapping t2 on t1.cpa_hospital_code = t2.cpa_hospital_code
  where
    length(t1.flag) > 6
union all
select '' as inst_code,'' as aia_hp_flag,'' as FLAG,'IQVIA-CHPA(Monthly)' as DATA_SOURCE
union all
select '' as inst_code,'' as aia_hp_flag,'' as FLAG,'XH Data(Quarterly)' as DATA_SOURCE
union all
select '' as inst_code,'' as aia_hp_flag,'' as FLAG,'EC(Monthly)' as DATA_SOURCE
union all
select '' as inst_code,'' as aia_hp_flag,'' as FLAG,'Retail(Quarterly)' as DATA_SOURCE
union all
select '' as inst_code,'' as aia_hp_flag,'' as FLAG,'CHC(Quarterly)' as DATA_SOURCE
union all
select '' as inst_code,'' as aia_hp_flag,'' as FLAG,'THC(Quarterly)' as DATA_SOURCE
union all
select '' as inst_code,'' as aia_hp_flag,'' as FLAG,'IQVIA-COUNTY(Quarterly)' as DATA_SOURCE
)

select t1.inst_code,
NVL(t2.aia_hp_flag,'') as aia_hp_flag,
NVL(t2.FLAG,'N') as FLAG,
'AIA(Monthly)' as DATA_SOURCE
from ALL_INS t1
left join flag t2
on t1.inst_code = t2.inst_code


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

--连续取数机构
----------------------------------------------------------------------------------------------------------------------
--修改时间：20241029
--修改人：FanXujia
--修改内容：
--根据事实表最新月，往前推12个月（含最新月），按机构 + 年月粒度汇总，如果每个月销量都不等于0，这家机构称为：滚动一年有数
--根据事实表最新月，往前推24个月（含最新月），按机构 + 年月粒度汇总，如果每个月销量都不等于0，这家机构称为：滚动两年有数
--2022年没有销量为正常情况，判断时仅考虑2023年01月起销量是否等于0
----------------------------------------------------------------------------------------------------------------------
insert overwrite table dm.dm_aia_provided_flag 
with max_ym as (
select max(ym) as max_ym
from dm.dm_ext_aia_sales
)
,ym_range as (
select max_ym,
case when max_ym <= '202412' then cast(right(max_ym,2) as int)
     else cast(12 as int)
end ym_12m,
case when max_ym <= '202312' then '202401'
     when right(max_ym,2) = '12' then concat(left(max_ym,4),'01')
     else concat(cast(left(max_ym, 4) - 1 as int),right(concat('0', cast(right(max_ym, 2) + 1 as int)), 2))
end as ym_12m_before,
case when max_ym <= '202312' then cast(right(max_ym,2) as int)
     when max_ym <= '202512' then cast(right(max_ym,2) as int) + 12
     else cast(24 as int)
end as ym_24m,
case when max_ym <= '202512' then '202401'
     when right(max_ym,2) = '12' then concat(cast(left(max_ym,4) - 1 as int),'01')
     else concat(cast(left(max_ym, 4) -2 as int),right(concat('0', cast(right(max_ym, 2) + 1 as int)), 2))
end as ym_24m_before
from max_ym
)
,sales_1 as (  
select
  inst_code,
  ym
from
  dm.dm_ext_aia_sales
group by
  ym,
  inst_code
having
  sum(value) <> 0
)
,sales_2 as (
select
  t1.inst_code,
  t1.ym,
  t2.max_ym,
  t2.ym_12m,
  t2.ym_12m_before,
  t2.ym_24m,
  t2.ym_24m_before
from sales_1 t1
cross join ym_range t2
)
,flag_12m as (
select inst_code,
'滚动一年有数' as aia_provided_flag,
'AIA(Monthly)' as DATA_SOURCE
from sales_2
where ym <= max_ym
and ym >= ym_12m_before
group by inst_code
having count(1) = (select ym_12m from ym_range)
)
,flag_24m as (
select inst_code,
'滚动两年有数' as aia_provided_flag,
'AIA(Monthly)' as DATA_SOURCE
from sales_2
where ym <= max_ym
and ym >= ym_24m_before
group by inst_code
having count(1) = (select ym_24m from ym_range)
)

select inst_code,
aia_provided_flag,
DATA_SOURCE
from flag_12m
union all
select inst_code,
aia_provided_flag,
DATA_SOURCE
from flag_24m
union all
select '' as inst_code,'' as aia_provided_flag,'IQVIA-CHPA(Monthly)' as data_source
union all
select '' as inst_code,'' as aia_provided_flag,'XH Data(Quarterly)' as data_source
union all
select '' as inst_code,'' as aia_provided_flag,'EC(Monthly)' as data_source
union all
select '' as inst_code,'' as aia_provided_flag,'Retail(Quarterly)' as data_source
union all
select '' as inst_code,'' as aia_provided_flag,'CHC(Quarterly)' as data_source
union all
select '' as inst_code,'' as aia_provided_flag,'THC(Quarterly)' as data_source
union all
select '' as inst_code,'' as aia_provided_flag,'IQVIA-COUNTY(Quarterly)' as data_source


-- 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_aia_pack_property t2
  on t1.pack_cod = t2.PACK_COD
)
--获取产品与市场的映射关系
,pack_market as (
select distinct 
PACK_COD,
MARKET
from dm.dm_aia_pack_property
)
--辖区表，获取到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 ('AIA 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
