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.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fzm.mall.server.admin.goods_center.entity.dto.CommentReplyDTO;
import com.fzm.mall.server.admin.goods_center.entity.vo.*;
import com.fzm.mall.server.admin.goods_center.enums.status.AnonymousStatusEnum;
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.mapper.GoodsCommentMapper;
import com.fzm.mall.server.admin.goods_center.model.UserAuth;
import com.fzm.mall.server.admin.goods_center.service.*;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fzm.mall.server.admin.order.model.OrderDetail;
import com.fzm.mall.server.admin.order.service.IOrderDetailService;
import com.fzm.mall.server.admin.user_center.model.User;
import com.fzm.mall.server.admin.user_center.service.IUserService;
import lombok.RequiredArgsConstructor;
import org.dozer.DozerBeanMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

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

    private final DozerBeanMapper beanMapper;
    private final ISkuPropService skuPropService;
    private final IUserAuthService userAuthService;
    private final IOrderDetailService orderDetailService;
    private final IUserService userService;

    @Override
    public IPage<GoodsCommentVO> page(String merchantId, String goodsId, String goodsName, Integer evaluateType, Integer replyStatus, Integer pageNum, Integer pageSize) {
        IPage<GoodsCommentVO> page = new Page<>(pageNum, pageSize);
        IPage<GoodsCommentVO> ipage = baseMapper.page(page, merchantId, goodsId, goodsName, evaluateType, replyStatus);
        List<GoodsCommentVO> records = ipage.getRecords();
        List<GoodsCommentVO> newRecords = new ArrayList<>();

        for (GoodsCommentVO vo : records) {
            if (vo.getAnonymous().equals(AnonymousStatusEnum.YES.getStatus()))
                vo.setUserName(null);
            newRecords.add(vo);
        }
        ipage.setRecords(newRecords);
        return ipage;
    }

    @Override
    public GoodsCommentDetailVO queryCommentDetailByCommentId(String commentId) {
        //评论记录
        GoodsComment goodsComment = queryByCommentId(commentId);

        //评论详情
        GoodsCommentDetailVO detailVO = beanMapper.map(goodsComment, GoodsCommentDetailVO.class);

        //商品规格
        List<SkuPropertyVO> propertyVOS = skuPropService.getBySkuId(goodsComment.getSkuId());
        StringBuilder sku = new StringBuilder();
        propertyVOS.forEach(v ->
                sku.append(v.getPropKey()).append(":").append(v.getPropVal()).append(","));
        String subSku = sku.substring(0, sku.lastIndexOf(","));
        detailVO.setSku(subSku);

        //用户名
        User user = userService.queryByUid(goodsComment.getUid());
        detailVO.setUserName(user == null ? null : user.getNickName());

        return detailVO;
    }

    @Override
    public void reply(CommentReplyDTO dto) {
        GoodsComment goodsComment = queryByCommentId(dto.getCommentId());
        goodsComment.setReplyContent(dto.getReplyContent());
        updateById(goodsComment);

        OrderDetail orderDetail = orderDetailService.queryByOidAndSkuId(goodsComment.getOid(), goodsComment.getSkuId());
        orderDetail.setSellerState(BuyerAndSellerStatusEnum.YES.getStatus());
        orderDetailService.updateById(orderDetail);
    }

    @Override
    public void delete(String commentId) {
        Integer status = CommentIsValidEnum.NOT_AVAILABLE.getStatus();
        GoodsComment goodsComment = queryByCommentId(commentId);
        goodsComment.setIsValid(status);
        updateById(goodsComment);
    }

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

}
