-- Databricks notebook source
-- 源表：DM.dm_td_org,dm.dm_td_territory_indication,dm.dm_td_date_config,dm.dm_ims_tf_sales
-- 目标表：dm.dm_ims_td_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 ----------

create or replace temporary view dm_td_org_tmp 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,
  p2.rsm_kcode,
  p2.rc_code,
  p2.rc_name_en,
  p2.region_code,
  p2.region_name_en,
  BU_CATEGORY,
  yyyymm,
  mr_trty_code,
  mr_kcode,
  dsm_trty_code,
  rsm_trty_code,
  dsm_kcode,
  rsd_trty_code
FROM
  DM.dm_td_org P2

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

--dm_org --使用和performance cvh 的org架构
-- insert overwrite table tmp.tmp_ims_td_org
create or replace temporary view tmp_ims_td_org as
select distinct
  rsm_trty_code,
  rsm_kcode,
  case
    when rsm_name = 'Vacant' then concat (rsm_trty_code, '_V')
    else rsm_name
  end as rsm_name,
  rsd_trty_code,
  rsd_kcode,
  case
    when rsd_name = 'Vacant' then concat (rsd_trty_code, '_V')
    else rsd_name
  end as rsd_name,
  nsd_trty_code,
  nsd_kcode,
  case
    when nsd_name = 'Vacant' then concat (nsd_trty_code, '_V')
    else nsd_name
  end as nsd_name,
  region_code,
  region_name,
  region_name_en,
  rc_code,
  rc_name,
  rc_name_en,
  bu_code,
  bu_name,
  bu_name_en,
  bu_category,
  sub_bu_code,
  sub_bu_name,
  sub_bu_name_en,
  t2.city_name,
  null as ims_pack_cod,
  t3.market,
  t2.province_code as ims_audit_cod,
  1 as ims_city_ratio
from
  dm_td_org_tmp t1
  left join (
    select distinct
      t1.yyyymm,
      t1.trty_code,
      t2.city_name,
      -- 20260210 Yusuo ADD
      t2.province_code,
      t4.ims_prod_cod,
      t4.ims_pack_cod
    from
      dm.dm_td_territory_indication t1
      left join dwd.dwd_td_institution t2 on t1.inst_code = t2.inst_code
      left join cdw_dwd_dim_product_wide t3 on t3.prd_family_cd = t1.family_code
      left join dwd.dwd_td_imscode_azcode t4 on t3.prd_brand_cd = t4.brandcode
    where
      t1.bu not in ('CUBE', 'KA')
  ) 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
  --使用和performance cvh 的org架构
where
  t1.yyyymm in (
    select
      key_value
    from
      dm.dm_td_date_config
    where
      key_code = 'pfmc_cvh'
  )

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

--将还没有做出来AZ产品关系的market挂给所有的对应BU
-- insert into tmp_ims_td_org
create or replace temporary view tmp_ims_td_org1 as
select distinct
  'Vacant' as rsm_trty_code,
  'Vacant' as rsm_kcode,
  'Vacant' as rsm_name,
  'Vacant' as rsd_trty_code,
  'Vacant' as rsd_kcode,
  'Vacant' as rsd_name,
  'Vacant' as nsd_trty_code,
  'Vacant' as nsd_kcode,
  'Vacant' as nsd_name,
  'Non AZ Region' as region_code,
  'Non AZ Region' as region_name,
  'Non AZ Region' as region_name_en,
  'Non AZ Region' as rc_code,
  'Non AZ Region' as rc_name,
  'Non AZ Region' as rc_name_en,
  bu_code,
  bu_name,
  bu_name_en,
  bu_category,
  sub_bu_code,
  sub_bu_name,
  sub_bu_name_en,
  city_name,
  null as ims_pack_cod,
  t2.market,
  ims_audit_cod,
  ims_city_ratio
from
  tmp_ims_td_org t1
  left join (
    select distinct
      t2.market,
      t2.bu
    from
      tmp_ims_td_org t1
      right join dwd.dwd_td_ims_market_ta_order t2 on t1.market = t2.market
    where
      t1.market is null
      and t2.market <> 'IMS ALL Market'
  ) t2 on t1.bu_name_en = t2.bu
where
  t2.market is not null

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

--将所有的ROC的市场数据补全，同时根据市场找到对应BU
-- insert into tmp_ims_td_org
create or replace temporary view tmp_ims_td_org2 as
select distinct
  rsm_trty_code,
  rsm_kcode,
  rsm_name,
  rsd_trty_code,
  rsd_kcode,
  rsd_name,
  nsd_trty_code,
  nsd_kcode,
  nsd_name,
  region_code,
  region_name,
  region_name_en,
  rc_code,
  rc_name,
  rc_name_en,
  t2.bu_code,
  t2.bu_name,
  t2.bu_name_en,
  t2.bu_category,
  t2.sub_bu_code,
  t2.sub_bu_name,
  t2.sub_bu_name_en,
  t2.city_name,
  null as ims_pack_cod,
  t2.market,
  t2.ims_audit_cod,
  t2.ims_city_ratio
