package com.amms.service.impl;

import java.util.List;
import java.util.Date;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.amms.mapper.AnnouncementMapper;
import com.amms.domain.Announcement;
import com.amms.service.IAnnouncementService;
import com.amms.security.SecurityUtils;

/**
 * 公告Service业务层处理
 */
@Service
public class AnnouncementServiceImpl implements IAnnouncementService {

    @Autowired
    private AnnouncementMapper announcementMapper;

    /**
     * 查询公告列表
     *
     * @param announcement 公告
     * @return 公告
     */
    @Override
    public List<Announcement> selectAnnouncementList(Announcement announcement) {
        return announcementMapper.selectAnnouncementList(announcement);
    }

    /**
     * 查询公告
     *
     * @param id 公告主键
     * @return 公告
     */
    @Override
    public Announcement selectAnnouncementById(Long id) {
        return announcementMapper.selectAnnouncementById(id);
    }

    /**
     * 新增公告
     *
     * @param announcement 公告
     * @return 结果
     */
    @Override
    public int insertAnnouncement(Announcement announcement) {
        announcement.setCreateTime(new Date());
        announcement.setCreator(SecurityUtils.getCurrentUser().getId());
        return announcementMapper.insertAnnouncement(announcement);
    }

    /**
     * 修改公告
     *
     * @param announcement 公告
     * @return 结果
     */
    @Override
    public int updateAnnouncement(Announcement announcement) {
        announcement.setUpdateTime(new Date());
        announcement.setUpdater(SecurityUtils.getCurrentUser().getId());
        return announcementMapper.updateAnnouncement(announcement);
    }

    /**
     * 删除公告信息
     *
     * @param id 公告主键
     * @return 结果
     */
    @Override
    public int deleteAnnouncementById(Long id) {
        return announcementMapper.deleteAnnouncementById(id);
    }

    @Override
    public int setTop(Long id) {
        Announcement query = new Announcement();
        query.setIsTop(1L);
        List<Announcement> tops = announcementMapper.selectAnnouncementList(query);
        Long currentTopId = (tops != null && !tops.isEmpty()) ? tops.get(0).getId() : null;

        Long userId = SecurityUtils.getCurrentUser().getId();
        Date now = new Date();

        if (currentTopId != null && !currentTopId.equals(id)) {
            Announcement clearTop = new Announcement();
            clearTop.setId(currentTopId);
            clearTop.setIsTop(0L);
            clearTop.setUpdater(userId);
            clearTop.setUpdateTime(now);
            announcementMapper.updateAnnouncement(clearTop);
        }

        Announcement setTop = new Announcement();
        setTop.setId(id);
        setTop.setIsTop(1L);
        setTop.setUpdater(userId);
        setTop.setUpdateTime(now);
        return announcementMapper.updateAnnouncement(setTop);
    }

    @Override
    public List<Announcement> selectTopAnnouncements() {
        return announcementMapper.selectTopAnnouncements();
    }

    @Override
    public List<Announcement> selectNormalAnnouncements(Announcement filter) {
        return announcementMapper.selectNormalAnnouncements(filter);
    }
}
