-- Databricks notebook source
-- CREATE OR REPLACE TABLE DM.DM_TD_EXT_AIA_ORG (
--   REGION_CODE STRING,
--   REGION_NAME_EN STRING,
--   RSM_NAME STRING,
--   RC_CODE STRING,
--   RC_NAME_EN STRING,
--   RC_NAME STRING,
--   REGIONCENTER STRING,
--   AUDIT_COD STRING,
--   MARKET STRING,
--   BU_CATEGORY STRING,
--   BU_NAME_EN STRING,
--   SUB_BU_NAME STRING,
--   SUB_BU_CODE_AUTH STRING,
--   BU_CODE_AUTH STRING,
--   DATA_SOURCE STRING,
--   REGION_RATIO FLOAT,
--   TEAM STRING,
--   MR_TRTY_CODE STRING,
--   NSD_KCODE STRING,
--   NSD_NAME STRING,
--   RSD_KCODE STRING,
--   RSD_NAME STRING,
--   RSM_KCODE STRING,
--   DSM_KCODE STRING,
--   DSM_NAME STRING,
--   MR_KCODE STRING,
--   MR_NAME STRING)
-- USING delta
-- LOCATION 'abfss://master@retaildlstoragetest.dfs.core.chinacloudapi.cn/DM/dm_td_ext_aia_org';

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

-- MAGIC %run ../../../Common/config

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

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

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

-- 20260130 由于DM.dm_td_org数据需要转换，新增这个CELL。
create or replace temporary view dm_td_org_temp
as 
SELECT org_key,mr_trty_tag,district_code,rsm_name,rc_name,p2.region_name,p2.rsd_kcode,p2.rsd_name
  ,CASE WHEN p2.yyyymm >= '202601' and p2.bu_code = 'PC12' then 'PC09' else p2.bu_code end bu_code
  ,CASE WHEN p2.yyyymm >= '202601' and p2.bu_report = 'EC' THEN 'Retail' ELSE p2.bu_report end bu_report
  ,CASE WHEN p2.yyyymm >= '202601' and p2.sub_bu_code = 'NA27' then 'NA06' else p2.sub_bu_code end sub_bu_code
  ,CASE WHEN p2.yyyymm >= '202601' and p2.sub_bu_name = 'EC' then 'Retail' else p2.sub_bu_name end sub_bu_name
  ,CASE WHEN p2.yyyymm >= '202601' and p2.sub_bu_name_en = 'EC' then 'Retail' else p2.sub_bu_name_en end sub_bu_name_en
  ,CASE WHEN p2.yyyymm >= '202601' and p2.bu_name = 'EC' then 'Retail' else p2.bu_name end bu_name
  ,CASE WHEN p2.yyyymm >= '202601' and p2.bu_name_en = 'EC' then 'Retail' else p2.bu_name_en end bu_name_en
  ,CASE WHEN p2.yyyymm >= '202601' and p2.nsd_kcode = 'ND_EC0001_V' then 'KSGX559' else p2.nsd_kcode end nsd_kcode
  ,CASE WHEN p2.yyyymm >= '202601' and p2.bu_name = 'EC'and p2.nsd_name = 'Vacant' then '李白杨' else p2.nsd_name end nsd_name
  ,CASE WHEN p2.yyyymm >= '202601' and p2.nsd_trty_code = 'ND_EC0001' then 'ND_RT0001' else p2.nsd_trty_code end nsd_trty_code
  ,CASE WHEN p2.yyyymm >= '202601' and p2.bsd_kcode = 'VP_EC0001_V' then 'VP_RT0001_V' else p2.bsd_kcode end bsd_kcode
  ,CASE WHEN p2.yyyymm >= '202601' and p2.bsd_trty_code = 'VP_EC0001' then 'VP_RT0001' else p2.bsd_trty_code end bsd_trty_code
  ,mr_kcode,dsm_trty_code,rsm_trty_code,dsm_kcode,rsd_trty_code
  ,district_name,dsm_name,mr_name,yyyymm,mr_trty_code,REGION_CODE,rc_code,bu_category,rsm_kcode
