package beilv.venue.service.impl;

import java.util.List;

import beilv.common.exception.ServiceException;
import beilv.session.mapper.BeilvSessionMapper;
import beilv.site.mapper.BeilvSiteMapper;
import beilv.venue.domain.BeilvVenue;
import beilv.venue.mapper.BeilvVenueMapper;
import beilv.venue.service.IBeilvVenueService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import beilv.common.core.text.Convert;

/**
 * 场馆信息Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-01-06
 */
@Service
public class BeilvVenueServiceImpl implements IBeilvVenueService
{
    @Autowired
    private BeilvVenueMapper beilvVenueMapper;

    @Autowired
    private BeilvSiteMapper beilvSiteMapper;

    @Autowired
    private BeilvSessionMapper beilvSessionMapper;

    /**
     * 查询场馆信息
     * 
     * @param id 场馆信息主键
     * @return 场馆信息
     */
    @Override
    public BeilvVenue selectBeilvVenueById(Long id)
    {
        return beilvVenueMapper.selectBeilvVenueById(id);
    }

    /**
     * 查询场馆信息列表
     * 
     * @param beilvVenue 场馆信息
     * @return 场馆信息
     */
    @Override
    public List<BeilvVenue> selectBeilvVenueList(BeilvVenue beilvVenue)
    {
        return beilvVenueMapper.selectBeilvVenueList(beilvVenue);
    }

    /**
     * 新增场馆信息
     * 
     * @param beilvVenue 场馆信息
     * @return 结果
     */
    @Override
    public int insertBeilvVenue(BeilvVenue beilvVenue)
    {
        return beilvVenueMapper.insertBeilvVenue(beilvVenue);
    }

    /**
     * 修改场馆信息
     * 
     * @param beilvVenue 场馆信息
     * @return 结果
     */
    @Override
    public int updateBeilvVenue(BeilvVenue beilvVenue)
    {
        return beilvVenueMapper.updateBeilvVenue(beilvVenue);
    }

    /**
     * 批量删除场馆信息
     * 
     * @param ids 需要删除的场馆信息主键
     * @return 结果
     */
    @Override
    public int deleteBeilvVenueByIds(String ids)
    {
        Long[] venueIds = Convert.toLongArray(ids);

        // 检查场馆是否绑定了球场或时段
        for (Long venueId : venueIds) {
            BeilvVenue beilvVenue = selectBeilvVenueById(venueId);
            if (countSessionByVenueId(venueId) > 0 || countSiteByVenueId(venueId) > 0)
            {
                throw new ServiceException(String.format("%1$s已分配,不能删除", beilvVenue.getVenueName()));
            }
        }
        return beilvVenueMapper.deleteBeilvVenueByIds(Convert.toStrArray(ids));
    }
    @Override
    public int countSessionByVenueId(Long venueId)
    {
        return beilvSessionMapper.countSessionByVenueId(venueId);
    }

    /**
     * 检查场馆是否绑定了球场
     */
    @Override
    public int countSiteByVenueId(Long venueId) {
        return beilvSiteMapper.countSiteByVenueId(venueId);
    }

    /**
     * 删除场馆信息信息
     * 
     * @param id 场馆信息主键
     * @return 结果
     */
    @Override
    public int deleteBeilvVenueById(Long id)
    {
        return beilvVenueMapper.deleteBeilvVenueById(id);
    }
}
