package com.yunyan.business.service.impl;

import java.util.ArrayList;
import java.util.List;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.yunyan.business.pojo.admin.vo.AlertWordCheckContentVo;
import com.yunyan.common.core.domain.AjaxResult;
import com.yunyan.common.core.page.PageDomain;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.yunyan.business.mapper.AlertWordMapper;
import com.yunyan.business.domain.AlertWord;
import com.yunyan.business.service.IAlertWordService;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yunyan.common.core.domain.AjaxResultVo;
import org.springframework.beans.BeanUtils;
import lombok.extern.slf4j.Slf4j;

import java.util.Objects;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;

/**
 * 敏感词管理Service业务层处理
 *
 * @author yunyan
 * @date 2023-07-27
 */
@Slf4j
@Service
public class AlertWordServiceImpl extends ServiceImpl<AlertWordMapper, AlertWord> implements IAlertWordService
{
    @Autowired
    private AlertWordMapper alertWordMapper;

    /**
     * 查询敏感词管理列表
     *
     * @param alertWord 敏感词管理
     * @return 敏感词管理
     */
    @Override
    public List<AlertWord> selectAlertWordList(AlertWord alertWord)
    {
        return alertWordMapper.selectAlertWordList(alertWord);
    }

    /**
    * 新增
    * @param
    * @return
    */
    @Override
    public AjaxResultVo add(AlertWord dto) {
        AlertWord entity = dto;

        this.save(entity);
        return AjaxResultVo.success();
    }

    /**
     * 编辑
     * @param
     * @return
     */
    @Override
    public AjaxResultVo edit(AlertWord dto) {

        AlertWord entity = this.getById(dto.getId());
        if(null == entity) {
            return AjaxResultVo.error("非法操作");
        }

        entity = new AlertWord();
        BeanUtils.copyProperties(dto, entity);

        this.updateById(entity);
        return AjaxResultVo.success();
    }

    /**
     * 详情
     * @param id
     * @return
     */
    @Override
    public AjaxResultVo info(Long id) {
        AlertWord entity = this.getById(id);
        if(null == entity) {
            return AjaxResultVo.error("数据查询异常~");
        }

        return AjaxResultVo.success(entity);
    }
    /**
    *检查匹配内容中的敏感词
    *
    * @author loovi
    * @date
    */
    @Override
    public AjaxResultVo checkContent(String content) {
        // 构造条件-查询启用的敏感词
        LambdaQueryWrapper<AlertWord> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(AlertWord::getStatus, 1);
        // 查询敏感词
        List<AlertWord> list = alertWordMapper.selectList(lambdaQueryWrapper);
        // 将敏感词归集到列表中
        List<String> sensitiveWords  = list.stream().map(AlertWord::getTitle).collect(Collectors.toList());
        // 构造正则表达式
        Pattern pattern = Pattern.compile(sensitiveWords.stream().collect(Collectors.joining("|")));
        // 创建收集触发敏感词的词列表
        List<String> matches = new ArrayList<>();
        // 匹配敏感词
        Matcher matcher = pattern.matcher(content);
        // 收集出发敏感的词汇
        while (matcher.find()) {
                matches.add(matcher.group());
        }

        AlertWordCheckContentVo vo = new AlertWordCheckContentVo();

        if(matches.size()==0){
            vo.setStatus(1);
            vo.setMsg("内容合法，可以提交。");
            return AjaxResultVo.success(vo);
        }

        vo.setStatus(0);
        vo.setMsg("内容包含敏感词汇，请修改后再提交。");
        vo.setNoValidList(matches);
        return AjaxResultVo.success(vo);
    }

}
