package com.ruoyi.system.service.impl;

import com.ruoyi.common.core.redis.RedisCache;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.system.domain.partyNews.PartyNews;
import com.ruoyi.system.mapper.PartyNewsMapper;
import com.ruoyi.system.service.IPartyNewsService;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

import static com.ruoyi.common.constant.Constants.STARS_PARTY_NEWS;
import static com.ruoyi.common.constant.Constants.STARS_PARTY_NEWS_COUNT;

/**
 * 党建资讯Service业务层处理
 *
 * @author boman
 * @date 2025-02-14
 */
@Service
public class PartyNewsServiceImpl implements IPartyNewsService {
    @Autowired
    private PartyNewsMapper partyNewsMapper;

    @Autowired
    private RedisCache redisCache;

    /**
     * 查询党建资讯
     *
     * @param partyId 党建资讯主键
     * @return 党建资讯
     */
    @Override
    public PartyNews selectPartyNewsByPartyId(Long partyId) {
        PartyNews partyNews = partyNewsMapper.selectPartyNewsByPartyId(partyId);
        //观看次数+1
        Long viewCount = partyNews.getViewCount();
        partyNews.setViewCount(viewCount + 1);
        partyNewsMapper.updatePartyNews(partyNews);
        String partyKey = STARS_PARTY_NEWS + SecurityUtils.getUserId();
        List<Long> partyIdList = redisCache.getCacheList(partyKey);

        partyNews.setIsStars("N");
        Long partyIdNews = partyNews.getPartyId();
        if (partyIdList.contains(partyIdNews)) {
            partyNews.setIsStars("Y");
        }
        //插入收藏数量
        partyNews.setStarsCount(0);
        Object startCount = redisCache.getCacheObject(STARS_PARTY_NEWS_COUNT + partyId);
        if (ObjectUtils.isNotEmpty(startCount)) {
            partyNews.setStarsCount(Integer.parseInt(startCount.toString()));
        }

        return partyNews;
    }

    /**
     * 查询党建资讯列表
     *
     * @param partyNews 党建资讯
     * @return 党建资讯
     */
    @Override
    public List<PartyNews> selectPartyNewsList(PartyNews partyNews) {
        List<PartyNews> partyNewsList = partyNewsMapper.selectPartyNewsList(partyNews);
        if (partyNewsList != null && partyNewsList.size() > 0) {
            String partyKey = STARS_PARTY_NEWS + SecurityUtils.getUserId();
            List<Long> partyIdList = redisCache.getCacheList(partyKey);
            for (PartyNews news : partyNewsList) {
                news.setIsStars("N");
                Long partyId = news.getPartyId();
                if (partyIdList.contains(partyId)) {
                    news.setIsStars("Y");
                }
                //插入收藏数量
                news.setStarsCount(0);
                Object startCount = redisCache.getCacheObject(STARS_PARTY_NEWS_COUNT + partyId);
                if (ObjectUtils.isNotEmpty(startCount)) {
                    news.setStarsCount(Integer.parseInt(startCount.toString()));
                }
            }
        }
        return partyNewsList;
    }

    /**
     * 新增党建资讯
     *
     * @param partyNews 党建资讯
     * @return 结果
     */
    @Override
    public int insertPartyNews(PartyNews partyNews) {
        partyNews.setCreateTime(DateUtils.getNowDate());
        return partyNewsMapper.insertPartyNews(partyNews);
    }

    /**
     * 修改党建资讯
     *
     * @param partyNews 党建资讯
     * @return 结果
     */
    @Override
    public int updatePartyNews(PartyNews partyNews) {
        partyNews.setUpdateTime(DateUtils.getNowDate());
        return partyNewsMapper.updatePartyNews(partyNews);
    }

    /**
     * 批量关闭党建资讯
     * @param partyIds
     * @return
     */
    @Override
    public int deletePartyNewsByPartyIds(Long[] partyIds) {
        return partyNewsMapper.updatePartyNewsByPartyIds(partyIds);
    }

    /**
     * 删除党建资讯信息
     *
     * @param partyId 党建资讯主键
     * @return 结果
     */
    @Override
    public int deletePartyNewsByPartyId(Long partyId) {
        return partyNewsMapper.deletePartyNewsByPartyId(partyId);
    }
}
