-- Databricks notebook source
-- CREATE OR REPLACE TABLE DM.DM_TF_EXT_CHC_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_chc_sales';
-- 上面是生产环境location，下面是测试环境location
-- LOCATION 'abfss://master@retaildlstoragetest.dfs.core.chinacloudapi.cn/DM/dm_tf_ext_chc_sales';

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

CREATE OR REPLACE TEMPORARY VIEW RAW_DATA
AS
SELECT
  CASE
    WHEN SUBSTRING(YQ, 5, 2) = 'Q1' 
      THEN CONCAT (SUBSTRING(YQ, 1, 4), '03')
    WHEN SUBSTRING(YQ, 5, 2) = 'Q2' 
      THEN CONCAT (SUBSTRING(YQ, 1, 4), '06')
    WHEN SUBSTRING(YQ, 5, 2) = 'Q3' 
      THEN CONCAT (SUBSTRING(YQ, 1, 4), '09')
    WHEN SUBSTRING(YQ, 5, 2) = 'Q4' 
      THEN CONCAT (SUBSTRING(YQ, 1, 4), '12')
        ELSE YQ 
          END AS YQ,
  CITY_C,
  PACK_COD,
  PROD_COD,
  VALUE,
  TOTALUNIT,
  COUNTINGUNIT
FROM
  DWD.DWD_GND_CHC_RAWDATA
WHERE
  YEAR >= '2021'

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

CREATE OR REPLACE TEMPORARY VIEW FILTER_YM
AS
SELECT 
  MAX(YQ) AS MAX_YYYYMM,
  CONCAT(CAST(FLOOR(MAX(YQ)/100-1) AS STRING),'01') AS MIN_YYYYMM
FROM 
  RAW_DATA

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

CREATE OR REPLACE TEMPORARY VIEW CORP_DATA
AS
SELECT
  PACK_COD,
  CORP_COD,
  CORP_DES
FROM
  DM.DM_IMS_TD_PACK_PROPERTY
GROUP BY
  PACK_COD,
  CORP_COD,
  CORP_DES

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

CREATE OR REPLACE TEMPORARY VIEW PACK_DATA
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 ----------

--CHC本身唯独表 存在CORP_COD以及 CORP_DES 与手工数据不一致的问题 
CREATE OR REPLACE TEMPORARY VIEW PACK_WIHT_CORP_DATA
AS
SELECT
  T1.PACK_COD,
  T1.IQVIA_PACK_CODE,
  T1.COUNTINGUNIT,
  COALESCE(T2.CORP_COD, T1.CORP_COD) AS CORP_COD,
  COALESCE(T2.CORP_DES, T1.CORP_DES) AS CORP_DES
FROM
  PACK_DATA T1
LEFT JOIN 
  CORP_DATA T2 
ON 
  T1.IQVIA_PACK_CODE = T2.PACK_COD

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

--GEO相关信息   在CHPA中没有 苏州、无锡、福州等数据,根据业务反馈，按照 CITY_C当作AUDIT_COD
CREATE OR REPLACE TEMPORARY VIEW DIM_EXCHC_GEO
AS 
SELECT
  T1.CITY_E,
  CONCAT (T1.CITY_C, '市') AS CITY_C,
  UPPER(NVL (T2.AUDIT_COD, T1.CITY_E)) AS AUDIT_COD
FROM
  DWD.DWD_GND_EXT_CHC_GEO T1
LEFT JOIN 
  DM.DM_IMS_TD_GEO T2 
ON 
  T1.CITY_E = T2.CITY

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

CREATE OR REPLACE TEMPORARY VIEW RAW_DATA_WIHT_PACK_CORP_GEO_DATA
AS
SELECT
  T1.YQ,
  T1.CITY_C,
  T3.IQVIA_PACK_CODE AS PACK_COD,
  T3.CORP_COD AS CORP_COD,
  C.AUDIT_COD AS AUDIT_COD,
  T1.PROD_COD,
  T1.VALUE,
  T1.TOTALUNIT,
  T1.TOTALUNIT * NVL (T3.COUNTINGUNIT, 1) AS COUNTINGUNIT
FROM
  RAW_DATA T1
LEFT JOIN 
  PACK_WIHT_CORP_DATA T3 
ON 
  T1.PACK_COD = T3.PACK_COD
LEFT JOIN 
  DIM_EXCHC_GEO C 
ON 
  T1.CITY_C = C.CITY_C

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

