package cn.iocoder.yudao.module.system.service.sys;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils;
import cn.iocoder.yudao.module.system.controller.admin.sys.vo.NewsPageReqVO;
import cn.iocoder.yudao.module.system.dal.dataobject.sys.NewsDO;
import cn.iocoder.yudao.module.system.dal.mysql.sys.NewsMapper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

import org.springframework.validation.annotation.Validated;

import java.time.LocalDateTime;


/**
 * 系统文章新闻 Service 实现类
 *
 * @author 系统管理员
 */
@Service
@Validated
public class NewsServiceImpl implements NewsService {

    @Resource
    private NewsMapper newsMapper;


    @Override
    public Long createNews(NewsDO newsDO) {
        newsDO.setCreatedBy(SecurityFrameworkUtils.getLoginUserNickname());
        newsDO.setCreatedTime(LocalDateTime.now());
        newsMapper.insert(newsDO);
        return newsDO.getId();
    }

    @Override
    public void deleteNews(Long[] ids) {
        for (Long id : ids) {
            newsMapper.deleteById(id);
        }
    }

    @Override
    public PageResult<NewsDO> getNewsPage(NewsPageReqVO pageReqVO) {
        return newsMapper.selectPage(pageReqVO);
    }

    @Override
    public NewsDO getNews(Long id) {
        return newsMapper.selectById(id);
    }

    @Override
    public void updateStatus(Long id, Integer status) {
        NewsDO newsDO = newsMapper.selectById(id);
        if (newsDO != null) {
            newsDO.setStatus(status);
            newsMapper.updateById(newsDO);
        }
    }

    @Override
    public void updateNews(NewsDO newsDO) {
        newsMapper.updateById(newsDO);
    }
}