FROM DM.dm_td_org P2

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

---------------------------------------------------------------------------------------
--修改时间：20241105
--修改人：Fanxujia
--修改内容：
--for AIA Dashboard，新增NSD、RSD、RSM、DSM、MR、TEAM等信息
--仅AIA渠道的数据增加值，其他渠道都为空
---------------------------------------------------------------------------------------
---------------------------------------------------AIA------------------------------------------------------------------------------
--AIA参考CHPA
------所有BU能看AIA

--从销量表取到全部的机构、市场组合
with ALL_AUDIT_Market as (
select distinct NVL(t1.AUDIT_COD,'ROC') AUDIT_COD,
NVL(t2.market,'ALL Market') 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
)

--获取城市与大区中心的mapping
,EXTERNAL_GEO as (
select distinct
concat(city_c, '市') as city_c,
REGION_CENTER
from DM.DM_TD_EXTERNAL_GEO_TEMP
where DATA_SOURCE = 'AIA(Monthly)'
)
--CVH架构月
,CVH_YM as (
select key_value as yyyymm
from dm.dm_td_date_config
where key_code = 'pfmc_cvh'
)
--org表
,org as (
select distinct
t1.yyyymm,
t1.bu_category,
t1.bu_code,
t1.bu_name_en,
t1.sub_bu_code,
t1.sub_bu_name,
t1.REGION_CODE,
t1.region_name,
t1.rc_code,
t1.rc_name,
t1.district_name as team,
t1.mr_trty_code,
t1.nsd_kcode,
case when t1.nsd_name = 'Vacant' then t1.nsd_kcode else t1.nsd_name end as nsd_name,
t1.rsd_kcode,
case when t1.rsd_name = 'Vacant' then t1.rsd_kcode else t1.rsd_name end as rsd_name,
t1.rsm_kcode,
case when t1.rsm_name = 'Vacant' then t1.rsm_kcode else t1.rsm_name end as rsm_name,
t1.dsm_kcode,
case when t1.dsm_name = 'Vacant' then t1.dsm_kcode else t1.dsm_name end as dsm_name,
t1.mr_kcode,
case when t1.mr_name = 'Vacant' then t1.mr_kcode else t1.mr_name end as mr_name
from dm_td_org_temp t1
inner join CVH_YM t2
on t1.yyyymm = t2.yyyymm
)
--territory表
,territory as (
select distinct
t1.yyyymm,
t1.trty_code,
t1.inst_code,
t1.family_code
from dm.dm_td_sd_territory_indication t1
inner join CVH_YM t2
on t1.yyyymm = t2.yyyymm
where t1.bu not in ('CUBE','KA')
and ((t1.key_hp in (2) and t1.bu <> 'Retail' and t1.trty_code <>'Eagle') or t1.key_hp = 1)--chenwu 20250904 排除双考影响
)
--将内部的Family_code转为外部的pack_cod
,inst_pack_previous as (
select distinct
t1.yyyymm,
t1.trty_code,
t1.inst_code,
t2.city_name,
t5.market,
case when t2.area_code = 'SOUTH' then 'South'
     when t2.area_code = 'WEST' then 'West'
		 when t2.area_code = 'CENTRAL' then 'Central'
		 when t2.area_code = 'NORTH' then 'North'
		 when t2.area_code = 'EAST' then 'East'
end as REGION_CENTER
from territory t1
left join dwd.dwd_td_institution t2
on t1.inst_code = t2.inst_code
left join cdw_dwd_dim_product_wide t3
on t1.family_code = t3.prd_family_cd
left join dwd.dwd_td_imscode_azcode t4
on t3.prd_brand_cd = t4.brandcode
left join pack_market t5
on t4.ims_pack_cod = t5.PACK_COD
)
/*
20251207 Chenwu 把可定的架构复制到立普妥上,直接替换市场 'Atorvastatin Market' = 'Rosuvastatin Market'
*/
,inst_pack as (
  select 
    yyyymm,
    trty_code,
    inst_code,
    city_name,
    market,
    REGION_CENTER
  from inst_pack_previous
  union all
  select
    yyyymm,
    trty_code,
    inst_code,
    city_name,
    'Atorvastatin Market' market,
    REGION_CENTER
  from inst_pack_previous 
  where market = 'Rosuvastatin Market'
)

