select
    *
from dm.v_ag_sal_collection a
where 1=1
    ${if(len(sBu) == 0,"","and bu_code in ('"+sBu+"')")}
    ${if(len(sRegion) == 0,"","and region_code in ('"+sRegion+"')")}
    ${if(len(sOffice) == 0,"","and office_code in ('"+sOffice+"')")}
    ${if(len(sCu) == 0,"","and city_unit_code in ('"+sCu+"')")}
    ${if(len(sPl_name) == 0,"","and pl_name in ('"+sPl_name+"')")}
    ${IF(sSub_main='主经销商',IF(len(sDeal) == 0,"","AND deal_code in ('"+sDeal+"')"),"")}
    ${IF(sSub_main='子经销商',IF(len(sDeal) == 0,"","AND src_deal_code in ('"+sDeal+"')"),"")}
    ${if(len(sBill_code) == 0,"","and bill_code in ('"+sBill_code+"')")}
    ${if(len(sStart_amt) == 0,"","and clc_amt >= "+sStart_amt)}
    ${if(len(sEnd_amt) == 0,"","and clc_amt <= "+sEnd_amt)}
    ${if(len(sStart_date) == 0,"","and bill_date >= date('"+sStart_date+"')")}
    ${if(len(sEnd_date) == 0,"","and bill_date < date('"+sEnd_date+"')")} + INTERVAL '1 days'
  and (exists(
    select * from dm.dm_bi_user_permisson
    where bi_user= '${fine_username}'
  and per_model in ('all','sal')
  and ( is_all=1
   or per_code = a.bu_code
   or per_code = a.region_code
   or per_code = a.office_code
--or per_code=a.dept_code
    )
    ) or exists ( /*城市经理权限配置*/
    select 1 from dwr.dim_dealer_administer dda
    where dda.bu_code = a.bu_code
  and dda.create_by = 'crm'
  and dda.deal_code = a.src_deal_code
  and dda.city_manager_code='${fine_username}'
  and dda.enabled_status=1
    ))