from
  (
    select
      'Vacant' as rsm_trty_code,
      'Vacant' as rsm_kcode,
      'Vacant' as rsm_name,
      'Vacant' as rsd_trty_code,
      'Vacant' as rsd_kcode,
      'Vacant' as rsd_name,
      'Vacant' as nsd_trty_code,
      'Vacant' as nsd_kcode,
      'Vacant' as nsd_name,
      'Other Low Tiers' as region_code,
      'Other Low Tiers' as region_name,
      'Other Low Tiers' as region_name_en,
      'Other Low Tiers' as rc_code,
      'Other Low Tiers' as rc_name,
      'Other Low Tiers' as rc_name_en,
      'Other Low Tiers' as city_name,
      'ROC' as audit_cod
  ) t1
  full join (
    select distinct
      bu_code,
      bu_name,
      bu_name_en,
      bu_category,
      sub_bu_code,
      sub_bu_name,
      sub_bu_name_en,
      market,
      ims_city_ratio,
      'Other Low Tiers' as city_name,
      'ROC' as ims_audit_cod
    from
      tmp_ims_td_org1
    where
      bu_code is not null
      --有销量的ROC数据
      and concat (market, '_ROC') in (
        select distinct
          concat(market,'_ROC' ) market_audit_key from
              dm.dm_tf_ext_unionall_market_sales
        where DATA_SOURCE = 'IQVIA-CHPA(Monthly)' and
          audit_cod = 'ROC'
      )
  ) t2 on t1.audit_cod = t2.ims_audit_cod

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

--将所有未能挂上Org的市场数据补全，同时根据市场找到对应BU
-- insert into tmp_ims_td_org
create or replace temporary view tmp_ims_td_org3 as
select distinct
  rsm_trty_code,
  rsm_kcode,
  rsm_name,
  rsd_trty_code,
  rsd_kcode,
  rsd_name,
  nsd_trty_code,
  nsd_kcode,
  nsd_name,
  region_code,
  region_name,
  region_name_en,
  rc_code,
  rc_name,
  rc_name_en,
  t2.bu_code,
  t2.bu_name,
  t2.bu_name_en,
  t2.bu_category,
  t2.sub_bu_code,
  t2.sub_bu_name,
  t2.sub_bu_name_en,
  t2.city_name,
  null as ims_pack_cod,
  t2.market,
  t2.ims_audit_cod,
  t2.ims_city_ratio
from
  (
    select
      'Vacant' as rsm_trty_code,
      'Vacant' as rsm_kcode,
      'Vacant' as rsm_name,
      'Vacant' as rsd_trty_code,
      'Vacant' as rsd_kcode,
      'Vacant' as rsd_name,
      'Vacant' as nsd_trty_code,
      'Vacant' as nsd_kcode,
      'Vacant' as nsd_name,
      'Non AZ Region' as region_code,
      'Non AZ Region' as region_name,
      'Non AZ Region' as region_name_en,
      'Non AZ Region' as rc_code,
      'Non AZ Region' as rc_name,
      'Non AZ Region' as rc_name_en
  ) t1
  full join (
    select distinct
      bu_code,
      bu_name,
      bu_name_en,
      bu_category,
      sub_bu_code,
      sub_bu_name,
      sub_bu_name_en,
      --     'ALL' as sub_bu_code,'ALL' as sub_bu_name,'ALL' as sub_bu_name_en,
      t2.audit_cod as ims_audit_cod,
      null as city_name,
      t2.market,
      ims_city_ratio
    from
      tmp_ims_td_org2 t1
      inner join (
        --筛选出挂不上Org的市场城市，同时又有销量的
        select distinct
          t2.market,
          t2.audit_cod,
          t3.bu
        from
          tmp_ims_td_org2 t1
          right join (
            select distinct
              audit_cod,
              market
            from
              dm.dm_tf_ext_unionall_market_sales
              where DATA_SOURCE = 'IQVIA-CHPA(Monthly)'
          ) t2 on concat (t1.market, '_', t1.ims_audit_cod) = concat (t2.market, '_', t2.audit_cod)
          left join dwd.dwd_td_ims_market_ta_order t3 on t2.market = t3.market
        where
          concat (t1.market, '_', t1.ims_audit_cod) is null
          and t2.market <> 'IMS ALL Market'
      ) t2 on t1.bu_name_en = t2.bu
  ) t2
where
  t2.bu_code is not null

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

create or replace temporary view tmp_ims_td_org4 as
select
  rsm_trty_code,
  rsm_kcode,
  rsm_name,
  rsd_trty_code,
  rsd_kcode,
  rsd_name,
  nsd_trty_code,
  nsd_kcode,
  nsd_name,
  region_code,
  region_name,
  region_name_en,
  rc_code,
  rc_name,
  rc_name_en,
  bu_code,
  bu_name,
  bu_name_en,
  bu_category,
  sub_bu_code,
  sub_bu_name,
  sub_bu_name_en,
  city_name,
  ims_pack_cod,
  market,
  ims_audit_cod,
  ims_city_ratio
from
  tmp_ims_td_org
