package com.fs.qw.service.impl;

import com.fs.common.BeanCopyUtils;
import com.fs.common.core.domain.R;
import com.fs.common.utils.DateUtils;
import com.fs.company.service.ICompanyConfigService;
import com.fs.course.mapper.FsCourseSopLogsMapper;
import com.fs.course.mapper.FsCourseSopMapper;
import com.fs.course.mapper.FsCourseWatchLogMapper;
import com.fs.crm.mapper.CrmCustomerMapper;
import com.fs.qw.domain.QwExternalContact;
import com.fs.qw.domain.QwTag;
import com.fs.qw.domain.QwTagGroup;
import com.fs.qw.mapper.*;
import com.fs.qw.param.QwExternalContactAddTagParam;
import com.fs.qw.service.IQwContactWayService;
import com.fs.qw.service.IQwExternalContactCrmService;
import com.fs.qw.service.IQwExternalContactService;
import com.fs.qw.service.IQwTagGroupService;
import com.fs.qw.vo.QwTagGroupAddParam;
import com.fs.qw.vo.QwTagGroupListVO;
import com.fs.qw.vo.QwTagGroupVO;
import com.fs.qw.vo.QwTagVO;
import com.fs.qwApi.domain.QwAddTagResult;
import com.fs.qwApi.domain.QwResult;
import com.fs.qwApi.domain.QwTagResult;
import com.fs.qwApi.domain.inner.InTag;
import com.fs.qwApi.domain.inner.TagData;
import com.fs.qwApi.domain.inner.TagGroup;
import com.fs.qwApi.param.QwAddTagParam;
import com.fs.qwApi.param.QwEditTagParam;
import com.fs.qwApi.param.QwTagParam;
import com.fs.qwApi.service.QwApiService;
import com.fs.sop.service.IQwSopService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * 企微客户标签组Service业务层处理
 *
 * @author fs
 * @date 2024-06-20
 */
@Service
@Slf4j
public class QwTagGroupServiceImpl implements IQwTagGroupService {
    @Autowired
    private QwTagGroupMapper qwTagGroupMapper;
    @Autowired
    QwApiService qwApiService;

    @Autowired
    ICompanyConfigService companyConfigService;

    @Autowired
    private QwTagMapper qwTagMapper;
    @Autowired
    private QwExternalContactMapper qwExternalContactMapper;

    @Autowired
    QwContactWayMapper qwContactWayMapper;

    @Autowired
    IQwContactWayService qwContactWayService;
    @Autowired
    QwContactWayUserMapper qwContactWayUserMapper;
    @Autowired
    QwContactWayLogsMapper qwContactWayLogsMapper;

    @Autowired
    QwFriendWelcomeMapper qwFriendWelcomeMapper;

    @Autowired
    QwAutoTagsMapper qwAutoTagsMapper;
    @Autowired
    CrmCustomerMapper crmCustomerMapper;
    @Autowired
    QwAutoTagsLogsMapper qwAutoTagsLogsMapper;

    @Autowired
    IQwSopService qwSopService;

    @Autowired
    IQwExternalContactCrmService contactCrmService;

    @Autowired
    FsCourseSopLogsMapper fsSopLogsMapper;

    @Autowired
    FsCourseSopMapper fsSopMapper;

    @Autowired
    CrmCustomerMapper customerMapper;

    @Autowired
    FsCourseWatchLogMapper fsCourseWatchLogMapper;


    @Autowired
    QwAppContactWayLogsMapper qwAppContactWayLogsMapper;


    /**
     * 查询企微客户标签组
     *
     * @param id 企微客户标签组主键
     * @return 企微客户标签组
     */
    @Override
    public QwTagGroup selectQwTagGroupById(Long id) {
        return qwTagGroupMapper.selectQwTagGroupById(id);
    }

    /**
     * 查询企微客户标签组列表
     *
     * @param qwTagGroup 企微客户标签组
     * @return 企微客户标签组
     */
    @Override
    public List<QwTagGroup> selectQwTagGroupList(QwTagGroup qwTagGroup) {
        return qwTagGroupMapper.selectQwTagGroupList(qwTagGroup);
    }

