-- dic_1_片区


SELECT DISTINCT
    organ_code AS 片区id,
    organ_name AS 片区名称
FROM dim_organ_mapping
;



-- report_1_空间运营

    WITH T1 AS
(SELECT
	organ_code,
	organ_name,
	assessment_occupany_rate AS 出租率
FROM dws_point_occupancy_rate_summary_pq
WHERE 1=1
	AND ym = '${REPLACE(p_ym,"-","")}'
    ${IF(p_kj = "低于50%","AND assessment_occupany_rate <= 50","AND assessment_occupany_rate >= 50")}
    ${IF(LEN(p_area)>0," AND organ_code IN ('"+JOINARRAY(p_area,"','")+"')", "")}
    ${IF(LEN(p_prgject)>0," AND comm_id IN ('"+JOINARRAY(p_prgject,"','")+"')", "")}
    )
SELECT
    ROW_NUMBER() OVER (ORDER BY 出租率) AS 序号,
    organ_code,
    organ_name,
    出租率
FROM T1
ORDER BY 出租率 ${IF(p_kj = "低于50%","ASC","DESC")}
;

-- report_2_新零售
WITH T1 AS (
    select
    organ_code,
    organ_name,
    sum(sale_amt)/10000 sale_amt
    from dws_innoveco_new_retail_m
    where create_ym = '${REPLACE(p_ym,"-","")}'
    group by organ_code,organ_name
    ),T2 AS (
    SELECT
    organ_code,organ_name,
    SUM(IFNULL(index_retail_target,0)) AS  sale_target,
    SUM(index_retail_fact) AS sale_fact
    FROM ods_innoveco_offlinesales
    WHERE `year_month` = '${REPLACE(p_ym,"-","")}'
    GROUP BY organ_code,organ_name
    ),T3 AS (
    SELECT
    T1.organ_code,
    T1.organ_name,
    T2.sale_target,
    (T1.sale_amt + T2.sale_fact) AS sale_amt,
    IFNULL((T1.sale_amt + T2.sale_fact) / T2.sale_target,0) AS 销售完成率
    FROM T1 LEFT JOIN T2 ON T1.organ_code = T2.organ_code
    )
SELECT
    ROW_NUMBER() OVER (ORDER BY 销售完成率) AS 序号,
    organ_code,
    organ_name,
    sale_target,
    sale_amt,
    销售完成率
FROM T3
WHERE 1=1
    ${IF(p_xls = "低于50%","AND 销售完成率 <= 0.5","AND 销售完成率 >= 0.5")}
ORDER BY 销售完成率 ${IF(p_xls = "低于50%","ASC","DESC")}
;

-- report_3_小悦到家

WITH T1 AS (
    select
    organ_code,
    organ_name,
    sum(xydj_amt) sale_amt
    from dws_innoveco_new_retail_m
    where create_ym = '${REPLACE(p_ym,"-","")}'
    group by organ_code,organ_name
    ),T2 AS (
    SELECT
    organ_code,organ_name,
    SUM(IFNULL(index_xiaoyuedj_target,0)) AS  sale_target,
    SUM(index_yuhetai_fact) AS sale_fact
    FROM ods_innoveco_offlinesales
    WHERE `year_month` = '${REPLACE(p_ym,"-","")}'
    GROUP BY organ_code,organ_name
    ),T3 AS (
    SELECT
    T1.organ_code,
    T1.organ_name,
    T2.sale_target,
    (T1.sale_amt + T2.sale_fact) AS sale_amt,
    IFNULL((T1.sale_amt + T2.sale_fact) / T2.sale_target,0) AS 销售完成率
    FROM T1 LEFT JOIN T2 ON T1.organ_code = T2.organ_code
    )
SELECT
    ROW_NUMBER() OVER (ORDER BY 销售完成率) AS 序号,
    organ_code,
    organ_name,
    sale_target,
    sale_amt,
    销售完成率
FROM T3
WHERE 1=1
    ${IF(p_xydj = "低于50%","AND 销售完成率 <= 0.5","AND 销售完成率 >= 0.5")}
