package cn.com.tenlion.configcontent.service.contentcomment.impl;

import cn.com.tenlion.configcontent.dao.contentcomment.IContentCommentDao;
import cn.com.tenlion.configcontent.pojo.bos.contentcomment.ContentCommentBO;
import cn.com.tenlion.configcontent.pojo.dtos.contentcomment.ContentCommentDTO;
import cn.com.tenlion.configcontent.pojo.pos.contentcomment.ContentCommentPO;
import cn.com.tenlion.configcontent.pojo.vos.contentcomment.ContentCommentVO;
import cn.com.tenlion.configcontent.service.contentcomment.IContentCommentService;
import cn.com.tenlion.configcontent.util.AntistopUtil;
import cn.com.tenlion.configcontent.util.IPUtils;
import com.github.pagehelper.util.StringUtil;
import ink.wgink.app.AppTokenManager;
import ink.wgink.common.base.DefaultBaseService;
import ink.wgink.exceptions.RemoveException;
import ink.wgink.exceptions.SaveException;
import ink.wgink.module.wechat.pojo.pos.miniapp.MiniappUserPO;
import ink.wgink.module.wechat.service.miniapp.IMiniappUserService;
import ink.wgink.pojo.ListPage;
import ink.wgink.pojo.app.AppTokenUser;
import ink.wgink.pojo.bos.UserInfoBO;
import ink.wgink.pojo.result.SuccessResultList;
import ink.wgink.util.map.HashMapUtil;
import ink.wgink.util.UUIDUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.*;

/**
 * @ClassName: ContentCommentServiceImpl
 * @Description: CBC内容管理评论记录表
 * @Author: CodeFactory
 * @Date: 2021-08-02 09:31:18
 * @Version: 3.0
 **/
@Service
public class ContentCommentServiceImpl extends DefaultBaseService implements IContentCommentService {

    @Autowired
    private IContentCommentDao contentCommentDao;

    @Autowired
    private IMiniappUserService iMiniappUserService;

    private String getUserId(String token) {
        String userId = "notUserId";
        if (StringUtils.isBlank(token)) {
            UserInfoBO userInfoBO = this.securityComponent.getCurrentUser();
            userId = userInfoBO.getUserId();
        } else {
            AppTokenUser appTokenUser = this.getAppTokenUser(token);
            userId = appTokenUser.getId();
        }
        return userId;
    }

    @Override
    public void updateLike(String contentCommentId, Integer count) {
        Map<String,Object> params = getHashMap(4);
        params.put("contentCommentId", contentCommentId);
        params.put("count", count.toString());
        contentCommentDao.updateLike(params);
    }

    @Override
    public void save(ContentCommentVO contentCommentVO) {
        saveReturnId(contentCommentVO);
    }

    @Override
    public void save(String token, ContentCommentVO contentCommentVO) {
        saveReturnId(token, contentCommentVO);
    }

    @Override
    public String saveReturnId(ContentCommentVO contentCommentVO) {
        return saveReturnId(null, contentCommentVO);
    }

    @Override
    public String saveReturnId(String token, ContentCommentVO contentCommentVO) {
        String contentCommentId = UUIDUtil.getUUID();
        try {
            contentCommentVO.setContentCommentIp(IPUtils.getRemortIP());
        } catch (Exception e) {
        }
        /**
         * 判断是否为二级评论
         */
        if(StringUtil.isEmpty(contentCommentVO.getContentCommentParentId())){
            contentCommentVO.setContentCommentParentId("0");
        }
        contentCommentVO.setContentCommentLike(0);
        contentCommentVO.setContentCommentDislike(0);
        if (token != null) {
            AppTokenUser appTokenUser = AppTokenManager.getInstance().getToken(token).getAppTokenUser();
            contentCommentVO.setContentCommentUserName(appTokenUser.getName());
            contentCommentVO.setContentCommentUserIcon(appTokenUser.getAvatar());
        } else {
            UserInfoBO userInfoBO = this.securityComponent.getCurrentUser();
            contentCommentVO.setContentCommentUserName(userInfoBO.getUserName());
        }
        /**
         * 判断评论是否违规
         */
        contentCommentVO.setContentCommentContent(contentCommentVO.getContentCommentContent().trim());
        // 评论内容敏感词识别
        String viewContent = AntistopUtil.buiderView(contentCommentVO.getContentCommentContent());
        // 敏感词触发次数
        int count = AntistopUtil.getCount(viewContent);
        // 发生敏感词
        if(count > 0) {
            throw new SaveException("评论内容涉嫌违规");
        }
        Map<String, Object> params = HashMapUtil.beanToMap(contentCommentVO);
        params.put("contentCommentId", contentCommentId);
        if (StringUtils.isBlank(token)) {
            setSaveInfo(params);
        } else {
            setAppSaveInfo(token, params);
        }
        /**
         * 重复评论不允许保存
         */
        List<ContentCommentDTO> contentCommentDTOS = getCommentByBusinessIdAndCommentContent(contentCommentVO.getBusinessId(), params.get("creator").toString(), contentCommentVO.getContentCommentContent());
        if(contentCommentDTOS != null && contentCommentDTOS.size() > 0) {
            throw new SaveException("请勿重复提交评论");
        }
        contentCommentDao.save(params);
        return contentCommentId;
    }

