-- Databricks notebook source
-- CREATE OR REPLACE TABLE dm.dm_chc_pack_property (
--   PACK_COD STRING,
--   pack_des STRING,
--   stgh_des STRING,
--   pack_lch STRING,
--   PROD_COD STRING,
--   prod_des STRING,
--   prod_des_c STRING,
--   Family_Code STRING,
--   Family_Name STRING,
--   cmps_cod STRING,
--   cmps_des STRING,
--   cmps_des_c STRING,
--   atc1_cod STRING,
--   atc1_des STRING,
--   atc1_des_c STRING,
--   atc2_cod STRING,
--   atc2_des STRING,
--   atc2_des_c STRING,
--   atc3_cod STRING,
--   atc3_des STRING,
--   atc3_des_c STRING,
--   atc4_cod STRING,
--   atc4_des STRING,
--   atc4_des_c STRING,
--   app1_cod STRING,
--   app1_des STRING,
--   app1_des_c STRING,
--   app2_cod STRING,
--   app2_des STRING,
--   app2_des_c STRING,
--   app3_cod STRING,
--   app3_des STRING,
--   app3_des_c STRING,
--   bio_desc STRING,
--   gene_orig_desc STRING,
--   eth_otc_desc STRING,
--   nrdl_desc STRING,
--   NRDL_Entry_Date STRING,
--   edl_desc STRING,
--   tcm_desc STRING,
--   paed_desc STRING,
--   gqce_desc STRING,
--   vbp_desc STRING,
--   manu_cod STRING,
--   manu_des STRING,
--   manu_des_c STRING,
--   mnfl_cod STRING,
--   mnfl_des STRING,
--   corp_cod STRING,
--   corp_des STRING,
--   corp_des_c STRING,
--   BrandType STRING,
--   IS_AZ STRING,
--   AZ_MAIN STRING,
--   AZ_Related STRING,
--   countingunit STRING,
--   ETL_INSERT_DT TIMESTAMP,
--   ETL_UPDATE_DT TIMESTAMP)
-- USING delta
-- -- LOCATION 'abfss://master@azcdatalakeprd.dfs.core.chinacloudapi.cn/DM/dm_aia_pack_property';
-- -- 上面是生产环境location，下面是测试环境location
-- LOCATION 'abfss://master@retaildlstoragetest.dfs.core.chinacloudapi.cn/DM/dm_aia_pack_property';

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

update dwd.dwd_gnd_ext_chc_pack_property
set iqvia_pack_code = if( iqvia_pack_code REGEXP '^[0-9]',right(concat('000000000000',iqvia_pack_code),12),iqvia_pack_code)
,iqvia_prod_code = right(concat('000000000',iqvia_prod_code),9)

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

create or replace temporary view dwd_pack_property
as
select distinct 
	iqvia_pack_code    ,
	iqvia_prod_code    ,
	pack_cod           ,
	app1_cod           ,
	app1_des           ,
	app1_des_c         ,
	app2_cod           ,
	app2_des           ,
	app2_des_c         ,
	app3_cod           ,
	app3_des           ,
	app3_des_c         ,
	atc1_cod           ,
	atc1_des           ,
	atc1_des_c         ,
	atc2_cod           ,
	atc2_des           ,
	atc2_des_c         ,
	atc3_cod           ,
	atc3_des           ,
	atc3_des_c         ,
	atc4_cod           ,
	atc4_des           ,
	atc4_des_c         ,
	bio_desc           ,
	cmps_cod           ,
	cmps_des           ,
	cmps_des_c         ,
	corp_cod           ,
	corp_des           ,
	corp_des_c         ,
	edl_desc           ,
	eth_otc_desc       ,
	gene_orig_desc     ,
	gqce_desc          ,
	manu_cod           ,
	manu_des           ,
	manu_des_c         ,
	mnfl_cod           ,
	mnfl_des           ,
	nrdl_desc          ,
	pack_des           ,
	stgh_des           ,
	pack_lch           ,
	paed_desc          ,
	prod_des           ,
	prod_des_c         ,
	tcm_desc           ,
	vbp_desc           ,
	builtinarchivedate ,
	source_file_path   ,
	source_file_name   ,
	etl_insert_dt      ,
	countingunit		
from  dwd.dwd_gnd_ext_chc_pack_property

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

