云
2 天以前 87733eb3623593da247c051593d64b8ff9e1d3a4
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
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;
    }
 
}