--庄园之旅场次
select
t1.deal_code as deal_code -- 经销商编码
,t1.team_name  AS deal_name
,dmd.fist_dept_code as bu_code -- 事业部编码
,dmd.fist_dept_name as bu_name -- 事业部
,dmd.secd_dept_code as region_code -- 大区编码
,dmd.secd_dept_name as region_name -- 大区
,dmd.thrd_dept_code as office_code -- 办事处编码
,dmd.thrd_dept_name as office_name -- 办事处
,dmd.dept_code      as  city_unit_code -- 城市单元编码
,dmd.dept_name      as  city_unit_name -- 城市单元
,count(distinct t1.team_code) as session_num -- 场次
,SUM(t2.num)    AS num  -- 人数
,SUM(t1.budget_amt) AS budget_amt -- 预算费用
,SUM(t1.visit_fee) AS  visit_fee -- 参观费用
,SUM(t1.exp_fee) AS  speak_fee -- 解说费用
from dwi.dwi_mak_estmate_apply t1
LEFT JOIN
(
   SELECT
    team_code
   ,COUNT(*) AS num
   FROM dwi.dwi_mak_estmate_guest
   GROUP by team_code
)t2
ON t1.team_code = t2.team_code
left join dwr.dim_department d
on t1.dept_code=d.dept_code
left join
(
    select deal_code,bu_code,max(city_unit_code) as city_unit_code
    from dwr.dim_dealer_administer
    where enabled_status=1
    group by deal_code, bu_code
) dda
on t1.deal_code=dda.deal_code
and if(d.fist_dept_code in('0202/0215','0202/0216'),'0202/0226',d.fist_dept_code)=dda.bu_code
left join dwr.dim_department dmd on nvl(dda.city_unit_code,d.dept_code)=dmd.dept_code
where team_type='庄园之旅'
and bill_status='已审核'
AND exists (
    select
    1
from
	dm.dm_bi_user_permisson
where
	bi_user = '${fine_username}'
	and per_model IN ('all','sal')
	and (
			is_all = 1
			OR per_code = dmd.fist_dept_code
			OR per_code = dmd.secd_dept_code
			OR per_code = dmd.thrd_dept_code
			OR per_code = dmd.dept_code
			)
)
${if(len(sBu) == 0,"","and dmd.fist_dept_code IN ('"+sBu+"')")}
${if(len(sRegion) == 0,"","and dmd.secd_dept_code IN ('"+sRegion+"')")}
${if(len(sOffice) == 0,"","and dmd.thrd_dept_code  IN ('"+sOffice+"')")}
${if(len(sCu) == 0,"","and dmd.dept_code IN ('"+sCu+"')")}
${IF(sDeal='',"","AND t1.deal_code IN ('"+sDeal+"')")}

and trip_end_date <=  date '${sEnd_date}'
GROUP BY t1.deal_code, t1.team_name, dmd.fist_dept_code, dmd.fist_dept_name, dmd.secd_dept_code, dmd.secd_dept_name, dmd.thrd_dept_code, dmd.thrd_dept_name, dmd.dept_code, dmd.dept_name




SELECT
t1.team_code AS team_code -- 团队id
,COUNT ( * ) AS num
FROM dwi.dwi_mak_estmate_guest t1
        INNER JOIN dwi.dwi_mak_estmate_apply t2 ON t1.team_code = t2.team_code AND t2.bill_status = '已审核'
        LEFT JOIN dwr.dim_department d ON t2.dept_code = d.dept_code
        LEFT JOIN (
        SELECT
            deal_code,
            bu_code,
            MAX ( city_unit_code ) AS city_unit_code
        FROM
            dwr.dim_dealer_administer
        WHERE
            enabled_status = 1
        GROUP BY
            deal_code,
            bu_code ) dda ON t2.deal_code = dda.deal_code AND IF( d.fist_dept_code IN ( '0202/0215', '0202/0216' ), '0202/0226', d.fist_dept_code ) = dda.bu_code
        LEFT JOIN dwr.dim_department dmd ON nvl ( dda.city_unit_code, d.dept_code ) = dmd.dept_code
WHERE
    t2.team_type = '庄园之旅'
  AND t2.bill_status = '已审核'

    ${IF(LEN(sCu) = 0,"","AND dmd.dept_code IN ('"+sCu+"')")}

    ${IF(LEN(sStart_date) = 0,"","AND t2.trip_start_date >= '"+sStart_date+"'")}
    ${IF(LEN(sEnd_date) = 0,"","AND t2.trip_start_date <= '"+sEnd_date+"'")}