package com.cyksj.service.claude.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cyksj.mapper.claudecode.AnnouncementMapper;
import com.cyksj.model.entity.Announcement;
import com.cyksj.service.claude.AnnouncementService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;

/**
 * @author chan
 * @date 2025/8/11 18:03
 */
@Service
@RequiredArgsConstructor

public class AnnouncementServiceImpl extends ServiceImpl<AnnouncementMapper, Announcement> implements AnnouncementService {

    /**
     * 公告服务
     */

    private final AnnouncementMapper announcementMapper;

    /**
     * 获取有效公告列表
     */
    @Override
    public List<Announcement> getActiveAnnouncements(String language) {
        QueryWrapper<Announcement> wrapper = new QueryWrapper<>();
        wrapper.eq("status", 1)
                .eq("language", language)
                .and(w -> w.isNull("start_date").or().le("start_date", new Date()))
                .and(w -> w.isNull("end_date").or().ge("end_date", new Date()))
                .orderByDesc("priority")
                .orderByDesc("created_at");

        return announcementMapper.selectList(wrapper);
    }

    /**
     * 创建公告
     */
    @Override
    public void createAnnouncement(Announcement announcement) {
        announcement.setCreatedTime(new Date());
        announcement.setUpdateTime(new Date());
        announcementMapper.insert(announcement);
    }

    /**
     * 更新公告
     */
    @Override
    public void updateAnnouncement(Announcement announcement) {
        announcement.setUpdateTime(new Date());
        announcementMapper.updateById(announcement);
    }

    /**
     * 删除公告
     */
    @Override
    public void deleteAnnouncement(Long id) {
        announcementMapper.deleteById(id);
    }

}
