UserOutboundServiceImpl.java 13 KB

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