    /**
     * 新增企微客户标签组
     *
     * @param qwTagGroup 企微客户标签组
     * @return 结果
     */
    @Override
    public int insertQwTagGroup(QwTagGroup qwTagGroup) {
        qwTagGroup.setCreateTime(DateUtils.getNowDate());
        return qwTagGroupMapper.insertQwTagGroup(qwTagGroup);
    }

    /**
     * 修改企微客户标签组
     *
     * @param qwTagGroup 企微客户标签组
     * @return 结果
     */
    @Override
    public int updateQwTagGroup(QwTagGroup qwTagGroup) {
        qwTagGroup.setUpdateTime(DateUtils.getNowDate());
        return qwTagGroupMapper.updateQwTagGroup(qwTagGroup);
    }

    /**
     * 批量删除企微客户标签组
     *
     * @param ids 需要删除的企微客户标签组主键
     * @return 结果
     */
    @Override
    public R deleteQwTagGroupByIds(Long[] ids) {
        for (Long id : ids) {
            QwTagGroup qwTagGroup = qwTagGroupMapper.selectQwTagGroupById(id);
            log.info("删除标签组-"+qwTagGroup);
            if (qwTagGroup.getGroupFrom() != null && qwTagGroup.getGroupFrom() == 1) {
                QwTagParam qwTagParam = new QwTagParam();
                qwTagParam.setGroup_id(Arrays.asList(qwTagGroup.getGroupId()));
                qwApiService.delTag(qwTagParam, qwTagGroup.getCorpId());

                qwTagMapper.deleteQwTagByGroupId(qwTagGroup.getGroupId());

                qwTagGroupMapper.deleteQwTagGroupByIds(ids);

                return R.ok();

            } else {
                //不能删除非本应用创建的标签组
            }
        }
        return R.ok();
    }

    /**
     * 删除企微客户标签组信息
     *
     * @param id 企微客户标签组主键
     * @return 结果
     */
    @Override
    public int deleteQwTagGroupById(Long id) {
        return qwTagGroupMapper.deleteQwTagGroupById(id);
    }

    @Override
    public R syncQwTagGroup(String corpId) {
//        CompanyConfig companyConfig = companyConfigService.selectCompanyConfigByKey(companyId, "sys:qw:config");
//        QWCompanyConfig qwCompanyConfig = JSON.parseObject(companyConfig.getConfigValue(), QWCompanyConfig.class);
//        String corpId = qwCompanyConfig.getCorpId();

        QwTagResult qwTag = qwApiService.getTag(new QwTagParam(), corpId);

        List<TagGroup> tagGroup = qwTag.getTag_group();
        for (TagGroup group : tagGroup) {
            List<InTag> tags = group.getTag();
            QwTagGroup qwTagGroup = new QwTagGroup();
//            qwTagGroup.setCompanyId(companyId);
            qwTagGroup.setGroupId(group.getGroup_id());
            List<QwTagGroup> qwTagGroups = qwTagGroupMapper.selectQwTagGroupList(qwTagGroup);

            qwTagGroup.setName(group.getGroup_name());
            qwTagGroup.setOrder(group.getOrder());
            qwTagGroup.setCorpId(corpId);
            qwTagGroup.setCreateTime(new Date(group.getCreate_time()));
            if (qwTagGroups == null || qwTagGroups.size() < 1) {

                qwTagGroupMapper.insertQwTagGroup(qwTagGroup);
            } else {
                qwTagGroup.setId(qwTagGroups.get(0).getId());
                qwTagGroupMapper.updateQwTagGroup(qwTagGroup);
            }
            for (InTag tag : tags) {
                QwTag tag1 = new QwTag();
                tag1.setGroupId(group.getGroup_id());
                tag1.setTagId(tag.getId());
//                tag1.setCompanyId(companyId);
                List<QwTag> qwTags = qwTagMapper.selectQwTagList(tag1);
                tag1.setOrder(tag.getOrder());
                tag1.setName(tag.getName());
                tag1.setCorpId(corpId);
                tag1.setCreateTime(new Date(tag.getCreate_time()));

                if (qwTags == null || qwTags.size() < 1) {

                    qwTagMapper.insertQwTag(tag1);
                } else {
                    tag1.setId(qwTags.get(0).getId());
                    qwTagMapper.updateQwTag(tag1);

                }
            }
        }

        return R.ok();
    }