union all
select
  rsm_trty_code,
  rsm_kcode,
  rsm_name,
  rsd_trty_code,
  rsd_kcode,
  rsd_name,
  nsd_trty_code,
  nsd_kcode,
  nsd_name,
  region_code,
  region_name,
  region_name_en,
  rc_code,
  rc_name,
  rc_name_en,
  bu_code,
  bu_name,
  bu_name_en,
  bu_category,
  sub_bu_code,
  sub_bu_name,
  sub_bu_name_en,
  city_name,
  ims_pack_cod,
  market,
  ims_audit_cod,
  ims_city_ratio
from
  tmp_ims_td_org1
union all
select
  rsm_trty_code,
  rsm_kcode,
  rsm_name,
  rsd_trty_code,
  rsd_kcode,
  rsd_name,
  nsd_trty_code,
  nsd_kcode,
  nsd_name,
  region_code,
  region_name,
  region_name_en,
  rc_code,
  rc_name,
  rc_name_en,
  bu_code,
  bu_name,
  bu_name_en,
  bu_category,
  sub_bu_code,
  sub_bu_name,
  sub_bu_name_en,
  city_name,
  ims_pack_cod,
  market,
  ims_audit_cod,
  ims_city_ratio
from
  tmp_ims_td_org2
union all
select
  rsm_trty_code,
  rsm_kcode,
  rsm_name,
  rsd_trty_code,
  rsd_kcode,
  rsd_name,
  nsd_trty_code,
  nsd_kcode,
  nsd_name,
  region_code,
  region_name,
  region_name_en,
  rc_code,
  rc_name,
  rc_name_en,
  bu_code,
  bu_name,
  bu_name_en,
  bu_category,
  sub_bu_code,
  sub_bu_name,
  sub_bu_name_en,
  city_name,
  ims_pack_cod,
  market,
  ims_audit_cod,
  ims_city_ratio
from
  tmp_ims_td_org3

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

insert overwrite table dm.dm_ims_td_org
select distinct
  concat (t1.market, '_', t2.audit_cod) as ORG_KEY,
  t1.rsm_trty_code,
  t1.rsm_kcode,
  t1.rsm_name,
  t1.rsd_trty_code,
  t1.rsd_kcode,
  t1.rsd_name,
  t1.nsd_trty_code,
  t1.nsd_kcode,
  t1.nsd_name,
  t1.region_code,
  t1.region_name,
  t1.region_name_en,
  t1.rc_code,
  t1.rc_name,
  t1.rc_name_en,
  t1.bu_code,
  t1.bu_name,
  t1.bu_name_en,
  t1.bu_category,
  t1.sub_bu_code,
  t1.sub_bu_name,
  t1.sub_bu_name_en,
  t1.sub_bu_code as sub_bu_code_auth, --前端权限配置需要的字段
  t1.bu_code as bu_code_auth, --前端权限配置需要的字段
  t2.audit_cod,
  t2.audit_des,
  t2.audit_des_c,
  t2.Province,
  t2.Province_C,
  t2.RegionCenter,
  t1.market,
  concat (t1.market, '_', t2.audit_cod) as market_audit_key,
  from_utc_timestamp (current_timestamp(), 'UTC+8') ETL_INSERT_DT,
  from_utc_timestamp (current_timestamp(), 'UTC+8') ETL_UPDATE_DT
from
  tmp_ims_td_org4 t1
  left join dws.dws_ims_td_geo t2 on t1.ims_audit_cod = t2.audit_cod
  --where t1.market is not null and t1.ims_audit_cod is not null

  /* 20260409 chenwu IMS ALL Market 不需要了
union all
select distinct
  concat ('IMS ALL Market_', t2.audit_cod) as ORG_KEY,
  t1.rsm_trty_code,
  t1.rsm_kcode,
  t1.rsm_name,
  t1.rsd_trty_code,
  t1.rsd_kcode,
  t1.rsd_name,
  t1.nsd_trty_code,
  t1.nsd_kcode,
  t1.nsd_name,
  t1.region_code,
  t1.region_name,
  t1.region_name_en,
  t1.rc_code,
  t1.rc_name,
  t1.rc_name_en,
  t1.bu_code,
  t1.bu_name,
  t1.bu_name_en,
  t1.bu_category,
  t1.sub_bu_code,
  t1.sub_bu_name,
  t1.sub_bu_name_en,
  'ALL' as sub_bu_code_auth,
  'ALL' as bu_code_auth,
  t2.audit_cod,
  t2.audit_des,
  t2.audit_des_c,
  t2.Province,
  t2.Province_C,
  t2.RegionCenter,
  'IMS ALL Market' as market,
  concat ('IMS ALL Market_', t2.audit_cod) as market_audit_key,
  from_utc_timestamp (current_timestamp(), 'UTC+8') ETL_INSERT_DT,
  from_utc_timestamp (current_timestamp(), 'UTC+8') ETL_UPDATE_DT
from
  tmp_ims_td_org4 t1
  left join dws.dws_ims_td_geo t2 on t1.ims_audit_cod = t2.audit_cod
where
  t1.market is not null
  and t1.ims_audit_cod is not null
  */