package com.ruoyi.frequency.like.service.impl;

import java.util.List;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.controller.req.LikeReq;
import com.ruoyi.frequency.activity.entity.Activity;
import com.ruoyi.frequency.activity.mapper.ActivityMapper;
import com.ruoyi.frequency.comment.entity.Comment;
import com.ruoyi.frequency.comment.mapper.CommentMapper;
import com.ruoyi.frequency.news.entity.News;
import com.ruoyi.frequency.news.mapper.NewsMapper;
import com.ruoyi.frequency.vote.entity.Vote;
import com.ruoyi.frequency.votelike.entity.VoteLike;
import com.ruoyi.response.ResponseData;
import com.ruoyi.vo.UserVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.code.PublicCommon;
import com.ruoyi.frequency.like.mapper.LikeMapper;
import com.ruoyi.frequency.like.entity.Like;
import com.ruoyi.frequency.like.service.LikeService;

/**
 * 点赞(新闻,活动)Service业务层处理
 *
 * @author liwenlong
 * @date 2024-04-16
 */
@Service
public class LikeServiceImpl extends ServiceImpl<LikeMapper, Like>  implements LikeService
{
    @Autowired
    private LikeMapper likeMapper;

    /**
     * 查询点赞(新闻,活动)
     *
     * @param id 点赞(新闻,活动)主键
     * @return 点赞(新闻,活动)
     */
    @Override
    public Like selectLikeById(Long id)
    {
        return likeMapper.selectLikeById(id);
    }

    /**
     * 查询点赞(新闻,活动)列表
     *
     * @param like 点赞(新闻,活动)
     * @return 点赞(新闻,活动)
     */
    @Override
    public List<Like> selectLikeList(Like like)
    {
        return likeMapper.selectLikeList(like);
    }

    /**
     * 新增点赞(新闻,活动)
     *
     * @param like 点赞(新闻,活动)
     * @return 结果
     */
    @Override
    public int insertLike(Like like)
    {
        like.setCreateData();
        return likeMapper.insert(like);
    }

    /**
     * 修改点赞(新闻,活动)
     *
     * @param like 点赞(新闻,活动)
     * @return 结果
     */
    @Override
    public int updateLike(Like like)
    {
        like.setUpdateData();
        return likeMapper.updateLike(like);
    }

    /**
     * 批量删除点赞(新闻,活动)
     *
     * @param ids 需要删除的点赞(新闻,活动)主键
     * @return 结果
     */
    @Override
    public int deleteLikeByIds(Long[] ids,Integer delFlag)
    {
        this.lambdaUpdate().set(Like::getDelFlag,delFlag).in(Like::getId,ids).update();
        return ids.length;
    }

    @Override
    public ResponseData like(LikeReq req, UserVo userVo) {
        Like like = new Like(req.getType(), req.getLinkId());

        //更新帖子点赞记录
        this.baseMapper.like(like,userVo);

        like = this.getOne(new QueryWrapper<Like>().lambda()
                .eq(Like::getLinkId,req.getLinkId())
                .eq(Like::getType,req.getType())
                .eq(Like::getUserType,userVo.getUserType())
                .eq(Like::getUserId,userVo.getUserId()));

        //是否点赞
        Boolean isLike = like.getDelFlag().equals(PublicCommon.启用);

        //1 新闻 2新闻评论 3 活动
        updateLikeNum(req.getType(),req.getLinkId(),isLike);

        return ResponseData.success();
    }

    @Autowired
    private NewsMapper newsMapper;

    @Autowired
    private CommentMapper commentMapper;

    @Autowired
    private ActivityMapper activityMapper;

    /**
     * 更新对应点赞数量
     * @param type 1 新闻 2新闻评论 3 活动
     * @param linkId
     * @param isLike
     */
    private void updateLikeNum(Integer type, Long linkId, Boolean isLike) {
        //点赞 +1 ，取消点赞 -1
        Integer num = isLike ? 1 : -1;

        switch (type){
            case 1:
                newsMapper.updateNews(new News().setId(linkId).setLikeNum(num));
                break;
            case 2:
                commentMapper.updateComment(new Comment().setId(linkId).setLikeNum(num));
                break;
            case 3:
                activityMapper.updateActivity(new Activity().setId(linkId).setLikeNum(num));
                break;
            default:
                throw new IllegalStateException("Unexpected value: " + type);
        }
    }
}