    @Override
    public List<QwTagGroupListVO> selectQwTagGroupListVO(QwTagGroup qwTagGroup) {

        List<QwTagGroupListVO> vo = qwTagGroupMapper.selectQwTagGroupListVO(qwTagGroup);

        for (QwTagGroupListVO qwTagGroupListVO : vo) {
            QwTag qwTag = new QwTag();
            qwTag.setGroupId(qwTagGroupListVO.getGroupId());
            qwTag.setCompanyId(qwTagGroupListVO.getCompanyId());
            List<QwTagVO> qwTags = qwTagMapper.selectQwTagListVO(qwTag);
            qwTagGroupListVO.setTag(qwTags);
        }
        return vo;
    }

    @Override
    public List<QwTagGroupListVO> selectQwGroupTagList(QwTagGroup qwTagGroup) {
        List<QwTagGroupListVO> vo = qwTagGroupMapper.selectQwTagGroups(qwTagGroup);

        for (QwTagGroupListVO qwTagGroupListVO : vo) {
            QwTag qwTag = new QwTag();
            qwTag.setGroupId(qwTagGroupListVO.getGroupId());
            qwTag.setCorpId(qwTagGroupListVO.getCorpId());
            qwTag.setName(qwTagGroup.getName());
            List<QwTagVO> qwTags = qwTagMapper.selectQwTagListVO(qwTag);
            qwTagGroupListVO.setTag(qwTags);
        }
        return vo;
    }

    @Override
    public QwTagGroupVO selectQwTagGroupByIdVO(Long id) {
        QwTagGroupVO qwTagGroupVO = qwTagGroupMapper.selectQwTagGroupByIdVO(id);
        QwTag qwTag = new QwTag();
        qwTag.setGroupId(qwTagGroupVO.getGroupId());
        qwTag.setCompanyId(qwTagGroupVO.getCompanyId());
        List<QwTag> qwTags = qwTagMapper.selectQwTagList(qwTag);
        qwTagGroupVO.setTag(qwTags);
        return qwTagGroupVO;
    }

    @Override
    public int insertQwTagGroupParam(QwTagGroupAddParam qwTagGroup) {
//        CompanyConfig companyConfig = companyConfigService.selectCompanyConfigByKey(companyId, "sys:qw:config");
//        QWCompanyConfig qwCompanyConfig = JSON.parseObject(companyConfig.getConfigValue(), QWCompanyConfig.class);
//        String corpId = qwCompanyConfig.getCorpId();
        QwAddTagParam qwAddTagParam = new QwAddTagParam();
        qwAddTagParam.setStrategy_id(1);
        qwAddTagParam.setGroup_name(qwTagGroup.getName());
        qwAddTagParam.setOrder(qwTagGroup.getOrder());
        List<TagData> tag = new ArrayList<>();
        List<QwTag> QwTags = qwTagGroup.getTag();
        for (QwTag qwTag : QwTags) {
            TagData tagData = new TagData();
            tagData.setOrder(qwTag.getOrder());
            tagData.setName(qwTag.getName());
            tag.add(tagData);
        }
        qwAddTagParam.setTag(tag);

        QwAddTagResult result = qwApiService.addTag(qwAddTagParam, qwTagGroup.getCorpId());
        System.out.println("标签组addTag-result:" + result);
        TagGroup group = result.getTag_group();
        List<InTag> tags = group.getTag();
        QwTagGroup qwTagGroupAdd = new QwTagGroup();

        qwTagGroupAdd.setGroupId(group.getGroup_id());
        qwTagGroupAdd.setName(group.getGroup_name());
        qwTagGroupAdd.setOrder(group.getOrder());
        qwTagGroupAdd.setCorpId(qwTagGroup.getCorpId());
        qwTagGroupAdd.setGroupFrom(1);
        qwTagGroupAdd.setCreateTime(new Date(group.getCreate_time()));
        qwTagGroupMapper.insertQwTagGroup(qwTagGroupAdd);

        for (InTag tagA : tags) {
            QwTag tag1 = new QwTag();
            tag1.setGroupId(group.getGroup_id());
            tag1.setTagId(tagA.getId());
            tag1.setOrder(tagA.getOrder());
            tag1.setName(tagA.getName());
            tag1.setCorpId(qwTagGroup.getCorpId());
            tag1.setTagFrom(1);
            tag1.setCreateTime(new Date(tagA.getCreate_time()));
            qwTagMapper.insertQwTag(tag1);

        }


        return 1;
    }