    @Override
    public List<ContentCommentDTO> getCommentByBusinessIdAndCommentContent(String businessId, String creator, String contentCommentContent) {
        Map<String, Object> params = getHashMap(3);
        params.put("businessId", businessId);
        params.put("creator", creator);
        params.put("contentCommentContent", contentCommentContent);
        return contentCommentDao.getCommentByBusinessIdAndCommentContent(params);
    }

    @Override
    public void remove(List<String> ids) {
        remove(null, ids);
    }

    @Override
    public void remove(String token, List<String> ids) {
        Map<String, Object> params = getHashMap(2);
        params.put("contentCommentIds", ids);
        /**
         * 只能删除自己的评论
         */
        String userId = getUserId(token);
        Map<String, Object> query = getHashMap(2);
        query.put("creator", userId);
        for(String contentCommentId : ids) {
            query.put("contentCommentId", contentCommentId);
            List<ContentCommentDTO> list = list(query);
            if(list.size() < 1) {
                throw new RemoveException("该评论不能删除");
            }
        }
        if (StringUtils.isBlank(token)) {
            setUpdateInfo(params);
        } else {
            setAppUpdateInfo(token, params);
        }
        contentCommentDao.remove(params);
    }

    @Override
    public void delete(List<String> ids) {
        Map<String, Object> params = getHashMap(2);
        params.put("contentCommentIds", ids);
        contentCommentDao.delete(params);
    }

    @Override
    public void update(String contentCommentId, ContentCommentVO contentCommentVO) {
        update(null, contentCommentId, contentCommentVO);
    }

    @Override
    public void update(String token, String contentCommentId, ContentCommentVO contentCommentVO) {
        Map<String, Object> params = HashMapUtil.beanToMap(contentCommentVO);
        params.put("contentCommentId", contentCommentId);
        if (StringUtils.isBlank(token)) {
            setUpdateInfo(params);
        } else {
            setAppUpdateInfo(token, params);
        }
        contentCommentDao.update(params);
    }

    @Override
    public ContentCommentDTO get(Map<String, Object> params) {
        return contentCommentDao.get(params);
    }

    @Override
    public ContentCommentDTO get(String contentCommentId) {
        Map<String, Object> params = super.getHashMap(2);
        params.put("contentCommentId", contentCommentId);
        return get(params);
    }

    @Override
    public ContentCommentBO getBO(Map<String, Object> params) {
        return contentCommentDao.getBO(params);
    }

    @Override
    public ContentCommentBO getBO(String contentCommentId) {
        Map<String, Object> params = super.getHashMap(2);
        params.put("contentCommentId", contentCommentId);
        return getBO(params);
    }

    @Override
    public ContentCommentPO getPO(Map<String, Object> params) {
        return contentCommentDao.getPO(params);
    }

    @Override
    public ContentCommentPO getPO(String contentCommentId) {
        Map<String, Object> params = super.getHashMap(2);
        params.put("contentCommentId", contentCommentId);
        return getPO(params);
    }

    @Override
    public List<ContentCommentDTO> list(Map<String, Object> params) {
        List<ContentCommentDTO> list = contentCommentDao.list(params);
        // 放入查看人员数据信息
        Set<String> userSet = new HashSet<String>();
        /**
         * 判断子节点的数量
         */
        Map<String, Object> query = getHashMap(5);
        query.putAll(params);
        for(ContentCommentDTO dto : list) {
            query.put("contentCommentParentId", dto.getContentCommentId());
            Integer integer = count(query);
            dto.setContentCommentSubCount(integer);
            userSet.add(dto.getCreator());
        }
        // 查询出人员的信息
        List<String> userList = new ArrayList<String>();
        userList.addAll(userSet);
        List<MiniappUserPO> miniappUserPOS = iMiniappUserService.listPO(userList);
        Map<String, MiniappUserPO> userPOMap = new HashMap<String, MiniappUserPO>();
        for(MiniappUserPO userPO : miniappUserPOS) {
            userPOMap.put(userPO.getUserId(), userPO);
        }
        for(ContentCommentDTO dto : list) {
            MiniappUserPO userPO = userPOMap.get(dto.getCreator());
            if (userPO != null) {
                /**
                 * 封装人员头像信息, 昵称信息
                 */
                dto.setContentCommentUserName(userPO.getNickName());
                dto.setContentCommentUserIcon(userPO.getAvatarUrl());
            }
        }
        return list;
    }

    @Override
    public List<ContentCommentBO> listBO(Map<String, Object> params) {
        return contentCommentDao.listBO(params);
    }

    @Override
    public List<ContentCommentPO> listPO(Map<String, Object> params) {
        return contentCommentDao.listPO(params);
    }

    @Override
    public SuccessResultList<List<ContentCommentDTO>> listPage(ListPage page) {
        PageHelper.startPage(page.getPage(), page.getRows());
        List<ContentCommentDTO> contentCommentDTOs = list(page.getParams());
        PageInfo<ContentCommentDTO> pageInfo = new PageInfo<>(contentCommentDTOs);
        return new SuccessResultList<>(contentCommentDTOs, pageInfo.getPageNum(), pageInfo.getTotal());
    }

    @Override
    public Integer count(Map<String, Object> params) {
        Integer count = contentCommentDao.count(params);
        return count == null ? 0 : count;
    }

}