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.TrsTechnicalWordsMapper;
import com.ruoyi.project.domain.TrsTechnicalWords;
import com.ruoyi.project.service.ITrsTechnicalWordsService;
import com.ruoyi.common.core.text.Convert;

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

/**
 * 专业术语Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-03-21
 */
@Service
public class TrsTechnicalWordsServiceImpl implements ITrsTechnicalWordsService 
{
    @Autowired
    private TrsTechnicalWordsMapper trsTechnicalWordsMapper;

    /**
     * 查询专业术语
     * 
     * @param id 专业术语主键
     * @return 专业术语
     */
    @Override
    public TrsTechnicalWords selectTrsTechnicalWordsById(Long id)
    {
        return trsTechnicalWordsMapper.selectTrsTechnicalWordsById(id);
    }

    /**
     * 查询专业术语列表
     * 
     * @param trsTechnicalWords 专业术语
     * @return 专业术语
     */
    @Override
    public List<TrsTechnicalWords> selectTrsTechnicalWordsList(TrsTechnicalWords trsTechnicalWords)
    {
        Long deptId = getSysUser().getDeptId();
        if(deptId == 100L){
            trsTechnicalWords.setDeptId(100L);
        }
        return trsTechnicalWordsMapper.selectTrsTechnicalWordsList(trsTechnicalWords);
    }

    /**
     * 新增专业术语
     * 
     * @param trsTechnicalWords 专业术语
     * @return 结果
     */
    @Override
    public int insertTrsTechnicalWords(TrsTechnicalWords trsTechnicalWords)
    {
        List<TrsTechnicalWords> sensitiveWords = trsTechnicalWordsMapper.selectTrsWordsListByErrorWord(trsTechnicalWords.getWrongWord());
        if(sensitiveWords.size()>0){
//            return -401;
            return trsTechnicalWordsMapper.insertTrsTechnicalWords(trsTechnicalWords);
        }else {
            return trsTechnicalWordsMapper.insertTrsTechnicalWords(trsTechnicalWords);
        }
    }

    /**
     * 修改专业术语
     * 
     * @param trsTechnicalWords 专业术语
     * @return 结果
     */
    @Override
    public int updateTrsTechnicalWords(TrsTechnicalWords trsTechnicalWords)
    {
        return trsTechnicalWordsMapper.updateTrsTechnicalWords(trsTechnicalWords);
    }

    /**
     * 批量删除专业术语
     * 
     * @param ids 需要删除的专业术语主键
     * @return 结果
     */
    @Override
    public int deleteTrsTechnicalWordsByIds(String ids)
    {
        return trsTechnicalWordsMapper.deleteTrsTechnicalWordsByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除专业术语信息
     * 
     * @param id 专业术语主键
     * @return 结果
     */
    @Override
    public int deleteTrsTechnicalWordsById(Long id)
    {
        return trsTechnicalWordsMapper.deleteTrsTechnicalWordsById(id);
    }

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

}
