-- dict_erp片区


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


-- report_车场启费

select
    a.ym, a.organ_code, a.organ_name, a.comm_id, a.comm_name, case when qifei_cost_num > 0 then 1 else 0 end qifei_comm_num
from (
         select
             ifnull(b.ym,${IF(LEN(p_ym)>0,"'" + p_ym+ "'","null")} ) ym, a.organ_code, a.organ_name, a.comm_id, a.comm_name
              ,sum(case when ifnull(case when ifnull(pr_total_receivable_amt, 0) = 0 then (case when ifnull(cu_receivable_amt,0) then ifnull(cu_paid_pr_amt,0) else cu_receivable_amt end) else pr_total_receivable_amt end,0) != 0 then 1 else 0 end) qifei_cost_num
         from (select distinct organ_code, organ_name, comm_id, comm_name from dim_organ_mapping where comm_id is not null) a
                  left join (
             select * from dwd_finance_fees_serial_d
             where MiddleCostCode in ('00010003','00030002')
                 ${IF(LEN(p_ym)>0," AND ym ='" + p_ym+ "'","")}
         ) b
                            on a.comm_id = b.commid
         group by b.ym, a.organ_code, a.organ_name, a.comm_id, a.comm_name
     ) a
where 1=1
    ${IF(LEN(p_area)>0," AND a.organ_code IN ('"+JOINARRAY(p_area,"','")+"')", "")}


-- report_设备完好率

SELECT  ${if(len(p_area)>0, "comm_id code, comm_name name,","organ_code code, organ_name name,")}
            comm_id, comm_name,
        good_equipment_num, equipment_num
FROM dws_engine_equipment_in_good_d;


-- 基础指标

select '停车场数量' indicator_name, count(*) indicator_value from dim_carpark_info_d
union all
select '车位数量', count(*) from dim_parking_info_d
union all
select '巡查点位数', count(*) from dim_engine_point_d
union all
select '设施设备总数', count(*) from dim_engine_equipment_d

-- 能耗环比

select *
from dws_engine_meter_dosage_m
where left(list_ym,4) = ${left(p_ym,4)}
  AND list_ym <> DATE_FORMAT(CURDATE(), '%Y%m')
  and MeterType in ('水表','电表')
    ${IF(LEN(p_area)>0," AND organ_code IN ('"+JOINARRAY(p_area,"','")+"')", "")}

-- 能耗排名

select ${IF(LEN(p_area)>0,"comm_id code, comm_name name,","organ_code code, organ_name name,")} dosage
from dws_engine_meter_dosage_m
where MeterType = '电表'
    ${IF(LEN(p_area)>0," AND organ_code IN ('"+JOINARRAY(p_area,"','")+"')", "")}
    ${IF(LEN(p_ym)>0," AND list_ym ='" + left(p_ym,4) + right(p_ym,2) + "'","")}
order by Dosage desc
limit 5

-- 能耗同比

SELECT
    organ_code
     , organ_name
     , comm_id
     , comm_name
     , MeterType
     , list_ym
     , Dosage
     , Amount
FROM dws_engine_meter_dosage_m
where 1=1
  AND list_ym <> DATE_FORMAT(CURDATE(), '%Y%m')
  AND list_ym >= '${FORMAT(MONTHDELTA(CONCATENATE(p_ym,"-01"),-60),"yyyyMM")}'
    ${IF(LEN(p_area)>0," AND organ_code IN ('"+JOINARRAY(p_area,"','")+"')", "")}
    ${IF(LEN(p_ym)>0," AND right(list_ym,2) ='" +right(p_ym,2) + "'","")}
  and MeterType in ('水表','电表')
order by list_ym asc


-- 设备巡查
select
    ${IF(LEN(p_area)>0,"organ_code, organ_name, comm_id code, comm_name name,","organ_code code, organ_name name,")}
        comm_id, comm_name,
    task_ym
     , task_finish_num
     , task_num
FROM dws_engine_equipment_inspect_task_m
where 1=1
    ${IF(LEN(p_area)>0," AND organ_code IN ('"+JOINARRAY(p_area,"','")+"')", "")}
      ${IF(LEN(p_ym)>0," AND task_ym ='" + left(p_ym,4) + right(p_ym,2) + "'","")}


-- 水电收缴率

select
    sum(pr_paid_cu_amt + cu_paid_cu_amt + pr_offset_cu_amt + cu_offset_cu_amt - cu_offset_pr_amt - cu_paid_pr_amt) / sum(cu_receivable_amt + pr_exempt_cu_amt + cu_exempt_cu_amt + pr_total_receivable_amt + pr_total_received_amt + cu_early_exempt_pr_amt + cu_exempt_pr_amt) collection_rate
from dwd_finance_fees_serial_d
where ParentCostCode = '0008'
    ${IF(LEN(p_area)>0," AND organ_code IN ('"+JOINARRAY(p_area,"','")+"')", "")}
    ${IF(LEN(p_ym)>0," AND ym ='" + p_ym+ "'","")}


-- 异常抬杆

select
    organ_code , organ_name
     , sum(manualOutNum) manualOutNum
     , sum(carflow) carflow
     , sum(manualOutNum) / sum(carflow) exceptionRate
from dws_engine_stat_exception_rate_w
where yw = (select distinct yw from dws_engine_stat_exception_rate_w order by yw desc limit 1)
group by organ_code , organ_name