package com.gameplatform.server.service.admin;

import com.gameplatform.server.mapper.admin.AnnouncementMapper;
import com.gameplatform.server.model.entity.admin.Announcement;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional
public class AnnouncementService {
    
    @Autowired
    private AnnouncementMapper announcementMapper;
    
    /**
     * 创建公告
     */
    public boolean createAnnouncement(Announcement announcement) {
        return announcementMapper.insert(announcement) > 0;
    }
    
    /**
     * 根据ID获取公告
     */
    public Announcement getAnnouncementById(Long id) {
        return announcementMapper.findById(id);
    }
    
    /**
     * 更新公告
     */
    public boolean updateAnnouncement(Announcement announcement) {
        return announcementMapper.update(announcement) > 0;
    }
    
    /**
     * 删除公告
     */
    public boolean deleteAnnouncement(Long id) {
        return announcementMapper.deleteById(id) > 0;
    }
    
    /**
     * 获取所有公告（分页）
     */
    public List<Announcement> getAllAnnouncements(int size, int offset) {
        return announcementMapper.findAll(size, offset);
    }
    
    /**
     * 获取公告总数
     */
    public long getAnnouncementCount() {
        return announcementMapper.countAll();
    }
    
    /**
     * 根据启用状态获取公告（分页）
     */
    public List<Announcement> getAnnouncementsByEnabled(Boolean enabled, int size, int offset) {
        return announcementMapper.findByEnabled(enabled, size, offset);
    }
    
    /**
     * 根据启用状态获取公告数量
     */
    public long getAnnouncementCountByEnabled(Boolean enabled) {
        return announcementMapper.countByEnabled(enabled);
    }
    
    /**
     * 更新公告启用状态
     */
    public boolean updateAnnouncementEnabled(Long id, Boolean enabled) {
        return announcementMapper.updateEnabled(id, enabled) > 0;
    }
    
    /**
     * 获取所有启用的公告（用于前端显示）
     */
    public List<Announcement> getEnabledAnnouncements() {
        return announcementMapper.findByEnabled(true, 10, 0); // 最多返回10条启用的公告
    }
    
    /**
     * 检查公告是否存在
     */
    public boolean announcementExists(Long id) {
        return getAnnouncementById(id) != null;
    }
    
    /**
     * 根据归属ID获取公告（分页）
     */
    public List<Announcement> getAnnouncementsByBelongId(Integer belongId, int size, int offset) {
        return announcementMapper.findByBelongId(belongId, size, offset);
    }
    
    /**
     * 根据归属ID获取公告数量
     */
    public long getAnnouncementCountByBelongId(Integer belongId) {
        return announcementMapper.countByBelongId(belongId);
    }
    
    /**
     * 根据归属ID和启用状态获取公告（分页）
     */
    public List<Announcement> getAnnouncementsByBelongIdAndEnabled(Integer belongId, Boolean enabled, int size, int offset) {
        return announcementMapper.findByBelongIdAndEnabled(belongId, enabled, size, offset);
    }
    
    /**
     * 根据归属ID和启用状态获取公告数量
     */
    public long getAnnouncementCountByBelongIdAndEnabled(Integer belongId, Boolean enabled) {
        return announcementMapper.countByBelongIdAndEnabled(belongId, enabled);
    }
    
    /**
     * 获取指定归属ID的启用公告（用于前端显示）
     */
    public List<Announcement> getEnabledAnnouncementsByBelongId(Integer belongId) {
        return announcementMapper.findByBelongIdAndEnabled(belongId, true, 10, 0); // 最多返回10条启用的公告
    }
}
