package com.ruoyi.bar;

import com.ruoyi.bar.domain.WyFavoriteBrewery;
import com.ruoyi.brewery.domain.WyBeer;
import com.ruoyi.brewery.domain.WyBeerReview;
import com.ruoyi.brewery.domain.WyBeerReviewLike;
import com.ruoyi.brewery.domain.WyBeerReviewReply;
import com.ruoyi.brewery.service.IWyBeerReviewLikeService;
import com.ruoyi.brewery.service.IWyBeerReviewReplyService;
import com.ruoyi.brewery.service.IWyBeerReviewService;
import com.ruoyi.brewery.service.IWyBeerService;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.model.BarLoginUser;
import com.ruoyi.common.core.domain.model.BeerReviewLikeVO;
import com.ruoyi.common.core.domain.model.BeerReviewReplyVO;
import com.ruoyi.common.core.domain.model.BeerReviewVO;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.SecurityUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Objects;

@Api(tags = "酒吧方酒评相关接口")
@RestController
@RequestMapping("/bar/beer/review")
public class BarReviewController extends BarBaseController {

    @Autowired
    private IWyBeerReviewService wyBeerReviewService;
    @Autowired
    private IWyBeerReviewLikeService wyBeerReviewLikeService;
    @Autowired
    private IWyBeerService wyBeerService;
    @Autowired
    private IWyBeerReviewReplyService wyBeerReviewReplyService;

    /**
     * 新增酒评
     */
    @ApiOperation("新增酒评")
    @PostMapping("/addReview")
    public AjaxResult addReview(@Validated @RequestBody BeerReviewVO beerReview) {
        BarLoginUser loginUser = SecurityUtils.getBarLoginUser();
        validateBarAuth();
        final WyBeer beer = wyBeerService.selectWyBeerById(beerReview.getBeerId());
        if (beer == null)   // 酒不存在
        {
            return AjaxResult.error("酒款不存在");
        }
        WyBeerReview wyBeerReview = new WyBeerReview();
        wyBeerReview.setBreweryId(beer.getBreweryId());
        wyBeerReview.setBeerId(beerReview.getBeerId());
        wyBeerReview.setBarId(loginUser.getBarId()); //酒吧id
        wyBeerReview.setReviewContent(beerReview.getReviewContent());
        wyBeerReview.setUserId(loginUser.getUser().getUserId());
        wyBeerReview.setReviewImg(beerReview.getReviewImg());
        wyBeerReview.setAromaRating(Double.valueOf(beerReview.getAromaRating()));
        wyBeerReview.setColorRating(Double.valueOf(beerReview.getColorRating()));
        wyBeerReview.setTasteRating(Double.valueOf(beerReview.getTasteRating()));
        wyBeerReview.setAnonymous(beerReview.getAnonymous());
        final double aromaRating = beerReview.getAromaRating() * 0.3; // 香气评分乘以0.3
        final double colorRating = beerReview.getColorRating() * 0.1; // 颜色评分乘以0.1
        final double tasteRating = beerReview.getTasteRating() * 0.6; // 口味评分乘以0.6
        double overallRating = (aromaRating + colorRating + tasteRating);
        overallRating = Math.round(overallRating * 10.0) / 10.0; // 四舍五入保留一位小数
        wyBeerReview.setOverallRating(overallRating);
        final WyBeerReview wyBeerReviewExist = wyBeerReviewService.selectWyBeerReviewByBeerIdAndCustomerId(beerReview.getBeerId(), loginUser.getUser().getUserId());
        if (wyBeerReviewExist != null)   // 该用户已评论过该酒
        {
//            return AjaxResult.error("该用户已评论过该酒款");
            wyBeerReview.setId(wyBeerReviewExist.getId());
            return AjaxResult.success(wyBeerReviewService.updateWyBeerReview(wyBeerReview));
        }else{
            return AjaxResult.success(wyBeerReviewService.insertWyBeerReview(wyBeerReview));
        }
    }

