package com.fs.sop.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fs.common.utils.DateUtils;
import com.fs.qw.mapper.QwTagMapper;
import com.fs.qw.param.QwExternalContactAddTagParam;
import com.fs.qw.service.IQwExternalContactService;
import com.fs.qw.vo.QwTagVO;
import com.fs.sop.domain.QwSopTag;
import com.fs.sop.domain.SopUserLogsInfo;
import com.fs.sop.mapper.QwSopTagMapper;
import com.fs.sop.mapper.SopUserLogsInfoMapper;
import com.fs.sop.service.IQwSopTagService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * sop打标签Service业务层处理
 *
 * @author fs
 * @date 2025-02-20
 */
@Service
public class QwSopTagServiceImpl extends ServiceImpl<QwSopTagMapper, QwSopTag> implements IQwSopTagService {

    private static final Logger log = LoggerFactory.getLogger(QwSopTagServiceImpl.class);
    @Autowired
    private QwSopTagMapper qwSopTagMapper;

    @Autowired
    private QwTagMapper qwTagMapper;
    @Autowired
    private SopUserLogsInfoMapper sopUserLogsInfoMapper;
    @Autowired
    IQwExternalContactService qwExternalContactService;
    /**
     * 查询sop打标签
     *
     * @param id sop打标签主键
     * @return sop打标签
     */
    @Override
    public QwSopTag selectQwSopTagById(Long id)
    {
        return baseMapper.selectQwSopTagById(id);
    }


    /**
     * 查询sop打标签列表
     *
     * @param qwSopTag sop打标签
     * @return sop打标签
     */
    @Override
    public List<QwSopTag> selectQwSopTagList(QwSopTag qwSopTag)
    {
        return baseMapper.selectQwSopTagList(qwSopTag);
    }

    /**
     * 新增sop打标签
     *
     * @param qwSopTag sop打标签
     * @return 结果
     */
    @Override
    public int insertQwSopTag(QwSopTag qwSopTag)
    {
        qwSopTag.setCreateTime(DateUtils.getNowDate());
        return baseMapper.insertQwSopTag(qwSopTag);
    }

    /**
     * 修改sop打标签
     *
     * @param qwSopTag sop打标签
     * @return 结果
     */
    @Override
    public int updateQwSopTag(QwSopTag qwSopTag)
    {
        qwSopTag.setUpdateTime(DateUtils.getNowDate());
        return baseMapper.updateQwSopTag(qwSopTag);
    }

    /**
     * 批量删除sop打标签
     *
     * @param ids 需要删除的sop打标签主键
     * @return 结果
     */
    @Override
    public int deleteQwSopTagByIds(Long[] ids)
    {
        return baseMapper.deleteQwSopTagByIds(ids);
    }

    /**
     * 删除sop打标签信息
     *
     * @param id sop打标签主键
     * @return 结果
     */
    @Override
    public int deleteQwSopTagById(Long id)
    {
        return baseMapper.deleteQwSopTagById(id);
    }

    @Override
    public void addTag() {
        List<QwSopTag> qwSopTags = baseMapper.selectQwSopTagByStatus1();
        for (QwSopTag qwSopTag : qwSopTags) {
            List<SopUserLogsInfo> sopUserLogsInfos = sopUserLogsInfoMapper.selectSopUserLogsInfoListByUserLogsId(qwSopTag.getSopUserLogsId());
            String addTags = qwSopTag.getAddTags();
            String delTags = qwSopTag.getDelTags();
            if (addTags!=null&& !addTags.equals("")) {
                ArrayList<String> tag =  tagListAdd(addTags,qwSopTag.getCorpId());
                if (tag.size()>0) {
                    for (SopUserLogsInfo sopUserLogsInfo : sopUserLogsInfos) {
                        QwExternalContactAddTagParam qwExternalContactAddTagParam = new QwExternalContactAddTagParam();
                        qwExternalContactAddTagParam.setTagIds(tag);
                        qwExternalContactAddTagParam.setUserIds(Arrays.asList(sopUserLogsInfo.getExternalId()));
                        qwExternalContactAddTagParam.setCorpId(qwSopTag.getCorpId());
                        try {
                            qwExternalContactService.addUserTag(qwExternalContactAddTagParam);
                        }catch (Exception e){
                            log.info("sop添加标签出错:"+sopUserLogsInfo.getExternalId());
                        }
                    }
                }
            }
            if (delTags!=null&& !delTags.equals("")) {
                ArrayList<String> tag =  tagListAdd(delTags,qwSopTag.getCorpId());
                if (tag.size()>0) {
                    for (SopUserLogsInfo sopUserLogsInfo : sopUserLogsInfos) {
                        QwExternalContactAddTagParam qwExternalContactAddTagParam = new QwExternalContactAddTagParam();
                        qwExternalContactAddTagParam.setTagIds(tag);
                        qwExternalContactAddTagParam.setUserIds(Arrays.asList(sopUserLogsInfo.getExternalId()));
                        qwExternalContactAddTagParam.setCorpId(qwSopTag.getCorpId());
                        try {
                            qwExternalContactService.delUserTag(qwExternalContactAddTagParam);
                        }catch (Exception e){
                            log.info("sop移除标签出错:"+sopUserLogsInfo.getExternalId());
                        }
                    }
                }
            }
            QwSopTag t = new QwSopTag();
            t.setId(qwSopTag.getId());
            t.setStatus(3);
            qwSopTagMapper.updateQwSopTag(t);
        }
    }

    ArrayList<String>  tagListAdd(String addTags,String corpId){
        ArrayList<String> tag = new ArrayList<>();
        List<String> strings = JSON.parseArray(addTags, String.class);
        for (String string : strings) {
            QwTagVO qwTagVO = qwTagMapper.selectQwTagByName(string, corpId);
            if (qwTagVO!=null){
                tag.add(qwTagVO.getTagId());
            }else {
                log.info("无标签内容");
            }
        }
        return tag;
    }
}
