-- Databricks notebook source
-- CREATE OR REPLACE TABLE DM.DM_TF_EXT_COUNTY_SALES (
--   YYYYMM STRING,
--   PACK_CODE STRING,
--   CORP_CODE STRING,
--   AUDIT_CODE STRING,
--   PLATFORM_TYPE STRING,
--   STORE_NAME STRING,
--   STORE_TYPE STRING,
--   REGION_TYPE STRING,
--   PACK_FLAG INT,
--   PROD_FLAG INT,
--   DTP_FLAG INT,
--   SALES_UNIT_CAL DECIMAL(38,10),
--   SALES_UNIT_CAL_LY DECIMAL(38,10),
--   SALES_VALUE_CAL DECIMAL(38,10),
--   SALES_VALUE_CAL_LY DECIMAL(38,10),
--   CONUTING_UNIT DECIMAL(38,10),
--   CONUTING_UNIT_LY DECIMAL(38,10),
--   DATA_SOURCE STRING,
--   INST_CODE STRING COMMENT '内部机构编码',
--   CMPS_FLAG STRING COMMENT '分子式标签',
--   DEPT_NAME STRING COMMENT '科室名称',
--   PRESCRIPTION DECIMAL(38,10) COMMENT '处方张数',
--   PRESCRIPTION_LY DECIMAL(38,10) COMMENT '去年同期处方张数',
--   NEW_CODE STRING COMMENT '主数据关联CODE',
--   AREA STRING COMMENT '城市',
--   H_LEVEL STRING COMMENT '医院类型',
--   REIMBURSE STRING COMMENT '报销情况',
--   REIMBURSE_TYPE STRING COMMENT '报销类型',
--   PRESCRIPTION_SOURCE STRING COMMENT '处方来源',
--   ETL_INSERT_DT TIMESTAMP,
--   ETL_UPDATE_DT TIMESTAMP)
-- USING delta
-- -- LOCATION 'abfss://master@azcdatalakeprd.dfs.core.chinacloudapi.cn/DM/dm_tf_ext_county_sales';
-- -- 上面是生产环境location，下面是测试环境location
-- LOCATION 'abfss://master@retaildlstoragetest.dfs.core.chinacloudapi.cn/DM/dm_tf_ext_county_sales';


-- CREATE OR REPLACE TABLE catalog_e2_ngbi_qas.dwd.dwd_gnd_county_pharbers_prov_fact (
--   year STRING,
--   ym STRING,
--   province_c STRING,
--   ims_drug_id STRING,
--   is_hosp_code STRING,
--   prod_corp STRING,
--   prod_cod STRING,
--   pack_cod STRING,
--   phcd STRING,
--   prod_des STRING,
--   cmps_cod STRING,
--   cmps_des STRING,
--   corp_cod STRING,
--   corp_des STRING,
--   mnfl_cod STRING,
--   prod_des_c STRING,
--   cmps_c STRING,
--   corp_des_c STRING,
--   pack_des STRING,
--   spec STRING,
--   conversion_ratio STRING,
--   dosage_form STRING,
--   atc4_cod 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,
--   vbp_batch STRING,
--   vbp STRING,
--   value STRING,
--   totalunit STRING,
--   countingunit STRING,
--   price STRING,
--   manu_cod STRING,
--   manu_des STRING,
--   manu_des_c STRING,
--   source_file_path STRING,
--   source_file_name STRING,
--   etl_insert_dt STRING)
-- USING delta
-- LOCATION 'abfss://master@retaildlstoragetest.dfs.core.chinacloudapi.cn/DWD/dwd_gnd_county_pharbers_prov_fact'


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

-- DBTITLE 1,dwd sales
CREATE OR REPLACE TEMPORARY VIEW FACT_COUNTY_PREVIOUS
AS
SELECT
  CAST(YM AS INT) AS YM,
  CAST(YEAR AS INT) AS YEAR,
  CAST(REPLACE(YM,YEAR,'') AS INT) AS  MONTH,
  CAST(VALUE AS DECIMAL(38,10)) AS VALUE,
  CAST(COUNTINGUNIT AS DECIMAL(38,10)) AS COUNTINGUNIT,
  CAST(TOTALUNIT AS DECIMAL(38,10)) AS TOTALUNIT,
  PROVINCE_C,
  PHCD,
  conversion_ratio  
