package com.platform.modules.sensitiveword.service.impl;

import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

import javax.annotation.PostConstruct;

import com.platform.common.exception.RRException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.platform.common.utils.PageUtils;
import com.platform.common.utils.Query;
import com.platform.common.utils.R;
import com.platform.common.utils.SensitiveWordUtil;
import com.platform.modules.sensitiveword.dao.SensitiveWordDao;
import com.platform.modules.sensitiveword.entity.SensitiveStopWordEntity;
import com.platform.modules.sensitiveword.entity.SensitiveWordEntity;
import com.platform.modules.sensitiveword.service.SensitiveStopWordService;
import com.platform.modules.sensitiveword.service.SensitiveWordService;

@Service("sensitiveWordService")
public class SensitiveWordServiceImpl extends ServiceImpl<SensitiveWordDao, SensitiveWordEntity>
		implements
			SensitiveWordService {

	@Autowired
	private SensitiveStopWordService sensitiveStopWordService;

	private SensitiveWordUtil sensitiveWordUtil = new SensitiveWordUtil();

	@PostConstruct
	public void init() {
		// 查找、构建敏感词及对应替换词
		List<SensitiveWordEntity> sensitiveWordEntities = queryAll();
		Map<String, String> sensitiveWords = sensitiveWordEntities.stream()
				.collect(Collectors.toMap(SensitiveWordEntity::getSensitiveWord, SensitiveWordEntity::getReplaceWord));
		// 查找、构建停顿词
		List<SensitiveStopWordEntity> stopWordEntities = sensitiveStopWordService.queryAll();
		Set<String> stopWords = stopWordEntities.stream().map(SensitiveStopWordEntity::getStopWord)
				.collect(Collectors.toSet());
		// 初始化敏感词库工具
		sensitiveWordUtil.init(sensitiveWords, stopWords);

	}

	private List<SensitiveWordEntity> queryAll() {
		EntityWrapper<SensitiveWordEntity> wrapper = new EntityWrapper<>();
		wrapper.eq("is_delete", 0);
		return selectList(wrapper);
	}

	@Override
	public PageUtils queryPage(Map<String, Object> params) {
		EntityWrapper<SensitiveWordEntity> wrapper = new EntityWrapper<>();
		wrapper.eq("is_delete", 0);
		Page<SensitiveWordEntity> page = this.selectPage(new Query<SensitiveWordEntity>(params).getPage(), wrapper);

		return new PageUtils(page);
	}

	@Override
	public PageUtils queryStopWordsPage(Map<String, Object> params) {
		return sensitiveStopWordService.queryPage(params);

	}

	@Override
	public void add(SensitiveWordEntity sensitiveWordEntity, Long userId) {
		String sensitiveWord = sensitiveWordEntity.getSensitiveWord();
		String replaceWord = sensitiveWordEntity.getReplaceWord();
		if (sensitiveWord == null) {
			return;
		}
		if (replaceWord == null) {
			replaceWord = sensitiveWordUtil.getReplaceChars('*', sensitiveWord.length());
		}
		EntityWrapper<SensitiveWordEntity> wrapper = new EntityWrapper<>();
		wrapper.eq("sensitive_word", sensitiveWord);
		if (selectCount(wrapper) > 0 ) {
		    throw new RRException("重复的敏感词");
        }
		sensitiveWordUtil.addSensitiveWordAndReplaceWord(sensitiveWord, replaceWord);
		sensitiveWordEntity.setIsDelete(0);
		sensitiveWordEntity.setCreateTime(new Date());
		sensitiveWordEntity.setCreateBy(userId);
		insert(sensitiveWordEntity);
	}

	@Override
	public void updateSensitive(SensitiveWordEntity sensitiveWordEntity) {
		String sensitiveWord = sensitiveWordEntity.getSensitiveWord();
		String replaceWord = sensitiveWordEntity.getReplaceWord();
		SensitiveWordEntity oriEntity = selectById(sensitiveWordEntity);
		if (oriEntity == null || oriEntity.getIsDelete() == 1) {
			return;
		}
		if (sensitiveWord == null) {
			return;
		}
		if (replaceWord == null) {
			replaceWord = sensitiveWordUtil.getReplaceChars('*', sensitiveWord.length());
		}
		sensitiveWordUtil.deleteSensitive(sensitiveWord);

		sensitiveWordUtil.addSensitiveWordAndReplaceWord(sensitiveWord, replaceWord);
		oriEntity.setSensitiveWord(sensitiveWord);
		oriEntity.setReplaceWord(replaceWord);
		updateById(oriEntity);
	}

	@Override
	public void deleteByIds(Integer[] ids) {
		for (Integer id : ids) {
			SensitiveWordEntity entity = selectById(id);
			if (entity != null) {
				String sensitiveWord = entity.getSensitiveWord();
				if (sensitiveWord != null) {
					sensitiveWordUtil.deleteSensitive(sensitiveWord);
					entity.setIsDelete(1);
					updateById(entity);
				}
			}
		}
	}

	@Override
	public String replace(String txt) {
		return sensitiveWordUtil.replaceSensitiveWord(txt);
	}

	@Override
	public R addStopWord(SensitiveStopWordEntity sensitiveStopWord, Long userId) {
		String stopWord = sensitiveStopWord.getStopWord();
		if (stopWord == null) {
			return R.error("空的停顿符");
		}
		if (stopWord.length() != 1) {
			return R.error("停顿符字符错误" + stopWord.length());
		}
		// 重复停顿符判断
		EntityWrapper<SensitiveStopWordEntity> wrapper = new EntityWrapper<>();
		wrapper.eq("stop_word", stopWord);
		Integer count = sensitiveStopWordService.selectCount(wrapper);
		if (count > 0) {
			return R.error("已存在该停顿词");
		}
		sensitiveWordUtil.addStopWord(stopWord);
		sensitiveStopWord.setCreateBy(userId);
		sensitiveStopWord.setCreateTime(new Date());
		sensitiveStopWord.setIsDelete(0);
		sensitiveStopWordService.insert(sensitiveStopWord);
		return R.ok();
	}

	@Override
	public R updateStopWord(SensitiveStopWordEntity sensitiveStopWord) {
		String stopWord = sensitiveStopWord.getStopWord();
		if (stopWord == null) {
			return R.error("空的停顿符");
		}
		if (stopWord.length() != 1) {
			return R.error("停顿符字符错误" + stopWord.length());
		}
		SensitiveStopWordEntity oriEntity = sensitiveStopWordService.selectById(sensitiveStopWord);
		if (oriEntity == null || oriEntity.getIsDelete() == 1) {
			return R.ok();
		}
		String oriStopWord = oriEntity.getStopWord();
		sensitiveWordUtil.deleteStopWord(oriStopWord);
		sensitiveWordUtil.addStopWord(stopWord);
		oriEntity.setStopWord(stopWord);
		sensitiveStopWordService.updateById(oriEntity);
		return R.ok();
	}

	@Override
	public void deleteStopWordsByIds(Integer[] ids) {

		for (Integer id : ids) {

			SensitiveStopWordEntity oriEntity = sensitiveStopWordService.selectById(id);
			if (oriEntity == null || oriEntity.getIsDelete() == 1) {
				return;
			}
			String stopWord = oriEntity.getStopWord();
			sensitiveWordUtil.deleteStopWord(stopWord);
			oriEntity.setIsDelete(1);
            sensitiveStopWordService.updateById(oriEntity);
		}
	}
}
