redis_port

Death is so terribly final, while life is full of possibilities.

package com.nine.service.impl;

import com.nine.cache.RedisPool;
import com.nine.dao.InformHuDao;
import com.nine.pojo.*;
import com.nine.service.InformHuService;
import com.nine.util.RedisUtil;
import com.nine.util.SsmConstants;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.List;

@Service
public class InformHuServiceImpl implements InformHuService {
    @Resource
    private InformHuDao informHuDao;

    @Override
    public List<News> selNewsHu(News news) {
        return informHuDao.selNewsHu(news);
    }

    @Override
    public int updateNewsRead(News news) {
       return informHuDao.updateNewsRead(news);
    }

    @Override
    public int updateNewsGood(News news) {
        return informHuDao.updateNewsGood(news);
    }

    @Override
    public int updateNewsCollect(News news) {
        return informHuDao.updateNewsCollect(news);
    }

    @Override
    public int updateNewsReward(News news) {
        return informHuDao.updateNewsReward(news);
    }


    @Override
    public int addCollectNewsHu(Collect collect) {
        return informHuDao.addCollectNewsHu(collect);
    }

    @Override
    public List<News> selCollectHu(int userId) {
        return informHuDao.selCollectHu(userId);
    }

    @Override
    public int selCollectCount(int userId) {
        return informHuDao.selCollectCount(userId);
    }

    @Override
    public int selWhetherCollect(Collect collect) {
        return informHuDao.selWhetherCollect(collect);
    }

    @Override
    public int deleteCollect(Collect collect) {
        return informHuDao.deleteCollect(collect);
    }

    /**
     * 举报 方法,
     * @param id
     */
    @Override
    public void infromNews(int id) {
        RedisPool redisPool = RedisUtil.getRedisPool("friendHu");
        String id1 = id + "";
        if (exit(id)) {
            int i = Integer.parseInt(redisPool.get(id1));
            if (i >= 5) {
                System.out.println("举报已超过5次");
                handle5(id);
            }
            redisPool.incr(id1);
          } else {
            redisPool.incr(id1);
          }
        System.out.println("举报:" + redisPool.get(id1));
    }

    @Override
    public int findBlackListCount() {
        return informHuDao.findBlackListCount();
    }

    @Override
    public List<BlackList> findAllBlackNews() {
        return informHuDao.findAllBlackNews();
    }

    //资讯黑名单的添加
    @Override
    public void addBlackListHu(BlackList blackList) {
        informHuDao.addBlackListHu(blackList);
    }

    //举报次数 大于5次 后发送消息列表
    @Override
    public void addBlackHu(Message message) {
        informHuDao.addBlackHu(message);
    }

    //举报次数 大于5次后 则从资讯列表里删除改咨询
    @Override
    public void deleteNewsByIdHu(int userId) {
        informHuDao.deleteNewsByIdHu(userId);
    }

    @Override
    public void reidsTest() {
        RedisPool redisPool = RedisUtil.getRedisPool("friendHu");
        BlackList blackList=new BlackList();
        blackList.setUserId(5);
        blackList.setTitle("皮肤白皙水嫩姿态娇美");
        blackList.setApplyState(1);
        BlackList blackList1=ApplyBlackHu(blackList);
        System.out.println(blackList1);
       /* News news=new News();
                news.setId(17);
        List<News> newsList =informHuDao.selNewsHu(news);
        News news1=newsList.get(0);
        redisPool.cacheObject(String.format(SsmConstants.CACHE_BLACKNEWS_LIST, 17), news1);
       //setCacheNews(2, SsmConstants.CACHE_BLACKNEWS_LIST, news);
        System.out.println(getCacheNews(17, SsmConstants.CACHE_BLACKNEWS_LIST));*/
        System.out.println(getCacheNews(16, SsmConstants.CACHE_BLACKNEWS_LIST));

    }

    @Override
    public BlackList ApplyBlackHu(BlackList blackList) {
        return informHuDao.ApplyBlackHu(blackList);
    }

    @Override
    public List<BlackList> selBlackListHu(int userId) {
        return informHuDao.selBlackListHu(userId);
    }

