package com.inkasso.factoring.file.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.inkasso.factoring.file.dao.SysAttachmentRefMapper;
import com.inkasso.factoring.file.entity.FileStorage;
import com.inkasso.factoring.file.entity.SysAttachmentRef;
import com.inkasso.factoring.file.model.dto.AttachmentDto;
import com.inkasso.factoring.file.service.FileStorageService;
import com.inkasso.factoring.file.service.ISysAttachmentRefService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 通用附件关系表 服务实现类
 * </p>
 *
 * @author daxian
 * @since 2022-02-28
 */
@Service
public class SysAttachmentRefServiceImpl extends
    ServiceImpl<SysAttachmentRefMapper, SysAttachmentRef> implements
    ISysAttachmentRefService {

    @Autowired
    private FileStorageService fileStorageService;

    @Override
    public List<FileStorage> getFiles(AttachmentDto attachmentDto) {
        List<SysAttachmentRef> list = lambdaQuery()
                .eq(SysAttachmentRef::getEntityId, attachmentDto.getEntityId())
                .eq(SysAttachmentRef::getEntityName, attachmentDto.getChildEnum().getCode())
                .eq(StringUtils.isNoneBlank(attachmentDto.getTypeExpand()), SysAttachmentRef::getEntityTypeExpand, attachmentDto.getTypeExpand())
                .list();

        List<FileStorage> fileStorageList = new ArrayList<>();
        if (list != null && list.size() > 0) {
            for (SysAttachmentRef sysAttachmentRef : list) {
                FileStorage fileStorage = fileStorageService.getById(sysAttachmentRef.getFileStorageId());
                if (fileStorage != null) {
                    fileStorageList.add(fileStorage);
                }
            }
        }
        return fileStorageList;
    }


}
