-- Databricks notebook source
-- CREATE OR REPLACE TABLE DM.DM_TD_EXT_CHPA_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_chpa_org';

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

----------------------------------------------------chpa------------------------------------------------------------------------------
------所有BU能看CHPA
insert overwrite DM.DM_TD_EXT_CHPA_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
CASE WHEN bu_category IN ('All Channel','EC') then 'ROC'
     when REGION_CODE IN ('Other Low Tiers', 'Non AZ Region') THEN 'ROC'
     ELSE REGION_CODE
 END REGION_CODE,
CASE WHEN bu_category IN ('All Channel','EC') then 'ROC'
      when region_name IN ('Other Low Tiers', 'Non AZ Region') THEN 'ROC'
      ELSE region_name
 END region_name,
CASE WHEN bu_category IN ('All Channel','EC') then 'ROC'
     when rc_code IN ('Other Low Tiers', 'Non AZ Region') THEN 'ROC'
     ELSE rc_code
 END rc_code,
CASE WHEN bu_category IN ('All Channel','EC') then 'ROC'
     when rc_name IN ('Other Low Tiers', 'Non AZ Region') THEN 'ROC'
     ELSE rc_name
 END rc_name,
  RegionCenter,
  AUDIT_COD,
  MARKET,
  bu_category,
  bu_name_en,
  sub_bu_name,
  sub_bu_code_auth,
  concat(bu_code_auth, 'IQVIA-CHPA(Monthly)') bu_code_auth,
  'IQVIA-CHPA(Monthly)' DATA_SOURCE,
  1 REGION_RATIO,
  rsm_name,
  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.DM_ims_TD_ORG
WHERE
  MARKET <> 'IMS ALL Market'
  and AUDIT_COD is not null
  and ORG_KEY is not null
--AND bu_category NOT IN ('All Channel','EC')

-- 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_CHPA_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_CHPA_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 = 'IQVIA-CHPA(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_CHPA_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