fs-danaus
2022-09-24 5c6457965206ba36c9ef8ed2bdfdf20cb0c381d9
提交 | 用户 | age
c86295 1 package com.yc.open.init.shcedule;
F 2
3 import com.alibaba.fastjson.JSON;
4 import com.yc.action.grid.GridUtils;
5 import com.yc.entity.DataSourceEntity;
6 import com.yc.exception.ApplicationException;
7 import com.yc.factory.FactoryBean;
8 import com.yc.multiData.SpObserver;
9 import com.yc.sdk.WebSocketMessage.action.WebSocketMessageServer;
10 import com.yc.sdk.WebSocketMessage.entity.MessageInfo;
11 import com.yc.sdk.WebSocketMessage.entity.MessageType;
c61217 12 import com.yc.sdk.WebSocketMessage.entity.WsMessageUserEntity;
c86295 13 import com.yc.service.BaseService;
5c6457 14 import com.yc.service.impl.BaseDoIfc;
c86295 15 import org.springframework.jdbc.core.BeanPropertyRowMapper;
F 16
17 import java.text.SimpleDateFormat;
18 import java.util.HashMap;
19 import java.util.List;
20 import java.util.Map;
c61217 21 import java.util.StringJoiner;
c86295 22 import java.util.regex.Matcher;
F 23 import java.util.regex.Pattern;
24 import java.util.stream.Collectors;
25
26 /**
27  * 定时显示右下角弹窗
28  */
29 public class MessagePopTipsSchedule extends BaseSchedule implements Runnable {
30
31     public MessagePopTipsSchedule(DataSourceEntity dataSourceEntity) {
32         super(dataSourceEntity);
33     }
34
35     @Override
36     public void run() {
37         try {
38             if (Thread.interrupted()) {
39                 throw new InterruptedException();
40             }
5c6457 41             //log.info(dataSourceEntity.getSystemID() + "右下角弹窗开始.....");
c86295 42             SpObserver.setDBtoInstance("_" + dataSourceEntity.getDbId());
5c6457 43             BaseDoIfc doIfc = (BaseDoIfc) FactoryBean.getBean("baseDoImpl");
F 44             BaseService baseService=(BaseService)FactoryBean.getBean("BaseService");
c61217 45             //获取当前已连接webscoket的用户
F 46             final List<WsMessageUserEntity> onlineUser = WebSocketMessageServer.getOnlineUser(dataSourceEntity.getDbId(), null);
47             StringJoiner joiner = new StringJoiner(",");
48             if (onlineUser != null && onlineUser.size() > 0){
49                 for(WsMessageUserEntity entity:onlineUser){
50                     joiner.add(entity.getUserCode());
51                 }
52                 final List<MessageTipsEntity> list = baseService.getJdbcTemplate().query("set nocount on ; declare @UserCodes varchar(max) =" +GridUtils.prossSqlParm(joiner.toString())+
53                         "\n select a.messid,a.messagetxt,a.unvaliddate,a.createuser,a.createtime,\n" +
54                         " a.rejustsrvflag,a.Readers,a.UsrReaded,a.tipcount,a.formid,\n" +
55                         " a.formtype,a.origfields,a.linkfields,a.linkmode,a.self_datafields,\n" +
56                         " a.link_datafields,a.efilter , b.usercode ,a.url ,\n" +
57                         " a.createusername ,a.topic ,a.messagetype ,a.isPublicUser\n" +
58                         "    from _sysMessageCount b  \n" +
59                         "   join _sysmessage a on a.messid = b.messid\n" +
60                         "       where b.usercode in (select list from getinstr(@UserCodes))  \n" +
61                         "       and b.tipcount < a.tipcount\n" +
62                         "       and a.unvaliddate >= getdate() \n" +
63                         "       and b.isRead = 0 ", new BeanPropertyRowMapper<>(MessageTipsEntity.class));
64             if (list != null && list.size() > 0) {
65                 final StringJoiner updateJoiner=new StringJoiner("\n");
66                 list.stream().unordered().distinct().forEach(x -> {
c86295 67                     //取每一个用户的所有消息
c61217 68                     String userCode = x.getUsercode();
c86295 69                     //--分二种消息类型,1右下角弹窗,2系统级消息显示
F 70                     final List<MessageTipsEntity> collect = list.stream().filter(y -> y.getUsercode().equalsIgnoreCase(x.getUsercode())).collect(Collectors.toList());
c61217 71                     if (collect != null && collect.size() > 0) {
c86295 72                         //----发送出去
F 73                         //---通知webscoket
74                         MessageInfo messageInfo = new MessageInfo();
75                         messageInfo.setDbId(dataSourceEntity.getDbId());
76                         messageInfo.setMsgType(MessageType.NOTICE_AND_TODO);
77                         messageInfo.setUserFromType("1");//TODO PC端
78                         messageInfo.setUserCode(userCode);
79                         messageInfo.setMsg(GridUtils.toJson(collect));
c61217 80                         //直接发送
F 81                         List<WsMessageUserEntity> wsMessageUserEntityList = WebSocketMessageServer.getOnlineUserByTips(dataSourceEntity.getDbId(), userCode);
82                         if (wsMessageUserEntityList != null && wsMessageUserEntityList.size() != 0) {
83                             for (WsMessageUserEntity wsMessageUserEntity : wsMessageUserEntityList) {
84                               boolean flag= wsMessageUserEntity.sendMessageV3(messageInfo);
85                               if(flag){
86                                   //成功才更新次数
87                                   collect.stream().forEach(z->{
5c6457 88                                       updateJoiner.add(" if not exists(select 1 from @table where MessId = "+z.getMessid()+" and UserCode = "+GridUtils.prossSqlParm(userCode)+")\n" +
F 89                                               " begin\n" +
90                                               " insert into @table(MessId,UserCode) values ( "+z.getMessid()+","+GridUtils.prossSqlParm(userCode)+")\n" +
91                                               " end\n");
c61217 92                                   });
F 93                               }
94                             }
95                         }
96
97                     }
c86295 98                 });
c61217 99                 //---更新次数
F 100                 if(updateJoiner.length()>0) {
101                     String sql = "set nocount on \n declare @table table(MessId int,UserCode varchar(50), Primary Key(MessId,UserCode))\n" +
102                             updateJoiner.toString() +
5c6457 103                             " \n update a  set tipcount = isnull(tipcount,0) + 1,LastPushTime=getDate()  \n" +
c61217 104                             "  from _sysMessageCount a \n" +
5c6457 105                             "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 )) ";
F 106                     doIfc.doExecute(sql);
107                     //log.info(dataSourceEntity.getSystemID() + "右下角弹窗完成:"+sql);
c61217 108                 }
c86295 109             }
c61217 110         }
c86295 111         } catch (InterruptedException ex) {
F 112             log.info(dataSourceEntity.getSystemID() + "右下角弹窗任务已被终止");
113         }  catch (Exception ex) {
114             ex.printStackTrace();
115             log.error(dataSourceEntity.getSystemDescribe() + ":" + ex.getMessage());
116         }finally {
117         SpObserver.setDBtoInstance();
118     }
119     }
120     public String replaceBlank(String str) {
121         if (str == null || str == "") { return ""; }
122         Matcher m = null;
123         try {
124             Pattern p = Pattern.compile("\t|\r|\n");
125             m = p.matcher(str);
126         } catch (Exception e) {
127             e.printStackTrace();
128             throw new ApplicationException(str + "-解析出错,存在有特殊字符");
129         }
130         return m.replaceAll(" ").replaceAll("\\$", "\\\\\\$");
131     }
132     public static String toString(Object obj){
133         if(obj==null)return "";
134         return obj.toString();
135     }
136 }