package com.smartor.service.impl; 
 | 
  
 | 
import com.fasterxml.jackson.core.JsonProcessingException; 
 | 
import com.fasterxml.jackson.databind.ObjectMapper; 
 | 
import com.ruoyi.common.utils.DateUtils; 
 | 
import com.ruoyi.common.utils.StringUtils; 
 | 
import com.smartor.domain.*; 
 | 
import com.smartor.domain.ServiceSubtaskDetail; 
 | 
import com.smartor.mapper.ServiceSubtaskDetailMapper; 
 | 
import com.smartor.mapper.ServiceSubtaskMapper; 
 | 
import com.smartor.service.IServiceSubtaskDetailService; 
 | 
import com.smartor.service.IServiceTaskService; 
 | 
import lombok.extern.slf4j.Slf4j; 
 | 
import org.apache.commons.collections4.CollectionUtils; 
 | 
import org.springframework.beans.factory.annotation.Autowired; 
 | 
import org.springframework.stereotype.Service; 
 | 
  
 | 
import java.math.BigDecimal; 
 | 
import java.math.RoundingMode; 
 | 
import java.util.*; 
 | 
import java.util.stream.Collectors; 
 | 
  
 | 
/** 
 | 
 * 语音任务呼叫明细Service业务层处理 
 | 
 * 
 | 
 * @author smartor 
 | 
 * @date 2023-03-24 
 | 
 */ 
 | 
@Slf4j 
 | 
@Service 
 | 
public class ServiceSubtaskDetailServiceImpl implements IServiceSubtaskDetailService { 
 | 
    @Autowired 
 | 
    private ServiceSubtaskDetailMapper ServiceSubtaskDetailMapper; 
 | 
  
 | 
    @Autowired 
 | 
    private ServiceSubtaskMapper serviceSubtaskMapper; 
 | 
  
 | 
    @Autowired 
 | 
    private IServiceTaskService serviceTaskService; 
 | 
  
 | 
  
 | 
    /** 
 | 
     * 查询语音任务呼叫明细 
 | 
     * 
 | 
     * @param calldetailid 语音任务呼叫明细主键 
 | 
     * @return 语音任务呼叫明细 
 | 
     */ 
 | 
    @Override 
 | 
    public ServiceSubtaskDetail selectServiceSubtaskDetailByCalldetailid(String calldetailid) { 
 | 
        return ServiceSubtaskDetailMapper.selectServiceSubtaskDetailByCalldetailid(calldetailid); 
 | 
    } 
 | 
  
