package com.izouma.jiashanxia.service;

import com.izouma.jiashanxia.domain.SetGoods;
import com.izouma.jiashanxia.domain.SetMeal;
import com.izouma.jiashanxia.dto.PageQuery;
import com.izouma.jiashanxia.enums.SetType;
import com.izouma.jiashanxia.exception.BusinessException;
import com.izouma.jiashanxia.repo.SetGoodsRepo;
import com.izouma.jiashanxia.repo.SetMealRepo;
import com.izouma.jiashanxia.utils.JpaUtils;
import lombok.AllArgsConstructor;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Service;

import java.util.List;


@Service
@AllArgsConstructor
public class SetMealService {

    private SetMealRepo    setMealRepo;
    private SetGoodsRepo   setGoodsRepo;
    private UserSetService userSetService;

    public Page<SetMeal> all(PageQuery pageQuery) {
        return setMealRepo.findAll(JpaUtils.toSpecification(pageQuery, SetMeal.class), JpaUtils.toPageRequest(pageQuery));
    }

    /*
    开通团队套餐
     */
    public void openTeamSet(Long userId, Long setMealId) {
        SetMeal set = setMealRepo.findById(setMealId).orElseThrow(new BusinessException("无套餐"));
        if (!SetType.TEAM.equals(set.getType())) {
            throw new BusinessException("不是团队套餐");
        }

        // 加入套餐商品
        List<SetGoods> setGoodsList = setGoodsRepo.findAllBySetMealId(setMealId);
        userSetService.joinUserSet(userId, setGoodsList);

    }
}
