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.TrsNameWordsMapper;
import com.ruoyi.project.domain.TrsNameWords;
import com.ruoyi.project.service.ITrsNameWordsService;
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 TrsNameWordsServiceImpl implements ITrsNameWordsService 
{
    @Autowired
    private TrsNameWordsMapper trsNameWordsMapper;

    /**
     * 查询人名职务
     * 
     * @param id 人名职务主键
     * @return 人名职务
     */
    @Override
    public TrsNameWords selectTrsNameWordsById(Long id)
    {
        return trsNameWordsMapper.selectTrsNameWordsById(id);
    }

    /**
     * 查询人名职务列表
     * 
     * @param trsNameWords 人名职务
     * @return 人名职务
     */
    @Override
    public List<TrsNameWords> selectTrsNameWordsList(TrsNameWords trsNameWords)
    {
        Long deptId = getSysUser().getDeptId();
        if(deptId == 100L){
            trsNameWords.setDeptId(100L);
        }
        return trsNameWordsMapper.selectTrsNameWordsList(trsNameWords);
    }

    /**
     * 新增人名职务
     * 
     * @param trsNameWords 人名职务
     * @return 结果
     */
    @Override
    public int insertTrsNameWords(TrsNameWords trsNameWords)
    {
        List<TrsNameWords> sensitiveWords = trsNameWordsMapper.selectTrsWordsListByErrorWord(trsNameWords.getWrongWord());
        if(sensitiveWords.size()>0){
//            return -401;
            return trsNameWordsMapper.insertTrsNameWords(trsNameWords);
        }else {
            return trsNameWordsMapper.insertTrsNameWords(trsNameWords);
        }
    }

    /**
     * 修改人名职务
     * 
     * @param trsNameWords 人名职务
     * @return 结果
     */
    @Override
    public int updateTrsNameWords(TrsNameWords trsNameWords)
    {
        return trsNameWordsMapper.updateTrsNameWords(trsNameWords);
    }

    /**
     * 批量删除人名职务
     * 
     * @param ids 需要删除的人名职务主键
     * @return 结果
     */
    @Override
    public int deleteTrsNameWordsByIds(String ids)
    {
        return trsNameWordsMapper.deleteTrsNameWordsByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除人名职务信息
     * 
     * @param id 人名职务主键
     * @return 结果
     */
    @Override
    public int deleteTrsNameWordsById(Long id)
    {
        return trsNameWordsMapper.deleteTrsNameWordsById(id);
    }

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

}
