package com.fs.store.service.impl;

import com.fs.common.utils.DateUtils;
import com.fs.store.domain.FsHomeArticleView;
import com.fs.store.mapper.FsHomeArticleMapper;
import com.fs.store.mapper.FsHomeArticleViewMapper;
import com.fs.store.param.home.HomeArticleViewListParam;
import com.fs.store.service.IFsHomeArticleViewService;
import com.fs.store.vo.home.HomeArticleViewsListVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * 期刊阅读Service业务层处理
 *
 * @author fs
 * @date 2025-05-21
 */
@Service
public class FsHomeArticleViewServiceImpl implements IFsHomeArticleViewService
{
    @Autowired
    private FsHomeArticleViewMapper fsHomeArticleViewMapper;

    @Autowired
    private FsHomeArticleMapper homeArticleMapper;

    /**
     * 查询期刊阅读
     *
     * @param viewId 期刊阅读ID
     * @return 期刊阅读
     */
    @Override
    public FsHomeArticleView selectFsHomeArticleViewById(Long viewId)
    {
        return fsHomeArticleViewMapper.selectFsHomeArticleViewById(viewId);
    }

    /**
     * 查询期刊阅读列表
     *
     * @param fsHomeArticleView 期刊阅读
     * @return 期刊阅读
     */
    @Override
    public List<FsHomeArticleView> selectFsHomeArticleViewList(FsHomeArticleView fsHomeArticleView)
    {
        return fsHomeArticleViewMapper.selectFsHomeArticleViewList(fsHomeArticleView);
    }

    /**
     * 新增期刊阅读
     *
     * @param fsHomeArticleView 期刊阅读
     * @return 结果
     */
    @Override
    public int insertFsHomeArticleView(FsHomeArticleView fsHomeArticleView)
    {
        fsHomeArticleView.setCreateTime(DateUtils.getNowDate());
        return fsHomeArticleViewMapper.insertFsHomeArticleView(fsHomeArticleView);
    }

    /**
     * 修改期刊阅读
     *
     * @param fsHomeArticleView 期刊阅读
     * @return 结果
     */
    @Override
    public int updateFsHomeArticleView(FsHomeArticleView fsHomeArticleView)
    {
        return fsHomeArticleViewMapper.updateFsHomeArticleView(fsHomeArticleView);
    }

    /**
     * 批量删除期刊阅读
     *
     * @param viewIds 需要删除的期刊阅读ID
     * @return 结果
     */
    @Override
    public int deleteFsHomeArticleViewByIds(Long[] viewIds)
    {
        return fsHomeArticleViewMapper.deleteFsHomeArticleViewByIds(viewIds);
    }

    /**
     * 删除期刊阅读信息
     *
     * @param viewId 期刊阅读ID
     * @return 结果
     */
    @Override
    public int deleteFsHomeArticleViewById(Long viewId)
    {
        return fsHomeArticleViewMapper.deleteFsHomeArticleViewById(viewId);
    }

    @Override
    @Transactional
    public int addArticleRecord(Long articleId, Long userId) {
        // 新增阅读记录
        FsHomeArticleView fsHomeArticleView = new FsHomeArticleView();
        fsHomeArticleView.setArticleId(articleId);
        fsHomeArticleView.setUserId(userId);
        fsHomeArticleView.setCreateTime(new Date());
        int i = fsHomeArticleViewMapper.insertFsHomeArticleView(fsHomeArticleView);

        //更新阅读数
        if(i > 0){
            return homeArticleMapper.updateArticleViews(articleId);
        } else {
            return 0;
        }

    }

    @Override
    public List<HomeArticleViewsListVO> selectHomeArticleViewsList(HomeArticleViewListParam param) {
        return fsHomeArticleViewMapper.selectHomeArticleViewsList(param);
    }

}
