-- Databricks notebook source
-- 20260119 依赖上面 dwd.dwd_ims_td_pack_property
-- 20260119 依赖上面补0 dwd.dwd_ims_td_pack_property dwd.dwd_gnd_ims_tblmarket
-----MARKET
--第一步：
--先处理 Extend_Market 和 NOT_IN_FLAG 都为空的数据，代表有明确定义得市场
insert overwrite table tmp.tmp_ims_td_temp_market 
select distinct t2.market,t1.*,
t2.bu,CASE WHEN t2.extend_market_ratio IS NULL THEN '1' ELSE t2.extend_market_ratio END AS Market_Ratio
from dwd.dwd_ims_td_pack_property t1
left join (select * from dwd.dwd_gnd_ims_tblmarket WHERE Extend_Market IS NULL AND ( NOT_IN_FLAG IS NULL or  NOT_IN_FLAG = '1' )) t2
on  t1.ATC1_COD = case when t2.ATC1_Code is null then t1.ATC1_COD else t2.ATC1_Code end
and t1.ATC2_COD = case when t2.ATC2_Code is null then t1.ATC2_COD else t2.ATC2_Code end
and t1.ATC3_COD = case when t2.ATC3_Code is null then t1.ATC3_COD else t2.ATC3_Code end
and t1.ATC4_COD = case when t2.ATC4_Code is null then t1.ATC4_COD else t2.ATC4_Code end
and t1.APP1_COD = case when t2.NFC1_Code is null then t1.APP1_COD else t2.NFC1_Code end
and t1.APP2_COD = case when t2.NFC2_Code is null then t1.APP2_COD else t2.NFC2_Code end
and t1.APP3_COD = case when t2.NFC3_Code is null then t1.APP3_COD else t2.NFC3_Code end
and t1.CORP_COD = case when t2.corporation_code is null then t1.CORP_COD else t2.corporation_code end
and t1.MANU_COD = case when t2.Manufacturer_Code is null then t1.MANU_COD else t2.Manufacturer_Code end
and t1.PROD_COD = case when t2.Product_Code is null then t1.PROD_COD else t2.Product_Code end
and t1.PACK_COD = case when t2.Pack_Code is null then t1.PACK_COD else t2.Pack_Code end
and t1.STGH_DES = case when t2.Strength is null then t1.STGH_DES else t2.Strength end
and t1.CMPS_COD = case when t2.Molecule_Code is null then t1.CMPS_COD else t2.Molecule_Code end
where t2.market is not null
;



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

--第二步：
--处理 Extend_Market 为空，并且 NOT_IN_FLAG 有值的数据，代表反选定义市场的规则
insert overwrite table tmp.tmp_ims_td_temp_market_del 
select distinct t2.market,t1.*,t2.bu,CASE WHEN t2.extend_market_ratio IS NULL THEN '1' ELSE t2.extend_market_ratio END AS Market_Ratio
	from dwd.dwd_ims_td_pack_property t1
	left join (select * from dwd.dwd_gnd_ims_tblmarket WHERE Extend_Market IS NULL AND  NOT_IN_FLAG = '0' ) t2
	on  t1.ATC1_COD = case when t2.ATC1_Code is null then t1.ATC1_COD else t2.ATC1_Code end
	and t1.ATC2_COD = case when t2.ATC2_Code is null then t1.ATC2_COD else t2.ATC2_Code end
	and t1.ATC3_COD = case when t2.ATC3_Code is null then t1.ATC3_COD else t2.ATC3_Code end
	and t1.ATC4_COD = case when t2.ATC4_Code is null then t1.ATC4_COD else t2.ATC4_Code end
	and t1.APP1_COD = case when t2.NFC1_Code is null then t1.APP1_COD else t2.NFC1_Code end
	and t1.APP2_COD = case when t2.NFC2_Code is null then t1.APP2_COD else t2.NFC2_Code end
	and t1.APP3_COD = case when t2.NFC3_Code is null then t1.APP3_COD else t2.NFC3_Code end
	and t1.CORP_COD = case when t2.corporation_code is null then t1.CORP_COD else t2.corporation_code end
	and t1.MANU_COD = case when t2.Manufacturer_Code is null then t1.MANU_COD else t2.Manufacturer_Code end
	and t1.PROD_COD = case when t2.Product_Code is null then t1.PROD_COD else t2.Product_Code end
	and t1.PACK_COD = case when t2.Pack_Code is null then t1.PACK_COD else t2.Pack_Code end
	and t1.STGH_DES = case when t2.Strength is null then t1.STGH_DES else t2.Strength end
	and t1.CMPS_COD = case when t2.Molecule_Code is null then t1.CMPS_COD else t2.Molecule_Code end
	where t2.market is not null
 ;


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

