package com.microservices.pms.document.service.impl;

import com.microservices.common.core.exception.ServiceException;
import com.microservices.common.core.utils.DateUtils;
import com.microservices.common.core.web.page.GenericsTableDataInfo;
import com.microservices.common.security.utils.SecurityUtils;
import com.microservices.pms.document.domain.PmsDocument;
import com.microservices.pms.document.domain.PmsDocumentUserFavorite;
import com.microservices.pms.document.domain.vo.PmsDocumentFavoriteSearchVo;
import com.microservices.pms.document.domain.vo.PmsDocumentVo;
import com.microservices.pms.document.mapper.PmsDocumentMapper;
import com.microservices.pms.document.mapper.PmsDocumentUserFavoriteMapper;
import com.microservices.pms.document.service.IPmsDocumentUserFavoriteService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

import static com.microservices.common.core.utils.PageUtils.toPage;

/**
 * 用户收藏文档Service业务层处理
 * 
 * @author 万佳
 * @date 2024-01-17
 */
@Service
public class PmsDocumentUserFavoriteServiceImpl implements IPmsDocumentUserFavoriteService 
{
    @Autowired
    private PmsDocumentUserFavoriteMapper pmsDocumentUserFavoriteMapper;

    @Autowired
    private PmsDocumentMapper pmsDocumentMapper;

    @Autowired
    private PmsDocumentServiceImpl pmsDocumentService;

    /**
     * 新增用户收藏文档
     * 
     * @param documentId 用户收藏文档id
     * @return 结果
     */
    @Override
    public int insertPmsDocumentUserFavorite(Long documentId)
    {
        checkDocExistenceAndCheckProjectAuth(documentId);
        PmsDocumentUserFavorite pmsDocumentUserFavorite = new PmsDocumentUserFavorite();
        pmsDocumentUserFavorite.setPmsDocumentId(documentId);
        pmsDocumentUserFavorite.setUserId(SecurityUtils.getUserId());
        pmsDocumentUserFavorite.setCreateTime(DateUtils.getNowDate());
        return pmsDocumentUserFavoriteMapper.insertPmsDocumentUserFavorite(pmsDocumentUserFavorite);
    }

    /**
     * 批量删除用户收藏文档
     * 
     * @param documentId 需要删除的用户收藏文档id
     * @return 结果
     */
    @Override
    public int deletePmsDocumentUserFavoriteByDocId(Long documentId)
    {
        checkDocExistenceAndCheckProjectAuth(documentId);
        return pmsDocumentUserFavoriteMapper.deletePmsDocumentUserFavoriteByDocId(SecurityUtils.getUserId() ,documentId);
    }

    @Override
    public GenericsTableDataInfo<PmsDocumentVo> selectPmsDocumentUserFavoriteList(PmsDocumentFavoriteSearchVo pmsDocumentFavoriteSearchVo) {
        pmsDocumentFavoriteSearchVo.setCurrentUserId(SecurityUtils.getUserId());
        List<PmsDocument> pmsDocumentList = pmsDocumentMapper.selectPmsFavoriteDocumentList(pmsDocumentFavoriteSearchVo);
        return toPage(pmsDocumentList,pmsDocumentService::buildPmsDocumentVo);
    }

    private void checkDocExistenceAndCheckProjectAuth(Long documentId) {
        PmsDocument pmsDocument = pmsDocumentMapper.selectPmsDocumentById(documentId);
        if (pmsDocument == null) {
            throw new ServiceException("文档不存在：[%s]", documentId);
        }
        pmsDocumentService.checkPmsProjectById(pmsDocument.getPmsProjectId());
    }
}
