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)));
|
}
|
|
}
|