--！！！注意此段是反选的规则，故执行的是从已经定义好的TempMKT中删除数据
--需要看TempMKT的字段结构，没有值的字段，在Value里面放个空值
MERGE INTO tmp.tmp_ims_td_temp_market AS t1
USING tmp.tmp_ims_td_temp_market_del AS t2
	 on  t1.ATC1_COD = t2.ATC1_COD
     and t1.ATC2_COD = t2.ATC2_COD
     and t1.ATC3_COD = t2.ATC3_COD
     and t1.ATC4_COD = t2.ATC4_COD
     and t1.APP1_COD = t2.APP1_COD
     and t1.APP2_COD = t2.APP2_COD
     and t1.APP3_COD = t2.APP3_COD
     and t1.CORP_COD = t2.CORP_COD
     and t1.MANU_COD = t2.MANU_COD
     and t1.PROD_COD = t2.PROD_COD
     and t1.PACK_COD = t2.PACK_COD
     and t1.STGH_DES = t2.STGH_DES
     and t1.CMPS_COD = t2.CMPS_COD
     and t1.market = t2.market
WHEN MATCHED THEN DELETE 
;


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

---第三步：
--处理 Extend_Market 有值得数据，用已有市场，定义新的市场，带上系数
insert into  tmp.tmp_ims_td_temp_market
select distinct 
t2.Market,
t1.PACK_COD ,
t1.PACK_DES ,
t1.STGH_DES ,
t1.PACK_LCH ,
t1.PROD_COD ,
t1.CMPS_COD ,
t1.CMPS_DES ,
t1.ATC1_COD ,
t1.ATC2_COD ,
t1.ATC3_COD ,
t1.ATC4_COD ,
t1.APP1_COD ,
t1.APP2_COD ,
t1.APP3_COD ,
t1.BIO_DESC ,
t1.GENE_ORIG_DESC ,
t1.ETH_OTC_DESC ,
t1.NRDL_DESC ,
t1.NRDL_Entry_Date ,
t1.EDL_DESC ,
t1.TCM_DESC ,
t1.PAED_DESC ,
t1.GQCE_DESC ,
t1.VBP_DESC ,
t1.MANU_COD ,
t1.MANU_DES ,
t1.MNFL_COD ,
t1.MNFL_DES ,
t1.CORP_COD ,
t1.CORP_DES ,
t2.BU,
t1.BrandType ,
case when t2.Extend_Market_Ratio is null then '1' end AS Market_Ratio--市场的系数若没有维护，默认为1
from tmp.tmp_ims_td_temp_market t1
left join (select Market, BU,Extend_Market,Extend_Market_Ratio from dwd.dwd_gnd_ims_tblmarket  WHERE Extend_Market IS NOT NULL) t2 on t1.Market=t2.Extend_Market
where t2.Market is not null
;


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

--将，市场的系数为空值的数据，初始化为1
UPDATE tmp.tmp_ims_td_temp_market SET Market_Ratio = 1 WHERE Market_Ratio IS NULL;

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

-- 20260119 依赖 tmp.tmp_ims_td_temp_market 
-- 20260119 依赖上面补0 dwd.dwd_gnd_ims_tblkeycompetitor

--KeyCompetitor部分
--定义Key Competitor
insert overwrite table tmp.tmp_ims_td_temp_market_upd1 
select distinct t2.keycompetitor as Key_Competitor,t2.no,t2.no1,t1.*
from tmp.tmp_ims_td_temp_market t1
left join (
  select
    ROW_NUMBER() OVER(ORDER BY 
      CASE
        WHEN ATC1_Code IS NOT NULL THEN 1
        WHEN ATC2_Code IS NOT NULL THEN 2
        WHEN ATC3_Code IS NOT NULL THEN 3
        WHEN ATC4_Code IS NOT NULL THEN 4
        WHEN Molecule_Code IS NOT NULL THEN 5
        WHEN Product_Code IS NOT NULL THEN 6
        WHEN Pack_Code IS NOT NULL THEN 7
        ELSE 999 END,
        CASE WHEN NFC1_CODE IS NULL THEN 0 ELSE 1 END,
        CASE WHEN NFC2_CODE IS NULL THEN 0 ELSE 1 END,
        CASE WHEN NFC2_CODE IS NULL THEN 0 ELSE 1 END,	 
        CASE WHEN NFC3_CODE IS NULL THEN 0 ELSE 1 END	 
	 ) AS no1,* from dwd.dwd_gnd_ims_tblkeycompetitor
)t2
	on  t1.ATC1_COD = case when t2.ATC1_Code is null then t1.ATC1_COD else t2.ATC1_Code end
	and t1.ATC2_COD = case when t2.ATC2_Code is null then t1.ATC2_COD else t2.ATC2_Code end
	and t1.ATC3_COD = case when t2.ATC3_Code is null then t1.ATC3_COD else t2.ATC3_Code end
	and t1.ATC4_COD = case when t2.ATC4_Code is null then t1.ATC4_COD else t2.ATC4_Code end
	and t1.APP1_COD = case when t2.NFC1_Code is null then t1.APP1_COD else t2.NFC1_Code end
	and t1.APP2_COD = case when t2.NFC2_Code is null then t1.APP2_COD else t2.NFC2_Code end
	and t1.APP3_COD = case when t2.NFC3_Code is null then t1.APP3_COD else t2.NFC3_Code end
	and t1.CORP_COD = case when t2.corporation_code is null then t1.CORP_COD else t2.corporation_code end
	and t1.MANU_COD = case when t2.Manufacturer_Code is null then t1.MANU_COD else t2.Manufacturer_Code end
	and t1.PROD_COD = case when t2.Product_Code is null then t1.PROD_COD else t2.Product_Code end
	and t1.PACK_COD = case when t2.Pack_Code is null then t1.PACK_COD else t2.Pack_Code end
	and t1.STGH_DES = case when t2.Strength is null then t1.STGH_DES else t2.Strength end
	and t1.CMPS_COD = case when t2.Molecule_Code is null then t1.CMPS_COD else t2.Molecule_Code end
    and t1.market   = case when t2.market is null then t1.market else t2.market end
