package com.fs.course.service.impl;

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

import com.fs.common.core.domain.R;
import com.fs.common.core.redis.RedisCache;
import com.fs.common.utils.DateUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fs.course.param.FsCourseWatchCommentListParam;
import com.fs.course.param.FsCourseWatchCommentPageParam;
import com.fs.course.param.FsCourseWatchCommentSaveParam;
import com.fs.course.vo.FsCourseWatchCommentListVO;
import com.fs.course.vo.FsCourseWatchCommentVO;
import com.fs.qw.mapper.QwExternalContactMapper;
import com.fs.system.domain.SysKeyword;
import com.fs.system.mapper.SysKeywordMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import com.fs.course.mapper.FsCourseWatchCommentMapper;
import com.fs.course.domain.FsCourseWatchComment;
import com.fs.course.service.IFsCourseWatchCommentService;

/**
 * 看课评论Service业务层处理
 *
 * @author fs
 * @date 2025-05-26
 */
@Service
public class FsCourseWatchCommentServiceImpl extends ServiceImpl<FsCourseWatchCommentMapper, FsCourseWatchComment> implements IFsCourseWatchCommentService {

    private static final String REDIS_KEY = "sys:keywords";

    @Autowired
    RedisCache redisCache;

    @Autowired
    private QwExternalContactMapper qwExternalContactMapper;

    @Autowired
    public RedisTemplate<String,String> redisTemplate;

    @Autowired
    private SysKeywordMapper mapper;

    /**
     * 查询看课评论
     *
     * @param commentId 看课评论主键
     * @return 看课评论
     */
    @Override
    public FsCourseWatchComment selectFsCourseWatchCommentByCommentId(Long commentId)
    {
        return baseMapper.selectFsCourseWatchCommentByCommentId(commentId);
    }

    /**
     * 查询看课评论列表
     *
     * @param fsCourseWatchCommentPageParam 看课评论
     * @return 看课评论
     */
    @Override
    public List<FsCourseWatchCommentListVO> selectFsCourseWatchCommentList(FsCourseWatchCommentPageParam fsCourseWatchCommentPageParam)
    {
        return baseMapper.selectFsCourseWatchCommentList(fsCourseWatchCommentPageParam);
    }

    /**
     * 新增看课评论
     *
     * @param fsCourseWatchComment 看课评论
     * @return 结果
     */
    @Override
    public int insertFsCourseWatchComment(FsCourseWatchComment fsCourseWatchComment)
    {
        fsCourseWatchComment.setCreateTime(DateUtils.getNowDate());
        return baseMapper.insertFsCourseWatchComment(fsCourseWatchComment);
    }

    /**
     * 修改看课评论
     *
     * @param fsCourseWatchComment 看课评论
     * @return 结果
     */
    @Override
    public int updateFsCourseWatchComment(FsCourseWatchComment fsCourseWatchComment)
    {
        fsCourseWatchComment.setUpdateTime(DateUtils.getNowDate());
        return baseMapper.updateFsCourseWatchComment(fsCourseWatchComment);
    }

    /**
     * 批量删除看课评论
     *
     * @param commentIds 需要删除的看课评论主键
     * @return 结果
     */
    @Override
    public int deleteFsCourseWatchCommentByCommentIds(Long[] commentIds)
    {
        return baseMapper.deleteFsCourseWatchCommentByCommentIds(commentIds);
    }

    /**
     * 删除看课评论信息
     *
     * @param commentId 看课评论主键
     * @return 结果
     */
    @Override
    public int deleteFsCourseWatchCommentByCommentId(Long commentId)
    {
        return baseMapper.deleteFsCourseWatchCommentByCommentId(commentId);
    }

    @Override
    public R saveH5CourseWatchComment(FsCourseWatchCommentSaveParam param) {
        // 需求：查询是否包含关键字，包含则不保存，并且需要将用户标记为黑名单；
        Set<String>  keywords = redisTemplate.opsForSet().members(REDIS_KEY);
        if(keywords == null || keywords.isEmpty()){
            SysKeyword sysKeywordParam = new SysKeyword();
            List<SysKeyword> sysKeywords = mapper.selectSysKeywordList(sysKeywordParam);
            keywords = sysKeywords.stream().map(SysKeyword::getKeyword).collect(Collectors.toSet());
        }
        if(!keywords.isEmpty()){
            for (String keyword : keywords) {
                if (param.getContent().contains(keyword)) {
                    //标记用户为黑名单，并且不保存数据
                    qwExternalContactMapper.updateQwExternalContactByFsUserId(1, param.getUserId());
                    return R.ok().put("status", false);
                }
            }
        }
        FsCourseWatchComment fsCourseWatchComment = new FsCourseWatchComment();
        BeanUtils.copyProperties(param, fsCourseWatchComment);
        fsCourseWatchComment.setCreateTime(DateUtils.getNowDate());
        int i = baseMapper.insertFsCourseWatchComment(fsCourseWatchComment);
        if (i > 0) {
            return R.ok().put("status", true);
        } else {
            return R.error();
        }
    }

    @Override
    public R revokeH5CourseWatchComment(Long commentId) {
        int i = baseMapper.revokeH5CourseWatchComment(commentId);
        if (i > 0){
            return R.ok();
        } else {
            return R.error();
        }
    }

    @Override
    public List<FsCourseWatchCommentVO> selectH5CourseWatchComments(FsCourseWatchCommentListParam param) {
        if(param.getOpenCommentStatus() != null && 2 == param.getOpenCommentStatus()){
            return baseMapper.selectH5CourseWatchCommentsRound(param);
        } else {
            return baseMapper.selectH5CourseWatchComments(param);
        }
    }

}
