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.TrsPolicyWordsMapper;
import com.ruoyi.project.domain.TrsPolicyWords;
import com.ruoyi.project.service.ITrsPolicyWordsService;
import com.ruoyi.common.core.text.Convert;

/**
 * 政治常识Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-03-21
 */
@Service
public class TrsPolicyWordsServiceImpl implements ITrsPolicyWordsService 
{
    @Autowired
    private TrsPolicyWordsMapper trsPolicyWordsMapper;

    /**
     * 查询政治常识
     * 
     * @param id 政治常识主键
     * @return 政治常识
     */
    @Override
    public TrsPolicyWords selectTrsPolicyWordsById(Long id)
    {
        return trsPolicyWordsMapper.selectTrsPolicyWordsById(id);
    }

    /**
     * 查询政治常识列表
     * 
     * @param trsPolicyWords 政治常识
     * @return 政治常识
     */
    @Override
    public List<TrsPolicyWords> selectTrsPolicyWordsList(TrsPolicyWords trsPolicyWords)
    {
        return trsPolicyWordsMapper.selectTrsPolicyWordsList(trsPolicyWords);
    }

    /**
     * 新增政治常识
     * 
     * @param trsPolicyWords 政治常识
     * @return 结果
     */
    @Override
    public int insertTrsPolicyWords(TrsPolicyWords trsPolicyWords)
    {
        List<TrsPolicyWords> sensitiveWords = trsPolicyWordsMapper.selectTrsWordsListByErrorWord(trsPolicyWords.getWrongWord());
        if(sensitiveWords.size()>0){
            return 401;
        }else {
            return trsPolicyWordsMapper.insertTrsPolicyWords(trsPolicyWords);
        }
    }

    /**
     * 修改政治常识
     * 
     * @param trsPolicyWords 政治常识
     * @return 结果
     */
    @Override
    public int updateTrsPolicyWords(TrsPolicyWords trsPolicyWords)
    {
        return trsPolicyWordsMapper.updateTrsPolicyWords(trsPolicyWords);
    }

    /**
     * 批量删除政治常识
     * 
     * @param ids 需要删除的政治常识主键
     * @return 结果
     */
    @Override
    public int deleteTrsPolicyWordsByIds(String ids)
    {
        return trsPolicyWordsMapper.deleteTrsPolicyWordsByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除政治常识信息
     * 
     * @param id 政治常识主键
     * @return 结果
     */
    @Override
    public int deleteTrsPolicyWordsById(Long id)
    {
        return trsPolicyWordsMapper.deleteTrsPolicyWordsById(id);
    }

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

}