    /**
     * 酒评点赞/取消点赞
     */
    @ApiOperation("酒评点赞/取消点赞")
    @PostMapping("/reviewLike")
    public AjaxResult reviewLike(@Validated @RequestBody BeerReviewLikeVO like) {
        BarLoginUser loginUser = SecurityUtils.getBarLoginUser();
        validateBarAuth();
        final WyBeerReview wyBeerReview = wyBeerReviewService.selectWyBeerReviewById(like.getReviewId());
        if (wyBeerReview == null) {
            return AjaxResult.error("酒评不存在");
        }
        WyBeerReviewLike wyBeerReviewLike = new WyBeerReviewLike();
        wyBeerReviewLike.setReviewId(like.getReviewId());
        wyBeerReviewLike.setBeerId(wyBeerReview.getBeerId());
        wyBeerReviewLike.setBreweryId(wyBeerReviewLike.getBreweryId());
        wyBeerReviewLike.setUserId(loginUser.getUser().getUserId());
        final WyBeerReviewLike wyBeerReviewLike1 = wyBeerReviewLikeService.selectWyBeerReviewLikeByReviewIdAndCustomerId(like.getReviewId(), loginUser.getUser().getUserId());
        if (like.getStatus() == 1) {
            if (wyBeerReviewLike1 != null)   // 该用户已点赞过该酒评
            {
                return AjaxResult.error("该用户已点赞过该酒评");
            }
            wyBeerReviewLikeService.insertWyBeerReviewLike(wyBeerReviewLike);
            return AjaxResult.success("点赞成功");
        } else {
            if (wyBeerReviewLike1 == null) {
                return AjaxResult.error("该用户未点赞过该酒评");
            }
            wyBeerReviewLikeService.deleteWyBeerReviewLikeById(wyBeerReviewLike1.getId());
            return AjaxResult.success("取消点赞成功");
        }
    }

    /**
     * 查询某酒评我的点赞情况
     */
    @ApiOperation("查询某酒评我的点赞情况")
    @GetMapping("/reviewLikeByReviewId")
    public AjaxResult favoriteBreweryList(@RequestParam(value = "reviewId") Long reviewId) {
        BarLoginUser loginUser = SecurityUtils.getBarLoginUser();
        final WyBeerReviewLike wyBeerReviewLike = wyBeerReviewLikeService.selectWyBeerReviewLikeByReviewIdAndCustomerId(reviewId,
                loginUser.getUser().getUserId());
        return AjaxResult.success(wyBeerReviewLike);
    }

    /**
     * 新增酒评回复
     */
    @ApiOperation("新增酒评回复")
    @PostMapping("/addReviewReply")
    public AjaxResult addReviewReply(@Validated @RequestBody BeerReviewReplyVO beerReviewReply) {
        BarLoginUser loginUser = SecurityUtils.getBarLoginUser();
        validateBarAuth();
        final WyBeerReview wyBeerReview = wyBeerReviewService.selectWyBeerReviewById(beerReviewReply.getReviewId());
        if (wyBeerReview == null) {
            return AjaxResult.error("酒评不存在");
        }
        final WyBeer beer = wyBeerService.selectWyBeerById(wyBeerReview.getBeerId());
        if (beer == null)   // 酒不存在
        {
            return AjaxResult.error("酒款不存在");
        }
        WyBeerReviewReply wyBeerReviewReply = new WyBeerReviewReply();
        wyBeerReviewReply.setReviewId(beerReviewReply.getReviewId());
        wyBeerReviewReply.setBeerId(beer.getId());
        wyBeerReviewReply.setBreweryId(beer.getBreweryId());
        wyBeerReviewReply.setUserId(loginUser.getUser().getUserId());
        wyBeerReviewReply.setReplayContent(beerReviewReply.getReplayContent());
        wyBeerReviewReplyService.insertWyBeerReviewReply(wyBeerReviewReply);
        return AjaxResult.success("回复成功");
    }

    /**
     * 删除酒评回复
     */
    @ApiOperation("删除酒评回复")
    @PostMapping("/deleteReviewReply")
    public AjaxResult deleteReviewReply(@RequestParam("id") Long replayId) {
        BarLoginUser loginUser = SecurityUtils.getBarLoginUser();
        validateBarAuth();
        final WyBeerReviewReply wyBeerReviewReply = wyBeerReviewReplyService.selectWyBeerReviewReplyById(replayId);
        if (wyBeerReviewReply == null) {
            return AjaxResult.error("回复不存在");
        }
        if (!Objects.equals(wyBeerReviewReply.getUserId(), loginUser.getUser().getUserId())) {
            return AjaxResult.error("只能删除自己的回复");
        }
        wyBeerReviewReplyService.deleteWyBeerReviewReplyById(replayId);
        return AjaxResult.success("删除成功");
    }

    /**
     * 删除酒评
     */
    @ApiOperation("删除酒评")
    @PostMapping("/deleteReview")
    public AjaxResult deleteReview(@RequestParam("reviewId") Long reviewId) {
        BarLoginUser loginUser = SecurityUtils.getBarLoginUser();
        validateBarAuth();
        final WyBeerReview wyBeerReview = wyBeerReviewService.selectWyBeerReviewById(reviewId);
        if (wyBeerReview == null) {
            return AjaxResult.error("酒评不存在");
        }
        if (!Objects.equals(wyBeerReview.getUserId(), loginUser.getUser().getUserId())) {
            return AjaxResult.error("只能删除自己的酒评");
        }
        final int i = wyBeerReviewService.deleteWyBeerReviewById(reviewId);
        return AjaxResult.success("删除成功");
    }
}
