UserOutboundServiceImpl.java 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. package com.goafanti.customer.service.impl;
  2. import com.alibaba.fastjson.JSON;
  3. import com.alibaba.fastjson.JSONObject;
  4. import com.goafanti.common.bo.Error;
  5. import com.goafanti.common.bo.InputCallCompleter;
  6. import com.goafanti.common.bo.Result;
  7. import com.goafanti.common.dao.*;
  8. import com.goafanti.common.model.*;
  9. import com.goafanti.common.utils.DateUtils;
  10. import com.goafanti.common.utils.HttpUtils;
  11. import com.goafanti.common.utils.LoggerUtils;
  12. import com.goafanti.common.utils.StringUtils;
  13. import com.goafanti.core.mybatis.BaseMybatisDao;
  14. import com.goafanti.core.mybatis.page.Pagination;
  15. import com.goafanti.core.websocket.SystemWebSocketHandler;
  16. import com.goafanti.customer.bo.InputCallNumber;
  17. import com.goafanti.customer.bo.InputUserCallList;
  18. import com.goafanti.customer.service.UserOutboundService;
  19. import org.apache.shiro.crypto.hash.SimpleHash;
  20. import org.springframework.stereotype.Service;
  21. import org.springframework.web.socket.TextMessage;
  22. import javax.annotation.Resource;
  23. import java.util.HashMap;
  24. import java.util.List;
  25. import java.util.Map;
  26. @Service("userOutboundService")
  27. public class UserOutboundServiceImpl extends BaseMybatisDao<CallLogMapper> implements UserOutboundService {
  28. static final String customer="C322";
  29. static final String seq="20241101";
  30. static final String pwd="084332D02CCF7B42461F34155AC8754F";
  31. static final String version= "2.0.6";
  32. static final String default_url ="https://webmc.zb-sx.cn:1443";
  33. @Resource
  34. private SystemWebSocketHandler systemWebSocketHandler;
  35. @Resource
  36. private UserMapper userMapper;
  37. @Resource
  38. private UserMidMapper userMidMapper;
  39. @Resource
  40. private AdminMapper adminMapper;
  41. @Resource
  42. private OrganizationContactBookMapper organizationContactBookMapper;
  43. @Resource
  44. private CallLogMapper callLogMapper;
  45. @Override
  46. public Object checkUser(Integer type) {
  47. String url= default_url +"/openapi/"+version+"/getClientInfo";
  48. Map<String,Object> param=new HashMap<>();
  49. param.put("authentication",getAuthentication());
  50. Map<String,Object> request=new HashMap<>();
  51. request.put("type",type);
  52. param.put("request",request);
  53. HttpUtils httpUtils=new HttpUtils();
  54. String s = httpUtils.sendHttpsRequest(url, "POST", param);
  55. System.out.println(s);
  56. return null;
  57. }
  58. private Map<String, Object> getAuthentication() {
  59. Map<String,Object> authentication=new HashMap<>();
  60. authentication.put("customer","C322");
  61. long timeMillis = System.currentTimeMillis();
  62. authentication.put("timestamp",timeMillis);
  63. authentication.put("seq",seq);
  64. String digestSource=customer+"@"+timeMillis+"@"+seq+"@"+pwd;
  65. // System.out.println("digestSource="+digestSource);
  66. String md5 = new SimpleHash("MD5", digestSource).toHex();
  67. // System.out.println("md5="+md5);
  68. authentication.put("digest",md5);
  69. return authentication;
  70. }
  71. @Override
  72. public Result pushCallNumber(InputCallNumber in) {
  73. String msg =null;
  74. if (StringUtils.isNotBlank(in.getUid())){
  75. User u =userMapper.queryById(in.getUid());
  76. if (u.getCallStatus()==1){
  77. return new Result().error(new Error("该用户正在通话中"));
  78. }
  79. }
  80. String url= default_url +"/openapi/"+version+"/callNumber";
  81. Map<String,Object> param=new HashMap<>();
  82. param.put("authentication",getAuthentication());
  83. Map<String,Object> request=new HashMap<>();
  84. request.put("agent",in.getAgent());
  85. request.put("callee",in.getCallee());
  86. request.put("userData",in.getUid());
  87. param.put("request",request);
  88. HttpUtils httpUtils=new HttpUtils();
  89. String s = httpUtils.sendHttpsRequest(url, "POST", param);
  90. System.out.println("url="+url);
  91. System.out.println("param="+param);
  92. System.out.println(s);
  93. // {"result":{"error":"0xF1000022","msg":"分机号未注册"},"data":{"seq":"20241101","response":{"seq":null,"userData":null,"result":{"error":4043309090},"data":null}}}
  94. JSONObject jsonObject = JSON.parseObject(s);
  95. JSONObject result = jsonObject.getJSONObject("result");
  96. msg = result.getString("msg");
  97. if (StringUtils.isNotBlank(msg)){
  98. if (msg.equals("成功")){
  99. if (StringUtils.isNotBlank(in.getUid())){
  100. User u =new User();
  101. u.setId(in.getUid());
  102. u.setCallStatus(1);
  103. userMapper.update(u);
  104. }
  105. return new Result().data("呼叫成功");
  106. }
  107. return new Result().error(new Error(msg));
  108. }
  109. return new Result().error(new Error("通信异常"));
  110. }
  111. @Override
  112. public Result blindTransferByAgent(InputCallNumber in) {
  113. String url= default_url +"/openapi/"+version+"/blindTransferByAgent";
  114. Map<String,Object> param=new HashMap<>();
  115. param.put("authentication",getAuthentication());
  116. Map<String,Object> request=new HashMap<>();
  117. request.put("agent",in.getAgent());
  118. request.put("transAgent",in.getTransAgent());
  119. request.put("userData",in.getUid());
  120. param.put("request",request);
  121. HttpUtils httpUtils=new HttpUtils();
  122. String s = httpUtils.sendHttpsRequest(url, "POST", param);
  123. LoggerUtils.debug(getClass(),"res="+s);
  124. JSONObject jsonObject = JSON.parseObject(s);
  125. JSONObject result = jsonObject.getJSONObject("result");
  126. String msg = result.getString("msg");
  127. String error = result.getString("error");
  128. if ("0".equals(error)){
  129. return new Result().data("转移成功");
  130. }else {
  131. LoggerUtils.debug(getClass(),"msg="+msg);
  132. return new Result().error(new Error(msg));
  133. }
  134. }
  135. @Override
  136. public Object pushCallCompleted(InputCallCompleter in) {
  137. Map<String, Object> notify = in.getNotify();
  138. String staffNo=String.valueOf(notify.get("staffNo"));
  139. Admin admin = adminMapper.selectByCallStaffNo(staffNo);
  140. String startTime = String.valueOf(notify.get("startTime"));
  141. String callee=String.valueOf(notify.get("callee"));
  142. String timeLength=String.valueOf(notify.get("timeLength"));
  143. String callResult=String.valueOf(notify.get("callResult"));
  144. String callResultMsg=String.valueOf(notify.get("callResultMsg"));
  145. String userDate=String.valueOf(notify.get("userData"));
  146. String name="暂无";
  147. String id=null;
  148. if(admin!=null){
  149. name=admin.getName();
  150. id=admin.getId();
  151. }
  152. String ContactName=null;
  153. String uid=null;
  154. if (userDate!=null){
  155. uid=userDate;
  156. }
  157. List<OrganizationContactBook> books = organizationContactBookMapper.selectUserByContact(callee);
  158. if (books!=null){
  159. if (uid==null){
  160. OrganizationContactBook book1 = books.get(0);
  161. ContactName=book1.getName();
  162. uid=book1.getUid();
  163. }else {
  164. for (OrganizationContactBook item:books){
  165. if (item.getUid().equals(uid)){
  166. ContactName=item.getName();
  167. break;
  168. }
  169. }
  170. }
  171. }
  172. CallLog callLog=new CallLog();
  173. callLog.setUid(uid);
  174. callLog.setAid(id);
  175. callLog.setAdminName(name);
  176. callLog.setContacts(ContactName);
  177. callLog.setCreateTime(DateUtils.parseDate(startTime));
  178. String substring = callee.substring(callee.length() - 4);
  179. callLog.setMobileLastDigits(substring);
  180. callLog.setDuration(Integer.parseInt(timeLength));
  181. callLog.setStatus(0);
  182. callLog.setRemarks(callResultMsg);
  183. callLogMapper.insert(callLog);
  184. pushCountUser(uid);
  185. //需要关闭客户呼叫中
  186. // SseResult res = SseMap.sseEmitterMap.get(TokenManager.getAdminId());
  187. try {
  188. // res.sseEmitter.send(callResult);
  189. systemWebSocketHandler.sendMessageToUser(admin.getId(), new TextMessage(callResult));
  190. // LoggerUtils.debug(getClass(),String.format("通信成功=》%s=%s",callResult,callResultMsg));
  191. }catch (Exception e ){
  192. LoggerUtils.error(getClass(),"通信失败");
  193. }
  194. // finally {
  195. // res.sseEmitter.complete();
  196. // SseMap.sseEmitterMap.remove(TokenManager.getAdminId());
  197. // }
  198. return 1;
  199. }
  200. /**
  201. * 客户通话时长统计
  202. * @param uid
  203. */
  204. private void pushCountUser(String uid) {
  205. List<CallLog> callLogList = callLogMapper.selectByUid(uid);
  206. int count=0;
  207. int duration=0;
  208. for (CallLog log : callLogList) {
  209. count++;
  210. duration+=log.getDuration();
  211. }
  212. UserMid mid=new UserMid();
  213. mid.setUid(uid);
  214. mid.setCallCount(count);
  215. mid.setCallDuration(duration);
  216. userMidMapper.updateByUid(mid);
  217. User u =new User();
  218. u.setId(uid);
  219. u.setCallStatus(0);
  220. userMapper.update(u);
  221. }
  222. @Override
  223. public Object agentList() {
  224. return adminMapper.agentList();
  225. }
  226. @Override
  227. public Object getDefaultMobile(String uid) {
  228. return organizationContactBookMapper.getMajor(uid,null);
  229. }
  230. @Override
  231. public Object userCallList(InputUserCallList in) {
  232. CallLog select=new CallLog();
  233. select.setUid(in.getUid());
  234. Pagination<?> page = findPage("findCallLogList", "findCallLogCount", select, in.getPageNo(), in.getPageSize());
  235. UserMid userMid = userMidMapper.selectByUid(in.getUid());
  236. Map<String,Object> sumMap = new HashMap();
  237. int count=0;
  238. int duration=0;
  239. if (userMid!=null){
  240. User user = userMapper.queryById(in.getUid());
  241. count=userMid.getCallCount();
  242. duration=userMid.getCallDuration();
  243. sumMap.put("callCount",count);
  244. sumMap.put("callDuration",duration);
  245. sumMap.put("name",user.getNickname());
  246. }
  247. Map<String,Object> res = new HashMap();
  248. res.put("page",page);
  249. res.put("sum",sumMap);
  250. return res;
  251. }
  252. }