package com.yc.open.init.shcedule;
|
|
import com.yc.action.grid.GridUtils;
|
import com.yc.entity.DataSourceEntity;
|
import com.yc.exception.ApplicationException;
|
import com.yc.factory.FactoryBean;
|
import com.yc.multiData.SpObserver;
|
import com.yc.sdk.WebSocketMessage.action.WebSocketMessageServer;
|
import com.yc.sdk.WebSocketMessage.entity.MessageInfo;
|
import com.yc.sdk.WebSocketMessage.entity.MessageType;
|
import com.yc.sdk.WebSocketMessage.entity.WsMessageUserEntity;
|
import com.yc.service.impl.BaseDoIfc;
|
|
import java.util.List;
|
import java.util.StringJoiner;
|
import java.util.regex.Matcher;
|
import java.util.regex.Pattern;
|
import java.util.stream.Collectors;
|
|
/**
|
* 定时显示右下角弹窗
|
*/
|
public class MessagePopTipsSchedule extends BaseSchedule implements Runnable {
|
|
public MessagePopTipsSchedule(DataSourceEntity dataSourceEntity) {
|
super(dataSourceEntity);
|
}
|
|
@Override
|
public void run() {
|
try {
|
if (Thread.interrupted()) {
|
throw new InterruptedException();
|
}
|
//log.info(dataSourceEntity.getSystemID() + "右下角弹窗开始.....");
|
SpObserver.setDBtoInstance("_" + dataSourceEntity.getDbId());
|
BaseDoIfc doIfc = (BaseDoIfc) FactoryBean.getBean("baseDoImpl");
|
//BaseService baseService=(BaseService)FactoryBean.getBean("BaseService");
|
//获取当前已连接webscoket的用户
|
final List<WsMessageUserEntity> onlineUser = WebSocketMessageServer.getOnlineUser(dataSourceEntity.getDbId(), null);
|
StringJoiner joiner = new StringJoiner(",");
|
if (onlineUser != null && onlineUser.size() > 0){
|
for(WsMessageUserEntity entity:onlineUser){
|
joiner.add(entity.getUserCode());
|
}
|
//因为用户已下线,为了避免发送失败导致用户下次登录收不到通知的情况,取数及更新次数分开处理
|
final List<MessageTipsEntity> list = doIfc.doQuery("set nocount on ; declare @UserCodes varchar(max) =" +GridUtils.prossSqlParm(joiner.toString())+
|
"\n select a.messid,a.messagetxt,a.unvaliddate,a.createuser,a.createtime,\n" +
|
" a.rejustsrvflag,a.Readers,a.UsrReaded,a.tipcount,a.formid,\n" +
|
" a.formtype,a.origfields,a.linkfields,a.linkmode,a.self_datafields,\n" +
|
" a.link_datafields,a.efilter , b.usercode ,a.url ,\n" +
|
" a.createusername ,a.topic ,a.messagetype ,a.isPublicUser\n" +
|
" from _sysMessageCount b \n" +
|
" join _sysmessage a on a.messid = b.messid\n" +
|
" where b.usercode in (select list from getinstr(@UserCodes)) \n" +
|
" and b.tipcount < a.tipcount\n" +
|
" and a.unvaliddate >= getdate() \n" +
|
" and b.isRead = 0 ", MessageTipsEntity.class);
|
if (list != null && list.size() > 0) {
|
final StringJoiner updateJoiner=new StringJoiner("\n");
|
list.stream().unordered().distinct().forEach(x -> {
|
//取每一个用户的所有消息
|
String userCode = x.getUsercode();
|
//--分二种消息类型,1右下角弹窗,2系统级消息显示
|
final List<MessageTipsEntity> collect = list.stream().filter(y -> y.getUsercode().equalsIgnoreCase(x.getUsercode())).collect(Collectors.toList());
|
if (collect != null && collect.size() > 0) {
|
//----发送出去
|
//---通知webscoket
|
MessageInfo messageInfo = new MessageInfo();
|
messageInfo.setDbId(dataSourceEntity.getDbId());
|
messageInfo.setMsgType(MessageType.NOTICE_AND_TODO);
|
messageInfo.setUserFromType("1");//TODO PC端
|
messageInfo.setUserCode(userCode);
|
messageInfo.setMsg(GridUtils.toJson(collect));
|
//直接发送
|
List<WsMessageUserEntity> wsMessageUserEntityList = WebSocketMessageServer.getOnlineUserByTips(dataSourceEntity.getDbId(), userCode);
|
if (wsMessageUserEntityList != null && wsMessageUserEntityList.size() != 0) {
|
for (WsMessageUserEntity wsMessageUserEntity : wsMessageUserEntityList) {
|
boolean flag= wsMessageUserEntity.sendMessageV3(messageInfo);
|
if(flag){
|
//成功才更新次数
|
collect.stream().forEach(z->{
|
updateJoiner.add(" if not exists(select 1 from @table where MessId = "+z.getMessid()+" and UserCode = "+GridUtils.prossSqlParm(userCode)+")\n" +
|
" begin\n" +
|
" insert into @table(MessId,UserCode) values ( "+z.getMessid()+","+GridUtils.prossSqlParm(userCode)+")\n" +
|
" end\n");
|
});
|
}
|
}
|
}
|
|
}
|
});
|
//---更新次数
|
if(updateJoiner.length()>0) {
|
String sql = "set nocount on \n declare @table table(MessId int,UserCode varchar(50), Primary Key(MessId,UserCode))\n" +
|
updateJoiner.toString() +
|
" \n update a set tipcount = isnull(tipcount,0) + 1,LastPushTime=getDate() \n" +
|
" from _sysMessageCount a \n" +
|
"where exists ( select 1 from @table b where a.MessId = b.MessId and a.UserCode = b.UserCode and (a.LastPushTime is null or datediff(second,a.LastPushTime,getdate()) > 45 )) ";
|
doIfc.doExecute(sql);
|
//log.info(dataSourceEntity.getSystemID() + "右下角弹窗完成:"+sql);
|
}
|
}
|
}
|
} catch (InterruptedException ex) {
|
log.info(dataSourceEntity.getSystemID() + "右下角弹窗任务已被终止");
|
} catch (Exception ex) {
|
ex.printStackTrace();
|
log.error(dataSourceEntity.getSystemDescribe() + ":" + ex.getMessage());
|
}finally {
|
SpObserver.setDBtoInstance();
|
}
|
}
|
public String replaceBlank(String str) {
|
if (str == null || str == "") { return ""; }
|
Matcher m = null;
|
try {
|
Pattern p = Pattern.compile("\t|\r|\n");
|
m = p.matcher(str);
|
} catch (Exception e) {
|
e.printStackTrace();
|
throw new ApplicationException(str + "-解析出错,存在有特殊字符");
|
}
|
return m.replaceAll(" ").replaceAll("\\$", "\\\\\\$");
|
}
|
public static String toString(Object obj){
|
if(obj==null)return "";
|
return obj.toString();
|
}
|
}
|