package com.cm.plugin.service.impl;

import com.alibaba.excel.EasyExcel;
import com.cm.common.base.AbstractService;
import com.cm.common.exception.RemoveException;
import com.cm.common.exception.SearchException;
import com.cm.common.pojo.ListPage;
import com.cm.common.result.SuccessResult;
import com.cm.common.result.SuccessResultList;
import com.cm.common.utils.HashMapUtil;
import com.cm.common.utils.UUIDUtil;
import com.cm.plugin.dao.ISensitiveWordsDao;
import com.cm.plugin.listener.excel.SensitiveWordsExcel;
import com.cm.plugin.listener.excel.SensitiveWordsExcelListener;
import com.cm.plugin.manager.SensitiveWordsManager;
import com.cm.plugin.pojo.dtos.SensitiveWordsDTO;
import com.cm.plugin.pojo.vos.SensitiveWordsVO;
import com.cm.plugin.service.ISensitiveWordsService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * @ClassName: SensitiveWordsServiceImpl
 * @Description: 敏感词
 * @Author: WenG
 * @Date: 2020-03-13 23:16
 * @Version: 1.0
 **/
@Service
public class SensitiveWordsServiceImpl extends AbstractService implements ISensitiveWordsService {

    @Autowired
    private ISensitiveWordsDao sensitiveWordsDao;

    @Override
    public SuccessResult saveSensitiveWords(SensitiveWordsVO sensitiveWordsVO) throws Exception {
        saveSensitiveWordsInfo(null, sensitiveWordsVO);
        return new SuccessResult();
    }

    @Override
    public SuccessResult saveSensitiveWordsByToken(String token, SensitiveWordsVO sensitiveWordsVO) throws Exception {
        saveSensitiveWordsInfo(token, sensitiveWordsVO);
        return new SuccessResult();
    }

    /**
     * 新增敏感词
     *
     * @param token
     * @param sensitiveWordsVO
     * @throws Exception
     */
    private void saveSensitiveWordsInfo(String token, SensitiveWordsVO sensitiveWordsVO) throws Exception {
        Map<String, Object> params = HashMapUtil.beanToMap(sensitiveWordsVO);
        params.put("sensitiveWordsId", UUIDUtil.getUUID());
        if (token != null) {
            setSaveInfo(token, params);
        } else {
            setSaveInfo(params);
        }
        sensitiveWordsDao.saveSensitiveWords(params);
        SensitiveWordsManager.getInstance().initSensitiveWords(listSensitiveWordsForString());
    }

    @Override
    public SuccessResult removeSensitiveWords(String ids) throws RemoveException {
        removeSensitiveWordsInfo(null, ids);
        return new SuccessResult();
    }

    @Override
    public SuccessResult removeSensitiveWordsByToken(String token, String ids) throws RemoveException {
        removeSensitiveWordsInfo(token, ids);
        return new SuccessResult();
    }

    /**
     * 删除敏感词
     *
     * @param token
     * @param ids
     */
    private void removeSensitiveWordsInfo(String token, String ids) {
        Map<String, Object> params = getHashMap(3);
        params.put("sensitiveWordsIds", Arrays.asList(ids.split("_")));
        if (token != null) {
            setUpdateInfo(token, params);
        } else {
            setUpdateInfo(params);
        }
        sensitiveWordsDao.removeSensitiveWords(params);
        SensitiveWordsManager.getInstance().initSensitiveWords(listSensitiveWordsForString());
    }

    @Override
    public SuccessResult updateSensitiveWords(String sensitiveWordsId, SensitiveWordsVO sensitiveWordsVO) throws Exception {
        updateSensitiveWordsInfo(null, sensitiveWordsId, sensitiveWordsVO);
        return new SuccessResult();
    }

    @Override
    public SuccessResult updateSensitiveWordsByToken(String token, String sensitiveWordsId, SensitiveWordsVO sensitiveWordsVO) throws Exception {
        updateSensitiveWordsInfo(token, sensitiveWordsId, sensitiveWordsVO);
        return new SuccessResult();
    }

    /**
     * 修改敏感词
     *
     * @param token
     * @param sensitiveWordsId
     * @param sensitiveWordsVO
     */
    private void updateSensitiveWordsInfo(String token, String sensitiveWordsId, SensitiveWordsVO sensitiveWordsVO) throws Exception {
        Map<String, Object> params = HashMapUtil.beanToMap(sensitiveWordsVO);
        params.put("sensitiveWordsId", sensitiveWordsId);
        if (token != null) {
            setUpdateInfo(token, params);
        } else {
            setUpdateInfo(params);
        }
        sensitiveWordsDao.updateSensitiveWords(params);
        SensitiveWordsManager.getInstance().initSensitiveWords(listSensitiveWordsForString());
    }

    @Override
    public SensitiveWordsDTO getSensitiveWordsById(String sensitiveWordsId) throws SearchException {
        Map<String, Object> params = super.getHashMap(1);
        params.put("sensitiveWordsId", sensitiveWordsId);
        return sensitiveWordsDao.getSensitiveWords(params);
    }

    @Override
    public List<SensitiveWordsDTO> listSensitiveWords(Map<String, Object> params) throws SearchException {
        return sensitiveWordsDao.listSensitiveWords(params);
    }

    @Override
    public List<String> listSensitiveWordsForString() throws SearchException {
        return sensitiveWordsDao.listSensitiveWordsForString();
    }

    @Override
    public SuccessResult saveSensitiveWordsExcel(MultipartFile multipartFile) throws SearchException, IOException {
        EasyExcel.read(multipartFile.getInputStream(), SensitiveWordsExcel.class, new SensitiveWordsExcelListener() {
            @Override
            public void listSensitiveWordsExcel(List<SensitiveWordsExcel> sensitiveWordsExcels) throws Exception {
                Map<String, Object> params = getHashMap(7);
                setSaveInfo(params);
                for (SensitiveWordsExcel sensitiveWordsExcel : sensitiveWordsExcels) {
                    params.put("sensitiveWordsId", UUIDUtil.getUUID());
                    params.put("name", sensitiveWordsExcel.getSensitiveWord().trim());
                    params.put("message", "存在敏感词，请修改");
                    sensitiveWordsDao.saveSensitiveWords(params);
                }
            }
        }).sheet().doRead();
        SensitiveWordsManager.getInstance().initSensitiveWords(listSensitiveWordsForString());
        return new SuccessResult();
    }

    @Override
    public SuccessResultList<List<SensitiveWordsDTO>> listPageSensitiveWords(ListPage page) throws SearchException {
        PageHelper.startPage(page.getPage(), page.getRows());
        List<SensitiveWordsDTO> sensitiveWordsDTOs = sensitiveWordsDao.listSensitiveWords(page.getParams());
        PageInfo<SensitiveWordsDTO> pageInfo = new PageInfo<>(sensitiveWordsDTOs);
        return new SuccessResultList<>(sensitiveWordsDTOs, pageInfo.getPageNum(), pageInfo.getTotal());
    }

}
