package com.ruoyi.system.service.impl;

import java.util.List;
import java.util.stream.Collectors;

import com.ruoyi.common.utils.DateUtils;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.TbNewsMapper;
import com.ruoyi.system.domain.TbNews;
import com.ruoyi.system.service.ITbNewsService;
import com.ruoyi.common.core.text.Convert;

/**
 * 新闻与活动Service业务层处理
 * 
 * @author ruoyi
 * @date 2022-10-25
 */
@Service
public class TbNewsServiceImpl implements ITbNewsService 
{
    @Autowired
    private TbNewsMapper tbNewsMapper;

    @Autowired
    private CommonServiceImpl commonService;

    /**
     * 查询新闻与活动
     * 
     * @param id 新闻与活动主键
     * @return 新闻与活动
     */
    @Override
    public TbNews selectTbNewsById(Long id)
    {
        TbNews tn=tbNewsMapper.selectTbNewsById(id);
        tn.setCreatorDesc(commonService.getUserName(tn.getCreator()));
        if(ObjectUtils.isNotEmpty(tn.getModifiedBy())){
            tn.setModifiedByDesc(commonService.getUserName(tn.getModifiedBy()));
        }
        return tn;
    }

    /**
     * 查询新闻与活动列表
     * 
     * @param tbNews 新闻与活动
     * @return 新闻与活动
     */
    @Override
    public List<TbNews> selectTbNewsList(TbNews tbNews)
    {
        List<TbNews> list=tbNewsMapper.selectTbNewsList(tbNews);
        list.stream().filter(tbNews1 -> {
            tbNews1.setCreatorDesc(commonService.getUserName(tbNews1.getCreator()));
            if(ObjectUtils.isNotEmpty(tbNews1.getModifiedBy())){
                tbNews1.setModifiedByDesc(commonService.getUserName(tbNews1.getModifiedBy()));
            }
            return true;
        }).collect(Collectors.toList());
        return list;
    }

    /**
     * 新增新闻与活动
     * 
     * @param tbNews 新闻与活动
     * @return 结果
     */
    @Override
    public int insertTbNews(TbNews tbNews)
    {
        tbNews.setStatus(1);
        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 ids 需要删除的新闻与活动主键
     * @return 结果
     */
    @Override
    public int deleteTbNewsByIds(String ids)
    {
        return tbNewsMapper.deleteTbNewsByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除新闻与活动信息
     * 
     * @param id 新闻与活动主键
     * @return 结果
     */
    @Override
    public int deleteTbNewsById(Long id)
    {
        return tbNewsMapper.deleteTbNewsById(id);
    }
}
