-- Databricks notebook source
--字段值的初步处理
create or replace temporary view aia_rawdata_1 as
select 
year,
qtr,
cast(year * 100 + ym as int) ym,
org_prov,
org_city,
ins_level,
case when ins_cd is not null and length(ins_cd) < 7 then right(concat('0000000',ins_cd),7) else ins_cd end as org_ins_cd,
atc1,
atc2,
atc3,
atc4,
atc,
mole_nm,
prod_nm,
org_prd_nm_c,
org_pk,
org_prd_str,
org_pth,
min_unit,
value,
unit,
cast(pack_desc as int) as pack_desc,
org_pk_un,
manu
from dwd.dwd_gnd_aia_rawdata

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


--关联出AUDIT_COD、inst_code
create or replace temporary view aia_rawdata_2 as
with hospital_mapping as (
select distinct 
case when length(cpa_hospital_code) < 7 then right(concat('0000000',cpa_hospital_code),7) else cpa_hospital_code end as cpa_hospital_code,
ins_cd_nl as inst_code
from dwd.dwd_gnd_hospital_not_provided
)

select upper(nvl(t2.inst_code,t1.org_ins_cd)) AUDIT_COD,
t1.*,
nvl(t2.inst_code,t1.org_ins_cd) as inst_code
from aia_rawdata_1 t1
left join hospital_mapping t2
on t1.org_ins_cd = t2.cpa_hospital_code
-- left join dm.dm_td_institution t3
-- on t2.ins_code = t3.inst_code
-- left join dm.dm_ims_td_geo t4
-- on t3.city_name_en = t4.city


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

/*
修改人 chenwu
修改时间 20250513
修改内容 打包分子的数据，不论 打通表 和 事实表，都没有系数，需要用 事实表里的 最小制剂单位数量 作为counting_unit
*/
--关联出pack_cod、corp_cod、counting_unit
create or replace temporary view aia_rawdata_3 as
select case when length(trim(t2.iqvia_pack_code)) < 12 and trim(t2.iqvia_pack_code) REGEXP '^[0-9]' then right(concat('000000000000',trim(t2.iqvia_pack_code)),12)
            else trim(t2.iqvia_pack_code)
       end as pack_cod,
t3.corp_cod,
case when left(t2.iqvia_pack_code,4) = 'AZP_' then cast(t1.min_unit/t1.org_pk_un  as decimal(38,10)) else t3.counting_unit end counting_unit,
-- t3.counting_unit,20250513
t1.*
from aia_rawdata_2 t1
left join (select distinct trim(iqvia_pack_code) as iqvia_pack_code,
ATC1,ATC2,ATC3,ATC4,org_prd_cd,org_mole_nm_c,
org_manu_prd_nm_c,org_prd_nm_c,org_pk,org_prd_str,
org_pth,min_pk_unit,org_pk_unit,org_manu_nm from dwd.dwd_gnd_ext_aia_cpt_data) t2
--ATC1编码+ATC2编码+ATC3编码+ATC4编码+药品编码(ATC)+药品通用名+药品产品名+药品商品名+规格+剂型+给药途径+最小销售包装单位+包装规格+生产企业
on nvl(t1.ATC1,'') = nvl(t2.ATC1,'')
and nvl(t1.ATC2,'') = nvl(t2.ATC2,'')
and nvl(t1.ATC3,'') = nvl(t2.ATC3,'')
and nvl(t1.ATC4,'') = nvl(t2.ATC4,'')
and nvl(t1.ATC,'') = nvl(t2.org_prd_cd,'')
and nvl(t1.MOLE_NM,'') = nvl(t2.org_mole_nm_c,'')
and nvl(t1.PROD_NM,'') = nvl(t2.org_manu_prd_nm_c,'')
and nvl(t1.ORG_PRD_NM_C,'') = nvl(t2.org_prd_nm_c,'')
and nvl(t1.ORG_PK,'') = nvl(t2.org_pk,'')
and nvl(t1.ORG_PRD_STR,'') = nvl(t2.org_prd_str,'')
and nvl(t1.ORG_PTH,'') = nvl(t2.org_pth,'')
and nvl(t1.UNIT,'') = nvl(t2.min_pk_unit,'')
and nvl(t1.PACK_DESC,'') = nvl(t2.org_pk_unit,'')
and nvl(t1.MANU,'') = nvl(t2.org_manu_nm,'')
left join (select distinct PACK_COD,corp_cod,counting_unit from dm.dm_aia_pack_property) t3
on t2.iqvia_pack_code = t3.PACK_COD


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


--计算出ly销量
create or replace temporary view aia_rawdata_4 as
with max_ym as (
select max(ym) as max_ym from aia_rawdata_3
)

select
    YM,
    org_ins_cd,
    inst_code,
    AUDIT_COD,
    pack_cod,
    corp_cod,
    sum(value) as value,
    sum(volume) as volume,
    sum(counting_unit) as counting_unit,
    sum(value_ly) as value_ly,
    sum(volume_ly) as volume_ly,
    sum(counting_unit_ly) as counting_unit_ly
from (
select
    ym,
    org_ins_cd,
    inst_code,
    AUDIT_COD,
    pack_cod,
    corp_cod,
    cast(value as decimal(38,10)) as value,
    cast(ORG_PK_UN as decimal(38,10)) as volume,
    cast(ORG_PK_UN as decimal(38,10)) * coalesce(cast(counting_unit as decimal(38,10)),1) as counting_unit,
    0 as value_ly,
    0 as volume_ly,
    0 as counting_unit_ly
from aia_rawdata_3
union all
select
    cast(cast(ym as int) + 100 as string) as ym,
    org_ins_cd,
    inst_code,
    AUDIT_COD,
    pack_cod,
    corp_cod,
    0 as value,
    0 as volume,
    0 as counting_unit,
    cast(value as decimal(38,10)) as value_ly,
    cast(ORG_PK_UN as decimal(38,10)) as volume_ly,
    cast(ORG_PK_UN as decimal(38,10)) * coalesce(cast(counting_unit as decimal(38,10)),1) as counting_unit_ly
from aia_rawdata_3 t1
join max_ym t2
on 1=1
where cast(cast(ym as int) + 100 as string) <= t2.max_ym
 )
group by ym,
    org_ins_cd,
    inst_code,
    AUDIT_COD,
    pack_cod,
    corp_cod


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

insert overwrite table dm.dm_ext_aia_sales(
ym,
org_ins_cd,
inst_code,
AUDIT_COD,
pack_cod,
corp_cod,
value,
volume,
counting_unit,
value_ly,
volume_ly,
counting_unit_ly
)
select 
ym,
org_ins_cd,
inst_code,
AUDIT_COD,
pack_cod,
corp_cod,
sum(value) as value,
sum(volume) as volume,
sum(counting_unit) as counting_unit,
sum(value_ly) as value_ly,
sum(volume_ly) as volume_ly,
sum(counting_unit_ly) as counting_unit_ly
from aia_rawdata_4
group by 
ym,
org_ins_cd,
inst_code,
AUDIT_COD,
pack_cod,
corp_cod