declare @tmessageid bigint
set @tmessageid=(select isnull(MAX(tmessageid),0) from tmessage)
insert into tmessage
(tmessageid, siteid, createdate, rmkenable, fmessage, ftype, fisread, freaddate, tenterprise_userid)
select @tmessageid+ROW_NUMBER()over(order by t1.fphonenumber), t1.siteid, getDate(), 0, '呜~~，别把我忘了，今天的数据还没有录入。', '系统', 0, null, t1.tenterprise_userid
from tenterprise_users t1
left join tcustomer_reception t2 on t1.siteid=t2.siteid and t1.tagentsid=t2.tagentsid and t1.fname=t2.createby and convert(varchar(10),t2.createdate,120)= convert(varchar(10),GETDATE(),120)
left join tsaleevent t3 on t1.siteid=t3.siteid and t1.tagentsid=t3.tagentsid and t1.fname=t3.createby and convert(varchar(10),t3.createdate,120)= convert(varchar(10),GETDATE(),120)
left join tagents_cost t4 on t1.siteid=t4.siteid and t1.tagentsid=t4.tagentsid and t1.fname=t4.createby and convert(varchar(10),t4.createdate,120)= convert(varchar(10),GETDATE(),120)
inner join tagents t5 on t1.siteid=t5.siteid and t1.tagentsid=t5.tagentsid
inner join tappmodelauth t6 on t1.siteid=t6.siteid and t6.fisdefault=1
where t1.fphonenumber is not null and t1.fusertype='经销商' and isnull(t1.frole,'') not in ('工人','推广人') and (t5.fappmodel='专业版'or (t5.fappmodel is null and t6.fappmodel='专业版'))  and t2.tcustomer_receptid is null and t3.tsaleeventid is null and t4.tagents_costid is null