-- create or replace table dm.dm_chc_pack_property
-- LOCATION 'abfss://master@retaildlstoragetest.dfs.core.chinacloudapi.cn/DM/dm_chc_pack_property'
-- as
insert overwrite table dm.dm_chc_pack_property
WITH dedup_l AS (
    SELECT *,
           ROW_NUMBER() OVER (PARTITION BY iqvia_pack_code ORDER BY iqvia_pack_code) AS rn
    FROM dwd_pack_property
    QUALIFY rn = 1
),
dedup_r AS (
    SELECT *,
           ROW_NUMBER() OVER (PARTITION BY PACK_COD ORDER BY PACK_COD) AS rn
    FROM tmp.tmp_ext_chpa_pack_propetry
    QUALIFY rn = 1
)
SELECT
    l.iqvia_pack_code AS PACK_COD,

    -- Pack / Product 描述
    COALESCE(r.PACK_DES,        l.pack_des)          AS pack_des,
    COALESCE(r.STGH_DES,        l.stgh_des)          AS stgh_des,
    COALESCE(r.PACK_LCH,        l.pack_lch)          AS pack_lch,
    COALESCE(r.PROD_COD,        l.iqvia_prod_code)   AS PROD_COD,
    COALESCE(r.PROD_DES,        l.prod_des)          AS prod_des,
    COALESCE(r.PROD_DES_C,      l.prod_des_c)        AS prod_des_c,

    -- 右表独有：Family
    r.Family_Code,
    r.Family_Name,

    -- 成分
    COALESCE(r.CMPS_COD,        l.cmps_cod)          AS cmps_cod,
    COALESCE(r.CMPS_DES,        l.cmps_des)          AS cmps_des,
    COALESCE(r.CMPS_DES_C,      l.cmps_des_c)        AS cmps_des_c,

    -- ATC
    COALESCE(r.ATC1_COD,        l.atc1_cod)          AS atc1_cod,
    COALESCE(r.atc1_des,        l.atc1_des)          AS atc1_des,
    COALESCE(r.atc1_des_c,      l.atc1_des_c)        AS atc1_des_c,
    COALESCE(r.ATC2_COD,        l.atc2_cod)          AS atc2_cod,
    COALESCE(r.atc2_des,        l.atc2_des)          AS atc2_des,
    COALESCE(r.atc2_des_c,      l.atc2_des_c)        AS atc2_des_c,
    COALESCE(r.ATC3_COD,        l.atc3_cod)          AS atc3_cod,
    COALESCE(r.atc3_des,        l.atc3_des)          AS atc3_des,
    COALESCE(r.atc3_des_c,      l.atc3_des_c)        AS atc3_des_c,
    COALESCE(r.ATC4_COD,        l.atc4_cod)          AS atc4_cod,
    COALESCE(r.atc4_des,        l.atc4_des)          AS atc4_des,
    COALESCE(r.atc4_des_c,      l.atc4_des_c)        AS atc4_des_c,

    -- APP
    COALESCE(r.APP1_COD,        l.app1_cod)          AS app1_cod,
    COALESCE(r.app1_des,        l.app1_des)          AS app1_des,
    COALESCE(r.app1_des_c,      l.app1_des_c)        AS app1_des_c,
    COALESCE(r.APP2_COD,        l.app2_cod)          AS app2_cod,
    COALESCE(r.app2_des,        l.app2_des)          AS app2_des,
    COALESCE(r.app2_des_c,      l.app2_des_c)        AS app2_des_c,
    COALESCE(r.APP3_COD,        l.app3_cod)          AS app3_cod,
    COALESCE(r.app3_des,        l.app3_des)          AS app3_des,
    COALESCE(r.app3_des_c,      l.app3_des_c)        AS app3_des_c,

    -- 标志位
    COALESCE(r.BIO_DESC,        l.bio_desc)          AS bio_desc,
    COALESCE(r.GENE_ORIG_DESC,  l.gene_orig_desc)    AS gene_orig_desc,
    COALESCE(r.ETH_OTC_DESC,    l.eth_otc_desc)      AS eth_otc_desc,
    COALESCE(r.NRDL_DESC,       l.nrdl_desc)         AS nrdl_desc,
    r.NRDL_Entry_Date,
    COALESCE(r.EDL_DESC,        l.edl_desc)          AS edl_desc,
    COALESCE(r.TCM_DESC,        l.tcm_desc)          AS tcm_desc,
    COALESCE(r.PAED_DESC,       l.paed_desc)         AS paed_desc,
    COALESCE(r.GQCE_DESC,       l.gqce_desc)         AS gqce_desc,
    COALESCE(r.VBP_DESC,        l.vbp_desc)          AS vbp_desc,

    -- 厂商
    COALESCE(r.MANU_COD,        l.manu_cod)          AS manu_cod,
    COALESCE(r.MANU_DES,        l.manu_des)          AS manu_des,
    COALESCE(r.MANU_DES_C,      l.manu_des_c)        AS manu_des_c,
    COALESCE(r.MNFL_COD,        l.mnfl_cod)          AS mnfl_cod,
    COALESCE(r.MNFL_DES,        l.mnfl_des)          AS mnfl_des,

    -- 公司
    COALESCE(r.CORP_COD,        l.corp_cod)          AS corp_cod,
    COALESCE(r.CORP_DES,        l.corp_des)          AS corp_des,
    COALESCE(r.CORP_DES_C,      l.corp_des_c)        AS corp_des_c,

    -- 右表独有：品牌 / AZ 标志
    r.BrandType,
    r.IS_AZ,
    r.AZ_MAIN,
    r.AZ_Related,
    -- 左表独有
    l.countingunit,
    from_utc_timestamp(current_timestamp(),'UTC+8') as ETL_INSERT_DT,
    from_utc_timestamp(current_timestamp(),'UTC+8') as ETL_UPDATE_DT
FROM dedup_l l
LEFT JOIN dedup_r r
    ON l.iqvia_pack_code = r.PACK_COD