    @Override
    public int updateQwTagGroupParam(QwTagGroupAddParam qwTagGroup) {
        System.out.println("传参：" + qwTagGroup);
        QwEditTagParam qwEditTagParam = new QwEditTagParam();
        qwEditTagParam.setName(qwTagGroup.getName());
        qwEditTagParam.setId(qwTagGroup.getGroupId());
        qwEditTagParam.setOrder(qwTagGroup.getOrder());
        QwResult qwResult = qwApiService.editTag(qwEditTagParam, qwTagGroup.getCorpId());


        QwTagGroup copy = BeanCopyUtils.copy(qwTagGroup, QwTagGroup.class);
        qwTagGroupMapper.updateQwTagGroup(copy);
        List<QwTag> tag = qwTagGroup.getTag();
        QwTag qwTagList = new QwTag();
        qwTagList.setGroupId(qwTagGroup.getGroupId());

        List<QwTag> qwTags = qwTagMapper.selectQwTagList(qwTagList);


        for (QwTag qwTag : tag) {

            if (qwTag.getTagId() != null) {
                QwEditTagParam editTagParam = new QwEditTagParam();
                editTagParam.setName(qwTag.getName());
                editTagParam.setId(qwTag.getTagId());
                editTagParam.setOrder(qwTag.getOrder());
                QwResult qwResult1 = qwApiService.editTag(editTagParam, qwTagGroup.getCorpId());


                QwTag QwTagCopy = BeanCopyUtils.copy(qwTag, QwTag.class);
                qwTagMapper.updateQwTag(QwTagCopy);
            } else {
                QwAddTagParam qwAddTagParam = new QwAddTagParam();
                qwAddTagParam.setGroup_id(qwTagGroup.getGroupId());
                TagData tagData = new TagData();
                tagData.setName(qwTag.getName());
                tagData.setOrder(qwTag.getOrder());
                List<TagData> list = Arrays.asList(tagData);
                qwAddTagParam.setTag(list);
                QwAddTagResult result = qwApiService.addTag(qwAddTagParam, qwTagGroup.getCorpId());
                if (result.getErrcode() == 0) {
                    List<InTag> addTag = result.getTag_group().getTag();
                    if (addTag != null && addTag.size() > 0) {
                        QwTag addQwTag = new QwTag();
                        addQwTag.setGroupId(qwTagGroup.getGroupId());
                        addQwTag.setTagId(addTag.get(0).getId());
                        addQwTag.setOrder(addTag.get(0).getOrder());
                        addQwTag.setName(addTag.get(0).getName());
                        addQwTag.setCorpId(qwTagGroup.getCorpId());
                        addQwTag.setCreateTime(new Date(addTag.get(0).getCreate_time()));
                        addQwTag.setTagFrom(1);
                        qwTagMapper.insertQwTag(addQwTag);
                    }
                }

            }
        }

        ArrayList<String> delTags = new ArrayList<>();
        for (QwTag qwTag1 : qwTags) {
            boolean b = true;
            for (QwTag qwTag : tag) {
                if (qwTag.getId() != null && qwTag.getId() - qwTag1.getId() == 0) {
                    b = false;
                }
            }
            if (b) {
                delTags.add(qwTag1.getTagId());
                qwTagMapper.deleteQwTagById(qwTag1.getId());
            }
        }
        QwTagParam qwTagParam = new QwTagParam();
        qwTagParam.setTag_id(delTags);
        qwApiService.delTag(qwTagParam, qwTagGroup.getCorpId());
        System.out.println("delTagqwTagGroup:" + qwTagParam);
        return 1;
    }

