package com.izouma.dingdong.service;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSONObject;
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.dto.OrderGoodsSpecDTO;
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 com.izouma.dingdong.service.user.ShoppingCartService;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

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

@Service
@AllArgsConstructor
public class OrderGoodsSpecService {

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

    /*
    添加规格
     */
    public OrderGoodsSpec add(OrderGoodsSpecDTO orderGoodsSpecDTO) {
        OrderGoodsSpec goodsSpec = null;
        if (orderGoodsSpecDTO.getId()!=null){
            goodsSpec = orderGoodsSpecRepo.findById(orderGoodsSpecDTO.getId()).orElseThrow(new BusinessException("无记录"));
        }

        String spec = null;
        //String ids = null;
        Goods goods = goodsRepo.findById(orderGoodsSpecDTO.getGoodsId()).orElseThrow(new BusinessException("无商品"));

        //判断商品是否通过审核
        if (!ApplyStatus.PASS.equals(goods.getStatus())) {
            throw new BusinessException("商品审核中或未通过");
        }

        BigDecimal total = goods.getAmount();
        BigDecimal real = goods.getDiscountAmount();

/*
        if (CollUtil.isNotEmpty(goodsSpecifications)) {
            for (GoodsSpecification g : goodsSpecifications) {
                total = total.add(g.getAmount());
                real = real.add(g.getAmount());


            }
            spec = JSONObject.toJSONString(goodsSpecifications);
*/

//        }
        return null;

    }

    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();


        if (StrUtil.isNotBlank(goodsSpecificationIds)) {
            LongArrayConverter longArrayConverter = new LongArrayConverter();
            List<Long> longs = longArrayConverter.convertToEntityAttribute(goodsSpecificationIds);

            for (Long l : longs) {
                GoodsSpecification spec = goodsSpecificationRepo.findById(l).orElseThrow(new BusinessException("无规格"));
                //价钱
                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 (num == null || num <= 0) {
            num = 1;
        }

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

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

        return orderGoodsSpecRepo.save(save);
    }


}
