select distinct t1.tarchives_scid,
                t1.siteid,
                t3.fsaleruserid,
                t2.tenterprise_userid,
                t3.fsaler,
                t3.fname,
                t3.fprovince,
                t3.fcity,
                t3.fcounty,
                t3.fphonenumber,
                case when t4.tarchives_sc_useid IS null then '未学习' else '已学习' end    freadstatus,
                case when t5.tenterprise_userid IS null then '未下载' else '已下载' end as fdownloadstatus
from tarchives_sc t1
         inner join tenterprise_users t2 on t1.siteid = t2.siteid and t2.fphonenumber is not null
    and ((t1.fsalercanread = 1 and t2.fusertype = '企业') or
         (t1.fagentcanread = 1 and t2.fusertype = '经销商' and fisadministrator = 1)
        or (t1.fagentsalercanread = 1 and t2.fusertype = '经销商' and fisadministrator = 0))
         inner join user_agent_saler_view t3 on t1.siteid = t3.siteid and t2.tenterprise_userid = t3.tenterprise_userid
         left join tarchives_sc_userdata t4 on t1.siteid = t4.siteid and t1.tarchives_scid = t4.tarchives_scid and
                                               t2.tenterprise_userid = t4.tenterprise_userid
         left join (select distinct t3.tenterprise_userid
                    from tattachment t1
                             inner join tattachment_downlog t3
                                        on t1.siteid = t3.siteid and t1.tattachmentid = t3.tattachmentid
                    where t1.siteid =$siteid$ and t1.ownertable = 'tarchives_sc' and t1.ownerid = $tarchives_scid$
) t5 on t2.tenterprise_userid = t5.tenterprise_userid
where t1.siteid = $siteid$
  and t1.tarchives_scid = $tarchives_scid$
  and t3.fsaleruserid in $fsaleruserid$