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

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

/*
修改时间：20250707
修改人：chenwu
内容：xiehe数据源变更，逻辑调整
*/
insert overwrite dws.dws_ext_xiehe_raw_data
with 
--法伯的code 和 iqviacode 的对应关系
pack_cod_2_iqvia_pack_code(
  select  
    pack_cod
    ,max(IQVIA_PACK_CODE) IQVIA_PACK_CODE
  from dwd.dwd_gnd_dept_pack_property
  group by 1
)
,geo_mapping(
  select 
    province_map fact_province
    ,city_map fact_city
    ,max(city_c) city
    ,max(province_c) province
    ,max(audit_cod) audit_cod
  from dm.dm_ext_xiehe_geo
  group by 1,2
)
,pack_countingunit(
  select 
    pack_cod
    ,max(countingunit) countingunit
  from dm.dm_xiehe_pack_property
  group by 1
)

,underlying_ly_data(
  select 
  c.audit_cod
  ,b.iqvia_pack_code
  ,c.city
  ,c.province
  ,a.yyyymm
  ,a.h_level
  ,a.reimburse
  ,a.prescription_source
  ,a.prescription_dept_lv1
  ,a.prescription_dept_lv2
  ,a.prescription_dept_lv3
  ,a.new_code
  ,a.atc
  ,a.common_name
  ,a.product_name 
  ,a.manu_des 
  ,a.pack_des 
  ,a.drug_delivery_route 
  ,a.nfc 
  ,a.PackSize
  ,a.PackageType
  ,a.prescription
  ,a.sales_vol*nvl(d.countingunit,0) as sales_vol
  -- ,a.price
  ,a.sales_value
  ,0 as prescription_ly
  ,0 as sales_vol_ly
  -- ,0 as price_ly
  ,0 as sales_value_ly
  from dwd.dwd_gnd_ext_xiehe_raw_data a
  left join pack_cod_2_iqvia_pack_code b
    on upper(a.new_code) = upper(b.pack_cod)
  left join geo_mapping c 
    on a.area = c.fact_province
    and a.city = c.fact_city
  left join pack_countingunit d
    on b.iqvia_pack_code = d.pack_cod
  union all
  select 
  c.audit_cod
  ,b.iqvia_pack_code
  ,c.city
  ,c.province
  ,(a.yyyymm + 100) yyyymm
  ,a.h_level
  ,a.reimburse
  ,a.prescription_source
  ,a.prescription_dept_lv1
  ,a.prescription_dept_lv2
  ,a.prescription_dept_lv3
  ,a.new_code
  ,a.atc
  ,a.common_name
  ,a.product_name 
  ,a.manu_des 
  ,a.pack_des 
  ,a.drug_delivery_route 
  ,a.nfc 
  ,a.PackSize
  ,a.PackageType
  ,0                   as prescription
  ,0                   as sales_vol    
  -- ,0                   as price      	
  ,0                   as sales_value  
  ,a.prescription      as prescription_ly
  ,a.sales_vol*nvl(d.countingunit,0) as sales_vol_ly
  -- ,a.price      			 as price_ly
  ,a.sales_value       as sales_value_ly
  from dwd.dwd_gnd_ext_xiehe_raw_data a
  left join pack_cod_2_iqvia_pack_code b
    on upper(a.new_code) = upper(b.pack_cod)
  left join geo_mapping c 
    on a.area = c.fact_province
    and a.city = c.fact_city
  left join pack_countingunit d
    on b.iqvia_pack_code = d.pack_cod
  where (a.yyyymm + 100) <= (select max(yyyymm) from dwd.dwd_gnd_ext_xiehe_raw_data)
)

  select
    yyyymm 
    ,iqvia_pack_code
    ,AUDIT_COD 
    ,city
    ,province
    ,replace(h_level,'.0','') h_level
    ,reimburse
    ,null as reimburse_type
    ,prescription_source
    ,prescription_dept_lv1
    ,prescription_dept_lv2
    ,prescription_dept_lv3
    ,prescription_dept_lv3 dept_name
    ,new_code
    ,atc
    ,common_name
    ,product_name 
    ,manu_des 
    ,pack_des 
    ,drug_delivery_route 
    ,nfc 
    ,PackSize
    ,PackageType
    ,sum(prescription)      prescription
    ,SUM(sales_vol)			counting_unit
    ,null as price
    ,SUM(sales_value)       sales_value
    ,sum(prescription_ly)   prescription_ly
    ,SUM(sales_vol_ly)      counting_unit_ly
    ,null as price_ly
    ,SUM(sales_value_ly)    sales_value_ly
    ,from_utc_timestamp(current_timestamp(),'UTC+8')	etl_insert_dt
    ,from_utc_timestamp(current_timestamp(),'UTC+8')	etl_update_dt
  from underlying_ly_data
  group by
    yyyymm 
    ,iqvia_pack_code
    ,AUDIT_COD 
    ,city
    ,province
    ,h_level
    ,reimburse
    -- ,null as reimburse_type
    ,prescription_source
    ,prescription_dept_lv1
    ,prescription_dept_lv2
    ,prescription_dept_lv3
    ,prescription_dept_lv3
    ,new_code
    ,atc
    ,common_name
    ,product_name 
    ,manu_des 
    ,pack_des 
    ,drug_delivery_route 
    ,nfc 
    ,PackSize
    ,PackageType

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

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

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

insert overwrite dm.dm_ext_xiehe_sales
select 
  yyyymm
  ,iqvia_pack_code
  ,AUDIT_COD
  ,city
  ,province
  ,h_level
  ,reimburse
  ,reimburse_type
  ,prescription_source
  ,prescription_dept_lv1
  ,prescription_dept_lv2
  ,prescription_dept_lv3
  ,dept_name
  ,new_code
  ,atc
  ,common_name
  ,product_name 
  ,manu_des 
  ,pack_des 
  ,drug_delivery_route 
  ,nfc 
  ,prescription
  ,counting_unit
  ,price
  ,sales_value
  ,prescription_ly
  ,counting_unit_ly
  ,price_ly
  ,sales_value_ly
from dws.dws_ext_xiehe_raw_data