package com.ruoyi.dxp.service.impl;

import java.util.List;

import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.dxp.mapper.LiteratureMapper;
import com.ruoyi.dxp.domain.Literature;
import com.ruoyi.dxp.service.ILiteratureService;

/**
 * 文献管理Service业务层处理
 * 
 * @author jy
 * @date 2022-11-11
 */
@Service
public class LiteratureServiceImpl implements ILiteratureService 
{
    @Autowired
    private LiteratureMapper literatureMapper;

    /**
     * 查询文献管理
     * 
     * @param id 文献管理主键
     * @return 文献管理
     */
    @Override
    public Literature selectLiteratureById(Long id)
    {
        return literatureMapper.selectLiteratureById(id);
    }

    /**
     * 查询文献管理列表
     * 
     * @param literature 文献管理
     * @return 文献管理
     */
    @Override
    public List<Literature> selectLiteratureList(Literature literature)
    {
        return literatureMapper.selectLiteratureList(literature);
    }

    /**
     * 查询文献管理列表
     *
     * @param literature 文献管理
     * @return 文献管理
     */
    @Override
    public List<Literature> selectHotLiteratureList(Literature literature)
    {
        return literatureMapper.selectHotLiteratureList(literature);
    }

    /**
     * 新增文献管理
     * 
     * @param literature 文献管理
     * @return 结果
     */
    @Override
    public int insertLiterature(Literature literature)
    {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        literature.setCreateBy(loginUser.getUsername());
        literature.setCreateTime(DateUtils.getNowDate());
        return literatureMapper.insertLiterature(literature);
    }

    /**
     * 修改文献管理
     * 
     * @param literature 文献管理
     * @return 结果
     */
    @Override
    public int updateLiterature(Literature literature)
    {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        literature.setUpdateBy(loginUser.getUsername());
        literature.setUpdateTime(DateUtils.getNowDate());
        return literatureMapper.updateLiterature(literature);
    }

    /**
     * 批量删除文献管理
     * 
     * @param ids 需要删除的文献管理主键
     * @return 结果
     */
    @Override
    public int deleteLiteratureByIds(Long[] ids)
    {
        return literatureMapper.deleteLiteratureByIds(ids);
    }

    /**
     * 删除文献管理信息
     * 
     * @param id 文献管理主键
     * @return 结果
     */
    @Override
    public int deleteLiteratureById(Long id)
    {
        return literatureMapper.deleteLiteratureById(id);
    }

    /**
     * 增加文献浏览次数
     * @param id 文献id
     */
    @Override
    public void addViewCount(Long id) {
        Literature literature = literatureMapper.selectLiteratureById(id);
        Long viewCount = literature.getViewCount();
        if (viewCount == null) {
            viewCount = 1L;
        } else {
            viewCount++;
        }

        literature.setViewCount(viewCount);
        literatureMapper.updateLiterature(literature);
    }

    /**
     * 增加文献下载次数
     * @param id 文献id
     */
    public void addDownloadCount(Long id) {
        Literature literature = literatureMapper.selectLiteratureById(id);
        Long downloadCount = literature.getDownloadCount();
        if (downloadCount == null) {
            downloadCount = 1L;
        } else {
            downloadCount++;
        }

        literature.setDownloadCount(downloadCount);
        literatureMapper.updateLiterature(literature);
    }
}
