package com.izouma.dingdong.service;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import com.izouma.dingdong.domain.Appraisal;
import com.izouma.dingdong.domain.OrderGoodsSpec;
import com.izouma.dingdong.domain.OrderInfo;
import com.izouma.dingdong.domain.User;
import com.izouma.dingdong.domain.merchant.Goods;
import com.izouma.dingdong.dto.AppraisalMerDTO;
import com.izouma.dingdong.enums.OrderStatus;
import com.izouma.dingdong.exception.BusinessException;
import com.izouma.dingdong.repo.AppraisalRepo;
import com.izouma.dingdong.repo.OrderGoodsSpecRepo;
import com.izouma.dingdong.repo.OrderInfoRepo;

import com.izouma.dingdong.service.merchant.SalesService;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

import javax.transaction.Transactional;
import java.util.List;
import java.util.stream.Collectors;

@Service
@AllArgsConstructor
public class AppraisalService {

    private AppraisalRepo appraisalRepo;
    private OrderInfoRepo orderInfoRepo;
    private OrderGoodsSpecRepo orderGoodsSpecRepo;
    private SalesService salesService;

    /*
    客户评价
     */
    @Transactional
    public Appraisal userAppraisal(Appraisal appraisal) {

        OrderInfo order = orderInfoRepo.findById(appraisal.getOrderInfoId()).orElseThrow(new BusinessException("无订单"));
        //OrderInfo order = appraisal.getOrder();
        if (OrderStatus.RATED.equals(order.getStatus())) {

            order.setRated(true);
            order.setStatus(OrderStatus.COMPLETED);
            orderInfoRepo.save(order);

            appraisal.setMerchantId(order.getMerchantId());
            appraisal.setRiderId(order.getRiderId());
            appraisal.setLikes(0);

             appraisal = appraisalRepo.save(appraisal);
            //更新销量数据
            salesService.addLike(appraisal);
            return appraisal;
        } else if (OrderStatus.COMPLETED.equals(order.getStatus())) {
            throw new BusinessException("订单已评价");
        } else {
            throw new BusinessException("订单未完成");
        }
    }

    /*
    按商品查找评价
     */
    public List<AppraisalMerDTO> goodsAppraisals(Long goodsId) {
        List<OrderGoodsSpec> specs = orderGoodsSpecRepo.findAllByGoodsId(goodsId);
        if (ObjectUtil.isNull(specs)) {
            return null;
        }
//        List<Appraisal> appraisals = CollUtil.newArrayList();
        List<AppraisalMerDTO> appraisals = CollUtil.newArrayList();
        specs.forEach(s -> {
            Appraisal appraisal = appraisalRepo.findByOrderInfoId(s.getOrderInfoId()).orElseThrow(new BusinessException("无订单"));
            if (ObjectUtil.isNotNull(appraisal)) {
                appraisals.add(this.toDTO(appraisal));
//                appraisals.add(appraisal);
            }
        });
        return appraisals;
    }


    public AppraisalMerDTO toDTO(Appraisal appraisal) {
        OrderInfo order = orderInfoRepo.findById(appraisal.getOrderInfoId()).orElseThrow(new BusinessException("无订单"));
        List<Goods> collect = order.getOrderGoodsSpecs().stream().map(OrderGoodsSpec::getGoods).collect(Collectors.toList());
        String goodsName = collect.stream().map(Goods::getName).collect(Collectors.joining(","));
        User user = order.getUser();
        return AppraisalMerDTO.builder()
                .appraiseTime(appraisal.getAppraiseTime())
                .avatar(user.getAvatar())
                .goodsAppraise(appraisal.getGoodsAppraise())
                .goodsBad(appraisal.getGoodsBad())
                .goodsLike(appraisal.getGoodsLike())
                .goodsName(goodsName)
                .merchantReply(appraisal.getMerchantReply())
                .nickname(user.getNickname())
                .img(appraisal.getImg())
                .build();
    }


}
