package com.ruoyi.news.service.impl;

import com.ruoyi.common.constant.Constants;
import com.ruoyi.common.core.redis.RedisCache;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.news.domain.BomanNews;
import com.ruoyi.news.mapper.BomanNewsMapper;
import com.ruoyi.news.service.IBomanNewsService;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;


/**
 * boman_newsService业务层处理
 *
 * @author boman
 * @date 2024-01-18
 */
@Service
public class BomanNewsServiceImpl implements IBomanNewsService {
    @Autowired
    private BomanNewsMapper bomanNewsMapper;
    @Resource
    private RedisCache redisCache;

    /**
     * 查询boman_news
     *
     * @param newsId boman_news主键
     * @return boman_news
     */
    @Override
    public BomanNews selectBomanNewsByNewsId(Long newsId) {
        String key = Constants.NEW_RED_NUM + newsId;
        //从redis中获取该文章的阅读数量,如果没有则新增,如果能被100整除,记录到数据库
        Object redNum = redisCache.getCacheObject(key);
        BomanNews bomanNews = bomanNewsMapper.selectBomanNewsByNewsId(newsId);
        if (ObjectUtils.isEmpty(redNum)) {
            redisCache.setCacheObject(key, 1);
            bomanNews.setReadnumber(1);
        } else {
            int num = Integer.parseInt(redNum.toString()) + 1;
            bomanNews.setReadnumber(num);
            if (num % 100 == 0){
                //更新到数据库
                bomanNewsMapper.updateBomanNews(bomanNews);
            }
            redisCache.setCacheObject(key, num);
        }
        return bomanNews;
    }

    /**
     * 查询boman_news列表
     *
     * @param bomanNews boman_news
     * @return boman_news
     */
    @Override
    public List<BomanNews> selectBomanNewsList(BomanNews bomanNews) {
        return bomanNewsMapper.selectBomanNewsList(bomanNews);
    }

    /**
     * 新增boman_news
     *
     * @param bomanNews boman_news
     * @return 结果
     */
    @Override
    public int insertBomanNews(BomanNews bomanNews) {
        bomanNews.setReadnumber(0);
        bomanNews.setCreateTime(DateUtils.getNowDate());
        return bomanNewsMapper.insertBomanNews(bomanNews);
    }

    /**
     * 修改boman_news
     *
     * @param bomanNews boman_news
     * @return 结果
     */
    @Override
    public int updateBomanNews(BomanNews bomanNews) {
        bomanNews.setUpdateTime(DateUtils.getNowDate());
        return bomanNewsMapper.updateBomanNews(bomanNews);
    }

    /**
     * 批量删除boman_news
     *
     * @param newsIds 需要删除的boman_news主键
     * @return 结果
     */
    @Override
    public int deleteBomanNewsByNewsIds(Long[] newsIds) {
        return bomanNewsMapper.deleteBomanNewsByNewsIds(newsIds);
    }

    /**
     * 删除boman_news信息
     *
     * @param newsId boman_news主键
     * @return 结果
     */
    @Override
    public int deleteBomanNewsByNewsId(Long newsId) {
        return bomanNewsMapper.deleteBomanNewsByNewsId(newsId);
    }
}
