liding
昨天 d75958896efd4c8a6daee9c56d048641b99cacf6
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
package com.ruoyi.business.service.impl;
 
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.basic.entity.StorageAttachment;
import com.ruoyi.basic.entity.StorageBlob;
import com.ruoyi.basic.entity.dto.StorageBlobDTO;
import com.ruoyi.basic.mapper.StorageAttachmentMapper;
import com.ruoyi.basic.mapper.StorageBlobMapper;
import com.ruoyi.basic.service.StorageAttachmentService;
import com.ruoyi.business.dto.QrCodeDto;
import com.ruoyi.business.dto.QrCodeScanRecordDto;
import com.ruoyi.business.entity.Archive;
import com.ruoyi.business.entity.QrCode;
import com.ruoyi.business.entity.QrCodeScanRecord;
import com.ruoyi.business.mapper.QrCodeMapper;
import com.ruoyi.business.mapper.QrCodeScanRecordMapper;
import com.ruoyi.business.service.QrCodeScanRecordService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.bean.BeanUtils;
import com.ruoyi.common.utils.file.MinioUtils;
import com.ruoyi.system.mapper.SysUserMapper;
import org.springframework.stereotype.Service;
import lombok.RequiredArgsConstructor;
 
import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;
 
import static com.ruoyi.common.constant.StorageAttachmentConstants.StorageAttachmentFile;
import static com.ruoyi.common.enums.StorageAttachmentRecordType.Archives;
import static com.ruoyi.common.enums.StorageAttachmentRecordType.QrCodeScanRecords;
 
/**
 * <p>
 * 二维码扫码记录表 服务实现类
 * </p>
 *
 * @author ld
 * @since 2025-06-24
 */
@Service
@RequiredArgsConstructor
public class QrCodeScanRecordServiceImpl extends ServiceImpl<QrCodeScanRecordMapper, QrCodeScanRecord> implements QrCodeScanRecordService {
 
    private final QrCodeScanRecordMapper qrCodeScanRecordMapper;
 
    private final QrCodeMapper qrCodeMapper;
 
    private final StorageAttachmentService storageAttachmentService;
 
    private final StorageBlobMapper storageBlobMapper;
 
    private final StorageAttachmentMapper storageAttachmentMapper;
 
    private final MinioUtils minioUtils;
 
    private final SysUserMapper sysUserMapper;
 
