select distinct t1.tenterprise_userid as userid
from tenterprise_users t1
         inner join tenterprise t2 on t1.siteid = t2.siteid and (fenddate is null or fenddate > getdate())
         inner join tuserrequestlog t3 on t1.siteid=t3.siteid and t1.tenterprise_userid=t3.tenterprise_userid
         inner join tappmodelauth t5 on t1.siteid = t5.siteid and t5.fisdefault = 1
where  ((t1.fusertype = '经销商' and t5.fagentamount > 0) or
       (t1.fusertype = '企业' and t5.fenterpriseuseramount > 0))
       and exists(
		   select siteid,tpayinfoid,tagentsid,tenterprise_userid from tpayinfo
		   where (fbegdate > GETDATE() or fenddate < GETDATE()) and t1.siteid=tpayinfo.siteid and (
		   (t1.fusertype = '经销商' and t1.tagentsid = tpayinfo.tagentsid) or
                                                             (t1.fusertype = '企业' and t1.tenterprise_userid = tpayinfo.tenterprise_userid)
		   )
        )
group by t1.tenterprise_userid  having max(t3.flastrequestdate)>DATEADD(DAY,-3, GETDATE())