-- Databricks notebook source
-- MAGIC %md
-- MAGIC ### dws层xiehegeo

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


insert overwrite dws.dws_ext_xiehe_geo
with geo_xiehe as (-- dwd层表
  select fact_province,fact_city,geo_city,nvl(geo_province,'') geo_province
  from dwd.dwd_gnd_xiehe_geo 
  group by fact_province,fact_city,geo_city,nvl(geo_province,'')
)

-- 特殊区域：'黑吉辽','云贵川','湖北','其他地区'
select 
  -- a.*,
  concat('xiehe_',nvl(b.AUDIT_COD,concat(a.geo_province,a.fact_province,a.fact_city))) as AUDIT_COD
  ,nvl(b.CITY,a.geo_city) CITY
  ,nvl(b.CITY_C,a.geo_city) CITY_C
  ,'City' CITY_TYPE
  ,'4' AZ_CITY_TIER
  ,a.geo_province PROVINCE
  ,a.geo_province PROVINCE_C
  ,concat('xiehe_',nvl(b.PROVINCE_CODE,a.geo_province)) PROVINCE_CODE
  ,a.fact_city as CITY_MAP
  ,a.fact_province as PROVINCE_MAP
  ,case when a.geo_province='' then a.fact_province else a.geo_province end as REGION_CENTER
  ,from_utc_timestamp(current_timestamp(),'UTC+8') ETL_INSERT_DT
  ,from_utc_timestamp(current_timestamp(),'UTC+8') ETL_UPDATE_DT
from geo_xiehe a
left join dm.dm_ims_td_geo b on a.geo_city = b.CITY_C
where fact_province in ('黑吉辽','云贵青','湖北','其他地区')

union all
-- 非特殊区域
select 
  -- a.* ,
  concat('xiehe_',nvl(b.AUDIT_COD,concat(a.geo_province,a.fact_province,a.fact_city))) as AUDIT_COD
  ,nvl(b.CITY,a.geo_city) CITY
  ,nvl(b.CITY_C,a.geo_city) CITY_C
  ,'City' CITY_TYPE
  ,nvl(b.AZ_CITY_TIER,'4') AZ_CITY_TIER
  ,a.geo_province PROVINCE
  ,a.geo_province PROVINCE_C
  ,concat('xiehe_',nvl(b.PROVINCE_CODE,a.geo_province)) PROVINCE_CODE
  ,a.fact_city as CITY_MAP
  ,a.fact_province as PROVINCE_MAP
  ,a.geo_province as REGION_CENTER
  ,from_utc_timestamp(current_timestamp(),'UTC+8') ETL_INSERT_DT
  ,from_utc_timestamp(current_timestamp(),'UTC+8') ETL_UPDATE_DT
from geo_xiehe a
left join dm.dm_ims_td_geo b on a.geo_city = b.CITY_C
where fact_province not in ('黑吉辽','云贵青','湖北','其他地区')

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

-- MAGIC %md
-- MAGIC ### dm

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

insert overwrite dm.dm_ext_xiehe_geo
select 
  AUDIT_COD
  ,CITY
  ,CITY_C
  ,CITY_TYPE
  ,AZ_CITY_TIER
  ,PROVINCE
  ,PROVINCE_C
  ,PROVINCE_CODE
  ,CITY_MAP
  ,PROVINCE_MAP
  ,REGION_CENTER
from dws.dws_ext_xiehe_geo