CREATE OR REPLACE TEMPORARY VIEW RAW_DATA_WIHT_LY
AS
SELECT
  A.YQ,
  A.AUDIT_COD,
  A.PACK_COD,
  A.CORP_COD,
  SUM(A.VALUE) VALUE,
  SUM(A.TOTALUNIT) TOTALUNIT,
  SUM(A.COUNTINGUNIT) COUNTINGUNIT,
  SUM(VALUE_LY) VALUE_LY,
  SUM(TOTALUNIT_LY) TOTALUNIT_LY,
  SUM(COUNTINGUNIT_LY) COUNTINGUNIT_LY
FROM
  (
    SELECT
      T1.YQ,
      T1.AUDIT_COD,
      T1.PACK_COD,
      T1.CORP_COD,
      T1.VALUE,
      T1.TOTALUNIT,
      T1.COUNTINGUNIT,
      0 VALUE_LY,
      0 TOTALUNIT_LY,
      0 COUNTINGUNIT_LY
    FROM
      RAW_DATA_WIHT_PACK_CORP_GEO_DATA T1
    UNION ALL
    SELECT
      CAST(T1.YQ + 100 AS INT),
      T1.AUDIT_COD,
      T1.PACK_COD,
      T1.CORP_COD,
      0 VALUE,
      0 TOTALUNIT,
      0 COUNTINGUNIT,
      T1.VALUE VALUE_LY,
      T1.TOTALUNIT TOTALUNIT_LY,
      T1.COUNTINGUNIT COUNTINGUNIT_LY
    FROM
      RAW_DATA_WIHT_PACK_CORP_GEO_DATA AS T1
    CROSS JOIN 
      FILTER_YM AS T2
    WHERE
      T1.YQ + 100 <= T2.MAX_YYYYMM
  ) A
GROUP BY
  A.YQ,
  A.AUDIT_COD,
  A.PACK_COD,
  A.CORP_COD

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

CREATE OR REPLACE TEMPORARY VIEW RAW_DATA_FINAL
AS
SELECT
  T1.YQ AS YYYYMM,
  T1.AUDIT_COD,
  T1.PACK_COD,
  T1.CORP_COD,
  SUM(TOTALUNIT) AS SALES_UNIT_CAL, -- TOTALUNIT,
  SUM(TOTALUNIT_LY) AS SALES_UNIT_CAL_LY, --TOTALUNIT_LY,
  SUM(VALUE) AS SALES_VALUE_CAL, --VALUE,
  SUM(VALUE_LY) AS SALES_VALUE_CAL_LY,
  SUM(COUNTINGUNIT) AS CONUTING_UNIT, --COUNTINGUNIT,
  SUM(COUNTINGUNIT_LY) AS CONUTING_UNIT_LY,
  'CHC(Quarterly)' AS DATA_SOURCE
FROM
  RAW_DATA_WIHT_LY T1
GROUP BY
  T1.YQ,
  T1.AUDIT_COD,
  T1.PACK_COD,
  T1.CORP_COD

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

CREATE OR REPLACE TEMPORARY VIEW DM_TF_EXT_CHC_SALES AS
SELECT
  A.YYYYMM,
  A.PACK_COD AS PACK_CODE,
  A.AUDIT_COD AS AUDIT_CODE,
  'CHC(Quarterly)' DATA_SOURCE,
  -------------------------------------
  SALES_UNIT_CAL,
  SALES_UNIT_CAL_LY,
  SALES_VALUE_CAL,
  SALES_VALUE_CAL_LY,
  CONUTING_UNIT,
  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 --可以移除，报告没有使用E
-------------------------------------
FROM
  RAW_DATA_FINAL A
WHERE
  YYYYMM
    >= (
      SELECT
        CAST((LEFT(MAX(YYYYMM), 4) - 3) * 100 + 1 AS INT)
      FROM
        RAW_DATA_FINAL
    )

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

INSERT OVERWRITE TABLE DM.DM_TF_EXT_CHC_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
)
  SELECT
    YYYYMM,
    NVL(NULLIF(PACK_CODE, ''), CONCAT('PACK_CODE_', DATA_SOURCE)) AS PACK_CODE,
    NVL(NULLIF(AUDIT_CODE, ''), 'ROC') AS 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,
    FROM_UTC_TIMESTAMP(CURRENT_TIMESTAMP(), 'UTC+8') AS ETL_INSERT_DT,
    FROM_UTC_TIMESTAMP(CURRENT_TIMESTAMP(), 'UTC+8') AS ETL_UPDATE_DT
  FROM
    DM_TF_EXT_CHC_SALES;