    @Override
    public void updateApplyHu(BlackList blackList) {
        informHuDao.updateApplyHu(blackList);
    }

    /**
     * TODO 虚调用 帅帅的 添加新闻的方法
     * TODO    把拉黑名单加进资讯表没做
     * @param session
     * @param systemLog
     */
    @Override
    public void addSystemLongHu(HttpSession session,SystemLog systemLog) {
        RedisPool redisPool = RedisUtil.getRedisPool("friendHu");

        //根据条件查询改黑名单的信息,
        BlackList blackList=selDelNewsBlack(session,redisPool);
        int newsId=blackList.getNewsId();
        System.out.println(blackList.getNewsId());
        //根据查询出的黑名单信息 从redis缓存里查出该条新闻资讯
        News news=getCacheNews(newsId,SsmConstants.CACHE_BLACKNEWS_LIST);
        System.out.println("申请-时从redis 里查出的news拉黑资讯:"+news);

        //TODO 取出后调用news添加方法

        //取出添加后 对改黑名单的资讯表的举报数进行清零
        cleZeroBlaRed(blackList,redisPool);

        //管理员操作完后 系统日志要进行添加
        informHuDao.addSystemLongHu(systemLog);
    }

    @Override
    public int selTopType(String kind) {
        return informHuDao.selTopType(kind);
    }

    //取出添加后 对改黑名单的资讯表的举报数进行清零
     private void  cleZeroBlaRed(BlackList blackList,RedisPool redisPool){
         String id1 =blackList.getNewsId() + "";
         redisPool.psetex(id1);
     }

    //根据条件查询改黑名单的信息, 在从redis里去除
    private BlackList selDelNewsBlack(HttpSession session,RedisPool redisPool) {
        int userId=(int) session.getAttribute("userId");
        String blackTitle=(String)session.getAttribute("blackTitle");
        BlackList blackList=new BlackList();
        //根据条件查询改黑名单的信息, 在从redis里去除
        blackList.setApplyState(1);
        blackList.setTitle(blackTitle);
        blackList.setUserId(userId);
        BlackList blackList1= ApplyBlackHu(blackList);
        return blackList1;
    }

    //判断键是否存在
    private boolean exit(int id) {
        RedisPool redisPool = RedisUtil.getRedisPool("friendHu");
        String id1 = id + "";
        return redisPool.exist(id1);
    }

    //举报查过5次后要  后发送消息列表,则从资讯列表里删除改咨询,资讯黑名单的添加
    private void handle5(int id) {
        Message message = new Message();
        BlackList blackList = new BlackList();
        News news = new News();
        news.setId(id);
        //根据文章id查询该文章内容
        List<News> newsList =informHuDao.selNewsHu(news);
        News news1=newsList.get(0);

        //发送消息列表
        message.setContent("您好,先生! 您的文章由于被举报多次而拉入黑名单,如果要要恢复被拉黑的咨询,请在5天之内在主页申请!谢谢配合!");
        message.setUserId(news1.getUserId());
        addBlackHu(message);

        //资讯黑名单的添加
        blackList.setNewsId(id);
        blackList.setUserId(news1.getUserId());
        blackList.setApplyState(0);
        addBlackListHu(blackList);

        //TODO 把该咨询添加到redis缓存里 --5天内自动删除
        setCacheNews(id, SsmConstants.CACHE_BLACKNEWS_LIST, news1);

        //从资讯列表里删除改咨询
        deleteNewsByIdHu(id);

    }

    /**
     * zxh-9.12
     * 将集合转为string存入缓存
     *
     * @param id
     * @param key
     * @param list
     */
    private void setCacheNews(int id, String key, News news) {
        RedisPool redisPool = RedisUtil.getRedisPool("friendHu");
        redisPool.cacheObject(String.format(key, id), news);
    }

    //从缓存取出字符串并转为集合类型
    private News  getCacheNews(int id, String key) {
        RedisPool redisPool = RedisUtil.getRedisPool("friendHu");
        News news = (News) redisPool.getCacheObject(String.format(key, id), News.class);
        return news;
    }
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值