package com.ruoyi.project.service.impl;

import java.util.Date;
import java.util.List;

import com.ruoyi.common.annotation.DataScope;
import com.ruoyi.project.domain.TrsPolicyWords;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.project.mapper.TrsSensitiveWordsMapper;
import com.ruoyi.project.domain.TrsSensitiveWords;
import com.ruoyi.project.service.ITrsSensitiveWordsService;
import com.ruoyi.common.core.text.Convert;

import static com.ruoyi.common.utils.ShiroUtils.getSysUser;

/**
 * 常见错误Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-03-17
 */
@Service
public class TrsSensitiveWordsServiceImpl implements ITrsSensitiveWordsService 
{
    @Autowired
    private TrsSensitiveWordsMapper trsSensitiveWordsMapper;

    /**
     * 查询常见错误
     * 
     * @param id 常见错误主键
     * @return 常见错误
     */
    @DataScope(deptAlias = "d")
    public TrsSensitiveWords selectTrsSensitiveWordsById(Long id)
    {
        TrsSensitiveWords param = new TrsSensitiveWords();
        param.setId(id);
        return trsSensitiveWordsMapper.selectTrsSensitiveWordsById(param);
    }

    /**
     * 查询常见错误列表
     * 
     * @param trsSensitiveWords 常见错误
     * @return 常见错误
     */
    public List<TrsSensitiveWords> selectTrsSensitiveWordsList(TrsSensitiveWords trsSensitiveWords)
    {
        Long deptId = getSysUser().getDeptId();
        if(deptId == 100L){
            trsSensitiveWords.setDeptId(100L);
        }
        List<TrsSensitiveWords> trsSensitiveWordsList = trsSensitiveWordsMapper.selectTrsSensitiveWordsList(trsSensitiveWords);
        return trsSensitiveWordsList;
    }

    /**
     * 新增常见错误
     * 
     * @param trsSensitiveWords 常见错误
     * @return 结果
     */
    @DataScope(deptAlias = "d")
    public int insertTrsSensitiveWords(TrsSensitiveWords trsSensitiveWords)
    {
        TrsSensitiveWords param = new TrsSensitiveWords();
        param.setWrongWord(trsSensitiveWords.getWrongWord());
        List<TrsSensitiveWords> sensitiveWords = trsSensitiveWordsMapper.selectTrsWordsListByWrongWord(param);
        if(sensitiveWords.size()>0){
//            return -401;
            return trsSensitiveWordsMapper.insertTrsSensitiveWords(trsSensitiveWords);
        }else {
            return trsSensitiveWordsMapper.insertTrsSensitiveWords(trsSensitiveWords);
        }
    }

    /**
     * 修改常见错误
     * 
     * @param trsSensitiveWords 常见错误
     * @return 结果
     */
    @DataScope(deptAlias = "d")
    public int updateTrsSensitiveWords(TrsSensitiveWords trsSensitiveWords)
    {
        return trsSensitiveWordsMapper.updateTrsSensitiveWords(trsSensitiveWords);
    }

    /**
     * 批量删除常见错误
     * 
     * @param ids 需要删除的常见错误主键
     * @return 结果
     */
    @Override
    public int deleteTrsSensitiveWordsByIds(String ids)
    {
        return trsSensitiveWordsMapper.deleteTrsSensitiveWordsByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除常见错误信息
     * 
     * @param id 常见错误主键
     * @return 结果
     */
    @Override
    public int deleteTrsSensitiveWordsById(Long id)
    {
        return trsSensitiveWordsMapper.deleteTrsSensitiveWordsById(id);
    }

    @DataScope(deptAlias = "d")
    public String importWords(List<TrsSensitiveWords> wordList, boolean updateSupport, Long deptId) {
        StringBuilder message = new StringBuilder("导入成功"+System.lineSeparator());  // 初始就加上换行符
        for (TrsSensitiveWords word : wordList) {
            word.setDeptId(deptId);
            word.setTime(new Date());
            if(updateSupport){
                if (word.getWrongWord() == null || "".equals(word.getWrongWord().trim())) {
                    continue;
                }
                TrsSensitiveWords param = new TrsSensitiveWords();
                param.setWrongWord(word.getWrongWord());
                List<TrsSensitiveWords> sensitiveWords = trsSensitiveWordsMapper.selectTrsWordsListByWrongWord(param);
                if(sensitiveWords.size()>0){
                    message.append(word.getWrongWord()).append(" 已存在"+System.lineSeparator());  // 使用StringBuilder更高效
                    word.setId(sensitiveWords.get(0).getId());
                    word.setStatus("待审核");
                    trsSensitiveWordsMapper.updateTrsSensitiveWords(word);
                } else {
                    word.setStatus("待审核");
                    trsSensitiveWordsMapper.insertTrsSensitiveWords(word);
                }
            } else {
                word.setStatus("待审核");
                trsSensitiveWordsMapper.insertTrsSensitiveWords(word);
            }
            System.out.println(word);
        }
        return message.toString();
    }
}
