package com.fs.fastGpt.service.impl;

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

import com.fs.common.core.domain.R;
import com.fs.common.core.redis.RedisCache;
import com.fs.common.utils.DateUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fs.fastGpt.vo.FastgptExtUserTagVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.fs.fastGpt.mapper.FastgptExtUserTagMapper;
import com.fs.fastGpt.domain.FastgptExtUserTag;
import com.fs.fastGpt.service.IFastgptExtUserTagService;

/**
 * 处理新客标签Service业务层处理
 * 
 * @author fs
 * @date 2025-09-10
 */
@Service
public class FastgptExtUserTagServiceImpl extends ServiceImpl<FastgptExtUserTagMapper, FastgptExtUserTag> implements IFastgptExtUserTagService {

    @Autowired
    private FastgptExtUserTagMapper fastgptExtUserTagMapper;

    @Autowired
    private RedisCache redisCache;

    private static final String AI_REPLY_TAG = "AI_REPLY_TAG:";

    /**
     * 查询处理新客标签
     * 
     * @param id 处理新客标签主键
     * @return 处理新客标签
     */
    @Override
    public FastgptExtUserTag selectFastgptExtUserTagById(Long id)
    {
        return baseMapper.selectFastgptExtUserTagById(id);
    }

    /**
     * 查询处理新客标签列表
     * 
     * @param fastgptExtUserTag 处理新客标签
     * @return 处理新客标签
     */
    @Override
    public List<FastgptExtUserTag> selectFastgptExtUserTagList(FastgptExtUserTag fastgptExtUserTag)
    {
        return baseMapper.selectFastgptExtUserTagList(fastgptExtUserTag);
    }

    /**
     * 新增处理新客标签
     * 
     * @param fastgptExtUserTag 处理新客标签
     * @return 结果
     */
    @Override
    public int insertFastgptExtUserTag(FastgptExtUserTag fastgptExtUserTag)
    {
        fastgptExtUserTag.setCreateTime(DateUtils.getNowDate());
        return baseMapper.insertFastgptExtUserTag(fastgptExtUserTag);
    }

    /**
     * 修改处理新客标签
     * 
     * @param fastgptExtUserTag 处理新客标签
     * @return 结果
     */
    @Override
    public int updateFastgptExtUserTag(FastgptExtUserTag fastgptExtUserTag)
    {
        return baseMapper.updateFastgptExtUserTag(fastgptExtUserTag);
    }

    /**
     * 批量删除处理新客标签
     * 
     * @param ids 需要删除的处理新客标签主键
     * @return 结果
     */
    @Override
    public int deleteFastgptExtUserTagByIds(Long[] ids)
    {
        List<FastgptExtUserTag> fastgptExtUserTags = fastgptExtUserTagMapper.selectFastgptExtUserTagByIds(ids);
        int i = baseMapper.deleteFastgptExtUserTagByIds(ids);
        if(i > 0){
            if(fastgptExtUserTags != null && !fastgptExtUserTags.isEmpty()){
                String corpId = fastgptExtUserTags.get(0).getCorpId();
                List<String> oldCache = redisCache.getCacheObject(AI_REPLY_TAG + corpId);
                for (FastgptExtUserTag fastgptExtUserTag : fastgptExtUserTags) {
                    if(fastgptExtUserTag != null && fastgptExtUserTag.getTagId() != null){
                        if(oldCache != null && !oldCache.isEmpty()){
                            oldCache.remove(fastgptExtUserTag.getTagId());
                        }
                    }
                }
                redisCache.setCacheObject(AI_REPLY_TAG + corpId,oldCache);
            }
        }
        return i;
    }

    /**
     * 删除处理新客标签信息
     * 
     * @param id 处理新客标签主键
     * @return 结果
     */
    @Override
    public int deleteFastgptExtUserTagById(Long id)
    {
        return baseMapper.deleteFastgptExtUserTagById(id);
    }

    @Override
    public R addFastGptTagByCorpId(FastgptExtUserTagVO fastgptExtUserTag) {
        int i = 0;
        try {
            i = fastgptExtUserTagMapper.addFastGptTagByCorpId(fastgptExtUserTag);
        } catch (Exception e) {
            return R.error("添加失败,存在重复的标签");
        }
        if(i > 0){
            if(fastgptExtUserTag != null && fastgptExtUserTag.getTagIds() != null){
                List<String> oldCache = redisCache.getCacheObject(AI_REPLY_TAG + fastgptExtUserTag.getCorpId());
                if(oldCache != null && !oldCache.isEmpty()){
                    oldCache.addAll(fastgptExtUserTag.getTagIds());
                    redisCache.setCacheObject(AI_REPLY_TAG + fastgptExtUserTag.getCorpId(),oldCache);
                }else{
                    List<String> newCacheTagList = new ArrayList<>(fastgptExtUserTag.getTagIds());
                    redisCache.setCacheObject(AI_REPLY_TAG + fastgptExtUserTag.getCorpId(),newCacheTagList);
                }
            }
        }
        return R.ok("添加成功");
    }
}
