select t1.tenterprise_userid                                                        as userid,
       t1.fphonenumber,
       t1.faccountno,
       t1.siteid,
       t1.tagentsid,
       t1.tstoreid,
       case when t1.siteid = 'AGENTS'or t1.fusertype='工人' then t3.fagentname else t2.fcompshortname end as fcompname,
       t3.fagentnum,
       t3.fagentname,
       t4.fstorenum,
       t4.fstorename,
       t1.fusertype,
       t1.frole,
       t1.fname,
       t1.fisadministrator,
       t1.fissysadministrator,
       t1.fcanreadagentanalysis,
       t1.fdatamodify,
       case
           when t1.fusertype = '经销商' and t3.tagentsid IS not null and t3.fappmodel is not null then t3.fappmodel
           else t5.fappmodel end                                                    as fappmodel,
       case
           when t6.tpayinfoid IS null and ((t1.fusertype = '经销商' and t5.fagentamount > 0) or
                                           (t1.fusertype = '企业' and t5.FENTERPRISEUSERAMOUNT > 0)) then 1
           else 0 end                                                               as fisneedpay,
       case
           when t6.tpayinfoid IS null and ((t1.fusertype = '经销商' and t5.fagentamount > 0) or
                                           (t1.fusertype = '企业' and t5.FENTERPRISEUSERAMOUNT > 0)) then '当前账号未激活'
           else '' end                                                              as fioserrmsg,
       case
           when t6.tpayinfoid IS null and t1.fusertype = '经销商' then t5.fagentamount
           when t6.tpayinfoid IS null and t1.fusertype = '企业' then t5.FENTERPRISEUSERAMOUNT
           else 0 end                                                               as fpayamount,
       t5.fagentoldamount,
       t2.floginmode,
       t1.accesstoken                                                               as token,
       t2.tenterpriseid
from tenterprise_users t1
         inner join tenterprise t2 on t1.siteid = t2.siteid and (fenddate is null or fenddate > getdate())
         left join tagents t3 on t1.siteid = t3.siteid and t1.tagentsid = t3.tagentsid
         left join tstore t4 on t1.siteid = t4.siteid and t1.tagentsid = t4.tagentsid and t1.tstoreid = t4.tstoreid
         inner join tappmodelauth t5 on t1.siteid = t5.siteid and t5.fisdefault = 1
         left join tpayinfo t6 on t1.siteid = t6.siteid and ((t1.fusertype = '经销商' and t1.tagentsid = t6.tagentsid) or
                                                             (t1.fusertype = '企业' and t1.tenterprise_userid = t6.tenterprise_userid)) and
                                  t6.fbegdate <= GETDATE() and t6.fenddate >= GETDATE()
where t1.accesstoken=$accesstoken$
