package com.hfkj.service.cms.impl;

import com.hfkj.common.utils.RedisUtil;
import com.hfkj.dao.CmsContentReadMapper;
import com.hfkj.entity.CmsContentRead;
import com.hfkj.service.cms.CmsContentReadService;
import org.springframework.stereotype.Service;

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

/**
 * @className: CmsContentReadServiceImpl
 * @author: HuRui
 * @date: 2024/9/26
 **/
@Service("cmsContentReadService")
public class CmsContentReadServiceImpl implements CmsContentReadService {

    @Resource
    private CmsContentReadMapper cmsContentReadMapper;
    @Resource
    private RedisUtil redisUtil;

    private final static String CACHE_KEY = "CMS_CONTENT_READ:";

    @Override
    public void create(Long contentId, Long userId) {
        CmsContentRead contentRead = new CmsContentRead();
        contentRead.setContentId(contentId);
        contentRead.setUserId(userId);
        contentRead.setStatus(1);
        contentRead.setCreateTime(new Date());
        cmsContentReadMapper.insert(contentRead);

        redisUtil.hset(CACHE_KEY+contentId, ""+userId, contentRead);
    }

    @Override
    public boolean isRead(Long contentId, Long userId) {
        return redisUtil.hHasKey(CACHE_KEY + contentId, "" + userId);
    }


}
