liusheng
2024-09-18 907641e56c2085aaa81f267946dc3e3e9fca73e7
smartor/src/main/java/com/smartor/service/impl/IvrLibaTargetServiceImpl.java
@@ -1,96 +1,264 @@
package com.smartor.service.impl;
import java.util.List;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.gson.Gson;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.DtoConversionUtils;
import com.ruoyi.common.utils.StringUtils;
import com.smartor.domain.*;
import com.smartor.mapper.IvrLibaTargetMapper;
import com.smartor.mapper.IvrLibaTargetTagMapper;
import com.smartor.mapper.IvrLibaTargetoptionMapper;
import com.smartor.service.IIvrLibaTargetService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.smartor.mapper.IvrLibaTargetMapper;
import com.smartor.domain.IvrLibaTarget;
import com.smartor.service.IIvrLibaTargetService;
import org.springframework.transaction.annotation.Transactional;
import java.lang.reflect.Array;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
/**
 * 指标库Service业务层处理
 *
 * @author smartor
 * @date 2023-03-22
 * 指标选项库Service业务层处理
 *
 * @author ruoyi
 * @date 2023-12-14
 */
@Slf4j
@Service
public class IvrLibaTargetServiceImpl implements IIvrLibaTargetService
{
public class IvrLibaTargetServiceImpl implements IIvrLibaTargetService {
    @Autowired
    private IvrLibaTargetMapper ivrLibaTargetMapper;
    @Autowired
    private IvrLibaTargetTagMapper ivrLibaTargetTagMapper;
    @Autowired
    private IvrLibaTargetoptionMapper ivrLibaTargetoptionMapper;
    /**
     * 查询指标库
     *
     * @param targetID 指标库主键
     * @return 指标库
     * 查询指标选项库
     *
     * @param id 指标选项库主键
     * @return 指标选项库
     */
    @Override
    public IvrLibaTarget selectIvrLibaTargetByTargetID(String targetID)
    {
        return ivrLibaTargetMapper.selectIvrLibaTargetByTargetID(targetID);
    public IvrLibaTarget selectIvrLibaTargetByTargetID(Long id) {
        return ivrLibaTargetMapper.selectIvrLibaTargetByTargetID(id);
    }
    /**
     * 查询指标库列表
     *
     * @param ivrLibaTarget 指标库
     * @return 指标库
     * 查询指标选项库列表
     *
     * @param ivrLibaTarget 指标选项库
     * @return 指标选项库
     */
    @Override
    public List<IvrLibaTarget> selectIvrLibaTargetList(IvrLibaTarget ivrLibaTarget)
    {
    public List<IvrLibaTarget> selectIvrLibaTargetList(IvrLibaTarget ivrLibaTarget) {
        return ivrLibaTargetMapper.selectIvrLibaTargetList(ivrLibaTarget);
    }
    @Override
    public List<IvrLibaTargetVO> targetInfo(IvrLibaTarget ivrLibaTarget) {
        List<IvrLibaTarget> ivrLibaTargets = ivrLibaTargetMapper.selectIvrLibaTargetList(ivrLibaTarget);
        List<IvrLibaTargetVO> ivrLibaTargetVOS = DtoConversionUtils.sourceToTarget(ivrLibaTargets, IvrLibaTargetVO.class);
        for (IvrLibaTargetVO ivrLibaTarget1 : ivrLibaTargetVOS) {
            ivrLibaTarget1.setSuitWayList(JSONArray.parseArray(ivrLibaTarget1.getSuitWay(), String.class));
            IvrLibaTargetoption ivrLibaTargetoption = new IvrLibaTargetoption();
            ivrLibaTargetoption.setTargetid(ivrLibaTarget1.getId());
            //获取该指标的选项
            List<IvrLibaTargetoption> ivrLibaTargetoptions = ivrLibaTargetoptionMapper.selectIvrLibaTargetoptionList(ivrLibaTargetoption);
            if (CollectionUtils.isNotEmpty(ivrLibaTargetoptions)) {
                for (IvrLibaTargetoption ivrLibaTargetoption1 : ivrLibaTargetoptions) {
                    ObjectMapper objectMapper = new ObjectMapper();
                    try {
                        if (StringUtils.isNotEmpty(ivrLibaTargetoption1.getDynamiccruxsJson()))
                            ivrLibaTargetoption1.setDynamiccruxs(objectMapper.readValue(ivrLibaTargetoption1.getDynamiccruxsJson(), List.class));
                        if (StringUtils.isNotEmpty(ivrLibaTargetoption1.getNodynamiccruxsJson()))
                            ivrLibaTargetoption1.setNodynamiccruxs(objectMapper.readValue(ivrLibaTargetoption1.getNodynamiccruxsJson(), List.class));
                    } catch (JsonProcessingException e) {
                        e.printStackTrace();
                    }
                }
                ivrLibaTarget1.setTargetoptionList(ivrLibaTargetoptions);
            }
            //获取该指标的标签
            IvrLibaTargetTag ivrLibaTargetTag = new IvrLibaTargetTag();
            ivrLibaTargetTag.setTargetid(ivrLibaTarget1.getId());
            List<IvrLibaTargetTag> ivrLibaTargetTags = ivrLibaTargetTagMapper.selectIvrLibaTargetTagList(ivrLibaTargetTag);
            ivrLibaTarget1.setIvrLibaTargetTagList(ivrLibaTargetTags);
        }
        //判断入参条件中,tagName是否有值,如果有值,则需要根据给的值进行标签过滤
        if (StringUtils.isNotEmpty(ivrLibaTarget.getTagName())) {
            List<IvrLibaTargetVO> ivrLibaTargetVOList = new ArrayList<>();
            aa:
            for (IvrLibaTargetVO ivrLibaTarget1 : ivrLibaTargetVOS) {
                for (IvrLibaTargetTag ivrLibaTargetTag1 : ivrLibaTarget1.getIvrLibaTargetTagList()) {
                    if (ivrLibaTargetTag1.getTagname().contains(ivrLibaTarget.getTagName().trim())) {
                        ivrLibaTargetVOList.add(ivrLibaTarget1);
                        continue aa;
                    }
                }
            }
            ivrLibaTargetVOS = ivrLibaTargetVOList;
        }
        //下面用左外感觉有点问题,where中的option的del会导致左边的查询不全,先不用吧
//        ivrLibaTargetMapper.targetInfo(ivrLibaTarget)
        return ivrLibaTargetVOS;
    }
    /**
     * 新增指标库
     *
     * @param ivrLibaTarget 指标库
     * 新增指标选项库
     *
     * @param ivrLibaTargetVO 指标选项库
     * @return 结果
     */
    @Override
    public int insertIvrLibaTarget(IvrLibaTarget ivrLibaTarget)
    {
    @Transactional(rollbackFor = Exception.class)
    public int insertIvrLibaTarget(IvrLibaTargetVO ivrLibaTargetVO) {
        //新增指标信息
        IvrLibaTarget ivrLibaTarget = DtoConversionUtils.sourceToTarget(ivrLibaTargetVO, IvrLibaTarget.class);
        ivrLibaTarget.setCreateTime(DateUtils.getNowDate());
        return ivrLibaTargetMapper.insertIvrLibaTarget(ivrLibaTarget);
        ivrLibaTargetMapper.insertIvrLibaTarget(ivrLibaTarget);
        //新增该指标对应的标签信息
        for (IvrLibaTargetTag ivrLibaTargetTag : ivrLibaTargetVO.getIvrLibaTargetTagList()) {
            ivrLibaTargetTag.setTargetid(ivrLibaTarget.getId());
            ivrLibaTargetTagMapper.insertIvrLibaTargetTag(ivrLibaTargetTag);
        }
        //新增该指标对应的指标选项信息
        for (IvrLibaTargetoption targetoption : ivrLibaTargetVO.getTargetoptionList()) {
            if (ObjectUtils.isEmpty(targetoption)) continue;
            log.info("targetoption的数据为:{}", targetoption);
            targetoption.setTargetid(ivrLibaTarget.getId());
            ivrLibaTargetoptionMapper.insertIvrLibaTargetoption(targetoption);
        }
        log.info("指标id为:{}", ivrLibaTarget.getId().intValue());
        return ivrLibaTarget.getId().intValue();
    }
    /**
     * 修改指标库
     *
     * @param ivrLibaTarget 指标库
     * 新增或修改指标选项库
     *
     * @param ivrLibaTargetVO 指标选项库
     * @return 结果
     */
    @Override
    public int updateIvrLibaTarget(IvrLibaTarget ivrLibaTarget)
    {
    @Transactional(rollbackFor = Exception.class)
    public int saveOrupdateIvrLibaTarget(IvrLibaTargetVO ivrLibaTargetVO) {
        IvrLibaTarget ivrLibaTarget = DtoConversionUtils.sourceToTarget(ivrLibaTargetVO, IvrLibaTarget.class);
        if (ObjectUtils.isNotEmpty(ivrLibaTargetVO.getSuitWayList()))
            ivrLibaTarget.setSuitWay(JSON.toJSONString(ivrLibaTargetVO.getSuitWayList()));
        ivrLibaTarget.setUpdateTime(DateUtils.getNowDate());
        return ivrLibaTargetMapper.updateIvrLibaTarget(ivrLibaTarget);
        if (ivrLibaTargetVO.getIsoperation() != null && ivrLibaTargetVO.getIsoperation() == 1) {
            //新增
            ivrLibaTarget.setCreateTime(new Date());
            ivrLibaTarget.setUpdateTime(new Date());
            ivrLibaTargetMapper.insertIvrLibaTarget(ivrLibaTarget);
        } else if (ivrLibaTargetVO.getIsoperation() != null && ivrLibaTargetVO.getIsoperation() == 2) {
            //修改
            ivrLibaTarget.setUpdateTime(new Date());
            ivrLibaTargetMapper.updateIvrLibaTarget(ivrLibaTarget);
        }
        if (CollectionUtils.isNotEmpty(ivrLibaTargetVO.getIvrLibaTargetTagList())) {
            for (IvrLibaTargetTag ivrLibaTargetTag : ivrLibaTargetVO.getIvrLibaTargetTagList()) {
                if (ivrLibaTargetTag.getId() == null) {
                    //新增
                    ivrLibaTargetTag.setTargetid(ivrLibaTarget.getId());
                    ivrLibaTargetTagMapper.insertIvrLibaTargetTag(ivrLibaTargetTag);
                } else if (ivrLibaTargetTag.getId() != null) {
                    //前端页面应该没有修改
                    ivrLibaTargetTagMapper.updateIvrLibaTargetTag(ivrLibaTargetTag);
                }
            }
        }
        if (CollectionUtils.isNotEmpty(ivrLibaTargetVO.getTargetoptionList()) && ivrLibaTargetVO.getValueType() == 1) {
            //选项处理
            for (IvrLibaTargetoption ivrLibaTargetoption : ivrLibaTargetVO.getTargetoptionList()) {
                if (CollectionUtils.isNotEmpty(ivrLibaTargetoption.getNodynamiccruxs())) {
                    ivrLibaTargetoption.setNodynamiccruxsJson(new Gson().toJson(ivrLibaTargetoption.getNodynamiccruxs()));
                }
                if (CollectionUtils.isNotEmpty(ivrLibaTargetoption.getDynamiccruxs())) {
                    ivrLibaTargetoption.setDynamiccruxsJson(new Gson().toJson(ivrLibaTargetoption.getDynamiccruxs()));
                }
                if (ivrLibaTargetoption.getIsoperation() != null && ivrLibaTargetoption.getIsoperation() == 1) {
                    //新增
                    ivrLibaTargetoption.setTargetid(ivrLibaTarget.getId());
                    ivrLibaTargetoptionMapper.insertIvrLibaTargetoption(ivrLibaTargetoption);
                } else if (ivrLibaTargetoption.getIsoperation() != null && ivrLibaTargetoption.getIsoperation() == 2) {
                    //修改
                    ivrLibaTargetoptionMapper.updateIvrLibaTargetoption(ivrLibaTargetoption);
                } else if (ivrLibaTargetoption.getIsoperation() != null && ivrLibaTargetoption.getIsoperation() == 3) {
                    // 删除
                    ivrLibaTargetoptionMapper.deleteIvrLibaTargetoptionByTargetoptionid(ivrLibaTargetoption.getTargetoptionid());
                }
            }
        } else if (ivrLibaTargetVO.getValueType() == 2 || ivrLibaTargetVO.getValueType() == 3) {
        }
        return ivrLibaTarget.getId().intValue();
    }
    /**
     * 批量删除指标库
     *
     * @param targetIDs 需要删除的指标库主键
     * 批量删除指标选项库
     *
     * @return 结果
     */
    @Override
    public int deleteIvrLibaTargetByTargetIDs(String[] targetIDs)
    {
        return ivrLibaTargetMapper.deleteIvrLibaTargetByTargetIDs(targetIDs);
    public int deleteIvrLibaTargetByTargetIDs(Long[] ids) {
        return ivrLibaTargetMapper.deleteIvrLibaTargetByTargetIDs(ids);
    }
    /**
     * 删除指标库信息
     *
     * @param targetID 指标库主键
     * 删除指标选项库信息
     *
     * @return 结果
     */
    @Override
    public int deleteIvrLibaTargetByTargetID(String targetID)
    {
        return ivrLibaTargetMapper.deleteIvrLibaTargetByTargetID(targetID);
    public int deleteIvrLibaTargetByTargetID(Long id) {
        return ivrLibaTargetMapper.deleteIvrLibaTargetByTargetID(id);
    }
    /**
     * 指标测试问题匹配
     */
    @Override
    public String targetQuesMate(IvrLibaTargetVO ivrLibaTargetVO) {
        for (IvrLibaTargetoption ivrLibaTargetoption : ivrLibaTargetVO.getTargetoptionList()) {
            Matcher matcher = null;
            if (StringUtils.isNotEmpty(ivrLibaTargetoption.getTargetregex())) {
                Pattern pattern = Pattern.compile(ivrLibaTargetoption.getTargetregex());
                matcher = pattern.matcher(ivrLibaTargetVO.getContent());
            }
            //不包含
            Matcher matcher2 = null;
            if (StringUtils.isNotEmpty(ivrLibaTargetoption.getTargetregex2())) {
                Pattern pattern2 = Pattern.compile(ivrLibaTargetoption.getTargetregex2());
                matcher2 = pattern2.matcher(ivrLibaTargetVO.getContent());
            }
            if (StringUtils.isNotEmpty(ivrLibaTargetoption.getTargetregex2()) && matcher2.matches() && StringUtils.isNotEmpty(ivrLibaTargetoption.getTargetregex()) && matcher.matches() || StringUtils.isEmpty(ivrLibaTargetoption.getTargetregex2()) && StringUtils.isNotEmpty(ivrLibaTargetoption.getTargetregex()) && matcher.matches()) {
                return ivrLibaTargetoption.getTargetvalue();
            }
        }
        return null;
    }
}