package com.ruoyi.hezhi.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.hezhi.domain.TbNews;
import com.ruoyi.hezhi.domain.vo.NewsVO;
import com.ruoyi.hezhi.mapper.TbNewsMapper;
import com.ruoyi.hezhi.service.ITbNewsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.util.List;

/**
 * 新闻咨询Service业务层处理
 *
 * @author ruoyi
 * @date 2024-09-14
 */
@Service
public class TbNewsServiceImpl implements ITbNewsService
{
    @Autowired
    private TbNewsMapper tbNewsMapper;

    /**
     * 查询新闻咨询
     *
     * @param newsId 新闻咨询主键
     * @return 新闻咨询
     */
    @Override
    public TbNews selectTbNewsByNewsId(Long newsId)
    {
        return tbNewsMapper.selectTbNewsByNewsId(newsId);
    }

    /**
     * 查询新闻咨询列表
     *
     * @param tbNews 新闻咨询
     * @return 新闻咨询
     */
    @Override
    public List<TbNews> selectTbNewsList(TbNews tbNews)
    {
        return tbNewsMapper.selectTbNewsList(tbNews);
    }

    /**
     * 新增新闻咨询
     *
     * @param tbNews 新闻咨询
     * @return 结果
     */
    @Override
    public int insertTbNews(TbNews tbNews)
    {
        tbNews.setCreateTime(DateUtils.getNowDate());
        return tbNewsMapper.insertTbNews(tbNews);
    }

    /**
     * 修改新闻咨询
     *
     * @param tbNews 新闻咨询
     * @return 结果
     */
    @Override
    public int updateTbNews(TbNews tbNews)
    {
        tbNews.setUpdateTime(DateUtils.getNowDate());
        return tbNewsMapper.updateTbNews(tbNews);
    }

    /**
     * 批量删除新闻咨询
     *
     * @param newsIds 需要删除的新闻咨询主键
     * @return 结果
     */
    @Override
    public int deleteTbNewsByNewsIds(Long[] newsIds)
    {
        return tbNewsMapper.deleteTbNewsByNewsIds(newsIds);
    }

    /**
     * 删除新闻咨询信息
     *
     * @param newsId 新闻咨询主键
     * @return 结果
     */
    @Override
    public int deleteTbNewsByNewsId(Long newsId)
    {
        return tbNewsMapper.deleteTbNewsByNewsId(newsId);
    }

    /**
     *  获取新闻列表
     * @return
     */
    @Override
    public List<NewsVO> getNewsList() {
        return tbNewsMapper.selectNewsList(new TbNews());
    }

    /**
     * 详情
     * @param id
     * @return
     */
    @Override
    public NewsVO getDetail(Long id) {
        TbNews detail = tbNewsMapper.selectTbNewsByNewsId(id);
        NewsVO vo = BeanUtil.toBean(detail,NewsVO.class);
        if(ObjectUtils.isEmpty(vo)){
            throw new ServiceException("查询新闻详情异常",500);
        }else{
            return vo;
        }
    }
}
