package com.gree.mall.miniapp.logic.order;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gree.mall.miniapp.bean.goods.CommentTagCount;
import com.gree.mall.miniapp.bean.goods.GoodsComment;
import com.gree.mall.miniapp.bean.goods.GoodsCommentCount;
import com.gree.mall.miniapp.bean.order.OrderCommentBean;
import com.gree.mall.miniapp.bean.user.CurrentCompanyWechat;
import com.gree.mall.miniapp.commonmapper.GoodsSpecDetailMapper;
import com.gree.mall.miniapp.constant.Constant;
import com.gree.mall.miniapp.exception.RemoteServiceException;
import com.gree.mall.miniapp.logic.common.CommonLogic;
import com.gree.mall.miniapp.plus.entity.CommentTag;
import com.gree.mall.miniapp.plus.entity.CommonFile;
import com.gree.mall.miniapp.plus.entity.OrderCommentTag;
import com.gree.mall.miniapp.plus.entity.OrderInfo;
import com.gree.mall.miniapp.plus.service.CommentTagService;
import com.gree.mall.miniapp.plus.service.OrderCommentService;
import com.gree.mall.miniapp.plus.service.OrderCommentTagService;
import com.gree.mall.miniapp.plus.service.OrderInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;
import java.util.Random;
import java.util.stream.Collectors;

@Service
public class OrderCommentLogic {

    @Autowired
    OrderCommentService orderCommentService;
    @Autowired
    OrderCommentTagService orderCommentTagService;
    @Autowired
    OrderInfoService orderInfoService;
    @Autowired
    CommentTagService commentTagService;
    @Autowired
    GoodsSpecDetailMapper goodsSpecDetailMapper;
    @Autowired
    CommonLogic commonLogic;


    /**
     * 评价标签列表
     */
    public List<CommentTag> list(HttpServletRequest request){
        CurrentCompanyWechat currentCompanyWechat = commonLogic.getCurrentCompanyWechat(request);
        String companyWechatId = currentCompanyWechat.getCurrentCompanyWechatId();

        List<CommentTag> list = commentTagService.lambdaQuery().eq(CommentTag::getCompanyWechatId,companyWechatId).list();
        return list;
    }

    /**
     * 商品的评价汇总
     */
    public GoodsCommentCount goodsCommentCount(HttpServletRequest request , String goodsId){
        CurrentCompanyWechat companyWechat = commonLogic.getCurrentCompanyWechat(request);

        //0-4的随机数
        int num = new Random().nextInt(5);

        GoodsCommentCount goodsCommentCount = new GoodsCommentCount();
        List<CommentTagCount> commentTagCounts = goodsSpecDetailMapper.goodsCommentCount(companyWechat.getCurrentCompanyWechatId(),goodsId);
        goodsCommentCount.setTagCountList(commentTagCounts);
        goodsCommentCount.setGoodRate(95 + num);
        return goodsCommentCount;
    }

    /**
     * 商品的评价列表
     */
    public IPage<GoodsComment> goodsCommentList(HttpServletRequest request ,String goodsId,String tag,Integer pageNo,Integer pageSize){

        CurrentCompanyWechat currentCompanyWechat = commonLogic.getCurrentCompanyWechat(request);

        IPage<GoodsComment> goodsCommentIPage = goodsSpecDetailMapper.goodsCommentList(new Page(pageNo, pageSize),
                currentCompanyWechat.getCurrentCompanyWechatId(),goodsId,tag);

        for(GoodsComment goodsComment : goodsCommentIPage.getRecords()){
            //标签
            List<OrderCommentTag> tagList = orderCommentTagService.lambdaQuery()
                    .eq(OrderCommentTag::getOrderId, goodsComment.getOrderId())
                    .eq(OrderCommentTag::getCompanyWechatId, currentCompanyWechat.getCurrentCompanyWechatId())
                    .list();
            if(tagList.size() > 0) {
                goodsComment.setTags(tagList.stream().map(OrderCommentTag::getTag).collect(Collectors.toList()));
            }
            //图片
            List<CommonFile> commonFiles = commonLogic.queryFileByObjId(goodsComment.getOrderId(), Constant.Img.ORDER_COMMENT);
            if(commonFiles.size() > 0){
                goodsComment.setImgs(commonFiles.stream().map(CommonFile::getUrl).collect(Collectors.toList()));
            }
        }
        return goodsCommentIPage;
    }

    /**
     * 新增评价
     * @param orderCommentBean
     */
    public void add(HttpServletRequest request , OrderCommentBean orderCommentBean){
        CurrentCompanyWechat currentCompanyWechat = commonLogic.getCurrentCompanyWechat(request);
        OrderInfo orderInfo1 = orderInfoService.getById(orderCommentBean.getOrderId());
        if(orderInfo1.getCommentGoods() > 0){
            throw new RemoteServiceException("请勿重复评价");
        }
        if(orderCommentBean.getCommentExpress() == 5 && orderCommentBean.getCommentGoods() == 5 && orderCommentBean.getCommentExpress() == 5){
            orderCommentBean.setIsShow(true);
        }
        orderCommentBean.setCreateTime(new Date());
        orderCommentBean.setCompanyWechatId(currentCompanyWechat.getCurrentCompanyWechatId());
        orderCommentBean.setCompanyName(currentCompanyWechat.getCurrentCompanyName());
        orderCommentService.save(orderCommentBean);
        //保存标签
        for(String tag : orderCommentBean.getTags()){
            OrderCommentTag orderCommentTag = new OrderCommentTag();
            orderCommentTag.setCreateTime(new Date());
            orderCommentTag.setOrderCommentId(orderCommentBean.getOrderCommentId());
            orderCommentTag.setOrderId(orderCommentBean.getOrderId());
            orderCommentTag.setTag(tag);
            orderCommentTag.setCompanyWechatId(currentCompanyWechat.getCurrentCompanyWechatId());
            orderCommentTag.setCompanyName(currentCompanyWechat.getCurrentCompanyName());
            orderCommentTag.insert();
        }
        //保存图片
        commonLogic.bindFile(orderCommentBean.getOrderId(), Constant.Img.ORDER_COMMENT, orderCommentBean.getFileIds());

        //更新订单表
        OrderInfo orderInfo = new OrderInfo();
        orderInfo.setOrderId(orderCommentBean.getOrderId());
        orderInfo.setCommentGoods(orderCommentBean.getCommentGoods());
        orderInfo.setCommentService(orderCommentBean.getCommentService());
        orderInfo.setCommentExpress(orderCommentBean.getCommentExpress());
        orderInfo.updateById();
    }



}
