package com.fzm.mall.server.front.goods.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.fzm.mall.server.front.goods.model.Comment;
import com.fzm.mall.server.front.goods.model.GoodsComment;
import com.fzm.mall.server.front.goods.mapper.GoodCommentMapper;
import com.fzm.mall.server.front.goods.model.vo.CommentVo;
import com.fzm.mall.server.front.goods.model.vo.OrderCommentVo;
import com.fzm.mall.server.front.goods.model.vo.PageVo;
import com.fzm.mall.server.front.goods.model.vo.PageVoWithOne;
import com.fzm.mall.server.front.goods.service.IGoodCommentService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fzm.mall.server.front.util.UUIdUtil;
import io.netty.util.internal.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;

/**
 * <p>
 * 商品评论表（用于对商品的评论） 服务实现类
 * </p>
 *
 * @author fzm
 * @since 2021-04-01
 */
@Service
public class GoodCommentServiceImpl extends ServiceImpl<GoodCommentMapper, GoodsComment> implements IGoodCommentService {

    @Autowired
    private GoodCommentMapper goodCommentMapper;

    @Override
    public List<CommentVo> getByGoodsId(String goodsId, PageVoWithOne pageVo) {
        int start = (pageVo.getPage() - 1) * pageVo.getSize();
        int end = pageVo.getSize();
        System.err.println("start:"+start + "end:"+end);
        return goodCommentMapper.getByGoodsId(goodsId, start, end);
    }

    @Override
    public Integer getCount(String goodsId) {
        return goodCommentMapper.getCount(goodsId);
    }

    @Override
    public String getCommonentId(String oid, String skuId) {
        QueryWrapper<GoodsComment> qw = new QueryWrapper<>();
        qw.eq("oid", oid);
        qw.eq("sku_id", skuId);
        GoodsComment one = getOne(qw);
        return one == null ? "" : one.getCommentId();
    }

    @Override
    public String getOidByCommentId(String commentId) {
        return goodCommentMapper.getOidByCommentId(commentId);
    }

    @Override
    public GoodsComment getByCommentId(String commentId) {
        QueryWrapper<GoodsComment> qw = new QueryWrapper<>();
        qw.eq("comment_id", commentId);
        return getOne(qw);
    }

    @Override
    public List<CommentVo> getByGoodsIdByCommentId(String commentId) {
        return goodCommentMapper.getByGoodsIdByCommentId(commentId);
    }

    @Override
    public BigDecimal getScope(String MerchantId) {
        return goodCommentMapper.getScope(MerchantId);
    }


    @Override
    public String addComment(String uid, OrderCommentVo orderCommentVo, String goodsId,String merchantId) {
        GoodsComment comment = new GoodsComment();
        String uuid = UUIdUtil.getUUID();
        comment.setSkuId(orderCommentVo.getSkuId());
        comment.setGoodsId(goodsId);
        comment.setCommentId(uuid);
        comment.setOid(orderCommentVo.getOid());
        comment.setEvaluate(orderCommentVo.getEvaluate());
        comment.setContent(orderCommentVo.getContent());
        comment.setUid(uid);
        comment.setMerchantId(merchantId);
        if (orderCommentVo.getImagesJson() == null) {
            comment.setImagesJson("");
        } else {
            comment.setImagesJson(orderCommentVo.getImagesJson().toJSONString());
        }
        comment.setVideo(orderCommentVo.getVideo());
        comment.setBusinessEvaluate(orderCommentVo.getBusinessEvaluate());
        comment.setAnonymous(orderCommentVo.getAnonymous());
        comment.setExpressEvaluate(orderCommentVo.getExpressEvaluate());
        goodCommentMapper.insert(comment);
        return  uuid;
    }

}
