fs-danaus
2024-09-10 05aec6b35a58057856c8b56428f2a9f127c958f8
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
package com.yc.crm.schedule;
 
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.open.init.shcedule.BaseSchedule;
import com.yc.open.init.shcedule.MessageTipsEntity;
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 MessagePopSchedule extends BaseSchedule implements Runnable {
 
    public MessagePopSchedule(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,"+dataSourceEntity.getDbId()+"as dbid\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();
    }
}