package com.ruoyi.project.service.impl;

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

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.project.mapper.TrsDomainWordsMapper;
import com.ruoyi.project.domain.TrsDomainWords;
import com.ruoyi.project.service.ITrsDomainWordsService;
import com.ruoyi.common.core.text.Convert;

/**
 * 领域常识Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-03-21
 */
@Service
public class TrsDomainWordsServiceImpl implements ITrsDomainWordsService 
{
    @Autowired
    private TrsDomainWordsMapper trsDomainWordsMapper;

    /**
     * 查询领域常识
     * 
     * @param id 领域常识主键
     * @return 领域常识
     */
    @Override
    public TrsDomainWords selectTrsDomainWordsById(Long id)
    {
        return trsDomainWordsMapper.selectTrsDomainWordsById(id);
    }

    /**
     * 查询领域常识列表
     * 
     * @param trsDomainWords 领域常识
     * @return 领域常识
     */
    @Override
    public List<TrsDomainWords> selectTrsDomainWordsList(TrsDomainWords trsDomainWords)
    {
        return trsDomainWordsMapper.selectTrsDomainWordsList(trsDomainWords);
    }

    /**
     * 新增领域常识
     * 
     * @param trsDomainWords 领域常识
     * @return 结果
     */
    @Override
    public int insertTrsDomainWords(TrsDomainWords trsDomainWords)
    {
        List<TrsDomainWords> sensitiveWords = trsDomainWordsMapper.selectTrsWordsListByErrorWord(trsDomainWords.getWrongWord());
        if(sensitiveWords.size()>0){
            return 401;
        }else {
            return trsDomainWordsMapper.insertTrsDomainWords(trsDomainWords);
        }
    }

    /**
     * 修改领域常识
     * 
     * @param trsDomainWords 领域常识
     * @return 结果
     */
    @Override
    public int updateTrsDomainWords(TrsDomainWords trsDomainWords)
    {
        return trsDomainWordsMapper.updateTrsDomainWords(trsDomainWords);
    }

    /**
     * 批量删除领域常识
     * 
     * @param ids 需要删除的领域常识主键
     * @return 结果
     */
    @Override
    public int deleteTrsDomainWordsByIds(String ids)
    {
        return trsDomainWordsMapper.deleteTrsDomainWordsByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除领域常识信息
     * 
     * @param id 领域常识主键
     * @return 结果
     */
    @Override
    public int deleteTrsDomainWordsById(Long id)
    {
        return trsDomainWordsMapper.deleteTrsDomainWordsById(id);
    }

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

}
