TelephoneRobotServiceImpl.java 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. package com.ruixuan.salesLeads.service.impl;
  2. import com.alibaba.excel.EasyExcel;
  3. import com.alibaba.fastjson.JSON;
  4. import com.alibaba.fastjson.JSONArray;
  5. import com.alibaba.fastjson.JSONObject;
  6. import com.github.pagehelper.PageInfo;
  7. import com.google.gson.JsonObject;
  8. import com.ruixuan.common.core.domain.ResultResponse;
  9. import com.ruixuan.common.core.page.TableDataInfo;
  10. import com.ruixuan.common.core.redis.RedisCache;
  11. import com.ruixuan.common.utils.DateUtils;
  12. import com.ruixuan.common.utils.PageUtils;
  13. import com.ruixuan.common.utils.StringUtils;
  14. import com.ruixuan.common.utils.file.FileTypeUtils;
  15. import com.ruixuan.common.utils.http.HttpUtil;
  16. import com.ruixuan.salesLeads.constant.RobotConstant;
  17. import com.ruixuan.salesLeads.listener.ImportExcelListener;
  18. import com.ruixuan.salesLeads.mapper.*;
  19. import com.ruixuan.salesLeads.pojo.*;
  20. import com.ruixuan.salesLeads.pojo.vo.SaleRecordVo;
  21. import com.ruixuan.salesLeads.pojo.vo.SaleTaskInfoVo;
  22. import com.ruixuan.salesLeads.service.ISaleClueInfoService;
  23. import com.ruixuan.salesLeads.service.ITelephoneRobotService;
  24. import com.ruixuan.salesLeads.utils.CsvImportUtil;
  25. import com.ruixuan.system.domain.SysAreaInfo;
  26. import com.ruixuan.system.domain.SysIndustryInfo;
  27. import com.ruixuan.system.mapper.SysAreaInfoMapper;
  28. import com.ruixuan.system.mapper.SysIndustryInfoMapper;
  29. import lombok.extern.slf4j.Slf4j;
  30. import org.apache.ibatis.annotations.Param;
  31. import org.springframework.beans.factory.annotation.Autowired;
  32. import org.springframework.beans.factory.annotation.Value;
  33. import org.springframework.stereotype.Service;
  34. import org.springframework.transaction.annotation.Transactional;
  35. import org.springframework.web.multipart.MultipartFile;
  36. import java.util.*;
  37. import java.util.concurrent.TimeUnit;
  38. import java.util.stream.Collectors;
  39. /**
  40. * ***************************************************
  41. *
  42. * @Auther: zianY
  43. * @Descipion: 外呼机器人api
  44. * @CreateDate: 2022-07-01
  45. * ****************************************************
  46. */
  47. @Service
  48. @Slf4j
  49. public class TelephoneRobotServiceImpl implements ITelephoneRobotService {
  50. @Value("${telephoneRobot.appKey}")
  51. private String rebotAppkey;
  52. @Value("${telephoneRobot.secret}")
  53. private String rebotSecret;
  54. @Autowired
  55. private RedisCache redisCache;
  56. @Autowired
  57. private SaleClueInfoMapper saleClueInfoMapper;
  58. @Autowired
  59. private SaleClueCallRecordMapper saleClueCallRecordMapper;
  60. @Autowired
  61. private SaleClueCallResultMapper saleClueCallResultMapper;
  62. /**
  63. *
  64. * @description: 外呼机器人获取token
  65. *
  66. * @param
  67. * @return: com.ruixuan.common.core.domain.ResultResponse
  68. * @author: zianY
  69. */
  70. @Override
  71. public JSONObject getTelephoneRobotToken(){
  72. JSONObject tokenObj = redisCache.getCacheObject("salesLeads:robotToken:str");
  73. if (!StringUtils.isEmpty(tokenObj)){
  74. log.info("外呼机器人获取redis中的token信息----->>>{}",tokenObj);
  75. return tokenObj;
  76. }
  77. Map<String,Object> params = new HashMap<>();
  78. params.put("appKey",rebotAppkey);
  79. params.put("secret",rebotSecret);
  80. /*Map<String,String> headers = new HashMap<>();
  81. params.put("Auth-Token",);
  82. */
  83. String content = HttpUtil.httpPostRequest(RobotConstant.REFRESH_TOKEN,params,null);
  84. JSONObject jsonObject = JSONObject.parseObject(content);
  85. redisCache.setCacheObject("salesLeads:robotToken:str",jsonObject,2, TimeUnit.HOURS);
  86. log.info("外呼机器人获取token信息----->>>{}",jsonObject);
  87. return jsonObject;
  88. }
  89. /**
  90. *
  91. * @description: 外呼机器人-导入号码-master
  92. *
  93. * @param sellerIds
  94. * @param userId
  95. * @return: com.ruixuan.common.core.domain.ResultResponse
  96. * @author: zianY
  97. */
  98. @Override
  99. @Transactional
  100. public ResultResponse importRobotTelephone(List<Long> sellerIds,String userId) throws Exception{
  101. Map<String,String> headers = new HashMap<>();
  102. Map<String,Object> result = new HashMap<>();
  103. //日期8位数 自增
  104. String flowCode = redisCache.getFlowCode();
  105. log.info("task------>>>>{}",flowCode);
  106. result.put("taskId",Integer.valueOf(flowCode));
  107. result.put("batchId",Integer.valueOf(flowCode));
  108. result.put("callBackUrl",RobotConstant.CALL_BACK_URL);
  109. result.put("canCover",true);
  110. List customerList = new ArrayList();
  111. for (Long sellerId : sellerIds) {
  112. //SaleRecordVo saleInfo = saleClueInfoMapper.selectSaleClueInfoBySellerId(sellerId);
  113. //销售线索id查询
  114. SaleRecordVo saleInfo = saleClueInfoMapper.selectSaleClueInfoById(sellerId.intValue());
  115. if (saleInfo == null){
  116. log.info("销售线索查询为空或已导入过联系方式---》》》线索id---{}",sellerId);
  117. continue;
  118. }
  119. Map<String,Object> param = new HashMap<>();
  120. param.put("id",saleInfo.getId());
  121. param.put("name",saleInfo.getSellerId());
  122. param.put("companyName",saleInfo.getSellerName());
  123. param.put("phone",saleInfo.getTelephone());
  124. param.put("website","");
  125. //修改线索的导入记录状态
  126. saleClueInfoMapper.updateSaleClueImportPhoneFlag(sellerId,1);
  127. customerList.add(param);
  128. }
  129. if (customerList.isEmpty()){
  130. return ResultResponse.error("销售线索查询为空或已导入。");
  131. }
  132. result.put("customerList",customerList);
  133. //headers.put("Auth-Token","eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJhdWQiOiJndW95YW5saW5nIiwiZXhwIjoxNjU4MTMyMjEyfQ.OyhpastRRzBxAlSc8JOfdxO3rK3NG592U3Qlh752xDQ");
  134. JSONObject tokenObj = redisCache.getCacheObject("salesLeads:robotToken:str");
  135. if (!StringUtils.isEmpty(tokenObj)){
  136. log.info("导入时获取redis中的token信息----->>>{}",tokenObj);
  137. }else {
  138. tokenObj = getTelephoneRobotToken();
  139. log.info("导入时redis中的token信息已失效,重新获取token----->>>{}");
  140. }
  141. headers.put("Auth-Token",JSONObject.parseObject(tokenObj.getString("data")).getString("token"));
  142. String content = HttpUtil.httpPostRequest(RobotConstant.CUSTOMER_IMPORT,result,headers);
  143. log.info("调用第三方接口------入参---{}-----返回信息---》》》》{}",result,content);
  144. JSONObject jsonObject = JSONObject.parseObject(content);
  145. if (!"0".equals(jsonObject.getString("code"))){
  146. log.info("导入数据接口异常======>>>>>{}"+jsonObject);
  147. return ResultResponse.error("访问过于频繁,请稍后再试。");
  148. }
  149. Thread.sleep(500);
  150. //更新最新的记录 用户信息
  151. //1分钟只能调取导入接口一次 所以可以保证最新的记录就是刚通过回调接口回来的数据信息
  152. saleClueCallRecordMapper.updateCallRecordUser(userId,flowCode);
  153. log.info("------更新操作用户-------");
  154. //查询 最新的记录
  155. SaleClueCallRecordPojo recordPojo = saleClueCallRecordMapper.selectCallRecordInfo();
  156. JSONObject jsonContent = JSONObject.parseObject(recordPojo.getContent());
  157. JSONArray success = jsonContent.getJSONArray("success");
  158. recordPojo.setSuccessList(success);
  159. JSONArray error = jsonContent.getJSONArray("error");
  160. recordPojo.setErrorList(error);
  161. return ResultResponse.successMsg("本次导入记录,成功"+ (StringUtils.isEmpty(success) ? 0 : success.size()) + "条;失败"+
  162. (StringUtils.isEmpty(error) ? 0 : error.size())+"条。",recordPojo);
  163. }
  164. /**
  165. *
  166. * @description: 外呼记录查询
  167. *
  168. * @param
  169. * @return: com.ruixuan.common.core.domain.ResultResponse
  170. * @author: zianY
  171. */
  172. @Override
  173. public ResultResponse getRobotTelephoneRecordList(){
  174. PageUtils.startPage();
  175. //查询导入记录
  176. List<SaleClueCallRecordPojo> callRecordList = saleClueCallRecordMapper.getSaleClueCallRecordInfo();
  177. for (SaleClueCallRecordPojo recordPojo : callRecordList) {
  178. String record = recordPojo.getContent();
  179. JSONObject recordJson = JSONObject.parseObject(record);
  180. recordPojo.setSuccessList(recordJson.getJSONArray("success"));
  181. recordPojo.setErrorList(recordJson.getJSONArray("error"));
  182. }
  183. return ResultResponse.successMsg("查询导入记录成功。",new TableDataInfo(callRecordList,new Long(new PageInfo(callRecordList).getTotal()).intValue()));
  184. }
  185. /**
  186. *
  187. * @description: 通话记录
  188. *
  189. * @param createTime 创建时间
  190. * @param state 销售线索状态 1-未开始 2-进行中 3-已结束
  191. * @return: com.ruixuan.common.core.domain.ResultResponse
  192. * @author: zianY
  193. */
  194. @Override
  195. public ResultResponse getRobotTelephoneResultList(String createTime,Integer state){
  196. PageUtils.startPage();
  197. //查询导入记录
  198. List<SaleClueCallResultPojo> callResultList = saleClueCallResultMapper.getSaleClueCallResultList(createTime,state);
  199. for (SaleClueCallResultPojo resultPojo : callResultList) {
  200. List<Map> contentList = JSONObject.parseArray(resultPojo.getContent(),Map.class);
  201. Map<String,List<Map>> map = contentList.stream().collect(Collectors.groupingBy(m -> m.get("speaker").toString()));
  202. resultPojo.setSpeakerContent(map == null ? null : map.get("Contact"));
  203. resultPojo.setRobotContent(map == null ? null : map.get("Robot"));
  204. }
  205. return ResultResponse.successMsg("查询通话记录成功。",new TableDataInfo(callResultList,new Long(new PageInfo(callResultList).getTotal()).intValue()));
  206. }
  207. }