package cn.iocoder.yudao.module.system.service.appearance;
|
|
import cn.hutool.core.collection.CollUtil;
|
import cn.iocoder.yudao.module.system.controller.admin.appearance.vo.SystemAppearanceRespVO;
|
import cn.iocoder.yudao.module.system.controller.admin.appearance.vo.SystemAppearanceSaveReqVO;
|
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.enums.storage.StorageApplicationTypeEnum;
|
import cn.iocoder.yudao.module.system.enums.storage.StorageRecordTypeEnum;
|
import cn.iocoder.yudao.module.system.service.storage.SystemStorageAttachmentService;
|
import cn.iocoder.yudao.module.system.util.storage.StorageFileUtil;
|
import jakarta.annotation.Resource;
|
import org.springframework.stereotype.Service;
|
|
import java.math.BigDecimal;
|
import java.util.Collections;
|
import java.util.List;
|
|
/**
|
* 系统外观 Service 实现类
|
*
|
* @author 超级管理员
|
*/
|
@Service
|
public class SystemAppearanceServiceImpl implements SystemAppearanceService {
|
|
/**
|
* 系统外观固定记录编号:外观是全局唯一配置,用一条附件记录承载
|
*/
|
private static final Long RECORD_ID = 1L;
|
|
@Resource
|
private SystemStorageAttachmentService storageAttachmentService;
|
@Resource
|
private StorageFileUtil storageFileUtil;
|
|
@Override
|
public SystemAppearanceRespVO getAppearance() {
|
SystemAppearanceRespVO respVO = new SystemAppearanceRespVO();
|
SystemStorageBlobRespVO logo = getSingleBlob(StorageApplicationTypeEnum.LOGO);
|
if (logo != null) {
|
respVO.setLogoBlobId(logo.getId());
|
respVO.setLogoUrl(buildPermanentUrl(logo));
|
}
|
SystemStorageBlobRespVO background = getSingleBlob(StorageApplicationTypeEnum.LOGIN_BACKGROUND);
|
if (background != null) {
|
respVO.setLoginBackgroundBlobId(background.getId());
|
respVO.setLoginBackgroundUrl(buildPermanentUrl(background));
|
}
|
return respVO;
|
}
|
|
@Override
|
public void saveAppearance(SystemAppearanceSaveReqVO reqVO) {
|
saveBlob(StorageApplicationTypeEnum.LOGO, reqVO.getLogoBlobId());
|
saveBlob(StorageApplicationTypeEnum.LOGIN_BACKGROUND, reqVO.getLoginBackgroundBlobId());
|
}
|
|
private SystemStorageBlobRespVO getSingleBlob(StorageApplicationTypeEnum application) {
|
SystemStorageAttachmentListReqVO reqVO = new SystemStorageAttachmentListReqVO();
|
reqVO.setApplication(application.getType());
|
reqVO.setRecordType(StorageRecordTypeEnum.SYSTEM_APPEARANCE.getType());
|
reqVO.setRecordId(RECORD_ID);
|
List<SystemStorageBlobRespVO> blobs = storageAttachmentService.listAttachments(reqVO);
|
return CollUtil.isEmpty(blobs) ? null : blobs.get(0);
|
}
|
|
/**
|
* 外观图需要登录前就能访问,因此使用永久公开地址
|
*/
|
private String buildPermanentUrl(SystemStorageBlobRespVO blob) {
|
return storageFileUtil.buildSignedUrl(blob, "/preview/", BigDecimal.valueOf(-1));
|
}
|
|
private void saveBlob(StorageApplicationTypeEnum application, Long blobId) {
|
SystemStorageAttachmentSaveReqVO reqVO = new SystemStorageAttachmentSaveReqVO();
|
reqVO.setApplication(application.getType());
|
reqVO.setRecordType(StorageRecordTypeEnum.SYSTEM_APPEARANCE.getType());
|
reqVO.setRecordId(RECORD_ID);
|
reqVO.setBlobItems(blobId == null
|
? Collections.emptyList()
|
: Collections.singletonList(buildBlobItem(blobId, application)));
|
storageAttachmentService.bindAttachments(reqVO);
|
}
|
|
private SystemStorageAttachmentSaveReqVO.BlobItem buildBlobItem(Long blobId, StorageApplicationTypeEnum application) {
|
SystemStorageAttachmentSaveReqVO.BlobItem item = new SystemStorageAttachmentSaveReqVO.BlobItem();
|
item.setBlobId(blobId);
|
item.setApplication(application.getType());
|
return item;
|
}
|
|
}
|