    @Override
    public void addTagByTagId(String tagType, String tagId, String corpId) {
        if (tagType.equals("tag")) {
            QwTagParam qwTagParam = new QwTagParam();
            qwTagParam.setTag_id(Arrays.asList(tagId));
            QwTagResult tag = qwApiService.getTag(qwTagParam, corpId);
            List<TagGroup> tagGroup = tag.getTag_group();
            for (TagGroup group : tagGroup) {
                List<InTag> tag2 = group.getTag();
                for (InTag inTag : tag2) {
                    QwTag tag1 = new QwTag();
                    tag1.setGroupId(group.getGroup_id());
                    tag1.setTagId(inTag.getId());
                    tag1.setCorpId(corpId);
                    List<QwTag> qwTags = qwTagMapper.selectQwTagList(tag1);
                    if (qwTags == null || qwTags.size() < 1) {
                        tag1.setOrder(inTag.getOrder());
                        tag1.setName(inTag.getName());
                        tag1.setCorpId(corpId);
                        tag1.setCreateTime(new Date(inTag.getCreate_time()));
                        qwTagMapper.insertQwTag(tag1);
                    }
                }

            }
        } else {
            QwTagParam qwTagParam = new QwTagParam();
            qwTagParam.setGroup_id(Arrays.asList(tagId));
            QwTagResult qwTag = qwApiService.getTag(qwTagParam, corpId);
            List<TagGroup> tagGroup = qwTag.getTag_group();
            for (TagGroup group : tagGroup) {
                List<InTag> tags = group.getTag();
                QwTagGroup qwTagGroup = new QwTagGroup();
                qwTagGroup.setCorpId(corpId);
                qwTagGroup.setGroupId(group.getGroup_id());
                List<QwTagGroup> qwTagGroups = qwTagGroupMapper.selectQwTagGroupList(qwTagGroup);

                if (qwTagGroups == null || qwTagGroups.size() < 1) {
                    qwTagGroup.setName(group.getGroup_name());
                    qwTagGroup.setOrder(group.getOrder());
                    qwTagGroup.setCorpId(corpId);
                    qwTagGroup.setCreateTime(new Date(group.getCreate_time()));
                    qwTagGroupMapper.insertQwTagGroup(qwTagGroup);
                }
                for (InTag tag : tags) {
                    QwTag tag1 = new QwTag();
                    tag1.setGroupId(group.getGroup_id());
                    tag1.setTagId(tag.getId());
                    tag1.setCorpId(corpId);
                    List<QwTag> qwTags = qwTagMapper.selectQwTagList(tag1);

                    if (qwTags == null || qwTags.size() < 1) {
                        tag1.setOrder(tag.getOrder());
                        tag1.setName(tag.getName());
                        tag1.setCorpId(corpId);
                        tag1.setCreateTime(new Date(tag.getCreate_time()));

                        qwTagMapper.insertQwTag(tag1);
                    }
                }
            }
        }
    }