---------------------------------------------------------------------------------------
--修改时间：20250220
--修改人：Zhu chenwu
--修改内容：
--原本存在部分Eagel的机构和市场没有挂上架构，针对于没有挂上的部分，用dws.dws_td_eg_cityterritory中的city 和family的关系对应到市场挂上架构。
----------------------------------------------m-----------------------------------------
,eagle_trty as 
(---eagle的city 和 family 对应的 trtycode取数据时，如果city和family对应的trtycode 有多个，以p12，p11,p11为准，取第一个。
  select SP_TRTY_CD,CITY_CD,FAMILY_CD from (
  select *
  ,row_number() over (partition by CITY_CD,FAMILY_CD order by PROJECT_CD desc) as num
  from (
    select SP_TRTY_CD,CITY_CD,FAMILY_CD,PROJECT_CD  from  dws.dws_td_eg_cityterritory t1 
    inner join CVH_YM t2
    on t1.YM = t2.yyyymm
    where SP_TRTY_CD not in (select distinct trty_code from dm.dm_td_sd_territory_indication where bu = 'Eagle')
    and PROJECT_CD in (select distinct project_code from DWS.dws_td_eg_project_instype WHERE ins_type='医院') 
    and cast(nvl(SALES_RATE,0) as decimal(20,6))<>'0.000000' 
    group by  1,2,3,4
  )
) where num = 1
)
,family_mapping as (--family和brand的mapping
  select prd_family_cd,prd_brand_cd from cdw_dwd_dim_product_wide
  group by 1,2
)
,packcode_mapping as (--pack 和 brand的mapping
  select brandcode,ims_pack_cod from dwd.dwd_td_imscode_azcode 
  group by 1,2
)
,geo_mapping as (--city的中文
  select city_code,city_name from dm.dm_td_geography
  group by 1,2
)
,eagle_mapping (--eagle表中city和family所对应的 岗位编码
select SP_TRTY_CD,city_name,market from (
	select 
	t1.SP_TRTY_CD
	-- ,t1.FAMILY_CD
	-- ,t3.prd_brand_cd
	,t4.ims_pack_cod
	,t5.city_name
	from eagle_trty t1
	left join family_mapping t3
	on t1.FAMILY_CD = t3.prd_family_cd --转换family 成 brand
	left join packcode_mapping t4
	on t3.prd_brand_cd = t4.brandcode
	left join geo_mapping t5
	on t1.CITY_CD = t5.city_code) A
inner join pack_market B on A.ims_pack_cod = B.PACK_COD
group by 1,2,3
)

,audit_geo as (--audit的城市信息
select AUDIT_COD,max(CITY_C) as city_c from DM.DM_TD_EXTERNAL_GEO 
where DATA_SOURCE = 'AIA(Monthly)'
group by 1)

,eagle_trty_with_city as ( --eagle中 挂上 部分机构归不到eagle的，按eagle的城市+品牌挂上
select
t0.AUDIT_COD,t0.market
,t10.city_c
,eagle_mapping.SP_TRTY_CD from ALL_AUDIT_Market t0
left join audit_geo t10 on t0.AUDIT_COD = t10.AUDIT_COD
left join eagle_mapping on eagle_mapping.city_name = t10.city_c and eagle_mapping.market = t0.market 
where eagle_mapping.SP_TRTY_CD is not null
group by 1,2,3,4)

