派生自 projectDept/qhighschool

EricsHu
2022-12-05 068fc7f2e81178e55fa191a13709af64b1a163f6
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
package com.qxueyou.scc.msg.service.impl;
 
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
 
import org.springframework.stereotype.Service;
 
import com.alibaba.fastjson.JSON;
import com.qxueyou.scc.base.model.Pager;
import com.qxueyou.scc.base.model.Result;
import com.qxueyou.scc.base.service.impl.CommonAppService;
import com.qxueyou.scc.base.util.ClientUtils;
import com.qxueyou.scc.base.util.CollectionUtils;
import com.qxueyou.scc.base.util.TraceUtils;
import com.qxueyou.scc.msg.model.MsgInfo;
import com.qxueyou.scc.msg.service.IMsgInfoService;
 
/**
 * 消息服务
 * 
 * @author kevin
 *
 */
@Service
public class MsgInfoService extends CommonAppService implements IMsgInfoService {
    
    @Override
    public List<MsgInfo> list(Short type, Integer pageSize, Integer pageNum) {
        StringBuffer hql = new StringBuffer();
        hql.append("from MsgInfo where receiverId=?  ");
        List<Object> params =CollectionUtils.newList(ClientUtils.getUserId());
        
        if(type!=null){
            hql.append(" and type = ? ");
            params.add(type);
        }
        
        hql.append(" and deleteFlag is false order by createTime desc");
                
        return findList(hql.toString(),new Pager(pageSize, pageNum),params, MsgInfo.class);
    }
 
    @Override
    public int listCount(Short type) {
        StringBuffer hql = new StringBuffer();
        hql.append("from MsgInfo where receiverId=?  ");
        List<Object> params =CollectionUtils.newList(ClientUtils.getUserId());
        
        if(type!=null){
            hql.append(" and type = ? ");
            params.add(type);
        }
        
        hql.append(" and deleteFlag is false");
        
        return findCount(hql.toString(),params);
    }
 
    @Override
    public Result deleteUserMsg(String [] msgId) {
        return bulkUpdateInLoop("update MsgInfo set deleteFlag = true where msgId=?", msgId);
    }
 
    @Override
    public Result deleteAllUserMsg(Short type) {
        return this.bulkUpdate("update MsgInfo set deleteFlag = true where deleteFlag is false and receiverId=? and type=?", 
                new Object[]{ClientUtils.getUserId(),type==null?10000:type});
    }
 
    @Override
    public Result updateReadStatus(String [] msgIds) {
        return bulkUpdateInLoop("update MsgInfo set readFlag = 1 where msgId=?", msgIds);
    }
    
    @Override
    public Result updateUserAllReadStatus(Short type) {
        return this.bulkUpdate("update MsgInfo set readFlag = 1 where deleteFlag is false and receiverId=? and type=?", 
                new Object[]{ClientUtils.getUserId(),type==null?10000:type});
    }
    
    @Override
    public Result doSendTextMsgToUsers(String[] userIdsToArr,short type,String msg) {
        return doSendTextMsgToUsers(userIdsToArr,type,msg,CollectionUtils.newStringMap("msg",msg));
    }
 
    @Override
    public Result doSendTextMsgToUsers(String[] userIdsToArr,short type,String msg,Map<String,String> attrs) {
        return doSendtMsgToTargets(userIdsToArr,type,attrs,CollectionUtils.newStringMap("msg",msg));
    }
    
    
    /**
     * 给指定目标发送消息
     * @param userIdFrom
     * @param targetIdsToArr
     * @param msg
     * @param attrs
     * @param targetType
     * @param msgInfo
     * @return
     */
    private Result doSendtMsgToTargets(String[] targetIdsToArr,short type,Map<String, String> attrs,Map<String,String> msgInfo){
        if(targetIdsToArr== null || targetIdsToArr.length==0){
            return new Result(false,"参数错误");
        }
        
        List<MsgInfo> lstMsg = new ArrayList<MsgInfo>(targetIdsToArr.length);
        for(String receiverId:targetIdsToArr){
            MsgInfo tempInfo  = new MsgInfo();
            tempInfo.setType(type);
            tempInfo.setReadFlag(String.valueOf(MsgInfo.READ_NOT));
            tempInfo.setContent(JSON.toJSONString(CollectionUtils.newObjectMap("ext", attrs,"msg", msgInfo)));
            tempInfo.setReceiverId(receiverId);
            TraceUtils.setCreateTrace(tempInfo);
            lstMsg.add(tempInfo);
        }
        return this.saveOrUpdateAll(lstMsg);
    }
    
    
    /**
     * 分批发送通知
     * 
     * @param userIdList
     * @param msg
     * @param extra
     * @return
     */
    public void doSendTextMsgToUsersLoop(List<String> userIdList,short type,String msg, Map<String,String> extra) {
        if(userIdList==null || userIdList.size()==0){
            return ;
        }
        String [] targetIds = null;
        for(int i=0;i<userIdList.size();i+=20){
            if((userIdList.size()-i)>20){
                targetIds = userIdList.subList(i, i+20).toArray(new String [20]);
            }else{
                targetIds = userIdList.subList(i,userIdList.size()).toArray(new String[userIdList.size()-i]);
            }
            this.doSendtMsgToTargets(targetIds,type,extra,CollectionUtils.newStringMap("msg",msg));
        }
    }
    
    @Override
    public  MsgInfo  readLastRemindMsg(){
    return     this.findUnique("from MsgInfo where receiverId=? and type=? and deleteFlag is false order by createTime desc",
                CollectionUtils.newList(ClientUtils.getUserId(),MsgInfo.TYPE_REMIND), MsgInfo.class);
    }
    
    @Override
    public Map<String,Object> queryUnReadMsgCountByType(){
        List<Map<String,Object>> resultList = this.findListWithMapByHql("select type as type ,count(1) as count from MsgInfo where receiverId=:receiverId and readFlag=:readFlag and deleteFlag is false group by type",
                        CollectionUtils.newObjectMap("receiverId",ClientUtils.getUserId(),"readFlag",String.valueOf(MsgInfo.READ_NOT)));
        
        Map<String,Object> result = null;
        if(resultList != null && resultList.size()>0){
            result = new HashMap<String,Object>(resultList.size());
            for(Map<String,Object> map:resultList){
                result.put(String.valueOf(map.get("type")), map.get("count"));
            }
        }
        
        return result;
    }
    
    @Override
    public int queryUnReadTotalCount(){
        return this.findCountByComplexHql("from MsgInfo where receiverId=:receiverId and readFlag=:readFlag and deleteFlag is false",
                        CollectionUtils.newObjectMap("receiverId",ClientUtils.getUserId(),"readFlag",String.valueOf(MsgInfo.READ_NOT)));
    }    
    
}