 | 
    /** 
 | 
     * 查询语音任务呼叫明细列表 
 | 
     * 
 | 
     * @param serviceSubtaskDetail 语音任务呼叫明细 
 | 
     * @return 语音任务呼叫明细 
 | 
     */ 
 | 
    @Override 
 | 
    public Map<String, Object> selectServiceSubtaskDetailList(ServiceSubtaskDetail serviceSubtaskDetail) { 
 | 
        List<ServiceSubtaskDetail> serviceSubtaskDetails = ServiceSubtaskDetailMapper.selectServiceSubtaskDetailList(serviceSubtaskDetail); 
 | 
        String taskName = null; 
 | 
        if (CollectionUtils.isNotEmpty(serviceSubtaskDetails)) { 
 | 
            //处理一下通配符 
 | 
            for (ServiceSubtaskDetail serviceSubtaskDetail1 : serviceSubtaskDetails) { 
 | 
                ServiceSubtask serviceSubtask = serviceSubtaskMapper.selectServiceSubtaskById(serviceSubtaskDetail1.getSubId()); 
 | 
                serviceSubtaskDetail1.setQuestiontext(getObject(serviceSubtask, serviceSubtaskDetail1.getQuestiontext())); 
 | 
            } 
 | 
            taskName = serviceTaskService.selectServiceTaskByTaskid(serviceSubtaskDetails.get(0).getTaskid()).getTaskName(); 
 | 
        } 
 | 
        //获取最后的总录音地址 
 | 
        Map<String, Object> map = new HashMap<>(); 
 | 
        map.put("code", 200); 
 | 
        map.put("msg", "查询成功"); 
 | 
        Map<String, Object> map1 = new HashMap<>(); 
 | 
        map1.put("serviceSubtaskDetails", serviceSubtaskDetails); 
 | 
        map1.put("filteredDetails", null); 
 | 
        map1.put("taskName", taskName); 
 | 
        //用于存储去重的数据 
 | 
        List<ServiceSubtaskDetail> serviceSubtaskDetailSET = new ArrayList<>(); 
 | 
        Boolean flag = false; 
 | 
        if (CollectionUtils.isNotEmpty(serviceSubtaskDetails)) { 
 | 
            for (ServiceSubtaskDetail serviceSubtaskDetail1 : serviceSubtaskDetails) { 
 | 
                if (serviceSubtaskDetailSET.size() == 0) { 
 | 
                    serviceSubtaskDetailSET.add(serviceSubtaskDetail1); 
 | 
                } else { 
 | 
//                    for (ServiceSubtaskDetail subtaskDetail : serviceSubtaskDetailSET) 
 | 
                    for (int i = 0; i < serviceSubtaskDetailSET.size(); i++) { 
 | 
                        ServiceSubtaskDetail subtaskDetail = serviceSubtaskDetailSET.get(i); 
 | 
                        if (subtaskDetail.getPatid().equals(serviceSubtaskDetail1.getPatid()) && subtaskDetail.getSubId().equals(serviceSubtaskDetail1.getSubId()) && subtaskDetail.getTaskid().equals(serviceSubtaskDetail1.getTaskid()) && subtaskDetail.getQuestiontext().equals(serviceSubtaskDetail1.getQuestiontext())) { 
 | 
                            if (StringUtils.isNotEmpty(serviceSubtaskDetail1.getMatchedtext())) { 
 | 
                                serviceSubtaskDetailSET.set(i, serviceSubtaskDetail1); 
 | 
                            } 
 | 
                            //匹配到这个数据,将flag改成false 
 | 
                            if (flag == true) flag = false; 
 | 
                        } else if ((subtaskDetail.getPatid().equals(serviceSubtaskDetail1.getPatid()) && subtaskDetail.getSubId().equals(serviceSubtaskDetail1.getSubId()) && subtaskDetail.getTaskid().equals(serviceSubtaskDetail1.getTaskid()) && !subtaskDetail.getQuestiontext().equals(serviceSubtaskDetail1.getQuestiontext()))) { 
 | 
                            //标识一下,暂时没有匹配到这个数据 
 | 
                            flag = true; 
 | 
                        } else if ((subtaskDetail.getPatid() == serviceSubtaskDetail1.getPatid() && subtaskDetail.getSubId() != serviceSubtaskDetail1.getSubId() && subtaskDetail.getTaskid() == serviceSubtaskDetail1.getTaskid())) { 
 | 
                            //标识一下,暂时没有匹配到这个数据 
 | 
                            flag = true; 
 | 
                        } 
 | 
                    } 
 | 
                    if (flag == true) { 
 | 
                        //如果去重循环都结束了,还没有匹配到这个数据,则往去重对象里新增一个 
 | 
                        serviceSubtaskDetailSET.add(serviceSubtaskDetail1); 
 | 
                        flag = false; 
 | 
                    } 
 | 
                } 
 | 
            } 
 | 
        } 
 | 
        Collections.sort(serviceSubtaskDetailSET, 
 | 
                Comparator.comparing(ServiceSubtaskDetail::getTemplatequestionnum, Comparator.nullsLast(Long::compareTo))); 
 | 
        map1.put("filteredDetails", serviceSubtaskDetailSET); 
 | 
  
 | 
        map1.put("voice", ""); 
 | 
        if (CollectionUtils.isNotEmpty(serviceSubtaskDetails) && StringUtils.isNotEmpty(serviceSubtaskDetails.get(0).getQuestionvoice())) { 
 | 
            //获取完整语音 
 | 
            String filePath = serviceSubtaskDetails.get(0).getQuestionvoice(); 
 | 
            int lastIndex = filePath.lastIndexOf('.'); 
 | 
            if (lastIndex != -1) { 
 | 
                int secondLastIndex = filePath.lastIndexOf('.', lastIndex - 1); 
 | 
                if (secondLastIndex != -1) { 
 | 
                    String voicePath = filePath.substring(0, secondLastIndex) + filePath.substring(lastIndex); 
 | 
                    map1.put("voice", voicePath); 
 | 
                } 
 | 
            } 
 | 
        } 
 | 
        map.put("data", map1); 
 | 
        return map; 
 | 
    } 
 | 
  
