-- Databricks notebook source
-- CREATE OR REPLACE TABLE DM.DM_TD_EXT_XIEHE_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_xiehe_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 ----------

---------------------------------------------------XIEHE------------------------------------------------------------------------------
--XIEHE参考CHPA
------所有BU能看XIEHE
insert overwrite dm.dm_td_ext_xiehe_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 t1.REGION_CODE,
  t1.region_name,
  t1.rc_code,
  t1.rc_name,
  t4.REGION_CENTER RegionCenter,
  t4.AUDIT_COD,
  t3.MARKET,
  t1.bu_category,
  t1.bu_name_en,
  t1.sub_bu_name,
  t1.sub_bu_code sub_bu_code_auth,
  concat(t1.bu_code, 'XH Data(Quarterly)') bu_code_auth,
  'XH Data(Quarterly)' DATA_SOURCE,
  1 REGION_RATIO,
  t1.RSM_Name,
  t1.rsd_name,
  '' as TEAM,
'' as MR_TRTY_CODE,
'' as NSD_KCODE,
'' as NSD_NAME,
'' as RSD_KCODE,
'' as RSD_NAME,
'' as RSM_KCODE,
'' as DSM_KCODE,
'' as DSM_NAME,
'' as MR_KCODE,
'' as MR_NAME
from
  dm_td_org_temp t1
  left join(
    select
      distinct A.yyyymm,
      A.trty_code,
      B.city_name,
      D.ims_prod_cod,
      D.ims_pack_cod
    from
      dm.dm_td_sd_territory_indication A
      left join dwd.dwd_td_institution B on A.inst_code = B.inst_code
      left join cdw_dwd_dim_product_wide C on C.prd_family_cd = A.family_code
      left join dwd.dwd_td_imscode_azcode D on C.prd_brand_cd = D.brandcode
    where A.bu not in ('CUBE','KA')
    and A.key_hp<>3 --chenwu 20250904 排除双考影响
  ) t2 on t2.trty_code = t1.mr_trty_code
  and t2.yyyymm = t1.yyyymm
  left join dws.dws_ims_td_market t3 on t2.ims_pack_cod = t3.pack_cod
  left join DM.DM_TD_EXTERNAL_GEO_TEMP t4 on t2.city_name = concat(t4.city_c, '市')
  AND t4.DATA_SOURCE = 'XH Data(Quarterly)' 
--使用和performance cvh 的org架构
where
  t1.yyyymm in(
    select
      key_value
    from
      dm.dm_td_date_config
    where
      key_code = 'pfmc_cvh'
  )
  and t3.market is not null
  and t4.audit_cod is not null

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

-- DBTITLE 1,add
--修改时间：20240808
--修改人：FanXuJia
--修改背景：
--事实表与维度表来自于不同手工表，目前存在一部分audit_cod在事实表有销量，但是在维度表没有这个audit_cod，导致销量缺失。
--因此需要从事实表补全这部分audit_cod
with sales as (
select distinct PACK_CODE,AUDIT_CODE,DATA_SOURCE from dm.DM_TF_EXT_UNIONALL_SALES
)
,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_XIEHE_ORG
)
,tmp_org as (
select distinct a.AUDIT_CODE,b.MARKET,a.DATA_SOURCE
from sales a 
left join market_pack b on a.PACK_CODE = b.PACK_COD and a.DATA_SOURCE = b.DATA_SOURCE
left join org c on a.DATA_SOURCE = c.DATA_SOURCE and a.AUDIT_CODE = c.AUDIT_COD and b.MARKET = c.MARKET
where C.AUDIT_COD IS NULL
)

insert into table dm.dm_td_ext_xiehe_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_CODE,
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 = 'XH Data(Quarterly)'

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


MERGE into dm.dm_td_ext_xiehe_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
