Explorar el Código

手动外呼同步接口修改

peicj hace 11 horas
padre
commit
54cf5f2dc4

+ 12 - 9
ruoyi-admin/src/main/java/com/ruoyi/aicall/controller/ApiController.java

@@ -2014,23 +2014,26 @@ public class ApiController extends BaseController {
     }
 
     /**
-     * 手动同步手动外呼记录
-     * @param ids
+     * 手动同步手动外呼记录(按 uuid;同一 uuid 多段断线重连合并为一条返回)
+     * @param uuids 本地通话 uuid 列表
      */
     @PostMapping(value ="/outboundcdr/manualPullTest")
     @ResponseBody
-    public AjaxResult outboundcdrManualPullTest(@RequestBody List<String> ids) {
-        List<CcOutboundCdr> list = outboundCdrService.selectCcOutboundCdrByIds(ids);
-        if(!CollectionUtils.isEmpty(list)){
-            for (CcOutboundCdr outboundCdr : list){
-                if(StringUtils.isNotEmpty(outboundCdr.getCaller())){
+    public AjaxResult outboundcdrManualPullTest(@RequestBody List<String> uuids) {
+        List<CcOutboundCdr> list = outboundCdrService.selectMergedCcOutboundCdrByUuids(uuids);
+        if (list == null) {
+            list = Collections.emptyList();
+        }
+        if (!CollectionUtils.isEmpty(list)) {
+            for (CcOutboundCdr outboundCdr : list) {
+                if (StringUtils.isNotEmpty(outboundCdr.getCaller())) {
                     try {
                         outboundCdr.setCaller(DESUtil.encrypt(URLEncoder.encode(outboundCdr.getCaller(), "UTF-8")));
                     } catch (Throwable e) {
                         outboundCdr.setCaller("");
                     }
                 }
-                if(StringUtils.isNotEmpty(outboundCdr.getCallee())){
+                if (StringUtils.isNotEmpty(outboundCdr.getCallee())) {
                     try {
                         outboundCdr.setCallee(DESUtil.encrypt(URLEncoder.encode(outboundCdr.getCallee(), "UTF-8")));
                     } catch (Throwable e) {
@@ -2039,7 +2042,7 @@ public class ApiController extends BaseController {
                 }
             }
         }
-        return AjaxResult.success("成功获取" + list.size() + "条数据",list);
+        return AjaxResult.success("成功获取" + list.size() + "条数据", list);
     }
 
     /**

+ 5 - 0
ruoyi-admin/src/main/java/com/ruoyi/cc/mapper/CcOutboundCdrMapper.java

@@ -61,4 +61,9 @@ public interface CcOutboundCdrMapper
     public int deleteCcOutboundCdrByIds(String[] ids);
 
     List<CcOutboundCdr> selectCcOutboundCdrByIds(@Param("ids") List<String> ids);
+
+    /**
+     * 按 uuid 批量查询外呼记录(同一 uuid 可能多条断线重连分段)
+     */
+    List<CcOutboundCdr> selectCcOutboundCdrByUuids(@Param("uuids") List<String> uuids);
 }

+ 6 - 0
ruoyi-admin/src/main/java/com/ruoyi/cc/service/ICcOutboundCdrService.java

@@ -62,4 +62,10 @@ public interface ICcOutboundCdrService
     List<CcOutboundCdr> selectCcOutboundCdrYlrzList(CcOutboundCdr outboundCdr);
 
     List<CcOutboundCdr> selectCcOutboundCdrByIds(List<String> ids);
+
+    /**
+     * 按 uuid 批量查询并合并断线重连分段:每个 uuid 仅返回一条。
+     * 合并规则:以 end_time 最晚一条为底;start_time、answered_time 取最早一条;time_len、time_len_valid 累加。
+     */
+    List<CcOutboundCdr> selectMergedCcOutboundCdrByUuids(List<String> uuids);
 }

+ 60 - 0
ruoyi-admin/src/main/java/com/ruoyi/cc/service/impl/CcOutboundCdrServiceImpl.java

@@ -8,12 +8,18 @@ import com.ruoyi.common.utils.DateUtils;
 import org.apache.commons.lang3.StringUtils;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Service;
+import org.springframework.util.CollectionUtils;
 
 import java.net.URLDecoder;
+import java.util.ArrayList;
 import java.util.Collections;
+import java.util.Comparator;
 import java.util.HashMap;
+import java.util.LinkedHashMap;
 import java.util.List;
 import java.util.Map;
+import java.util.Objects;
+import java.util.stream.Collectors;
 
 /**
  * 外呼记录Service业务层处理
@@ -204,4 +210,58 @@ public class CcOutboundCdrServiceImpl implements ICcOutboundCdrService
     public List<CcOutboundCdr> selectCcOutboundCdrByIds(List<String> ids) {
         return ccOutboundCdrMapper.selectCcOutboundCdrByIds(ids);
     }
+
+    @Override
+    public List<CcOutboundCdr> selectMergedCcOutboundCdrByUuids(List<String> uuids) {
+        if (CollectionUtils.isEmpty(uuids)) {
+            return Collections.emptyList();
+        }
+        List<String> distinctUuids = uuids.stream()
+                .filter(StringUtils::isNotBlank)
+                .distinct()
+                .collect(Collectors.toList());
+        if (distinctUuids.isEmpty()) {
+            return Collections.emptyList();
+        }
+        List<CcOutboundCdr> list = ccOutboundCdrMapper.selectCcOutboundCdrByUuids(distinctUuids);
+        if (CollectionUtils.isEmpty(list)) {
+            return Collections.emptyList();
+        }
+        Map<String, List<CcOutboundCdr>> grouped = list.stream()
+                .filter(Objects::nonNull)
+                .filter(item -> StringUtils.isNotBlank(item.getUuid()))
+                .collect(Collectors.groupingBy(CcOutboundCdr::getUuid, LinkedHashMap::new, Collectors.toList()));
+
+        List<CcOutboundCdr> merged = new ArrayList<>(grouped.size());
+        for (List<CcOutboundCdr> segments : grouped.values()) {
+            merged.add(mergeReconnectSegments(segments));
+        }
+        return merged;
+    }
+
+    /**
+     * 断线重连多段合并:end_time 倒序后,取最晚一条为底;
+     * start_time、answered_time 取最早一条;time_len、time_len_valid 全部分段累加。
+     */
+    private CcOutboundCdr mergeReconnectSegments(List<CcOutboundCdr> segments) {
+        if (segments.size() == 1) {
+            return segments.get(0);
+        }
+        List<CcOutboundCdr> ordered = segments.stream()
+                .sorted(Comparator.comparing(CcOutboundCdr::getEndTime, Comparator.nullsLast(Long::compareTo)).reversed())
+                .collect(Collectors.toList());
+        CcOutboundCdr latest = ordered.get(0);
+        CcOutboundCdr earliest = ordered.get(ordered.size() - 1);
+        latest.setStartTime(earliest.getStartTime());
+        latest.setAnsweredTime(earliest.getAnsweredTime());
+        long timeLenSum = 0L;
+        long timeLenValidSum = 0L;
+        for (CcOutboundCdr item : ordered) {
+            timeLenSum += item.getTimeLen() == null ? 0L : item.getTimeLen();
+            timeLenValidSum += item.getTimeLenValid() == null ? 0L : item.getTimeLenValid();
+        }
+        latest.setTimeLen(timeLenSum);
+        latest.setTimeLenValid(timeLenValidSum);
+        return latest;
+    }
 }

+ 14 - 0
ruoyi-admin/src/main/resources/mapper/cc/CcOutboundCdrMapper.xml

@@ -152,4 +152,18 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
             </if>
         </where>
     </select>
+
+    <select id="selectCcOutboundCdrByUuids" resultMap="CcOutboundCdrResult">
+        <include refid="selectCcOutboundCdrVo"/>
+        <where>
+            <if test="uuids != null and uuids.size() > 0">
+                uuid IN
+                <foreach collection="uuids" item="uuid" open="(" separator="," close=")">
+                    #{uuid}
+                </foreach>
+            </if>
+            AND end_time &gt; 0
+        </where>
+        order by end_time desc
+    </select>
 </mapper>