package com.sqx.modules.post.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sqx.modules.post.dao.ViewPostDao;
import com.sqx.modules.post.entity.ViewPost;
import com.sqx.modules.post.mapper.PostMapper;
import com.sqx.modules.post.service.ViewPostService;
import org.joda.time.LocalDateTime;
import org.springframework.stereotype.Service;

@Service
public class ViewPostServiceImpl  extends ServiceImpl<ViewPostDao,ViewPost> implements ViewPostService {
    private final PostMapper postMapper;

    public ViewPostServiceImpl(PostMapper postMapper) {
        this.postMapper = postMapper;
    }

    @Override
    public void addViewPost(Integer postId, Long userId) {
        QueryWrapper<ViewPost> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id",userId).eq("post_id",postId);
        if (getBaseMapper().selectOne(queryWrapper) == null ){

            ViewPost viewPost = new ViewPost();
            viewPost.setPostId(postId);
            viewPost.setIsCollect(0);
            viewPost.setUserId(userId);
            viewPost.setCreatTime(String.valueOf(LocalDateTime.now()));
            getBaseMapper().insert(viewPost);
        }
    }

    @Override
    public Object getViewPostList(Long userId) {
        return postMapper.getViewPostList(userId);
    }

    @Override
    public Object getCollectPost(Long userId) {
        return postMapper.getCollectPostList(userId);
    }

    @Override
    public Object getPushPost(Long userId) {
        return postMapper.getPushPost(userId);
    }
}
