package cn.iocoder.yudao.module.system.api.storage; import cn.iocoder.yudao.framework.common.util.object.BeanUtils; import cn.iocoder.yudao.module.system.api.storage.dto.StorageBlobRespDTO; import cn.iocoder.yudao.module.system.controller.admin.storage.vo.SystemStorageAttachmentListReqVO; import cn.iocoder.yudao.module.system.controller.admin.storage.vo.SystemStorageAttachmentSaveReqVO; import cn.iocoder.yudao.module.system.controller.admin.storage.vo.SystemStorageBlobRespVO; import cn.iocoder.yudao.module.system.service.storage.SystemStorageAttachmentService; import jakarta.annotation.Resource; import org.springframework.stereotype.Service; import java.util.ArrayList; import java.util.List; @Service public class StorageAttachmentApiImpl implements StorageAttachmentApi { @Resource private SystemStorageAttachmentService systemStorageAttachmentService; @Override public void bindAttachments(String application, String recordType, Long recordId, List blobIds) { if (blobIds == null || blobIds.isEmpty()) { return; } SystemStorageAttachmentSaveReqVO reqVO = new SystemStorageAttachmentSaveReqVO(); reqVO.setApplication(application != null ? application : "file"); reqVO.setRecordType(recordType); reqVO.setRecordId(recordId); List blobItems = new ArrayList<>(); for (Long blobId : blobIds) { SystemStorageAttachmentSaveReqVO.BlobItem item = new SystemStorageAttachmentSaveReqVO.BlobItem(); item.setBlobId(blobId); blobItems.add(item); } reqVO.setBlobItems(blobItems); systemStorageAttachmentService.bindAttachments(reqVO); } @Override public void updateAttachments(String application, String recordType, Long recordId, List newBlobIds) { if (newBlobIds == null) { return; } if (newBlobIds.isEmpty()) { // 清空所有附件 systemStorageAttachmentService.deleteAttachmentsByApplicationAndRecordTypeAndRecordId( application != null ? application : "file", recordType, recordId); return; } // 全量替换:bindAttachments 内部先删后插,直接传入全部新 blobId 即可 bindAttachments(application, recordType, recordId, newBlobIds); } @Override public List listAttachments(String recordType, Long recordId) { SystemStorageAttachmentListReqVO reqVO = new SystemStorageAttachmentListReqVO(); reqVO.setApplication("file"); reqVO.setRecordType(recordType); reqVO.setRecordId(recordId); List list = systemStorageAttachmentService.listAttachments(reqVO); return BeanUtils.toBean(list, StorageBlobRespDTO.class); } @Override public void deleteAttachments(List attachmentIds) { if (attachmentIds == null || attachmentIds.isEmpty()) { return; } systemStorageAttachmentService.deleteAttachments(attachmentIds); } @Override public void deleteAttachmentsByRecord(String recordType, Long recordId) { systemStorageAttachmentService.deleteAttachmentsByRecordTypeAndRecordId(recordType, recordId); } }