package com.playlet.web.service.app.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.playlet.system.domain.PublicCommentResponse;
import com.playlet.system.domain.PublicCommentStar;
import com.playlet.system.domain.PublicDetailComment;
import com.playlet.system.service.IPublicCommentResponseService;
import com.playlet.system.service.IPublicCommentStarService;
import com.playlet.system.service.IPublicDetailCommentService;
import com.playlet.web.service.app.PublicDetailCommentAppService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Slf4j
@Service
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class PublicDetailCommentAppServiceImpl implements PublicDetailCommentAppService {

    private final IPublicDetailCommentService iPublicDetailCommentService;

    private final IPublicCommentResponseService iPublicCommentResponseService;

    private final IPublicCommentStarService iPublicCommentStarService;

    @Override
    public void add(PublicDetailComment detailComment) {
        iPublicDetailCommentService.save(detailComment);
    }

    @Override
    public void update(PublicDetailComment detailComment) {
        iPublicDetailCommentService.updateById(detailComment);
    }

    @Override
    public PageInfo<PublicDetailComment> query(PublicDetailComment detailComment, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<PublicDetailComment> comments = iPublicDetailCommentService.lambdaQuery()
                        .eq(PublicDetailComment::getDetailId, detailComment.getDetailId())
                        .eq(PublicDetailComment::getTopStatus, "01")
                        .orderByDesc(PublicDetailComment::getStarCount)
                        .orderByDesc(PublicDetailComment::getCreateTime)
                        .list();
        comments.forEach(model->{
            model.setCommentResponse(iPublicCommentResponseService.lambdaQuery().eq(PublicCommentResponse::getCommentId, model.getId()).one());
            if(model.getUserId() != null){
                long star = iPublicCommentStarService.lambdaQuery()
                        .eq(PublicCommentStar::getCommentId, model.getId())
                        .eq(PublicCommentStar::getUserId, detailComment.getUserId())
                        .count();
                if(star > 0){
                    model.setIsStar("02");
                }else {
                    model.setIsStar("01");
                }
            }
        });
        return PageInfo.of(comments);
    }

    @Override
    public void star(PublicCommentStar publicCommentStar) {
        publicCommentStar.setCreateTime(new Date());
        iPublicCommentStarService.insertPublicCommentStar(publicCommentStar);
        // 增加点赞数
        PublicDetailComment comment = iPublicDetailCommentService.getById(publicCommentStar.getCommentId());
        if(comment.getStarCount() == null){
            comment.setStarCount(1L);
        }else {
            comment.setStarCount(comment.getStarCount() + 1);
        }
        iPublicDetailCommentService.updateById(comment);
    }

    @Override
    public void cancelStar(PublicCommentStar publicCommentStar) {
        iPublicCommentStarService.removeById(publicCommentStar.getId());
        // 增加点赞数
        PublicDetailComment comment = iPublicDetailCommentService.getById(publicCommentStar.getCommentId());
        comment.setStarCount(comment.getStarCount() - 1);
        iPublicDetailCommentService.updateById(comment);
    }

    @Override
    public void delete(PublicDetailComment detailComment) {
        iPublicDetailCommentService.removeById(detailComment.getId());
    }

    @Override
    public PublicDetailComment getTopComment(PublicDetailComment detailComment) {
        // 查询是否已经有置顶的评论
        PublicDetailComment model = iPublicDetailCommentService.lambdaQuery()
                .eq(PublicDetailComment::getDetailId, detailComment.getDetailId())
                .eq(PublicDetailComment::getTopStatus, "02")
                .one();
        model.setCommentResponse(iPublicCommentResponseService.lambdaQuery().eq(PublicCommentResponse::getCommentId, model.getId()).one());
        if(detailComment.getUserId() != null){
            long star = iPublicCommentStarService.lambdaQuery()
                    .eq(PublicCommentStar::getCommentId, model.getId())
                    .eq(PublicCommentStar::getUserId, detailComment.getUserId())
                    .count();
            if(star > 0){
                model.setIsStar("02");
            }else {
                model.setIsStar("01");
            }
        }
        return model;
    }
}
