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.TbTrends;
import com.ruoyi.hezhi.domain.vo.TrendsVO;
import com.ruoyi.hezhi.mapper.TbTrendsMapper;
import com.ruoyi.hezhi.service.ITbTrendsService;
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 TbTrendsServiceImpl implements ITbTrendsService
{
    @Autowired
    private TbTrendsMapper tbTrendsMapper;

    /**
     * 查询院内动态
     *
     * @param trendsId 院内动态主键
     * @return 院内动态
     */
    @Override
    public TbTrends selectTbTrendsByTrendsId(Long trendsId)
    {
        return tbTrendsMapper.selectTbTrendsByTrendsId(trendsId);
    }

    /**
     * 查询院内动态列表
     *
     * @param tbTrends 院内动态
     * @return 院内动态
     */
    @Override
    public List<TbTrends> selectTbTrendsList(TbTrends tbTrends)
    {
        return tbTrendsMapper.selectTbTrendsList(tbTrends);
    }

    /**
     * 新增院内动态
     *
     * @param tbTrends 院内动态
     * @return 结果
     */
    @Override
    public int insertTbTrends(TbTrends tbTrends)
    {
        tbTrends.setCreateTime(DateUtils.getNowDate());
        return tbTrendsMapper.insertTbTrends(tbTrends);
    }

    /**
     * 修改院内动态
     *
     * @param tbTrends 院内动态
     * @return 结果
     */
    @Override
    public int updateTbTrends(TbTrends tbTrends)
    {
        tbTrends.setUpdateTime(DateUtils.getNowDate());
        return tbTrendsMapper.updateTbTrends(tbTrends);
    }

    /**
     * 批量删除院内动态
     *
     * @param trendsIds 需要删除的院内动态主键
     * @return 结果
     */
    @Override
    public int deleteTbTrendsByTrendsIds(Long[] trendsIds)
    {
        return tbTrendsMapper.deleteTbTrendsByTrendsIds(trendsIds);
    }

    /**
     * 删除院内动态信息
     *
     * @param trendsId 院内动态主键
     * @return 结果
     */
    @Override
    public int deleteTbTrendsByTrendsId(Long trendsId)
    {
        return tbTrendsMapper.deleteTbTrendsByTrendsId(trendsId);
    }

    /**
     * 获取动态list
     * @return
     */
    @Override
    public List<TrendsVO> getTrendsVoList() {
        return tbTrendsMapper.getTrendsList(new TbTrends());
    }

    /**
     * 详情
     * @param id
     * @return
     */
    @Override
    public TrendsVO getDetail(Long id) {
        TbTrends tbTrends = tbTrendsMapper.selectTbTrendsByTrendsId(id);
        TrendsVO vo = BeanUtil.toBean(tbTrends,TrendsVO.class);
        if(ObjectUtils.isEmpty(vo)){
            throw new ServiceException("查询动态详情异常",500);
        }else{
            return vo;
        }
    }
}