FROM dwd.dwd_gnd_county_pharbers_prov_fact

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

-- DBTITLE 1,dwd pach2pack
CREATE OR REPLACE TEMPORARY VIEW DIM_PACK_INFO
AS
SELECT
  PACK_COD,
  MAX(IQVIA_PACK_CODE) AS IQVIA_PACK_CODE,
  MAX(countingunit) as COUNTINGUNIT_RATIO
FROM
  dwd.dwd_gnd_dept_pack_property
GROUP BY
  PACK_COD

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

CREATE OR REPLACE TEMPORARY VIEW DIM_PROVINCE
AS
select 
  t2.province_code,
  CASE 
    WHEN 
      t2.province_name 
        IN ('上海','北京','天津','重庆') 
          THEN
            CONCAT(t2.province_name,'市')
    WHEN 
      t2.province_name 
        IN ('内蒙古','西藏') 
          THEN
            CONCAT(t2.province_name,'自治区')      
    WHEN 
      t2.province_name = '宁夏'
        THEN CONCAT(t2.province_name,'回族自治区')  
    WHEN 
      t2.province_name = '广西'
        THEN CONCAT(t2.province_name,'壮族自治区') 
    WHEN 
      t2.province_name = '新疆'
        THEN CONCAT(t2.province_name,'维吾尔自治区')   
          ELSE CONCAT(t2.province_name,'省')     
            END AS province_name            
from dm.dm_td_geography t2 
where t2.geo_key = t2.province_code
and province_name not in ('台湾','澳门','香港')

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

INSERT OVERWRITE TABLE DM.DM_TF_EXT_COUNTY_SALES (
  YYYYMM,
  PACK_CODE,
  AUDIT_CODE,
  DATA_SOURCE,
  SALES_UNIT_CAL,
  SALES_UNIT_CAL_LY,
  SALES_VALUE_CAL,
  SALES_VALUE_CAL_LY,
  CONUTING_UNIT,
  CONUTING_UNIT_LY,
  PRESCRIPTION,
  PRESCRIPTION_LY,
  PACK_FLAG,
  PROD_FLAG,
  DTP_FLAG,
  CMPS_FLAG,
  PLATFORM_TYPE,
  STORE_NAME,
  STORE_TYPE,
  REGION_TYPE,
  INST_CODE,
  DEPT_NAME,
  NEW_CODE,
  AREA,
  H_LEVEL,
  REIMBURSE,
  REIMBURSE_TYPE,
  PRESCRIPTION_SOURCE,
  CORP_CODE,
  ETL_INSERT_DT,
  ETL_UPDATE_DT
)

-- ════════════════════════════════════════════
-- Part 1：省份数据
-- 20260518 county的省份数据重写
-- ════════════════════════════════════════════
WITH original_sales AS (
    SELECT /*+ BROADCAST(T2,T3) */
        T1.YM                  AS YYYYMM,
        T2.PROVINCE_CODE       AS AUDIT_COD,
        T3.IQVIA_PACK_CODE     AS PACK_COD,
        SUM(T1.VALUE         ) AS SALES_VALUE_CAL,
        SUM(T1.TOTALUNIT     ) AS SALES_UNIT_CAL,
        SUM(T1.COUNTINGUNIT  ) AS CONUTING_UNIT
    FROM FACT_COUNTY_PREVIOUS   AS T1
    LEFT JOIN DIM_PROVINCE      AS T2 ON T1.PROVINCE_C = T2.PROVINCE_NAME
    LEFT JOIN DIM_PACK_INFO AS T3 ON T1.PHCD      = T3.PACK_COD
    GROUP BY T1.YM,T2.PROVINCE_CODE,T3.IQVIA_PACK_CODE
)

