package cn.exploring.engine.server.db.service;

import cn.exploring.engine.server.db.dao.WxSensitiveWordsMapper;
import cn.exploring.engine.server.db.domain.WxSensitiveWords;
import cn.exploring.engine.server.db.domain.WxSensitiveWordsExample;
import cn.exploring.engine.server.db.util.ResponseUtil;
import cn.exploring.engine.server.db.util.sensitiveword.SensitiveFilter;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

//敏感词汇 service
@Service
public class WxSensitiveWordsService {
    @Resource
    private WxSensitiveWordsMapper sensitiveWordsMapper;

    public List<WxSensitiveWords> findAll() {
        WxSensitiveWordsExample example = new WxSensitiveWordsExample();
        WxSensitiveWordsExample.Criteria criteria = example.createCriteria();
        criteria.andDeletedEqualTo(false);
        return sensitiveWordsMapper.selectByExample(example);
    }

    //根据条件分页查询
    public Object list(WxSensitiveWords object, Integer page, Integer limit, String sort, String order) {
        WxSensitiveWordsExample example = new WxSensitiveWordsExample();
        WxSensitiveWordsExample.Criteria criteria = example.createCriteria();

        if(null != object.getId() && object.getId() != 0){
            criteria.andIdEqualTo(object.getId());
        }
        //敏感词汇
        if (!StringUtils.isEmpty(object.getSensitiveWords())) {
            criteria.andSensitiveWordsLike("%"+object.getSensitiveWords()+"%");
        }
        //替换为
        if (!StringUtils.isEmpty(object.getToWords())) {
            criteria.andToWordsLike("%"+object.getToWords()+"%");
        }

        //是否删除
       // if (!StringUtils.isEmpty(object.getDeleted())) {
            criteria.andDeletedEqualTo(false);
       // }
        if (!StringUtils.isEmpty(sort) && !StringUtils.isEmpty(order)) {
            example.setOrderByClause(sort + " " + order);
        }

        PageHelper.startPage(page, limit);
        List<WxSensitiveWords> jmList = sensitiveWordsMapper.selectByExample(example);
        long total = PageInfo.of(jmList).getTotal();

        Map<String, Object> data = new HashMap<>();
        data.put("total", total);
        data.put("items", jmList);
        return  ResponseUtil.ok(data);
    }

    //获取自定义的所有敏感词汇和对应替换的词汇
    public List<WxSensitiveWords> selectAllSensitiveWordDB(){
        WxSensitiveWordsExample example = new WxSensitiveWordsExample();
        WxSensitiveWordsExample.Criteria criteria = example.createCriteria();
        criteria.andDeletedEqualTo(false);
        List<WxSensitiveWords> sensitiveWordsDBList =  sensitiveWordsMapper.selectByExample(example);
        return sensitiveWordsDBList;
    }

    //根据id查询
    public Object selectByPrimaryKey(Integer id){
        return  ResponseUtil.ok(sensitiveWordsMapper.selectByPrimaryKey(id));
    }

    //添加
    public Object add(WxSensitiveWords object) {
        SensitiveFilter.DEFAULT.put(object.getSensitiveWords());

        LocalDateTime now = LocalDateTime.now();
        object.setUpdateTime(now);
        object.setAddTime(now);
        return ResponseUtil.ok(sensitiveWordsMapper.insertSelective(object));
    }

    //编辑
    public Object updateById(WxSensitiveWords object) {
        object.setUpdateTime(LocalDateTime.now());
        Object result = ResponseUtil.ok(sensitiveWordsMapper.updateByPrimaryKeySelective(object));
        SensitiveFilter.DEFAULT.update(findAll());

        return result;
    }

    //批量删除
    @Transactional(rollbackFor=Exception.class)
    public Object deleteByIds(Integer[] ids){
        for(Integer id:ids){
            WxSensitiveWords sensitiveWords = sensitiveWordsMapper.selectByPrimaryKey(id);
            sensitiveWords.setDeleted(true);
            sensitiveWordsMapper.updateByPrimaryKeySelective(sensitiveWords);
        }

        SensitiveFilter.DEFAULT.update(findAll());

        return ResponseUtil.ok();
    }
}
