1
0
mirror of https://github.com/chatopera/cosin.git synced 2025-08-01 16:38:02 +08:00

Fix AgentUserProxy

This commit is contained in:
dengchao@xgtl 2020-04-16 11:02:47 +08:00
parent 1bbf4c90b7
commit 4c4809cce1

View File

@ -26,13 +26,16 @@ import com.chatopera.cc.persistence.es.ContactsRepository;
import com.chatopera.cc.persistence.repository.*; import com.chatopera.cc.persistence.repository.*;
import com.chatopera.cc.socketio.message.Message; import com.chatopera.cc.socketio.message.Message;
import freemarker.template.TemplateException; import freemarker.template.TemplateException;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang.StringUtils; import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort; import org.springframework.data.domain.Sort;
import org.springframework.http.HttpStatus;
import org.springframework.lang.NonNull;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
import org.springframework.ui.ModelMap; import org.springframework.ui.ModelMap;
import org.springframework.web.server.ResponseStatusException;
import org.springframework.web.servlet.ModelAndView; import org.springframework.web.servlet.ModelAndView;
import javax.servlet.http.Cookie; import javax.servlet.http.Cookie;
@ -42,6 +45,7 @@ import java.io.IOException;
import java.util.*; import java.util.*;
@Component @Component
@RequiredArgsConstructor
public class AgentUserProxy { public class AgentUserProxy {
private final static Logger logger = LoggerFactory.getLogger(AgentUserProxy.class); private final static Logger logger = LoggerFactory.getLogger(AgentUserProxy.class);
@ -60,57 +64,51 @@ public class AgentUserProxy {
// 转接聊天 // 转接聊天
private final static String AUTH_KEY_AUDIT_TRANS = "A13_A01_A03"; private final static String AUTH_KEY_AUDIT_TRANS = "A13_A01_A03";
@Autowired @NonNull
private ACDPolicyService acdPolicyService; private final ACDPolicyService acdPolicyService;
@Autowired @NonNull
private AgentUserRepository agentUserRes; private final AgentUserRepository agentUserRes;
@Autowired @NonNull
private RoleAuthRepository roleAuthRes; private final RoleAuthRepository roleAuthRes;
@Autowired @NonNull
private UserRepository userRes; private final UserRepository userRes;
@Autowired @NonNull
private UserRoleRepository userRoleRes; private final UserRoleRepository userRoleRes;
@Autowired @NonNull
private AgentServiceRepository agentServiceRes; private final AgentServiceRepository agentServiceRes;
@Autowired @NonNull
private SNSAccountRepository snsAccountRes; private final SNSAccountRepository snsAccountRes;
@Autowired @NonNull
private AgentServiceProxy agentServiceProxy; private final AgentServiceProxy agentServiceProxy;
@Autowired @NonNull
private ContactsRepository contactsRes; private final ContactsRepository contactsRes;
@Autowired @NonNull
private OnlineUserRepository onlineUserRes; private final OnlineUserRepository onlineUserRes;
@Autowired @NonNull
private AgentUserContactsRepository agentUserContactsRes; private final AgentUserContactsRepository agentUserContactsRes;
@Autowired @NonNull
private Cache cache; private final Cache cache;
@Autowired @NonNull
private AgentStatusRepository agentStatusRes; private final AgentStatusRepository agentStatusRes;
@Autowired @NonNull
private PeerSyncIM peerSyncIM; private final PeerSyncIM peerSyncIM;
/** /**
* 与联系人主动聊天前查找获取AgentUser * 与联系人主动聊天前查找获取AgentUser
*
* @param channels
* @param contactid
* @param logined
* @return
* @throws CSKefuException
*/ */
public AgentUser figureAgentUserBeforeChatWithContactInfo(final String channels, final String contactid, final User logined) throws CSKefuException { public AgentUser figureAgentUserBeforeChatWithContactInfo(final String channels, final String contactid, final User logined) throws CSKefuException {
// 聊天依赖的对象 // 聊天依赖的对象
@ -123,17 +121,19 @@ public class AgentUserProxy {
String channel = StringUtils.split(channels, ",")[0]; String channel = StringUtils.split(channels, ",")[0];
// 查找联系人 // 查找联系人
final Contacts contact = contactsRes.findOne(contactid); final Contacts contact = contactsRes.findById(contactid)
.orElseThrow(() -> {
String reason = String.format("Contacts %s not found", contactid);
return new ResponseStatusException(HttpStatus.NOT_FOUND, reason);
});
// 查找 OnlineUser // 查找 OnlineUser
onlineUser = onlineUserRes.findOneByContactidAndOrigAndChannel( onlineUser = onlineUserRes.findOneByContactidAndOrigAndChannel(contactid, logined.getOrgi(), channel)
contactid, logined.getOrgi(), channel).orElseGet(() -> { .orElseGet(() -> OnlineUserProxy.createNewOnlineUserWithContactAndChannel(contact, logined, channel));
return OnlineUserProxy.createNewOnlineUserWithContactAndChannel(contact, logined, channel);
});
// 查找满足条件的 AgentUser // 查找满足条件的 AgentUser
final Optional<AgentUser> op = agentUserRes.findOneByContactIdAndChannelAndOrgi(contactid, channel, final Optional<AgentUser> op = agentUserRes.findOneByContactIdAndChannelAndOrgi(contactid, channel,
logined.getOrgi()); logined.getOrgi());
if (op.isPresent()) { if (op.isPresent()) {
if (StringUtils.equals(op.get().getAgentno(), logined.getId())) { if (StringUtils.equals(op.get().getAgentno(), logined.getId())) {
agentUser = op.get(); agentUser = op.get();
@ -159,11 +159,11 @@ public class AgentUserProxy {
outMessage.setChannelMessage(agentUser); outMessage.setChannelMessage(agentUser);
outMessage.setAgentUser(agentUser); outMessage.setAgentUser(agentUser);
peerSyncIM.send(MainContext.ReceiverType.AGENT, peerSyncIM.send(MainContext.ReceiverType.AGENT,
MainContext.ChannelType.WEBIM, MainContext.ChannelType.WEBIM,
agentUser.getAppid(), agentUser.getAppid(),
MainContext.MessageType.NEW, MainContext.MessageType.NEW,
logined.getId(), logined.getId(),
outMessage, true); outMessage, true);
} else { } else {
logger.info("[figureAgentUserBeforeChatWithContactInfo] agent user is null"); logger.info("[figureAgentUserBeforeChatWithContactInfo] agent user is null");
} }
@ -174,16 +174,6 @@ public class AgentUserProxy {
/** /**
* 聊天列表数据 * 聊天列表数据
*
* @param view
* @param map
* @param request
* @param response
* @param sort
* @param logined
* @param orgi
* @throws IOException
* @throws TemplateException
*/ */
public void buildIndexViewWithModels( public void buildIndexViewWithModels(
final ModelAndView view, final ModelAndView view,
@ -207,7 +197,7 @@ public class AgentUserProxy {
} }
} }
if (StringUtils.isNotBlank(sort)) { if (StringUtils.isNotBlank(sort)) {
List<Sort.Order> list = new ArrayList<Sort.Order>(); List<Sort.Order> list = new ArrayList<>();
if (sort.equals("lastmessage")) { if (sort.equals("lastmessage")) {
list.add(new Sort.Order(Sort.Direction.DESC, "status")); list.add(new Sort.Order(Sort.Direction.DESC, "status"));
list.add(new Sort.Order(Sort.Direction.DESC, "lastmessage")); list.add(new Sort.Order(Sort.Direction.DESC, "lastmessage"));
@ -215,20 +205,20 @@ public class AgentUserProxy {
list.add(new Sort.Order(Sort.Direction.DESC, "status")); list.add(new Sort.Order(Sort.Direction.DESC, "status"));
list.add(new Sort.Order(Sort.Direction.DESC, "createtime")); list.add(new Sort.Order(Sort.Direction.DESC, "createtime"));
} else if (sort.equals("default")) { } else if (sort.equals("default")) {
defaultSort = new Sort(Sort.Direction.DESC, "status"); defaultSort = Sort.by(Sort.Direction.DESC, "status");
Cookie name = new Cookie("sort", null); Cookie name = new Cookie("sort", null);
name.setMaxAge(0); name.setMaxAge(0);
response.addCookie(name); response.addCookie(name);
} }
if (list.size() > 0) { if (list.size() > 0) {
defaultSort = new Sort(list); defaultSort = Sort.by(list);
Cookie name = new Cookie("sort", sort); Cookie name = new Cookie("sort", sort);
name.setMaxAge(60 * 60 * 24 * 365); name.setMaxAge(60 * 60 * 24 * 365);
response.addCookie(name); response.addCookie(name);
map.addAttribute("sort", sort); map.addAttribute("sort", sort);
} }
} else { } else {
defaultSort = new Sort(Sort.Direction.DESC, "status"); defaultSort = Sort.by(Sort.Direction.DESC, "status");
} }
List<AgentUser> agentUserList = agentUserRes.findByAgentnoAndOrgi( List<AgentUser> agentUserList = agentUserRes.findByAgentnoAndOrgi(
@ -262,16 +252,12 @@ public class AgentUserProxy {
if (agentUserList.size() > 0) { if (agentUserList.size() > 0) {
view.addObject("agentUserList", agentUserList); view.addObject("agentUserList", agentUserList);
agentServiceProxy.bundleDialogRequiredDataInView(view, agentServiceProxy.bundleDialogRequiredDataInView(view,
map, agentUserList.get(0), orgi, logined); map, agentUserList.get(0), orgi, logined);
} }
} }
/** /**
* 获得坐席访客会话相关的订阅者会话监控人员 * 获得坐席访客会话相关的订阅者会话监控人员
*
* @param orgi
* @param agentUser
* @return
*/ */
public HashMap<String, String> getAgentUserSubscribers(final String orgi, final AgentUser agentUser) { public HashMap<String, String> getAgentUserSubscribers(final String orgi, final AgentUser agentUser) {
HashMap<String, String> result = new HashMap<>(); HashMap<String, String> result = new HashMap<>();
@ -313,10 +299,6 @@ public class AgentUserProxy {
/** /**
* 根据指定的KEY从数据库加载权限数据 * 根据指定的KEY从数据库加载权限数据
*
* @param key
* @param bypass
* @param result
*/ */
private void loadPermissionsFromDB(final String orgi, final String key, final HashSet<String> bypass, final HashMap<String, String> result) { private void loadPermissionsFromDB(final String orgi, final String key, final HashSet<String> bypass, final HashMap<String, String> result) {
List<RoleAuth> roleAuths = roleAuthRes.findByDicvalueAndOrgi(key, orgi); List<RoleAuth> roleAuths = roleAuthRes.findByDicvalueAndOrgi(key, orgi);
@ -332,21 +314,14 @@ public class AgentUserProxy {
/** /**
* 同时增加权限 * 同时增加权限
*
* @param user
* @param permissions
* @param result
*/ */
@SuppressWarnings("SameParameterValue")
private void addPermissions(final String user, final String permissions, final HashMap<String, String> result) { private void addPermissions(final String user, final String permissions, final HashMap<String, String> result) {
result.put(user, permissions); result.put(user, permissions);
} }
/** /**
* 增加权限 * 增加权限
*
* @param userId
* @param authKey
* @param result
*/ */
private void addPermission(final String userId, final String authKey, final HashMap<String, String> result) { private void addPermission(final String userId, final String authKey, final HashMap<String, String> result) {
if (!result.containsKey(userId)) { if (!result.containsKey(userId)) {
@ -378,15 +353,6 @@ public class AgentUserProxy {
/** /**
* 创建AgentUser * 创建AgentUser
*
* @param onlineUser
* @param contact
* @param agent
* @param channel
* @param status
* @param creator
* @return
* @throws CSKefuException
*/ */
public AgentUser createAgentUserWithContactAndAgentAndChannelAndStatus( public AgentUser createAgentUserWithContactAndAgentAndChannelAndStatus(
final OnlineUser onlineUser, final OnlineUser onlineUser,
@ -450,23 +416,12 @@ public class AgentUserProxy {
/** /**
* 查找AgentUser * 查找AgentUser
* 先从缓存查找再从数据库查找 * 先从缓存查找再从数据库查找
*
* @param userid
* @param agentuserid
* @param orgi
* @return
* @throws CSKefuException
*/ */
public AgentUser resolveAgentUser(final String userid, final String agentuserid, final String orgi) throws CSKefuException { public AgentUser resolveAgentUser(final String userid, final String agentuserid, final String orgi) throws CSKefuException {
Optional<AgentUser> opt = cache.findOneAgentUserByUserIdAndOrgi(userid, orgi); Optional<AgentUser> opt = cache.findOneAgentUserByUserIdAndOrgi(userid, orgi);
if (!opt.isPresent()) { if (!opt.isPresent()) {
AgentUser au = agentUserRes.findOne(agentuserid); return agentUserRes.findById(agentuserid)
if (au == null) { .orElseThrow(() -> new CSKefuException("Invalid transfer request, agent user not exist."));
throw new CSKefuException("Invalid transfer request, agent user not exist.");
} else {
return au;
}
} }
return opt.get(); return opt.get();
} }
@ -474,9 +429,6 @@ public class AgentUserProxy {
/** /**
* 更新坐席当前服务中的用户状态 * 更新坐席当前服务中的用户状态
* #TODO 需要分布式锁 * #TODO 需要分布式锁
*
* @param agentStatus
* @param orgi
*/ */
public synchronized void updateAgentStatus(AgentStatus agentStatus, String orgi) { public synchronized void updateAgentStatus(AgentStatus agentStatus, String orgi) {
int users = cache.getInservAgentUsersSizeByAgentnoAndOrgi(agentStatus.getAgentno(), orgi); int users = cache.getInservAgentUsersSizeByAgentnoAndOrgi(agentStatus.getAgentno(), orgi);
@ -487,18 +439,13 @@ public class AgentUserProxy {
/** /**
* 使用AgentUser查询 * 使用AgentUser查询
*
* @param id
* @return
*/ */
public Optional<AgentUser> findOne(final String id) { public Optional<AgentUser> findOne(final String id) {
return Optional.ofNullable(agentUserRes.findOne(id)); return agentUserRes.findById(id);
} }
/** /**
* 保存 * 保存
*
* @param agentUser
*/ */
public void save(final AgentUser agentUser) { public void save(final AgentUser agentUser) {
agentUserRes.save(agentUser); agentUserRes.save(agentUser);