insert overwrite dm.DM_TD_EXT_AIA_ORG (
    REGION_CODE,
    region_name_en,
    rc_code,
    rc_name_en,
    RegionCenter,
    AUDIT_COD,
    MARKET,
    bu_category,
    bu_name_en,
    sub_bu_name,
    sub_bu_code_auth,
    bu_code_auth,
    DATA_SOURCE,
    REGION_RATIO,
    RSM_Name,
    RC_Name,
TEAM,
MR_TRTY_CODE,
NSD_KCODE,
NSD_NAME,
RSD_KCODE,
RSD_NAME,
RSM_KCODE,
DSM_KCODE,
DSM_NAME,
MR_KCODE,
MR_NAME
  )

SELECT distinct
  COALESCE(t2.REGION_CODE,t6.REGION_CODE,'ROC') as REGION_CODE,
  COALESCE(t2.region_name,t6.region_name,'ROC') as region_name_en,
  COALESCE(t2.rc_code,t6.rc_code,'ROC') as rc_code,
  COALESCE(t2.rc_name,t6.rc_name,'ROC') as rc_name_en,
  COALESCE(t1.REGION_CENTER,t4.REGION_CENTER,t7.REGION_CENTER,'') as RegionCenter,
  t0.AUDIT_COD,
  t0.MARKET,
  COALESCE(t2.bu_category,t6.bu_category,'') as bu_category,
  COALESCE(t2.bu_name_en,t6.bu_name_en,'') as bu_name_en,
  COALESCE(t2.sub_bu_name,t6.sub_bu_name,'') as sub_bu_name,
  COALESCE(t2.sub_bu_code,t6.sub_bu_code,'') as sub_bu_code_auth,
  concat(COALESCE(t2.bu_code,t6.bu_code,''),'AIA(Monthly)') as bu_code_auth,
  'AIA(Monthly)' as DATA_SOURCE,
  1 REGION_RATIO,
  COALESCE(t2.rsm_name,t6.rsm_name,'Vacant') as rsm_name,
  COALESCE(t2.rsd_name,t6.rsd_name,'Vacant') as RC_Name,
  COALESCE(t2.team,t6.team,'ROC') as team,
  COALESCE(t2.mr_trty_code,t6.mr_trty_code,'Vacant') as mr_trty_code,
  COALESCE(t2.nsd_kcode,t6.nsd_kcode,'Vacant') as nsd_kcode,
  COALESCE(t2.nsd_name,t6.nsd_name,'Vacant') as nsd_name,
  COALESCE(t2.rsd_kcode,t6.rsd_kcode,'Vacant') as rsd_kcode,
  COALESCE(t2.rsd_name,t6.rsd_name,'Vacant') as rsd_name,
  COALESCE(t2.rsm_kcode,t6.rsm_kcode,'Vacant') as rsm_kcode,
  COALESCE(t2.dsm_kcode,t6.dsm_kcode,'Vacant') as dsm_kcode,
  COALESCE(t2.dsm_name,t6.dsm_name,'Vacant') as dsm_name,
  COALESCE(t2.mr_kcode,t6.mr_kcode,'Vacant') as mr_kcode,
  COALESCE(t2.mr_name,t6.mr_name,'Vacant') as mr_name
-- ,t5.SP_TRTY_CD
from ALL_AUDIT_Market t0
left join inst_pack t1
on t0.AUDIT_COD = t1.inst_code
and t0.market = t1.market
left join org t2
on t1.trty_code = t2.mr_trty_code
and t1.yyyymm = t2.yyyymm
left join EXTERNAL_GEO t4
on t1.city_name = t4.city_c

left join eagle_trty_with_city t5
on t0.AUDIT_COD = t5.AUDIT_COD
and t0.market = t5.market and t1.inst_code is null
left join org t6
on t5.SP_TRTY_CD = t6.mr_trty_code
left join EXTERNAL_GEO t7
on t5.city_c = t7.city_c

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

