| | |
| | | package cn.gistack.sm.intelligentCall.service.impl; |
| | | |
| | | import cn.gistack.sm.intelligentCall.constant.CallConstant; |
| | | import cn.gistack.sm.intelligentCall.mapper.CallTaskMapper; |
| | | import cn.gistack.sm.intelligentCall.service.CallService; |
| | | import cn.gistack.sm.intelligentCall.vo.CallTaskResultVO; |
| | | import cn.gistack.sm.intelligentCall.vo.CallTaskStatistic; |
| | | import com.alibaba.fastjson.JSONArray; |
| | | import com.alibaba.fastjson.JSONObject; |
| | | import com.baomidou.dynamic.datasource.annotation.DS; |
| | | import com.baomidou.mybatisplus.core.metadata.IPage; |
| | | import lombok.extern.slf4j.Slf4j; |
| | | import org.apache.commons.lang3.RandomUtils; |
| | | import org.apache.commons.lang3.StringUtils; |
| | | import org.springframework.beans.factory.annotation.Autowired; |
| | | import org.springframework.http.HttpEntity; |
| | | import org.springframework.http.HttpHeaders; |
| | | import org.springframework.http.HttpStatus; |
| | | import org.springframework.http.ResponseEntity; |
| | | import org.springframework.stereotype.Service; |
| | | import org.springframework.util.DigestUtils; |
| | | import org.springframework.web.client.RestTemplate; |
| | | import java.time.LocalDateTime; |
| | | import java.time.format.DateTimeFormatter; |
| | | import java.util.ArrayList; |
| | | import java.util.Arrays; |
| | | import java.util.List; |
| | | import java.util.Map; |
| | | |
| | | @Slf4j |
| | | @Service |
| | | public class CallServiceImpl { |
| | | public class CallServiceImpl implements CallService { |
| | | |
| | | @Autowired |
| | | private CallTaskMapper callTaskMapper; |
| | | |
| | | @Autowired |
| | | private RestTemplate restTemplate; |
| | | |
| | | // @Value("${dial.task.url}") |
| | | private String url = CallConstant.HOST; |
| | | |
| | | // @Value("${dial.task.empId}") |
| | | private String empId = CallConstant.empId; |
| | | |
| | | // @Value("${dial.task.appId}") |
| | | private String appId = CallConstant.AppKey; |
| | | |
| | | // @Value("${dial.task.appSecret}") |
| | | private String appSecret = CallConstant.AppSecret; |
| | | |
| | | // @Value("${dial.task.scenesId}") |
| | | private String scenesId = CallConstant.scenesId; |
| | | |
| | | // @Value("${dial.task.callingNumbers}") |
| | | private String callingNumbers = CallConstant.callingNumbers; |
| | | |
| | | private static final String CONTENT_TYPE = "application/json; charset=UTF-8"; |
| | | private static final String ACCEPT_TYPE = "application/json"; |
| | | private static final String ACCEPT_ENCODING = "gzip"; |
| | | |
| | | private final static String CREATE_TASK_URL = "/api/task/createTask.json"; |
| | | private final static String CALL_DETAIL_URL = "/api/task/callDetail.json"; |
| | | |
| | | /** |
| | | * 创建任务 |
| | | * @param map |
| | | * @return |
| | | */ |
| | | @Override |
| | | public Object createTask(Map<String,Object> map) { |
| | | List<Map<String, String>> params = (List<Map<String, String>>) map.get("list"); |
| | | String requestUrl = url + CREATE_TASK_URL; |
| | | LocalDateTime now = LocalDateTime.now(); |
| | | String timestamp = now.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss SSS")); |
| | | String transId = generateTransId(now); |
| | | String taskName = now.format(DateTimeFormatter.ofPattern("yyyy年MM月dd日HH:mm")) + "智能外呼";; |
| | | if(null!= map.get("taskName") && !map.get("taskName").equals("")){ |
| | | taskName = map.get("taskName").toString(); |
| | | } |
| | | String[] numbers = StringUtils.split(callingNumbers,","); |
| | | String taskScheduleTime = LocalDateTime.now().plusHours(1).format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")); |
| | | if (null!=map.get("taskScheduleTime") && !map.get("taskScheduleTime").equals("")){ |
| | | taskScheduleTime = map.get("taskScheduleTime").toString(); |
| | | } |
| | | String scenesIds = scenesId; |
| | | if (null!=map.get("scenesId") && !map.get("scenesId").equals("")){ |
| | | scenesIds = map.get("scenesId").toString(); |
| | | } |
| | | JSONArray arr = getCalleeInfo(params); |
| | | HttpHeaders httpHeaders = new HttpHeaders(); |
| | | httpHeaders.add("Content-Type", CONTENT_TYPE); |
| | | httpHeaders.add("Accept", ACCEPT_TYPE); |
| | | httpHeaders.add("Accept-Encoding", ACCEPT_ENCODING); |
| | | JSONObject paramJson = new JSONObject(); |
| | | paramJson.put("taskName", taskName); |
| | | paramJson.put("scenesId", scenesIds); |
| | | paramJson.put("callingNumbers", numbers); |
| | | paramJson.put("taskScheduleTime", taskScheduleTime); |
| | | paramJson.put("calleeData", arr); |
| | | paramJson.put("empId", empId); |
| | | String token = getToken(appId, timestamp, transId, appSecret, paramJson.toJSONString()); |
| | | JSONObject headerJson = new JSONObject(); |
| | | headerJson.put("appId", appId); |
| | | headerJson.put("timestamp", timestamp); |
| | | headerJson.put("transId", transId); |
| | | headerJson.put("token", token); |
| | | JSONObject bodyJson = new JSONObject(); |
| | | bodyJson.put("head", headerJson); |
| | | bodyJson.put("body", paramJson); |
| | | HttpEntity<String> stringHttpEntity = new HttpEntity<>(bodyJson.toJSONString(), httpHeaders); |
| | | log.info("创建超汛限智能外呼任务请求参数:{}", bodyJson.toJSONString()); |
| | | ResponseEntity<String> response = restTemplate.postForEntity(requestUrl, stringHttpEntity, String.class); |
| | | log.info("创建外呼任务请求结果:{}", response); |
| | | if (response.getStatusCode() != HttpStatus.OK) { |
| | | log.error("创建智能外呼任务失败:{}", response.getStatusCode()); |
| | | } |
| | | return response; |
| | | } |
| | | |
| | | /** |
| | | * 获取通话详情 |
| | | * @param taskId |
| | | * @param calleeNumber |
| | | * @return |
| | | */ |
| | | @Override |
| | | public Object getCallDetail(String taskId, String calleeNumber) { |
| | | String requestUrl = url + CALL_DETAIL_URL; |
| | | LocalDateTime now = LocalDateTime.now(); |
| | | String timestamp = now.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss SSS")); |
| | | String transId = generateTransId(now); |
| | | HttpHeaders httpHeaders = new HttpHeaders(); |
| | | httpHeaders.add("Content-Type", CONTENT_TYPE); |
| | | httpHeaders.add("Accept", ACCEPT_TYPE); |
| | | httpHeaders.add("Accept-Encoding", ACCEPT_ENCODING); |
| | | JSONObject paramJson = new JSONObject(); |
| | | paramJson.put("taskId", taskId); |
| | | paramJson.put("calleeNumber", calleeNumber); |
| | | JSONObject bodyJson = new JSONObject(); |
| | | bodyJson.put("head", getHeadJson(appId, timestamp, transId, appSecret, paramJson.toJSONString())); |
| | | bodyJson.put("body", paramJson); |
| | | HttpEntity<String> stringHttpEntity = new HttpEntity<>(bodyJson.toJSONString(), httpHeaders); |
| | | ResponseEntity<String> response = restTemplate.postForEntity(requestUrl, stringHttpEntity, String.class); |
| | | log.info(response.toString()); |
| | | if (response.getStatusCode() == HttpStatus.OK) { |
| | | JSONObject responseJson = (JSONObject) JSONObject.parse(response.getBody()); |
| | | assert responseJson != null; |
| | | // return JSONObject.parseObject(responseJson.getString("data"), CallBackDetailVO.class); |
| | | return response.getBody(); |
| | | } |
| | | return null; |
| | | } |
| | | |
| | | private String getToken(String appId, String timestamp, String transId, String appSecret, String body) { |
| | | String sb = "appId" + appId + "timestamp" + timestamp + "transId" + transId + body + appSecret; |
| | | return DigestUtils.md5DigestAsHex(sb.getBytes()); |
| | | } |
| | | |
| | | private JSONObject getHeadJson(String appId, String timestamp, String transId, String appSecret, String body) { |
| | | String token = getToken(appId, timestamp, transId, appSecret, body); |
| | | JSONObject headerJson = new JSONObject(); |
| | | headerJson.put("appId", appId); |
| | | headerJson.put("timestamp", timestamp); |
| | | headerJson.put("transId", transId); |
| | | headerJson.put("token", token); |
| | | return headerJson; |
| | | } |
| | | |
| | | private String generateTransId(LocalDateTime now) { |
| | | String s = "000000" + RandomUtils.nextLong(0, 999999); |
| | | String randomStr = StringUtils.substring(s, -6); |
| | | return now.format(DateTimeFormatter.ofPattern("yyyyMMddHHmmssSSS")) + randomStr; |
| | | } |
| | | |
| | | /** |
| | | * 封装被呼人对象 |
| | | * @param params 被呼人信息 |
| | | * @return 被呼人数组 |
| | | */ |
| | | private JSONArray getCalleeInfo(List<Map<String, String>> params) { |
| | | JSONArray arr = new JSONArray(); |
| | | params.forEach(map -> { |
| | | String phone = map.get("phone"); |
| | | String name = map.get("name"); |
| | | String code = map.get("code"); |
| | | String userName = map.get("userName"); |
| | | String userId = map.get("userId"); |
| | | String over = map.get("over"); |
| | | if (StringUtils.isNotEmpty(phone) && StringUtils.isNotEmpty(name)) { |
| | | JSONObject json = new JSONObject(); |
| | | json.put("被叫号码", phone); |
| | | json.put("水库名称", name); |
| | | json.put("水库编码", code); |
| | | json.put("用户名", userName); |
| | | json.put("用户id", userId); |
| | | json.put("超汛限多少米", over); |
| | | arr.add(json); |
| | | } |
| | | }); |
| | | return arr; |
| | | } |
| | | |
| | | /** |
| | | * 查詢任务列表数据 |
| | | * @param page |
| | | * @param callTask |
| | | * @return |
| | | */ |
| | | @Override |
| | | public IPage<CallTaskStatistic> getCallListPage(IPage<CallTaskStatistic> page, CallTaskStatistic callTask) { |
| | | return page.setRecords(callTaskMapper.getCallListPage(page,callTask)); |
| | | } |
| | | |
| | | /** |
| | | * 查询呼叫详情列表信息 |
| | | * @param callTaskResult |
| | | * @param page |
| | | * @return |
| | | */ |
| | | @Override |
| | | public IPage<CallTaskResultVO> getCallTaskResultListPage(IPage<CallTaskResultVO> page, CallTaskResultVO callTaskResult) { |
| | | List<String> list = new ArrayList<>(); |
| | | if (null!= callTaskResult.getAreaName() && !callTaskResult.getAreaName().equals("")){ |
| | | // 查询所有下级区域code |
| | | list = getAllChildrenAreaByAreaCode(callTaskResult.getAreaName()); |
| | | } |
| | | List<CallTaskResultVO> resultListPage = callTaskMapper.getCallTaskResultListPage(page, callTaskResult,list,null); |
| | | return page.setRecords(resultListPage); |
| | | } |
| | | |
| | | /** |
| | | * 导出呼叫详情列表信息 |
| | | * @param callTaskResult |
| | | * @return |
| | | */ |
| | | @Override |
| | | public Object getCallTaskResultList(CallTaskResultVO callTaskResult) { |
| | | List<String> list = new ArrayList<>(); |
| | | List<String> idsList = new ArrayList<>(); |
| | | if (null!= callTaskResult.getAreaName() && !callTaskResult.getAreaName().equals("")){ |
| | | // 查询所有下级区域code |
| | | list = getAllChildrenAreaByAreaCode(callTaskResult.getAreaName()); |
| | | } |
| | | if (null!= callTaskResult.getIds() && !callTaskResult.getIds().equals("")){ |
| | | // 查询所有下级区域code |
| | | idsList = Arrays.asList(callTaskResult.getIds().split(",")); |
| | | } |
| | | List<CallTaskResultVO> resultListPage = callTaskMapper.getCallTaskResultListPage(null, callTaskResult,list,idsList); |
| | | // 返回 |
| | | return resultListPage; |
| | | } |
| | | |
| | | /** |
| | | * 查询智能外呼统计数据-按日期-行政区统计 |
| | | * @param callTaskStatistic |
| | | * @return |
| | | */ |
| | | @Override |
| | | public Object getOutgoingStatisticList(IPage<CallTaskStatistic> page,CallTaskStatistic callTaskStatistic) { |
| | | List<String> list = new ArrayList<>(); |
| | | if (null!= callTaskStatistic.getAdName() && !callTaskStatistic.getAdName().equals("")){ |
| | | // 查询所有下级区域code |
| | | list = getAllChildrenAreaByAreaCode(callTaskStatistic.getAdName()); |
| | | } |
| | | // 勾选导出行政区 id 组装 |
| | | if (null!= callTaskStatistic.getIds() && !callTaskStatistic.getIds().equals("")){ |
| | | list = Arrays.asList(callTaskStatistic.getIds().split(",")); |
| | | } |
| | | // 判断是否分页 |
| | | if (null!= callTaskStatistic.getIsPage()){ |
| | | return callTaskMapper.getOutgoingStatisticList(null,callTaskStatistic,list); |
| | | } |
| | | return page.setRecords(callTaskMapper.getOutgoingStatisticList(page,callTaskStatistic,list)); |
| | | } |
| | | |
| | | /** |
| | | * 查询智能外呼统计数据-行政区统计 |
| | | * @param callTaskStatistic |
| | | * @return |
| | | */ |
| | | @Override |
| | | public Object getOutgoingStatisticByArea(CallTaskStatistic callTaskStatistic) { |
| | | return callTaskMapper.getOutgoingStatisticByArea(callTaskStatistic); |
| | | } |
| | | |
| | | /** |
| | | * 查询所有下级区域 |
| | | * @param areaCode |
| | | * @return |
| | | */ |
| | | private List<String> getAllChildrenAreaByAreaCode(String areaCode) { |
| | | return callTaskMapper.getAllChildrenAreaByAreaCode(areaCode); |
| | | } |
| | | |
| | | /** |
| | | * 通过水库编码获取行政区名称 |
| | | * @param adCode |
| | | * @return |
| | | */ |
| | | private String getAreaNameByAdCode(String adCode) { |
| | | // 通过adCode查询区域名称 |
| | | return callTaskMapper.getAreaNameByAdCode(adCode); |
| | | } |
| | | |
| | | /** |
| | | * 通过行政区编号获取水库名称 |
| | | * @param areaCode |
| | | * @return |
| | | */ |
| | | private List<String> getWaterNameByAreaCode(String areaCode) { |
| | | // 通过行政区编号获取水库名称 |
| | | return callTaskMapper.getWaterNameByAreaCode(areaCode); |
| | | } |
| | | } |