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.Goods;
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.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;
    }
}
