package com.fzm.mall.server.admin.goods_center.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.fzm.mall.server.admin.goods_center.entity.dto.CommentReplyDTO;
import com.fzm.mall.server.admin.goods_center.entity.vo.GoodsCommentAppendDetailVO;
import com.fzm.mall.server.admin.goods_center.enums.status.BuyerAndSellerStatusEnum;
import com.fzm.mall.server.admin.goods_center.enums.status.CommentIsValidEnum;
import com.fzm.mall.server.admin.goods_center.model.GoodsComment;
import com.fzm.mall.server.admin.goods_center.model.GoodsCommentAppend;
import com.fzm.mall.server.admin.goods_center.mapper.GoodsCommentAppendMapper;
import com.fzm.mall.server.admin.goods_center.service.IGoodsCommentAppendService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fzm.mall.server.admin.goods_center.service.IGoodsCommentService;
import com.fzm.mall.server.admin.order.model.OrderDetail;
import com.fzm.mall.server.admin.order.service.IOrderDetailService;
import lombok.RequiredArgsConstructor;
import org.dozer.DozerBeanMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author wangp
 * @date 2021/5/12 16:06
 * @description 商品追加评论表（用于对商品的评论） 服务实现类
 * @since JDK 1.8
 */
@Service
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class GoodsCommentAppendServiceImpl extends ServiceImpl<GoodsCommentAppendMapper, GoodsCommentAppend> implements IGoodsCommentAppendService {

    private final DozerBeanMapper beanMapper;
    private final IOrderDetailService orderDetailService;
    private final IGoodsCommentService goodsCommentService;

    @Override
    public GoodsCommentAppendDetailVO queryAppendCommentDetailByCommentId(String commentId) {
        GoodsCommentAppend commentAppend = queryByCommentId(commentId);
        if (commentAppend == null)
            return null;
        GoodsCommentAppendDetailVO vo = beanMapper.map(commentAppend, GoodsCommentAppendDetailVO.class);
        GoodsComment goodsComment = goodsCommentService.queryByCommentId(commentId);
        OrderDetail orderDetail = orderDetailService.queryByOidAndSkuId(goodsComment.getOid(), goodsComment.getSkuId());
        vo.setSellerState(orderDetail.getSellerState());
        return vo;
    }

    @Override
    public void appendReply(CommentReplyDTO dto) {
        GoodsCommentAppend commentAppend = queryByCommentId(dto.getCommentId());
        commentAppend.setReplyContent(dto.getReplyContent());
        updateById(commentAppend);

        GoodsComment goodsComment = goodsCommentService.queryByCommentId(dto.getCommentId());
        OrderDetail orderDetail = orderDetailService.queryByOidAndSkuId(goodsComment.getOid(), goodsComment.getSkuId());
        orderDetail.setSellerState(BuyerAndSellerStatusEnum.APPEND.getStatus());
        orderDetailService.updateById(orderDetail);
    }

    @Override
    public void appendDelete(String commentId) {
        Integer status = CommentIsValidEnum.NOT_AVAILABLE.getStatus();
        GoodsCommentAppend commentAppend = queryByCommentId(commentId);
        commentAppend.setIsValid(status);
        updateById(commentAppend);
    }

    public GoodsCommentAppend queryByCommentId(String commentId) {
        Integer status = CommentIsValidEnum.AVAILABLE.getStatus();
        QueryWrapper<GoodsCommentAppend> qw = new QueryWrapper<>();
        qw.lambda().eq(GoodsCommentAppend::getCommentId, commentId)
                .eq(GoodsCommentAppend::getIsValid, status);
        return getOne(qw);
    }
}


