package com.izouma.dingdong.service.merchant;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import com.izouma.dingdong.domain.merchant.GoodsSpecification;
import com.izouma.dingdong.exception.BusinessException;
import com.izouma.dingdong.repo.merchant.GoodsRepo;
import com.izouma.dingdong.repo.merchant.GoodsSpecificationRepo;
import com.izouma.dingdong.utils.ObjUtils;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

@Service
@AllArgsConstructor
public class GoodsSpecificationService {

    private GoodsSpecificationRepo goodsSpecificationRepo;

    private GoodsRepo goodsRepo;

    public List<GoodsSpecification> saveAll(List<GoodsSpecification> spec) {
        List<GoodsSpecification> all = new ArrayList<>();
        spec.forEach(s -> {
/*            if (s.getAmount().compareTo(BigDecimal.ZERO) < 0) {
                throw new BusinessException("规格价钱不得小于零");
            }*/
            if (s.getId() != null && s.getId() < 0 && s.getParent() == null) {
                goodsRepo.findById(s.getGoodsId()).orElseThrow(new BusinessException("商品不存在"));
                GoodsSpecification save = goodsSpecificationRepo.save(s);
//                if (s.getParent() == null) {
                Set<GoodsSpecification> collect = spec.stream()
                        .filter(g -> g.getParent() != null && g.getParent().equals(s.getId()))
                        .collect(Collectors.toSet());

                collect.forEach(c -> {
                    //c.setParent(save.getId());
                    GoodsSpecification g = new GoodsSpecification();
                    BeanUtil.copyProperties(c, g);
                    g.setParent(save.getId());
                    if (c.getId() != null && c.getId() < 0) {
                        all.add(goodsSpecificationRepo.save(g));
                    }
                });
//                }
                all.add(save);
            } else if ((s.getId() == null || s.getId() < 0) && s.getParent() > 0) {
                goodsRepo.findById(s.getGoodsId()).orElseThrow(new BusinessException("商品不存在"));
                goodsSpecificationRepo.findById(s.getParent()).orElseThrow(new BusinessException("无分类"));
                all.add(goodsSpecificationRepo.save(s));
            } else if (s.getId() > 0) {
                //修改
                GoodsSpecification goodsSpecification = goodsSpecificationRepo.findById(s.getId())
                        .orElseThrow(new BusinessException("不存在"));
                ObjUtils.merge(goodsSpecification, s);
                goodsRepo.findById(goodsSpecification.getGoodsId()).orElseThrow(new BusinessException("商品不存在"));
                all.add(goodsSpecificationRepo.save(goodsSpecification));
            }
        });

        return all;
    }

    public List<GoodsSpecification> save(List<GoodsSpecification> specifications) {
        List<GoodsSpecification> all = new ArrayList<>();
        specifications.forEach(s -> {
            goodsRepo.findById(s.getGoodsId()).orElseThrow(new BusinessException("商品不存在"));
            GoodsSpecification save;
            if (s.getId() != null) {
                GoodsSpecification specification = goodsSpecificationRepo.findById(s.getId())
                        .orElseThrow(new BusinessException("无记录"));
                ObjUtils.merge(specification, s);
                save = goodsSpecificationRepo.save(specification);
            } else {
                save = goodsSpecificationRepo.save(s);
            }
            all.add(save);
            if (CollUtil.isNotEmpty(s.getChildren())) {
                s.getChildren().forEach(c -> {
                    c.setParent(save.getId());
                    if (c.getId() != null) {
                        GoodsSpecification specification = goodsSpecificationRepo.findById(s.getId())
                                .orElseThrow(new BusinessException("无记录"));
                        ObjUtils.merge(specification, s);
                        all.add(goodsSpecificationRepo.save(specification));
                    } else {
                        all.add(goodsSpecificationRepo.save(c));
                    }
                });
            }
        });

        return all;
    }

}
