package com.yingpaipay.business.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.yingpaipay.business.domain.Document;
import com.yingpaipay.business.enumeration.DocumentStatusEnum;
import com.yingpaipay.business.mapper.DocumentMapper;
import lombok.RequiredArgsConstructor;
import org.dromara.common.core.utils.StringUtils;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;

@Service
@RequiredArgsConstructor
public class CommonDocumentService {

    private final DocumentMapper baseMapper;


    public List<Document> queryListByProjectIds(List<Long> projectIds) {
        if (projectIds.isEmpty()) {
            return List.of();
        }
        return baseMapper.selectList(
            Wrappers.lambdaQuery(Document.class)
                .in(Document::getProjectId, projectIds)
        );
    }

    public Document getById(Long documentId) {
        return baseMapper.selectById(documentId);
    }

    public Document getByOssId(Long ossId) {
        return baseMapper.selectOne(
            Wrappers.lambdaQuery(Document.class)
                .eq(Document::getActualDocument, ossId)
        );
    }

    public List<Document> selectByOutofIds(List<Long> documentIds) {
        if (documentIds.isEmpty()) {
            return baseMapper.selectList();
        }
        return baseMapper.selectList(
            Wrappers.lambdaQuery(Document.class)
                .eq(Document::getStatus, DocumentStatusEnum.ARCHIEVED.getValue())
                .notIn(Document::getId, documentIds)
        );
    }

    public List<Document> getByIds(List<Long> ids) {
        if (ids.isEmpty()) {
            return Collections.emptyList();
        }
        return baseMapper.selectList(
            Wrappers.lambdaQuery(Document.class)
                .in(Document::getId, ids)
        );
    }

    public List<Document> selectByProjectId(Long projectId) {
        return baseMapper.selectList(
            Wrappers.lambdaQuery(Document.class)
                .eq(Document::getProjectId, projectId)
        );
    }

    public List<Document> getByName(Long projectId, String name) {
        return baseMapper.selectList(
            Wrappers.lambdaQuery(Document.class)
                .eq(Document::getProjectId, projectId)
                .like(StringUtils.isNotBlank(name), Document::getName, name)
        );
    }

    public boolean updateByIds(List<Document> documents) {
        return !baseMapper.updateById(documents).isEmpty();
    }

    public List<Document> selectByFolderIds(List<Long> folderIds) {
        if (folderIds.isEmpty()) {
            return Collections.emptyList();
        }
        return baseMapper.selectList(
            Wrappers.lambdaQuery(Document.class)
                .in(Document::getFolderId, folderIds)
        );
    }

    public boolean deleteByIds(List<Long> documentIds) {
        return baseMapper.deleteByIds(documentIds) > 0;
    }

    public boolean updateStatusByIds(List<Long> documentIds, DocumentStatusEnum status) {
        if (documentIds.isEmpty()) {
            return true;
        }
        return baseMapper.update(
            Wrappers.lambdaUpdate(Document.class)
                .in(Document::getId, documentIds)
                .set(Document::getStatus, status.getValue())
        ) > 0;
    }

    public Document selectById(Long documentId) {
        return baseMapper.selectById(documentId);
    }

    public boolean updateById(Document document) {
        return baseMapper.updateById(document) > 0;
    }

    public List<Document> selectByName(String name, List<Long> projectIds) {
        if (projectIds.isEmpty()) {
            return Collections.emptyList();
        }
        return baseMapper.selectList(
            Wrappers.lambdaQuery(Document.class)
                .like(StringUtils.isNotBlank(name), Document::getName, name)
                .in(Document::getProjectId, projectIds)
        );
    }
}
