package com.ruoyi.web.work.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.web.work.domain.Notice;
import com.ruoyi.web.work.mapper.NoticeMapper;
import com.ruoyi.web.work.service.INoticeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author 高伟森
 * @date 2024-04-18
 */
@Service
public class NoticeServiceImpl extends ServiceImpl<NoticeMapper, Notice> implements INoticeService {
    @Autowired
    private NoticeMapper noticeMapper;

    @Override
    public List<Notice> selectList(Notice notice) {
        return noticeMapper.selectList(notice);
    }

    @Override
    public List<Notice> homeList(String type) {
        return noticeMapper.homeList(type);
    }

    @Override
    public AjaxResult add(Notice notice) {
        Notice bean = noticeMapper.selectByTitle(notice.getTitle());
        notice.setContents(notice.getContents().replace("/dev-api", "").replace("/prod-api", ""));
        if (bean != null) {
            return AjaxResult.error("该标题已存在");
        }
        if (!save(notice)) {
            return AjaxResult.error("新增通知失败");
        }
        return AjaxResult.success();
    }

    @Override
    public AjaxResult detail(Long id) {
        Notice notice = getById(id);
        if (notice == null || notice.getState() != 0) {
            return AjaxResult.error("通知不存在或停用");
        }
        return AjaxResult.success(notice);
    }
}
