package com.izouma.dingdong.service.backstage;


import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import com.izouma.dingdong.domain.backstage.Category;
import com.izouma.dingdong.domain.merchant.Merchant;
import com.izouma.dingdong.domain.merchant.MerchantSettings;
import com.izouma.dingdong.dto.MerchantDTO;
import com.izouma.dingdong.exception.BusinessException;
import com.izouma.dingdong.repo.backstage.CategoryRepo;
import com.izouma.dingdong.repo.merchant.MerchantRepo;
import com.izouma.dingdong.repo.merchant.MerchantSettingsRepo;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;


@Service
@AllArgsConstructor
public class CategoryService {

    private CategoryRepo         categoryRepo;
    private MerchantRepo         merchantRepo;
    private MerchantSettingsRepo merchantSettingsRepo;

    /*
    按类别显示商户列表
     */
/*    public List<MerchantDTO> categoryMer(Long categoryId) {
        Category category = categoryRepo.findById(categoryId).orElseThrow(new BusinessException("无分类"));
        List<MerchantSettings> settings = merchantSettingsRepo.findAllByCategoryContains(category);

        if (ObjectUtil.isNull(settings)) {
            return null;
        }
        List<MerchantDTO> dtos = CollUtil.newArrayList();
        settings.forEach(s -> {
            Merchant merchant = merchantRepo.findById(s.getMerchantId()).orElseThrow(new BusinessException("无商家"));
            dtos.add(new MerchantDTO(merchant, s));
        });
        return dtos;
    }*/

    public List<MerchantDTO> categoryMer(Long categoryId) {
        //1级
        Category category = categoryRepo.findById(categoryId).orElseThrow(new BusinessException("无分类"));

        List<Category> children = category.getChildren();

        //1 子为空 表示是最底层
        if (CollUtil.isEmpty(children)) {
            return toDTO(merchantSettingsRepo.findAllByCategoryContains(category));
        }

        //2级 子不为空
        List<Category> children2 = new ArrayList<>();
        //子下面的所有都加入list
        children.forEach(c -> children2.addAll(c.getChildren()));

        List<MerchantSettings> list = new ArrayList<>();

        if (CollUtil.isEmpty(children2)) {
            //子下为空，循环children
            children.forEach(c -> list.addAll(merchantSettingsRepo.findAllByCategoryContains(c)));
        } else {
            //3级 子的子不为空 循环子下的子
            children2.forEach(i -> list.addAll(merchantSettingsRepo.findAllByCategoryContains(i)));
        }
        return toDTO(list);

    }

    public List<MerchantDTO> toDTO(List<MerchantSettings> settings) {
        if (ObjectUtil.isNull(settings)) {
            return null;
        }
        List<MerchantDTO> dtos = CollUtil.newArrayList();
        settings.forEach(s -> {
            Merchant merchant = merchantRepo.findById(s.getMerchantId()).orElseThrow(new BusinessException("无商家"));
            dtos.add(new MerchantDTO(merchant, s));
        });
        return dtos;
    }


}