,formated_count_sales_table_detail (
  SELECT
    cy.YYYYMM,
    cy.AUDIT_COD,
    cy.PACK_COD,
    cy.SALES_VALUE_CAL,
    cy.SALES_UNIT_CAL,
    cy.CONUTING_UNIT,
    0  AS SALES_VALUE_CAL_LY,
    0   AS SALES_UNIT_CAL_LY,
    0 AS CONUTING_UNIT_LY
  FROM original_sales AS cy

  union ALL

  SELECT
    ly.YYYYMM+100 as YYYYMM,
    ly.AUDIT_COD,
    ly.PACK_COD,
    0 as SALES_VALUE_CAL,
    0 as SALES_UNIT_CAL,
    0 as CONUTING_UNIT,
    -- 去年同期
    ly.SALES_VALUE_CAL  AS SALES_VALUE_CAL_LY,
    ly.SALES_UNIT_CAL   AS SALES_UNIT_CAL_LY,
    ly.CONUTING_UNIT AS CONUTING_UNIT_LY
  FROM original_sales AS ly
  WHERE ly.yyyymm+100 <= (select max( yyyymm ) from original_sales )
)

,formated_count_sales_table (
  SELECT 
    YYYYMM,
    AUDIT_COD,
    PACK_COD,
    SUM(SALES_VALUE_CAL) AS SALES_VALUE_CAL,
    SUM(SALES_UNIT_CAL) AS SALES_UNIT_CAL,
    SUM(CONUTING_UNIT) AS CONUTING_UNIT,
    SUM(SALES_VALUE_CAL_LY) AS SALES_VALUE_CAL_LY,
    SUM(SALES_UNIT_CAL_LY) AS SALES_UNIT_CAL_LY,
    SUM(CONUTING_UNIT_LY) AS CONUTING_UNIT_LY
  FROM formated_count_sales_table_detail
  GROUP BY 1,2,3
)

-- ════════════════════════════════════════════
-- Part 2：延续合同并表中的结构
-- 20260518 county的省份数据重写
-- ════════════════════════════════════════════
  SELECT
    A.YYYYMM,
    A.PACK_COD AS PACK_CODE,
    A.AUDIT_COD AS AUDIT_CODE,
    'IQVIA-COUNTY(Quarterly)' DATA_SOURCE,
    -------------------------------------
    SUM(SALES_UNIT_CAL) SALES_UNIT_CAL,
    SUM(SALES_UNIT_CAL_LY) as SALES_UNIT_CAL_LY,
    SUM(SALES_VALUE_CAL) SALES_VALUE_CAL,
    SUM(SALES_VALUE_CAL_LY) as SALES_VALUE_CAL_LY,
    SUM(CONUTING_UNIT) CONUTING_UNIT,
    SUM(CONUTING_UNIT_LY) as CONUTING_UNIT_LY,
    null as prescription,
    null as prescription_ly,
    -------------------------------------
    --Retail藏数逻辑标签------------------
    1 PACK_FLAG,
    1 PROD_FLAG,
    0 DTP_FLAG,
    null as cmps_flag,
    -------------------------------------
    --EC数据标签--------------------------
    '' PLATFORM_TYPE,
    '' STORE_NAME,
    '' STORE_TYPE,
    -------------------------------------
    --COUNTY数据标签----------------------
    '' REGION_TYPE,
    -------------------------------------
    --AIA数据标签-------------------------
    null inst_code, --可以移除，报告没有使用
    -------------------------------------
    --XIE HE 数据标签---------------------
    '' as dept_name,
    '' as new_code,
    '' as area,
    '' as h_level,
    '' as reimburse,
    '' as reimburse_type,
    '' as prescription_source,
    -------------------------------------
    '' as CORP_CODE, --可以移除，报告没有使用
    -------------------------------------
    FROM_UTC_TIMESTAMP(CURRENT_TIMESTAMP(), 'UTC+8') AS ETL_INSERT_DT,
    FROM_UTC_TIMESTAMP(CURRENT_TIMESTAMP(), 'UTC+8') AS ETL_UPDATE_DT
  FROM
    formated_count_sales_table A
  where
    A.YYYYMM >= '202001'
  GROUP BY
    A.YYYYMM,
    A.PACK_COD,
    A.AUDIT_COD