;


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

--删除被重复定义的Key Competitor，删除写入时间早的（No 小的）的对应行即可，保留No最大的一行即最新的规则
insert overwrite table tmp.tmp_ims_td_temp_market_upd2 
select * from(
  select row_number() over(partition by market,pack_cod,prod_cod order by no1 desc,no desc) as id,* 
    from tmp.tmp_ims_td_temp_market_upd1) where id>1
;


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

MERGE INTO tmp.tmp_ims_td_temp_market_upd1 AS t1
USING tmp.tmp_ims_td_temp_market_upd2 AS t2
	 on  t1.ATC1_COD = t2.ATC1_COD
     and t1.ATC2_COD = t2.ATC2_COD
     and t1.ATC3_COD = t2.ATC3_COD
     and t1.ATC4_COD = t2.ATC4_COD
     and t1.APP1_COD = t2.APP1_COD
     and t1.APP2_COD = t2.APP2_COD
     and t1.APP3_COD = t2.APP3_COD
     and t1.CORP_COD = t2.CORP_COD
     and t1.MANU_COD = t2.MANU_COD
     and t1.PROD_COD = t2.PROD_COD
     and t1.PACK_COD = t2.PACK_COD
     and t1.STGH_DES = t2.STGH_DES
     and t1.CMPS_COD = t2.CMPS_COD
     and t1.market = t2.market
     and t1.Key_Competitor = t2.Key_Competitor
     and t1.no=t2.no
WHEN MATCHED THEN DELETE 
;


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

--没有挂上KeyCompetitor的，更新为Others ,最后去掉No，写入临时表备用
update tmp.tmp_ims_td_temp_market_upd1 set Key_Competitor = 'Others' where Key_Competitor is NULL;


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

insert overwrite table tmp.tmp_ims_td_temp_market_upd 
select 
	Key_Competitor,market,PACK_COD,PACK_DES,STGH_DES,PACK_LCH,PROD_COD,CMPS_COD,CMPS_DES,
	ATC1_COD,ATC2_COD,ATC3_COD,ATC4_COD,APP1_COD,APP2_COD,APP3_COD,BIO_DESC,GENE_ORIG_DESC,
	ETH_OTC_DESC,NRDL_DESC,NRDL_Entry_Date,EDL_DESC,TCM_DESC,PAED_DESC,GQCE_DESC,VBP_DESC,
	MANU_COD,MANU_DES,MNFL_COD,MNFL_DES,CORP_COD,CORP_DES,BrandType,bu,Market_Ratio
from tmp.tmp_ims_td_temp_market_upd1
;

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

-- 20260119 市场维度信息 我感觉可以拉到前面去
--市场维度信息
insert overwrite table dws.dws_ims_td_market
select distinct	market,PACK_COD,PACK_DES,STGH_DES,PACK_LCH,PROD_COD,CMPS_COD,CMPS_DES,
	ATC1_COD,ATC2_COD,ATC3_COD,ATC4_COD,APP1_COD,APP2_COD,APP3_COD,BIO_DESC,GENE_ORIG_DESC,
	ETH_OTC_DESC,NRDL_DESC,NRDL_Entry_Date,EDL_DESC,TCM_DESC,PAED_DESC,GQCE_DESC,VBP_DESC,
	MANU_COD,MANU_DES,MNFL_COD,MNFL_DES,CORP_COD,CORP_DES,BrandType,bu,Market_Ratio,Key_Competitor,
    from_utc_timestamp(current_timestamp(),'UTC+8'),
    from_utc_timestamp(current_timestamp(),'UTC+8')
from tmp.tmp_ims_td_temp_market_upd 
;