package com.ruoyi.system.service.impl;

import java.util.List;

import com.ruoyi.common.core.domain.entity.ImageDatas;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.system.mapper.ImageDataMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.ColumnNewsMapper;
import com.ruoyi.common.core.domain.entity.ColumnNews;
import com.ruoyi.system.service.IColumnNewsService;

import static com.ruoyi.common.utils.SecurityUtils.getUsername;

/**
 * 新闻信息Service业务层处理
 *
 * @author boman
 * @date 2021-12-24
 */
@Service
public class ColumnNewsServiceImpl implements IColumnNewsService {
    @Autowired
    private ColumnNewsMapper columnNewsMapper;

    @Autowired
    private ImageDataMapper imageDataMapper;

    /**
     * 查询新闻信息
     *
     * @param newsId 新闻信息主键
     * @return 新闻信息
     */
    @Override
    public ColumnNews selectColumnNewsByNewsId(Integer newsId) {
        ColumnNews columnNews = columnNewsMapper.selectColumnNewsByNewsId(newsId);
        return columnNews;
    }

    /**
     * 门户查询新闻信息
     *
     * @param newsId
     * @return
     */
    @Override
    public ColumnNews selectColumnNewsDetailMenHu(Integer newsId) {
        ColumnNews columnNews = columnNewsMapper.selectColumnNewsDetailMenHu(newsId);
        Integer viewsNum = columnNews.getViewsNum();
        if (viewsNum != null) {
            columnNews.setViewsNum(viewsNum + 1);
        }
        columnNewsMapper.updateColumnNews(columnNews);
        return columnNews;
    }

    /**
     * 查询新闻信息列表
     *
     * @param columnNews 新闻信息
     * @return 新闻信息
     */
    @Override
    public List<ColumnNews> selectColumnNewsList(ColumnNews columnNews) {
        return columnNewsMapper.selectColumnNewsList(columnNews);
    }

    /**
     * 查询新闻信息列表门户
     *
     * @param columnNews 新闻信息
     * @return 新闻信息
     */
    @Override
    public List<ColumnNews> selectColumnNewsListMenHu(ColumnNews columnNews) {
        return columnNewsMapper.selectColumnNewsListMenHu(columnNews);
    }

    /**
     * 新增新闻信息
     *
     * @param columnNews 新闻信息
     * @return 结果
     */
    @Override
    public int insertColumnNews(ColumnNews columnNews) {
        columnNews.setCreateTime(DateUtils.getNowDate());
        int i = columnNewsMapper.insertColumnNews(columnNews);
        //新增附件信息
        //判断附件信息是否存在
        List<ImageDatas> imageDatasList = columnNews.getImageDatasList();
        if (imageDatasList != null && imageDatasList.size() > 0) {
            for (ImageDatas imageDatas : imageDatasList) {
                imageDatas.setNewsId(columnNews.getNewsId());
                imageDatas.setCreateBy(getUsername());
                imageDatas.setCreateTime(DateUtils.getNowDate());
                imageDatas.setStatus("0");
                imageDataMapper.insertImageDatas(imageDatas);
            }
        }
        return i;
    }

    /**
     * 修改新闻信息
     *
     * @param columnNews 新闻信息
     * @return 结果
     */
    @Override
    public int updateColumnNews(ColumnNews columnNews) {
        columnNews.setUpdateTime(DateUtils.getNowDate());
        imageDataMapper.deleteImageDatasByNewsId(columnNews.getNewsId());
        //判断附件信息是否存在
        List<ImageDatas> imageDatasList = columnNews.getImageDatasList();
        if (imageDatasList != null && imageDatasList.size() > 0) {
            for (ImageDatas imageDatas : imageDatasList) {
                imageDatas.setNewsId(columnNews.getNewsId());
                imageDatas.setCreateBy(getUsername());
                imageDatas.setCreateTime(DateUtils.getNowDate());
                imageDatas.setStatus("0");
                imageDataMapper.insertImageDatas(imageDatas);
            }
        }
        return columnNewsMapper.updateColumnNews(columnNews);
    }

    /**
     * 批量删除新闻信息
     *
     * @param newsIds 需要删除的新闻信息主键
     * @return 结果
     */
    @Override
    public int deleteColumnNewsByNewsIds(Integer[] newsIds) {
        return columnNewsMapper.deleteColumnNewsByNewsIds(newsIds);
    }

    /**
     * 删除新闻信息信息
     *
     * @param newsId 新闻信息主键
     * @return 结果
     */
    @Override
    public int deleteColumnNewsByNewsId(Integer newsId) {
        return columnNewsMapper.deleteColumnNewsByNewsId(newsId);
    }

    /**
     * 审核通过
     *
     * @param newsId
     * @return
     */
    @Override
    public int examineColumnNews(Integer newsId) {
        //查看这个人是否有审核权限
        return columnNewsMapper.examineColumnNews(newsId);
    }

    /**
     * 获取首页轮播图
     *
     * @return
     */
    @Override
    public List<ColumnNews> getRotation() {
        List<ColumnNews> rotation = columnNewsMapper.getRotation();
        return rotation;
    }
}