-- DBTITLE 1,add
--修改时间：20240808
--修改人：FanXuJia
--修改背景：
--事实表与维度表来自于不同手工表，目前存在一部分audit_cod在事实表有销量，但是在维度表没有这个audit_cod，导致销量缺失。
--因此需要从事实表补全这部分audit_cod
with sales as (
select distinct PACK_COD,AUDIT_COD,DATA_SOURCE from dm.dm_tf_external_sales
union
select distinct PACK_COD,AUDIT_COD,DATA_SOURCE from dm.dm_tf_external_sales_thc
)
,market_pack as (
select distinct PACK_COD,MARKET,DATA_SOURCE from dm.dm_td_external_market_pack_mapping
)
,org as (
select distinct AUDIT_COD,MARKET,DATA_SOURCE from dm.DM_TD_EXT_AIA_ORG
)
,tmp_org as (
select distinct a.AUDIT_COD,b.MARKET,a.DATA_SOURCE
from sales a 
left join market_pack b on a.PACK_COD = b.PACK_COD and a.DATA_SOURCE = b.DATA_SOURCE
left join org c on a.DATA_SOURCE = c.DATA_SOURCE and a.AUDIT_COD = c.AUDIT_COD and b.MARKET = c.MARKET
where C.AUDIT_COD IS NULL
)
insert into table dm.DM_TD_EXT_AIA_ORG
(
REGION_CODE,
region_name_en,
RSM_Name,
rc_code,
rc_name_en,
RC_Name,
RegionCenter,
AUDIT_COD,
MARKET,
bu_category,
bu_name_en,
sub_bu_name,
sub_bu_code_auth,
bu_code_auth,
DATA_SOURCE,
REGION_RATIO,
TEAM,
MR_TRTY_CODE,
NSD_KCODE,
NSD_NAME,
RSD_KCODE,
RSD_NAME,
RSM_KCODE,
DSM_KCODE,
DSM_NAME,
MR_KCODE,
MR_NAME
)
select
'ROC' as REGION_CODE,
'ROC' as region_name_en,
'Vacant' as RSM_Name,
'ROC' as rc_code,
'ROC' as rc_name_en,
'Vacant' as RC_Name,
null as RegionCenter,
AUDIT_COD,
MARKET,
null as bu_category,
null as bu_name_en,
null as sub_bu_name,
null as sub_bu_code_auth,
DATA_SOURCE as bu_code_auth,
DATA_SOURCE,
1 as REGION_RATIO,
'ROC' as TEAM,
'Vacant' as MR_TRTY_CODE,
'Vacant' as NSD_KCODE,
'Vacant' as NSD_NAME,
'Vacant' as RSD_KCODE,
'Vacant' as RSD_NAME,
'Vacant' as RSM_KCODE,
'Vacant' as DSM_KCODE,
'Vacant' as DSM_NAME,
'Vacant' as MR_KCODE,
'Vacant' as MR_NAME
from tmp_org
where DATA_SOURCE = 'AIA(Monthly)'

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

-- DBTITLE 1,hidden_market
-----------------------------------------------------------
--修改时间：20241118
--修改人：Fanxujia
--修改内容：
--使用手工文件排除掉每个渠道的不需要显示的市场
-----------------------------------------------------------
with hidden_market as (
select distinct 
case when upper(data_source) = 'CHPA'    then 'IQVIA-CHPA(Monthly)'
     when upper(data_source) = '科室市场' then 'XH Data(Quarterly)'
     when upper(data_source) = 'EC'      then 'EC(Monthly)'
     when upper(data_source) = 'RETAIL'  then 'Retail(Quarterly)'
     when upper(data_source) = 'CHC'     then 'CHC(Quarterly)'
     when upper(data_source) = 'AIA'     then 'AIA(Monthly)'
     when upper(data_source) = 'THC'     then 'THC(Quarterly)'
     when upper(data_source) = 'COUNTY'  then 'IQVIA-COUNTY(Quarterly)'
     else data_source
end as data_source,
upper(hidden_market) as hidden_market
from dwd.dwd_gnd_ims_hidden_market
union all 
select
'AIA(Monthly)' as data_source,
'AIA ALL Market'
)


MERGE into dm.DM_TD_EXT_AIA_ORG as t1
USING hidden_market t2
on t1.data_source = t2.data_source
and upper(t1.MARKET) = upper(t2.hidden_market)
WHEN MATCHED THEN
DELETE