    @Override
    public void shuffleTagByTagId(String id, String corpId) {

        QwTagResult qwTag = qwApiService.getTag(new QwTagParam(), corpId);

        List<TagGroup> tagGroup = qwTag.getTag_group();
        for (TagGroup group : tagGroup) {
            List<InTag> tags = group.getTag();
            QwTagGroup qwTagGroup = new QwTagGroup();
            qwTagGroup.setCorpId(corpId);
            qwTagGroup.setGroupId(group.getGroup_id());
            List<QwTagGroup> qwTagGroups = qwTagGroupMapper.selectQwTagGroupList(qwTagGroup);
            for (QwTagGroup qwTagGroupa : qwTagGroups) {
                qwTagGroup.setId(qwTagGroupa.getId());
                qwTagGroup.setName(group.getGroup_name());
                qwTagGroup.setOrder(group.getOrder());
                qwTagGroupMapper.updateQwTagGroup(qwTagGroup);
            }

            for (InTag tag : tags) {
                QwTag tag1 = new QwTag();
                tag1.setGroupId(group.getGroup_id());
                tag1.setTagId(tag.getId());
                tag1.setCorpId(corpId);
                List<QwTag> qwTags = qwTagMapper.selectQwTagList(tag1);
                for (QwTag qwTag1 : qwTags) {
                    if (qwTag1.getTagId().equals(tag.getId())) {
                        tag1.setId(qwTag1.getId());
                        tag1.setName(tag.getName());
                        tag1.setOrder(tag.getOrder());
                        tag1.setCreateTime(new Date(tag.getCreate_time()));
                        qwTagMapper.updateQwTag(tag1);
                    }
                }

            }
        }
    }

    @Override
    public void deleteTagByTagId(String tagType, String id, String corpId) {
        if (tagType.equals("tag")) {
            QwTag qwTag = new QwTag();
            qwTag.setCorpId(corpId);
            qwTag.setTagId(id);
            List<QwTag> qwTags = qwTagMapper.selectQwTagList(qwTag);
            if (qwTags != null && qwTags.size() > 0) {
                for (QwTag tag : qwTags) {
                    qwTagMapper.deleteQwTagById(tag.getId());
                }
            }
        } else {
            QwTagGroup qwTagGroup = new QwTagGroup();
            qwTagGroup.setGroupId(id);
            qwTagGroup.setCorpId(corpId);
            List<QwTagGroup> qwTagGroups = qwTagGroupMapper.selectQwTagGroupList(qwTagGroup);
            if (qwTagGroups != null && qwTagGroups.size() > 0) {
                for (QwTagGroup tagGroup : qwTagGroups) {
                    qwTagGroupMapper.deleteQwTagGroupById(tagGroup.getId());
                    QwTag qwTag = new QwTag();
                    qwTag.setCorpId(corpId);
                    qwTag.setGroupId(tagGroup.getGroupId());
                    List<QwTag> qwTags = qwTagMapper.selectQwTagList(qwTag);
                    if (qwTags != null && qwTags.size() > 0) {
                        for (QwTag tag : qwTags) {
                            qwTagMapper.deleteQwTagById(tag.getId());
                        }
                    }
                }
            }

        }
    }

    @Override
    public void updateTagByTagId(String tagType, String id, String corpId) {
        if (tagType.equals("tag")) {
            QwTag qwTag = new QwTag();
            qwTag.setCorpId(corpId);
            qwTag.setTagId(id);
            List<QwTag> qwTags = qwTagMapper.selectQwTagList(qwTag);
            if (qwTags != null && qwTags.size() > 0) {
                for (QwTag tag : qwTags) {
                    QwTagParam qwTagParam = new QwTagParam();
                    qwTagParam.setTag_id(Arrays.asList(id));
                    QwTagResult tagResult = qwApiService.getTag(qwTagParam, corpId);
                    List<TagGroup> tagGroup = tagResult.getTag_group();
                    for (TagGroup group : tagGroup) {
                        List<InTag> tagS = group.getTag();
                        for (InTag inTag : tagS) {
                            QwTag tag1 = new QwTag();
                            tag1.setTagId(tag.getTagId());
                            tag1.setOrder(inTag.getOrder());
                            tag1.setName(inTag.getName());
                            tag1.setCorpId(corpId);
                            qwTagMapper.updateQwTag(tag1);
                        }
                    }

                }
            }
        } else {
            QwTagGroup tagGroup = new QwTagGroup();
            tagGroup.setCorpId(corpId);
            tagGroup.setGroupId(id);
            List<QwTagGroup> qwTagGroups = qwTagGroupMapper.selectQwTagGroupList(tagGroup);
            for (QwTagGroup qwTagGroup : qwTagGroups) {
                QwTagParam qwTagParam = new QwTagParam();
                qwTagParam.setGroup_id(Arrays.asList(id));
                QwTagResult tagResult = qwApiService.getTag(qwTagParam, corpId);
                List<TagGroup> tagGroups = tagResult.getTag_group();
                for (TagGroup group : tagGroups) {
                    QwTagGroup qwTagGroup1 = new QwTagGroup();
                    qwTagGroup1.setId(qwTagGroup.getId());
                    qwTagGroup1.setOrder(group.getOrder());
                    qwTagGroup1.setName(group.getGroup_name());
                    qwTagGroupMapper.updateQwTagGroup(qwTagGroup1);
                }
            }
        }
    }

