-- dic_门店


SELECT DISTINCT
    comm_id AS 项目id,/*门店*/
    comm_name AS 项目名称
FROM dim_organ_mapping
WHERE 1=1
    ${IF(LEN(p_area)>0," AND organ_code IN ('"+JOINARRAY(p_area,"','")+"')", "")}

-- dic_片区


SELECT DISTINCT
    organ_code AS 片区id,
    organ_name AS 片区名称
FROM dws_finance_fees_serial_m
where 1=1 and organ_code is not null
  and organ_name not in ('东湖商管','领悦总部','演示机构')
    ${if(GETUSERDEPARTMENTS(2)="领悦集团总部","","and organ_name = '"+GETUSERDEPARTMENTS(2)+"'")}

-- dict_费用类型

select distinct ParentCostCode, case when ParentCostCode = '0008' then '代收代缴水电费' else ParentCostName end ParentCostName
from dws_finance_fees_serial_m
where ParentCostCode in ('0001','0007','0008')
order by ParentCostCode asc


-- report_收缴率


select * from (
                  select
                      ${if(len(p_area) == 0,"a.organ_code code, a.organ_name name,","a.comm_id code, a.comm_name name,")}
                      a.ym, ParentCostCode, ParentCostName
                       , (ifnull(pr_paid_cu_amt,0) + ifnull(cu_paid_cu_amt,0) - ifnull(pr_offset_cu_amt,0) - ifnull(cu_offset_cu_amt,0))/10000 cu_paid_amt
                       , (ifnull(cu_receivable_amt,0) + ifnull(pr_exempt_cu_amt,0) + ifnull(cu_exempt_cu_amt,0))/10000 cu_receivable_amt
                       , (- ifnull(cu_paid_pr_amt,0) - ifnull(cu_offset_pr_amt,0))/10000 pr_paid_amt
                       , (ifnull(pr_total_receivable_amt,0) + ifnull(pr_total_received_amt,0) + ifnull(cu_early_exempt_pr_amt,0) + ifnull(cu_exempt_pr_amt,0))/10000 pr_receivable_amt
                       , case when a.ParentCostCode = '0001' then b.index_dq_target else 0 end cu_coll_rate_target
                       , case when a.ParentCostCode = '0001' then b.index_wq_target else 0 end pr_coll_rate_target
                       , case when a.ParentCostCode = '0008' then b.index_sd_target
                              when a.ParentCostCode = '0007' then b.index_dj_target else 0 end total_coll_rate_target
                  from dws_finance_fees_serial_m a
                           left join ods_finance_collection_target_area b
                                     on a.organ_code = b.organ_code and a.ym = b.ym
                           left join ods_finance_collection_target_project c
                                     on a.comm_id = c.comm_id and a.ym = c.ym
                  where 1=1 and a.organ_code is not null
                    and a.organ_name not in ('东湖商管','领悦总部','演示机构') and a.comm_name not like '%天富%'
                      ${if(len(p_startdate) == 0,"","and a.ym = '" + p_startdate + "'")}
                      ${if(len(p_feestype) == 0,"","and a.ParentCostCode = '" + p_feestype+ "'")}
                      -- ${if(len(p_area) == 0,"","and a.organ_code = '" + p_area+ "'")}
                      ${IF(LEN(p_area)>0," AND a.organ_code IN ('"+JOINARRAY(p_area,"','")+"')", "")}
                      ${if(left(fine_role,2)=="项目","and a.comm_name = '"+GETUSERDEPARTMENTS(3)+"'","")}
              ) a where not (cu_paid_amt=0 and cu_receivable_amt=0 and pr_paid_amt=0 and pr_receivable_amt = 0)

-- report_收缴率目标


select
    organ_code code
     , organ_name name
     , ym
     , index_dq_target
     , index_wq_target
     , index_sd_target
     , index_dj_target
     , index_ac_target_dq
     , index_ac_target_wq
     , index_ac_target_zh
from ods_finance_collection_target_area
where 1=1
    ${if(len(p_startdate) == 0,"","and ym = '" + p_startdate + "'")}
union all
select
    comm_id code
     , comm_name name
     , ym
     , index_dq_target
     , index_wq_target
     , index_sd_target
     , index_dj_target
     , index_ac_target_dq
     , index_ac_target_wq
     , index_ac_target_zh
from ods_finance_collection_target_project
where 1=1
    ${if(len(p_startdate) == 0,"","and ym = '" + p_startdate + "'")}


-- report_中科目收缴率

select
    b.organ_code, b.organ_name, b.comm_id, b.comm_name, ym
                                                                                                           ${if(len(p_feestype) == 0,", ParentCostCode CostCode, ParentCostName CostName",", MiddleCostCode CostCode, MiddleCostName CostName")}
     , (pr_paid_cu_amt + cu_paid_cu_amt + pr_exempt_cu_amt + cu_exempt_cu_amt)/10000 cu_paid_amt
     , (cu_receivable_amt + pr_exempt_cu_amt + cu_exempt_cu_amt)/10000 cu_receivable_amt
     , (- cu_paid_pr_amt + cu_offset_pr_amt)/10000 pr_paid_amt
     , (pr_total_receivable_amt + pr_total_received_amt + cu_early_exempt_pr_amt + cu_exempt_pr_amt)/10000 pr_receivable_amt
from dwd_finance_fees_serial_d a
         inner join (select distinct organ_code, organ_name, comm_id, comm_name from dim_organ_mapping where comm_id is not null) b
                    on a.commid = b.comm_id
where 1=1
    ${if(len(p_startdate) == 0,"","and ym = '" + p_startdate + "'")}
      ${if(len(p_feestype) == 0,"","and ParentCostCode = '" + p_feestype+ "'")}
      ${if(len(p_area) == 0,"","and b.organ_code = '" + p_area+ "'")}