ORDER BY 销售完成率 ${IF(p_xydj = "低于50%","ASC","DESC")}
;


-- report_4_御和泰

WITH T1 AS (
    select
    organ_code,
    organ_name,
    sum(yht_amt)/10000 sale_amt
    from dws_innoveco_new_retail_m
    where create_ym = '${REPLACE(p_ym,"-","")}'
    group by organ_code,organ_name
    ),T2 AS (
    SELECT
    organ_code,organ_name,
    SUM(IFNULL(index_yuhetai_target,0)) AS  sale_target,
    SUM(index_xiaoyuedj_fact) AS sale_fact
    FROM ods_innoveco_offlinesales
    WHERE `year_month` = '${REPLACE(p_ym,"-","")}'
    GROUP BY organ_code,organ_name
    ),T3 AS (
    SELECT
    T1.organ_code,
    T1.organ_name,
    T2.sale_target,
    (T1.sale_amt + T2.sale_fact) AS sale_amt,
    IFNULL((T1.sale_amt + T2.sale_fact) / T2.sale_target,0) AS 销售完成率
    FROM T1 LEFT JOIN T2 ON T1.organ_code = T2.organ_code
    )
SELECT
    ROW_NUMBER() OVER (ORDER BY 销售完成率) AS 序号,
    organ_code,
    organ_name,
    sale_target,
    sale_amt,
    销售完成率
FROM T3
WHERE 1=1
    ${IF(p_yht = "低于50%","AND 销售完成率 <= 0.5","AND 销售完成率 >= 0.5")}
ORDER BY 销售完成率 ${IF(p_yht = "低于50%","ASC","DESC")}
;


-- report_指标卡


WITH T1 AS (/*各类别应收与实收*/
    SELECT
    index_type,
    SUM(IFNULL(index_receivable,0)) AS 应收金额,
    SUM(IFNULL(index_receipts,0)) AS 实收金额
    FROM ods_caiwu_profit
    WHERE 1=1
    AND `year_month` = '${REPLACE(p_ym,"-","")}'
    ${IF(LEN(p_area)>0," AND organ_code IN ('"+JOINARRAY(p_area,"','")+"')", "")}
    ${IF(LEN(p_prgject)>0," AND comm_id IN ('"+JOINARRAY(p_prgject,"','")+"')", "")}
    GROUP BY index_type
    )
SELECT
    IFNULL(SUM(CASE WHEN index_type = '附属资源/空间运营' THEN 实收金额 ELSE 0 END),0) AS 空间运营实收,
    IFNULL(SUM(CASE WHEN index_type = '附属资源/空间运营' THEN 实收金额 / 应收金额 ELSE 0 END),0) AS 空间运营完成率,
    IFNULL(SUM(CASE WHEN index_type = '美居' THEN 实收金额 ELSE 0 END),0) AS 美居实收,
    IFNULL(SUM(CASE WHEN index_type = '美居' THEN 实收金额 / 应收金额 ELSE 0 END),0) AS 美居完成率,
    IFNULL(SUM(CASE WHEN index_type = '社商/新零售' THEN 实收金额 ELSE 0 END),0) AS 新零售实收,
    IFNULL(SUM(CASE WHEN index_type = '社商/新零售' THEN 实收金额 / 应收金额 ELSE 0 END),0) AS 新零售完成率,
    IFNULL(SUM(CASE WHEN index_type = '资产运营/资产租售' THEN 实收金额 ELSE 0 END),0) AS 资产出租实收,
    IFNULL(SUM(CASE WHEN index_type = '资产运营/资产租售' THEN 实收金额 / 应收金额 ELSE 0 END),0) AS 资产出租完成率,
    IFNULL(SUM(实收金额),0) AS 全部实收,
    IFNULL(SUM(实收金额) / SUM(应收金额),0) AS 完成率
FROM T1
;


-- 全国综合考核率

SELECT
    CONCAT(assessment_occupany_rate,'%') AS 完成率
FROM dws_point_occupancy_rate_summary_all
WHERE ym = '${REPLACE(p_ym,"-","")}'