    @Override
    public IPage<QrCodeScanRecordDto> selectQrCodeScanRecordList(Page<QrCodeScanRecord> page, QrCodeScanRecordDto qrCodeScanRecordDto) {
        // 1. 构建基础查询条件
        LambdaQueryWrapper<QrCodeScanRecord> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.orderByDesc(QrCodeScanRecord::getCreateTime);
 
        // 2. 执行分页查询
        IPage<QrCodeScanRecord> scanRecordIPage = qrCodeScanRecordMapper.selectPage(page, queryWrapper);
 
        // 3. 无数据提前返回
        if (CollectionUtils.isEmpty(scanRecordIPage.getRecords())) {
            return new Page<>(scanRecordIPage.getCurrent(), scanRecordIPage.getSize(), scanRecordIPage.getTotal());
        }
 
        // 4. 批量获取所有记录ID和二维码ID
        List<Long> recordIds = scanRecordIPage.getRecords().stream()
                .map(QrCodeScanRecord::getId)
                .collect(Collectors.toList());
 
        Set<Long> qrCodeIds = scanRecordIPage.getRecords().stream()
                .map(QrCodeScanRecord::getQrCodeId)
                .filter(Objects::nonNull)
                .collect(Collectors.toSet());
 
        // 5. 批量查询关联数据(使用最新API)
        // 5.1 查询二维码信息(替换selectBatchIds为selectByIds)
        Map<Long, QrCode> qrCodeMap = qrCodeIds.isEmpty()
                ? Collections.emptyMap()
                : qrCodeMapper.selectByIds(qrCodeIds).stream()
                .collect(Collectors.toMap(QrCode::getId, Function.identity()));
 
        // 5.2 查询附件关联关系
        Map<Long, List<StorageAttachment>> attachmentsMap = storageAttachmentMapper
                .selectList(new LambdaQueryWrapper<StorageAttachment>()
                        .in(StorageAttachment::getRecordId, recordIds)
                        .eq(StorageAttachment::getRecordType, QrCodeScanRecords.ordinal()))
                .stream()
                .collect(Collectors.groupingBy(StorageAttachment::getRecordId));
 
        // 5.3 查询文件数据(使用selectByIds)
        Set<Long> blobIds = attachmentsMap.values().stream()
                .flatMap(List::stream)
                .map(StorageAttachment::getStorageBlobId)
                .collect(Collectors.toSet());
 
        Map<Long, StorageBlob> blobMap = blobIds.isEmpty()
                ? Collections.emptyMap()
                : storageBlobMapper.selectByIds(blobIds).stream()
                .collect(Collectors.toMap(StorageBlob::getId, Function.identity()));
 
        // 6. 组装DTO数据
        List<QrCodeScanRecordDto> dtoList = scanRecordIPage.getRecords().stream().map(record -> {
            QrCodeScanRecordDto dto = new QrCodeScanRecordDto();
            BeanUtils.copyProperties(record, dto);
 
            SysUser sysUser = sysUserMapper.selectUserById(record.getScannerId());
            dto.setScanner(sysUser.getNickName());
 
            // 6.1 设置二维码信息
            Optional.ofNullable(qrCodeMap.get(record.getQrCodeId()))
                    .ifPresent(qrCode -> {
                        BeanUtils.copyProperties(qrCode, dto); // 复制到父类
                        dto.setQrCode(qrCode); // 设置完整对象
                    });
 
            // 6.2 设置附件信息
            dto.setStorageBlobDTO(
                    Optional.ofNullable(attachmentsMap.get(record.getId()))
                            .orElse(Collections.emptyList())
                            .stream()
                            .map(att -> {
                                StorageBlobDTO blobDTO = new StorageBlobDTO();
                                Optional.ofNullable(blobMap.get(att.getStorageBlobId()))
                                        .ifPresent(blob -> {
                                            BeanUtils.copyProperties(blob, blobDTO);
                                            blobDTO.setUrl(minioUtils.getPreviewUrls(blob.getBucketFilename(), blob.getBucketName(), true));
                                            blobDTO.setDownloadUrl(minioUtils.getDownloadUrls(blob.getBucketFilename(),blob.getBucketName(),blob.getOriginalFilename(),true));
                                        });
                                return blobDTO;
                            })
                            .filter(blobDTO -> blobDTO.getId() != null) // 过滤无效附件
                            .collect(Collectors.toList())
            );
            return dto;
        }).collect(Collectors.toList());
 
        // 7. 构建返回分页对象
        IPage<QrCodeScanRecordDto> resultPage = new Page<>();
        BeanUtils.copyProperties(scanRecordIPage, resultPage);
        resultPage.setRecords(dtoList);
 
        return resultPage;
    }
 
    @Override
    public int addOrEditQrCodeRecord(QrCodeScanRecordDto qrCodeScanRecordDto) {
        QrCodeScanRecord qrCodeScanRecord = new QrCodeScanRecord();
        BeanUtils.copyProperties(qrCodeScanRecordDto, qrCodeScanRecord);
        int i;
        if (Objects.isNull(qrCodeScanRecordDto.getId())) {
            i = qrCodeScanRecordMapper.insert(qrCodeScanRecord);
        } else {
            i = qrCodeScanRecordMapper.updateById(qrCodeScanRecord);
        }
 
        if (qrCodeScanRecordDto.getStorageBlobDTO() != null && !qrCodeScanRecordDto.getStorageBlobDTO().isEmpty()) {
            List<StorageAttachment> attachments = new ArrayList<>();
 
            for (StorageBlobDTO storageBlobDTO : qrCodeScanRecordDto.getStorageBlobDTO()) {
                StorageAttachment storageAttachment = new StorageAttachment(
                        StorageAttachmentFile,
                        (long) QrCodeScanRecords.ordinal(),
                        qrCodeScanRecord.getId()
                );
                storageAttachment.setStorageBlobDTO(storageBlobDTO);
                attachments.add(storageAttachment);
            }
            storageAttachmentService.saveStorageAttachment(attachments, qrCodeScanRecord.getId(), QrCodeScanRecords, StorageAttachmentFile);
        }
        return i;
    }
 
    @Override
    public int delByIds(Long[] ids) {
        return qrCodeScanRecordMapper.deleteByIds(Arrays.asList(ids));
    }
}