    @Override
    public void addQwTagByAi(String trimTag, Long extId) {
        QwExternalContact qwExternalContact = qwExternalContactMapper.selectQwExternalContactById(extId);
        if (qwExternalContact != null) {
            QwTagVO qwTagVO = qwTagMapper.selectQwTagByName(trimTag, qwExternalContact.getCorpId());
            if (qwTagVO != null) {
                log.info("Ai添加标签:" + qwTagVO.getName() + ":" + qwTagVO.getTagId() + ":" + qwExternalContact.getName());
                QwExternalContactAddTagParam qwExternalContactAddTagParam = new QwExternalContactAddTagParam();
                qwExternalContactAddTagParam.setTagIds(Arrays.asList(qwTagVO.getTagId()));
                qwExternalContactAddTagParam.setUserIds(Arrays.asList(extId));
                qwExternalContactAddTagParam.setCorpId(qwExternalContact.getCorpId());
                try {
                    qwExternalContactService.addUserTag(qwExternalContactAddTagParam);
                } catch (Exception e) {
                    log.info("Ai添加标签出错:" + qwTagVO.getName() + ":" + qwExternalContact.getName());
                }
            }
        }
    }

    @Autowired
    IQwExternalContactService qwExternalContactService;

    @Override
    public void delQwTagByAi(String trimTag, Long extId) {
        QwExternalContact qwExternalContact = qwExternalContactMapper.selectQwExternalContactById(extId);
        if (qwExternalContact != null) {
            QwTagVO qwTagVO = qwTagMapper.selectQwTagByName(trimTag, qwExternalContact.getCorpId());
            if (qwTagVO != null) {
                log.info("Ai移除标签:" + qwTagVO.getName() + ":" + qwTagVO.getTagId() + ":" + qwExternalContact.getName());
                QwExternalContactAddTagParam qwExternalContactAddTagParam = new QwExternalContactAddTagParam();
                qwExternalContactAddTagParam.setTagIds(Arrays.asList(qwTagVO.getTagId()));
                qwExternalContactAddTagParam.setUserIds(Arrays.asList(extId));
                qwExternalContactAddTagParam.setCorpId(qwExternalContact.getCorpId());

                qwExternalContactService.delUserTag(qwExternalContactAddTagParam);
            }
        }

    }

    @Override
    public List<QwTagGroupListVO> selectQwTagGroupListVOPage(QwTagGroup qwTagGroup) {
        List<QwTagGroupListVO> vo = qwTagGroupMapper.selectQwTagGroupListVOPage(qwTagGroup);

        for (QwTagGroupListVO qwTagGroupListVO : vo) {
            QwTag qwTag = new QwTag();
            qwTag.setGroupId(qwTagGroupListVO.getGroupId());
            qwTag.setCompanyId(qwTagGroupListVO.getCompanyId());
            List<QwTagVO> qwTags = qwTagMapper.selectQwTagListVO(qwTag);
            qwTagGroupListVO.setTag(qwTags);
        }
        return vo;
    }
}
