package com.izouma.dingdong.service;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.izouma.dingdong.converter.LongArrayConverter;
import com.izouma.dingdong.domain.OrderGoodsSpec;
import com.izouma.dingdong.domain.merchant.Goods;
import com.izouma.dingdong.domain.merchant.GoodsSpecification;
import com.izouma.dingdong.enums.ApplyStatus;
import com.izouma.dingdong.exception.BusinessException;
import com.izouma.dingdong.repo.OrderGoodsSpecRepo;
import com.izouma.dingdong.repo.merchant.GoodsRepo;
import com.izouma.dingdong.repo.merchant.GoodsSpecificationRepo;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

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

@Service
@AllArgsConstructor
public class OrderGoodsSpecService {

    private OrderGoodsSpecRepo     orderGoodsSpecRepo;
    private GoodsSpecificationRepo goodsSpecificationRepo;
    private GoodsRepo              goodsRepo;


    public OrderGoodsSpec add(Long goodsId, String goodsSpecificationIds, Integer num) {
        Goods goods = goodsRepo.findById(goodsId).orElseThrow(new BusinessException("无商品"));
        //判断商品是否通过审核
        if (!ApplyStatus.PASS.equals(goods.getStatus())) {
            throw new BusinessException("商品审核中或未通过");
        }
        StringBuilder specName = null;
        BigDecimal total = goods.getAmount();
        BigDecimal real = goods.getDiscountAmount();

        List<Long> longs = new ArrayList<>();

        if (StrUtil.isNotBlank(goodsSpecificationIds)) {
            LongArrayConverter longArrayConverter = new LongArrayConverter();
            longs = longArrayConverter.convertToEntityAttribute(goodsSpecificationIds);

            List<Long> parents = new ArrayList<>();
            for (Long l : longs) {
                GoodsSpecification spec = goodsSpecificationRepo.findById(l).orElseThrow(new BusinessException("无规格"));

                if (!spec.getGoodsId().equals(goodsId)) {
                    throw new BusinessException("不是此商品下规格");
                }
                if (ObjectUtil.isEmpty(spec.getParent())) {
                    throw new BusinessException("选择规格分类下的规格商品");
                } else {
                    GoodsSpecification parentSpec = goodsSpecificationRepo.findById(spec.getParent())
                            .orElseThrow(new BusinessException("无规格分类"));
                    if (!parentSpec.getMultiple()) {
                        parents.add(spec.getParent());
                    }
                }

                if (ObjectUtil.isEmpty(spec.getAmount())) {
                    spec.setAmount(BigDecimal.ZERO);
                }

                //价钱
                total = total.add(spec.getAmount());
                real = real.add(spec.getAmount());

                //名字
                if (specName != null) {
                    specName.append(",").append(spec.getName());
                } else {
                    specName = new StringBuilder(spec.getName());
                }
            }

            if (parents.size() != new HashSet<>(parents).size()) {
                throw new BusinessException("存在不可多选规格");
            }
        }
        if (num == null || num <= 0) {
            num = 1;
        }

        OrderGoodsSpec save = OrderGoodsSpec.builder()
                .goodsId(goodsId)
                .specificationId(longs)
                //.specificationId(longs)
                .num(num)
                .goodsPrice(total)
                .goodsRealPrice(real)
                .build();

        if (StrUtil.isNotBlank(specName)) {
            save.setSpecification(specName.toString());
        }
        return orderGoodsSpecRepo.save(save);
    }


}