 | 
    /** 
 | 
     * 新增语音任务呼叫明细 
 | 
     * 
 | 
     * @param serviceSubtaskDetail 语音任务呼叫明细 
 | 
     * @return 结果 
 | 
     */ 
 | 
    @Override 
 | 
    public int insertServiceSubtaskDetail(ServiceSubtaskDetail serviceSubtaskDetail) { 
 | 
        serviceSubtaskDetail.setCreateTime(DateUtils.getNowDate()); 
 | 
        return ServiceSubtaskDetailMapper.insertServiceSubtaskDetail(serviceSubtaskDetail); 
 | 
    } 
 | 
  
 | 
    @Override 
 | 
    public int batchInsertServiceSubtaskDetail(List<ServiceSubtaskDetail> serviceSubtaskDetails) { 
 | 
        int i = 0; 
 | 
        if (CollectionUtils.isNotEmpty(serviceSubtaskDetails)) { 
 | 
            for (ServiceSubtaskDetail serviceSubtaskDetail : serviceSubtaskDetails) { 
 | 
                ServiceSubtask serviceSubtask = serviceSubtaskMapper.selectServiceSubtaskById(serviceSubtaskDetail.getSubId()); 
 | 
                if (serviceSubtaskDetail.getId() == null) { 
 | 
                    serviceSubtaskDetail.setCreateTime(DateUtils.getNowDate()); 
 | 
                    serviceSubtaskDetail.setOrgid(serviceSubtask.getOrgid()); 
 | 
                    i = ServiceSubtaskDetailMapper.insertServiceSubtaskDetail(serviceSubtaskDetail); 
 | 
                } else { 
 | 
                    serviceSubtaskDetail.setUpdateTime(DateUtils.getNowDate()); 
 | 
                    serviceSubtaskDetail.setOrgid(serviceSubtask.getOrgid()); 
 | 
                    i = ServiceSubtaskDetailMapper.updateServiceSubtaskDetail(serviceSubtaskDetail); 
 | 
                } 
 | 
            } 
 | 
        } 
 | 
        return i; 
 | 
    } 
 | 
  
 | 
    /** 
 | 
     * 修改语音任务呼叫明细 
 | 
     * 
 | 
     * @param serviceSubtaskDetail 语音任务呼叫明细 
 | 
     * @return 结果 
 | 
     */ 
 | 
    @Override 
 | 
    public int updateServiceSubtaskDetail(ServiceSubtaskDetail serviceSubtaskDetail) { 
 | 
        serviceSubtaskDetail.setUpdateTime(DateUtils.getNowDate()); 
 | 
        return ServiceSubtaskDetailMapper.updateServiceSubtaskDetail(serviceSubtaskDetail); 
 | 
    } 
 | 
  
 | 
    @Override 
 | 
    public int updateSSDByCondition(ServiceSubtaskDetail serviceSubtaskDetail) { 
 | 
        serviceSubtaskDetail.setUpdateTime(DateUtils.getNowDate()); 
 | 
        return ServiceSubtaskDetailMapper.updateSSDByCondition(serviceSubtaskDetail); 
 | 
    } 
 | 
  
 | 
    /** 
 | 
     * 批量删除语音任务呼叫明细 
 | 
     * 
 | 
     * @param calldetailids 需要删除的语音任务呼叫明细主键 
 | 
     * @return 结果 
 | 
     */ 
 | 
    @Override 
 | 
    public int deleteServiceSubtaskDetailByCalldetailids(String[] calldetailids) { 
 | 
        return ServiceSubtaskDetailMapper.deleteServiceSubtaskDetailByCalldetailids(calldetailids); 
 | 
    } 
 | 
  
 | 
    /** 
 | 
     * 删除语音任务呼叫明细信息 
 | 
     * 
 | 
     * @param calldetailid 语音任务呼叫明细主键 
 | 
     * @return 结果 
 | 
     */ 
 | 
    @Override 
 | 
    public int deleteServiceSubtaskDetailByCalldetailid(String calldetailid) { 
 | 
        return ServiceSubtaskDetailMapper.deleteServiceSubtaskDetailByCalldetailid(calldetailid); 
 | 
    } 
 | 
  
