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

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

--dm_org --使用和performance cvh 的org架构
insert overwrite table tmp.tmp_ims_td_org_hvh
select distinct
  t1.yyyymm,
  dsm_trty_code,
  dsm_kcode,
  case
    when dsm_name = 'Vacant' then concat (dsm_trty_code, '_V')
    else dsm_name
  end as dsm_name,
  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,
  district_code,
  district_name,
  district_name_en,
  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,
  --福厦泉 苏锡 珠三角 对应多个city的情况的拆分比
  -- 20260210 Yusuo Update
  -- case when t4.ratio is null then 1 else t4.ratio end as ims_city_ratio
  1 as ims_city_ratio
from
  dm.dm_td_org t1
  left join (
    select distinct
      t1.yyyymm,
      t1.trty_code,
      t2.city_name,
      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
  -- left join dws.dws_td_ims_city_mapping t4 on t2.city_name=concat(t4.city_c_d,'市')
  --使用和performance cvh 的org架构
where
  left (t1.yyyymm, 4) in (
    select
      left (key_value, 4)
    from
      dm.dm_td_date_config
    where
      key_code = 'pfmc_cvh'
  )

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

  --将还没有做出来AZ产品关系的market挂给所有的对应BU
insert into
  tmp.tmp_ims_td_org_hvh
select distinct
  t1.yyyymm,
  'Vacant' as dsm_trty_code,
  'Vacant' as dsm_kcode,
  'Vacant' as dsm_name,
  '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 district_code,
  'Non AZ Region' as district_name,
  'Non AZ Region' as district_name_en,
  '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.tmp_ims_td_org_hvh t1
  left join (
    select distinct
      t2.market,
      t2.bu
    from
      tmp.tmp_ims_td_org_hvh 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.tmp_ims_td_org_hvh
select distinct
  yyyymm,
  dsm_trty_code,
  dsm_kcode,
  dsm_name,
  rsm_trty_code,
  rsm_kcode,
  rsm_name,
  rsd_trty_code,
  rsd_kcode,
  rsd_name,
  nsd_trty_code,
  nsd_kcode,
  nsd_name,
  district_code,
  district_name,
  district_name_en,
  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 dsm_trty_code,
      'Vacant' as dsm_kcode,
      'Vacant' as dsm_name,
      '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 district_code,
      'Non AZ Region' as district_name,
      'Non AZ Region' as district_name_en,
      '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,
      --     'ALL' as sub_bu_code,'ALL' as sub_bu_name,'ALL' as sub_bu_name_en,
      market,
      ims_city_ratio,
      yyyymm,
      'Other Low Tiers' as city_name,
      'ROC' as ims_audit_cod
    from
      tmp.tmp_ims_td_org_hvh
      --     )t2 --on t1.bu=t2.bu_name_en
    where
      bu_code is not null
      --有销量的ROC数据
      and concat (market, '_ROC') in (
        select distinct
          market_audit_key
        from
          dm.dm_ims_tf_sales
        where
          audit_cod = 'ROC'
      )
  ) t2 on t1.audit_cod = t2.ims_audit_cod

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

  --将所有未能挂上Org的市场数据补全，同时根据市场找到对应BU
insert into
  tmp.tmp_ims_td_org_hvh
select distinct
  yyyymm,
  dsm_trty_code,
  dsm_kcode,
  dsm_name,
  rsm_trty_code,
  rsm_kcode,
  rsm_name,
  rsd_trty_code,
  rsd_kcode,
  rsd_name,
  nsd_trty_code,
  nsd_kcode,
  nsd_name,
  district_code,
  district_name,
  district_name_en,
  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 dsm_trty_code,
      'Vacant' as dsm_kcode,
      'Vacant' as dsm_name,
      '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 district_code,
      'Non AZ Region' as district_name,
      'Non AZ Region' as district_name_en,
      '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,
      t1.yyyymm,
      t2.market,
      ims_city_ratio
    from
      tmp.tmp_ims_td_org_hvh t1
      inner join (
        --筛选出挂不上Org的市场城市，同时又有销量的
        select distinct
          t2.market,
          t2.audit_cod,
          t3.bu
        from
          tmp.tmp_ims_td_org_hvh t1
          right join (
            select distinct
              audit_cod,
              market
            from
              dm.dm_ims_tf_sales
          ) 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 ----------

  insert overwrite table dm.dm_ims_td_org_hvh
select distinct
  t1.yyyymm,
  concat (
    t1.market,
    '_',
    t2.audit_cod,
    '_',
    right (t1.yyyymm, 2)
  ) as ORG_KEY,
  t1.dsm_trty_code,
  t1.dsm_kcode,
  t1.dsm_name,
  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.district_code,
  t1.district_name,
  t1.district_name_en,
  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.tmp_ims_td_org_hvh 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
union all
select distinct
  t1.yyyymm,
  concat (
    'IMS ALL Market',
    '_',
    t2.audit_cod,
    '_',
    right (t1.yyyymm, 2)
  ) as ORG_KEY,
  t1.dsm_trty_code,
  t1.dsm_kcode,
  t1.dsm_name,
  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.district_code,
  t1.district_name,
  t1.district_name_en,
  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.tmp_ims_td_org_hvh t1
  left join dws.dws_ims_td_geo t2 on t1.ims_audit_cod = t2.audit_cod