-- Databricks notebook source
-- CREATE or replace TABLE spark_catalog.dm.dm_tf_external_sales_thc (
--   YYYYMM STRING,
--   PACK_COD STRING,
--   CORP_COD STRING,
--   AUDIT_COD 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 '处方来源')
-- USING delta
-- -- LOCATION 'abfss://master@retaildlstoragetest.dfs.core.chinacloudapi.cn/DM/dm_tf_external_sales'
-- TBLPROPERTIES (
--   'Type' = 'EXTERNAL',
--   'delta.deletedFileRetentionDuration' = 'interval 31 days',
--   'delta.logRetentionDuration' = 'interval 31 days',
--   'delta.minReaderVersion' = '1',
--   'delta.minWriterVersion' = '2')


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

-- DBTITLE 1,待禁用
--------------------------------------------------------------------------------------
--修改时间：20241106
--修改人：Fanxujia
--修改内容：
--THC的全国数据半年给一次，省份数据是一个季度给一次。
--目前需求是将省份数据写入模型，全国数据单独成一张事实表
--------------------------------------------------------------------------------------
insert OVERWRITE table dm.dm_tf_external_sales_thc (
    YYYYMM,
    PACK_COD,
    CORP_COD,
    AUDIT_COD,
    PLATFORM_TYPE,
    STORE_NAME,
    STORE_TYPE,
    REGION_TYPE,
    DATA_SOURCE,
    PACK_FLAG,
    PROD_FLAG,
    DTP_FLAG,
    SALES_UNIT_CAL,
    SALES_UNIT_CAL_LY,
    SALES_VALUE_CAL,
    SALES_VALUE_CAL_LY,
    CONUTING_UNIT,
    CONUTING_UNIT_LY,
    inst_code,
    cmps_flag,
    dept_name,
    prescription,
    prescription_ly,
    new_code,
    area,
    h_level,
    reimburse,
    reimburse_type,
    prescription_source
  )
SELECT
  YYYYMM,
  PACK_CODE,
  CORP_CODE,
  AUDIT_CODE,
  '' as PLATFORM_TYPE,
  '' as STORE_NAME,
  '' as STORE_TYPE,
  '' as REGION_TYPE,
  'THC(Quarterly)' as DATA_SOURCE,
  1 as PACK_FLAG,
  1 as PROD_FLAG,
  0 as DTP_FLAG,
  SALES_UNIT_CAL,
  SALES_UNIT_CAL_LY,
  SALES_VALUE_CAL,
  SALES_VALUE_CAL_LY,
  CONUTING_UNIT,
  CONUTING_UNIT_LY,
  '' as inst_code,
  '' as cmps_flag,
  '' as dept_name,
  null as prescription,
  null as prescription_ly,
  '' as new_code,
  '' as area,
  '' as h_level,
  '' as reimburse,
  '' as reimburse_type,
  '' as prescription_source
FROM
  DM.DM_TF_EXT_THC_SALES
where AUDIT_CODE <> 'CHT'