 | 
    private String getObject(ServiceSubtask serviceSubtask, String scriptContent) { 
 | 
        Map<String, Map<String, String>> param = getParam(serviceSubtask.getTaskid()); 
 | 
        for (Map<String, String> map : param.values()) { 
 | 
            for (String key : map.keySet()) { 
 | 
                scriptContent = scriptContent.replace(key, org.apache.commons.lang3.StringUtils.isNotEmpty(map.get(key)) ? map.get(key) : ""); 
 | 
            } 
 | 
        } 
 | 
        scriptContent = scriptContent.replace("${name}", org.apache.commons.lang3.StringUtils.isNotEmpty(serviceSubtask.getSendname()) ? serviceSubtask.getSendname() : ""); 
 | 
        scriptContent = scriptContent.replace("${dzz}", org.apache.commons.lang3.StringUtils.isNotEmpty(serviceSubtask.getAddr()) ? serviceSubtask.getAddr() : ""); 
 | 
        scriptContent = scriptContent.replace("${phone}", org.apache.commons.lang3.StringUtils.isNotEmpty(serviceSubtask.getTelcode()) ? serviceSubtask.getTelcode() : ""); 
 | 
  
 | 
        return scriptContent; 
 | 
    } 
 | 
  
 | 
  
 | 
    /** 
 | 
     * 获取任务里的通配符 
 | 
     * 
 | 
     * @param taskId 
 | 
     * @return 
 | 
     */ 
 | 
    private Map<String, Map<String, String>> getParam(Long taskId) { 
 | 
        ServiceTask serviceTask = serviceTaskService.selectServiceTaskByTaskid(taskId); 
 | 
        ObjectMapper objectMapper = new ObjectMapper(); 
 | 
        Map<String, Map<String, String>> serviceTaskMap = null; 
 | 
        try { 
 | 
            serviceTaskMap = objectMapper.readValue(serviceTask.getTextParam(), Map.class); 
 | 
        } catch (JsonProcessingException e) { 
 | 
            e.printStackTrace(); 
 | 
        } 
 | 
        return serviceTaskMap; 
 | 
    } 
 | 
  
 | 
  
 | 
    /* 
 | 
     * @param targetid 
 | 
     * @return 指标统计 
 | 
     */ 
 | 
    @Override 
 | 
    public List<ServiceSubtaskDetailTarget> countPatByTarget(Long targetid) { 
 | 
        List<ServiceSubtaskDetailTarget> serviceSubtaskDetailTargets = ServiceSubtaskDetailMapper.countPatByTarget(targetid); 
 | 
        if (CollectionUtils.isNotEmpty(serviceSubtaskDetailTargets)) { 
 | 
            Long totalCount = serviceSubtaskDetailTargets.stream().filter(target -> target.getMatchedtext() == null || target.getMatchedtext().isEmpty()).mapToLong(target -> target.getCount() != null ? target.getCount() : 0) // 确保 count 为 0 如果为 null 
 | 
                    .sum(); 
 | 
  
 | 
            Long allShowCount = 0L; 
 | 
  
 | 
            // 遍历列表,将 matchedText 为""赋值给 null 
 | 
            for (ServiceSubtaskDetailTarget target : serviceSubtaskDetailTargets) { 
 | 
                if (target.getMatchedtext() == null) { 
 | 
                    target.setCount(target.getCount() != null ? target.getCount() + totalCount : totalCount);  // 给 matchedText 为 null 的对象赋值 
 | 
                } 
 | 
                //允许null相加,不允许""相加 
 | 
                if (target.getMatchedtext() == null || !target.getMatchedtext().isEmpty()) { 
 | 
                    allShowCount = allShowCount + target.getCount(); 
 | 
                } 
 | 
            } 
 | 
  
 | 
            // 使用迭代器删除 matchedText 为 "" 的对象 
 | 
            Iterator<ServiceSubtaskDetailTarget> iterator = serviceSubtaskDetailTargets.iterator(); 
 | 
            while (iterator.hasNext()) { 
 | 
                ServiceSubtaskDetailTarget target = iterator.next(); 
 | 
                if (target.getMatchedtext() != null && target.getMatchedtext().isEmpty()) { 
 | 
                    iterator.remove();  // 删除 matchedText 为 "" 的对象 
 | 
                } 
 | 
            } 
 | 
            for (ServiceSubtaskDetailTarget target : serviceSubtaskDetailTargets) { 
 | 
                target.setTargetShowCount(allShowCount.toString()); 
 | 
                BigDecimal count = new BigDecimal(target.getCount() != null ? target.getCount().toString() : "0"); 
 | 
                BigDecimal allCount = new BigDecimal(allShowCount.toString()); 
 | 
                if (allCount.compareTo(BigDecimal.ZERO) == 0) 
 | 
                    target.setPercentage(count.divide(allCount, 2, RoundingMode.HALF_UP).toString()); 
 | 
            } 
 | 
        } 
 | 
  
 | 
        return serviceSubtaskDetailTargets; 
 | 
    } 
 | 
  
 | 
} 
 |