fei
10 小时以前 f41e9a8dd4aa5de0de323ffe2377c896cb44ea37
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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
package com.ruoyi.service.impl;
 
 
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.MapUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.bean.BeanUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.domain.ArchiveCategory;
import com.ruoyi.domain.ArchiveRecords;
import com.ruoyi.domain.DocumentMaterials;
import com.ruoyi.domain.vo.*;
import com.ruoyi.mapper.DocumentMaterialsMapper;
import com.ruoyi.service.IDocumentMaterialsService;
import com.ruoyi.util.ErrorcodeExceptionextends;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
 
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.*;
/**
 * 【文件材料综合信息】Service业务层处理
 *
 * @author ruoyi
 * @date 2025-07-26
 */
@Service
public class DocumentMaterialsServiceImpl  extends ServiceImpl<DocumentMaterialsMapper, DocumentMaterials> implements IDocumentMaterialsService {
    private LambdaQueryWrapper<DocumentMaterials> buildCondition(DocumentMaterials documentMaterials){
        LambdaQueryWrapper<DocumentMaterials> lqw = new LambdaQueryWrapper<>();
 
        lqw.like(!StringUtils.isEmpty(documentMaterials.getCreator()), DocumentMaterials::getCreator, documentMaterials.getCreator())
                .like(!StringUtils.isEmpty(documentMaterials.getStage()), DocumentMaterials::getStage, documentMaterials.getStage())
                .like(!StringUtils.isEmpty(documentMaterials.getIsCanceled()), DocumentMaterials::getIsCanceled, documentMaterials.getIsCanceled())
                .like(!StringUtils.isEmpty(documentMaterials.getIsAttachment()), DocumentMaterials::getIsAttachment, documentMaterials.getIsAttachment())
                .like(!StringUtils.isEmpty(documentMaterials.getIsSensitive()), DocumentMaterials::getIsSensitive, documentMaterials.getIsSensitive())
                .like(!StringUtils.isEmpty(documentMaterials.getPublicity()), DocumentMaterials::getPublicity, documentMaterials.getPublicity())
                .like(!StringUtils.isEmpty(documentMaterials.getRetentionPeriod()), DocumentMaterials::getRetentionPeriod, documentMaterials.getRetentionPeriod())
                .like(!StringUtils.isEmpty(documentMaterials.getSecurityLevel()), DocumentMaterials::getSecurityLevel, documentMaterials.getSecurityLevel())
                .like(!StringUtils.isEmpty(documentMaterials.getTitle()), DocumentMaterials::getTitle, documentMaterials.getTitle())
 
 
                .eq(documentMaterials.getDate()!=null, DocumentMaterials::getDate, documentMaterials.getDate())
                .eq(documentMaterials.getRecordId()!=null, DocumentMaterials::getRecordId, documentMaterials.getRecordId())
        .eq(documentMaterials.getPageNumber()!=null, DocumentMaterials::getPageNumber, documentMaterials.getPageNumber());
 
        //                .like(!StringUtils.isEmpty(archiveRecords.getFilingNumber()), ArchiveRecords::getFilingNumber, archiveRecords.getFilingNumber())
//                .like(!StringUtils.isEmpty(archiveRecords.getArchiveRoomNumber()), ArchiveRecords::getArchiveRoomNumber, archiveRecords.getArchiveRoomNumber())
//                .like(!StringUtils.isEmpty(archiveRecords.getRecordId()), ArchiveRecords::getRecordId, archiveRecords.getRecordId());
//        //                .like(!StringUtils.isEmpty(zfProperty.getLocation()), ZfProperty::getLocation, zfProperty.getLocation())
//                .like(!StringUtils.isEmpty(zfProperty.getHolder()), ZfProperty::getHolder, zfProperty.getHolder())
//                .like(!StringUtils.isEmpty(zfProperty.getAddress()), ZfProperty::getAddress, zfProperty.getAddress())
//                .like(!StringUtils.isEmpty(zfProperty.getRemark()), ZfProperty::getRemark, zfProperty.getRemark())
//                .eq(zfProperty.getFamilyId()!=null,ZfProperty::getFamilyId,zfProperty.getFamilyId())
//                .in(ZfProperty::getFamilyId,familyIdList)
//                .eq(zfProperty.getHappenTime()!=null,ZfProperty::getHappenTime,zfProperty.getHappenTime())
//                .between(zfProperty.getHappenStartTime() != null && zfProperty.getHappenEndTime() != null, ZfProperty::getHappenTime, zfProperty.getHappenStartTime(), zfProperty.getHappenEndTime());
//        lqw.orderByDesc(ZfProperty::getCreateTime);
        // lqw.orderByDesc(ArchiveRecords::isOwnData)
        lqw.orderByAsc(DocumentMaterials::getPageNumber);
 
        System.out.println("ssssssssssssddd0000000000000000");
        return lqw;
 
    }
    @Override
    public AjaxResult selectDataList(DocumentMaterials documentMaterials, Integer pageNum, Integer pageSize) {
        LambdaQueryWrapper<DocumentMaterials> lqw = buildCondition(documentMaterials);
 
 
        Page<DocumentMaterials> zfClanPage = new Page<>(pageNum, pageSize);
        Page<DocumentMaterials> pageResult = page(zfClanPage, lqw);
 
        List<DocumentMaterials> beanRecords = pageResult.getRecords();//得到查询出来的数据
 
        System.out.println(beanRecords);
//        beanRecords.forEach(record -> {
//            if (record.getUserId() != null) {
//                SysUser user = userMapper.selectUserById(record.getUserId());
//                record.setSysUser(user); // 假设Archiverecordstouser中有setUser方法
//            }
//        });
        //  List<ArchiveRecords> beanRecords = list(lqw);
        //   log.info("从数据库中查到的为:{}", beanRecords);
        //    return markOwnData(familyId, fatherFaId, motherFaId, beanRecords);
        HashMap<String, Object> data = MapUtils.getResult(pageResult, beanRecords);
 
 
        return AjaxResult.success(data);    }
 
 
 
    @Override
    public DocumentMaterials selectDocumentMaterialsByMaterialId(String materialId) {
 
 
        LambdaQueryWrapper<DocumentMaterials> lqw = new LambdaQueryWrapper<>();
        lqw.eq(materialId!=null, DocumentMaterials::getMaterialId, materialId);
        List<DocumentMaterials> records = list(lqw);
        if(!records.isEmpty())
            return records.get(0);
        else
            return null;
    }
 
    @Override
    public List<DocumentMaterials> selectDocumentMaterialsList(DocumentMaterials documentMaterials) {
        LambdaQueryWrapper<DocumentMaterials> lambdaQueryWrapper = buildCondition(documentMaterials);
        List<DocumentMaterials> beanRecords = list(lambdaQueryWrapper);
        return beanRecords;
    }
 
    @Override
    public List<DocumentMaterialsVoLarge> selectDocumentMaterialsAllByRecordId(Long id) {
 
        return this.baseMapper.getDocumentMaterialsvoLarge(Math.toIntExact(id));
    }
 
    @Override
    public List<DocumentMaterialsFileList> selectDocumentMaterialsFileList(Long id) {
        return this.baseMapper.getDocumentMaterialsFileList(Math.toIntExact(id));
    }
 
    @Override
    public int insertDocumentMaterials(DocumentMaterials documentMaterials) {
        LocalDateTime time = LocalDateTime.now();
 
        Date date = Date.from(time.atZone(ZoneId.systemDefault()).toInstant());
        documentMaterials.setCreatedAt(date);
        System.out.println(documentMaterials.getFileStyle());
        System.out.println(documentMaterials.getPageNumber());
//        if(documentMaterials.getPageNumber()==null)
//        {
//            documentMaterials.setPageNumber(-1L);
//        }
        boolean res = false;
        try {
             res = this.save(documentMaterials);
 
        } catch (Exception e) {
            System.out.println(e);
            throw new ErrorcodeExceptionextends(500, "同一个案卷内,不允许页码重复!");
        }
        if(res)
            return 1;
        else
            return 0;
    }
 
    @Override
    public List<DocumentMaterials> selectDocumentMaterialsByIds(Long[] ids) {
        // 获取当前用户ID
        Long userid = SecurityUtils.getUserId();
 
        // 创建查询条件
        LambdaQueryWrapper<DocumentMaterials> lambdaQueryWrapper = new LambdaQueryWrapper<>();
 
        // 根据ids查询
        lambdaQueryWrapper.in(DocumentMaterials::getMaterialId, Arrays.asList(ids));
        lambdaQueryWrapper.orderByAsc(DocumentMaterials::getPageNumber);
        // 如果不是管理员,需要考虑权限过滤(可根据实际权限需求调整)
        if (userid != 1) {
            // 这里可以添加权限相关的过滤条件
        }
 
        List<DocumentMaterials> beanRecords = list(lambdaQueryWrapper);
        return beanRecords;
    }
 
    @Override
    public int updateDocumentMaterials(DocumentMaterials documentMaterials) {
        boolean result = false;
        try {
 
//            //修改页号成功之后,要重新生成文件材料序号,页次
//            //根据pageNumber拿到title,然后计算pageOrder
//            LambdaQueryWrapper<DocumentMaterials> lqw = new LambdaQueryWrapper<>();
//            lqw.eq(documentMaterials.getRecordId()!=null, DocumentMaterials::getRecordId, documentMaterials.getRecordId());
//            lqw.eq(documentMaterials.getPageNumber()!=null, DocumentMaterials::getPageNumber, documentMaterials.getPageNumber());
//            List<DocumentMaterials> records = list(lqw);
 
 
//
//            //计算pageOrder
//            Long pageOrder = 1L;
//            if(documentMaterials.getPageNumber().equals(1))
//                pageOrder = 1L;
//            else {
//                Long maxPOrder = this.baseMapper.getMaxPageOrder(records.get(0).getTitle(), documentMaterials.getPageNumber(), records.get(0).getStage(), documentMaterials.getRecordId());
//                if (maxPOrder != null)
//                    pageOrder = maxPOrder + 1;
//            }
//            //设置pageOrder
//            documentMaterials.setPageOrder(pageOrder);
//            //计算fileNumber
//            Long fileNumber = this.getFiNum(documentMaterials.getPageNumber(), documentMaterials.getRecordId());
//            documentMaterials.setFileNumber(fileNumber);
            //修改页号
            result = updateById(documentMaterials);
//            //把剩下所有的的fileNumber和pageOrder都更新一遍
//            LambdaQueryWrapper<DocumentMaterials> lqw1 = new LambdaQueryWrapper<>();
//            lqw.eq(documentMaterials.getRecordId()!=null, DocumentMaterials::getRecordId, documentMaterials.getRecordId());
//          //  lqw.eq(documentMaterials.getPageNumber()!=null, DocumentMaterials::getPageNumber, documentMaterials.getPageNumber());
//            List<DocumentMaterials> recordss = list(lqw1);
//            for(DocumentMaterials dt: recordss)
//            {
//                //计算pageOrder
//                Long pageOrder1 = 1L;
//                if(dt.getPageNumber().equals(1))
//                    pageOrder1 = 1L;
//                else {
//                    Long maxPOrder1 = this.baseMapper.getMaxPageOrder(dt.getTitle(), dt.getPageNumber(), dt.getStage(), dt.getRecordId());
//                    if (maxPOrder1 != null)
//                        pageOrder1 = maxPOrder1 + 1;
//                }
//                //设置pageOrder
//                dt.setPageOrder(pageOrder1);
//                //计算fileNumber
//                Long fileNumber1 = this.getFiNum(dt.getPageNumber(), dt.getRecordId());
//                dt.setFileNumber(fileNumber1);
//                //修改页号
//                updateById(dt);
//            }
 
 
        } catch (Exception e) {
            System.out.println(e);
            throw new ErrorcodeExceptionextends(500, "同一个案卷内,不允许页码重复!");
        }
        if(result)
            return 1;
        else
            return 0;
    }
 
    @Override
    public int deleteDocumentMaterialsByMaterialIds(String[] materialIds) {
        if (this.removeByIds(Arrays.asList(materialIds))) {
            return 1;
        }
        else
            return 0;
    }
 
    @Override
    public int deleteDocumentMaterialsByMaterialId(String materialId) {
        return 0;
    }
 
    @Override
    public Long getFiNum(Long pageNumber, Long recordId) {
        return this.baseMapper.getFileNumber(pageNumber, recordId);
    }
 
    @Override
    public int updateByPageNumber(Long pageNumber, String sizeType, Long fileNumber, int width, int height, int horizontalResolution, int verticalResolution, double fileSize, String url, String format, Long recordId) {
        //根据pageNumber拿到title,然后计算pageOrder
        LambdaQueryWrapper<DocumentMaterials> lqw = new LambdaQueryWrapper<>();
        lqw.eq(recordId!=null, DocumentMaterials::getRecordId, recordId);
        lqw.eq(pageNumber!=null, DocumentMaterials::getPageNumber, pageNumber);
        List<DocumentMaterials> records = list(lqw);
 
 
        //计算pageOrder
        Long pageOrder = 1L;
        if(pageNumber.equals(1))
            pageOrder = 1L;
        else {
            Long maxPOrder = this.baseMapper.getMaxPageOrder(records.get(0).getTitle(), pageNumber, records.get(0).getStage(), recordId);
            if (maxPOrder != null)
                pageOrder = maxPOrder + 1;
        }
 
        this.baseMapper.updateInfoByPageNumber(pageNumber, sizeType,fileNumber,pageOrder, width, height, horizontalResolution, verticalResolution, fileSize, url,format, recordId);
 
        return 0;
    }
 
    @Override
    public List<DocumentMaterialsVo> findArchMInfo(String recordId) {
        return this.baseMapper.getArchiveMatInfo(Integer.parseInt(recordId));
    }
 
    @Override
    public List<DocumentMaterialFileStyle> findFileStyleInfo(int recordId) {
        return this.baseMapper.getFileStyleInfo(recordId);
    }
 
    @Override
    public AjaxResult importExcel(MultipartFile file, String recordId) {
        ExcelUtil<DocumentMaterialFileSmallVo> util = new ExcelUtil<>(DocumentMaterialFileSmallVo.class);
        List<DocumentMaterialFileSmallVo> dataList = null;
        try {
            dataList = util.importExcel(file.getInputStream());
        } catch (Exception e) {
            throw new RuntimeException("没有按照规则导入数据");
        }
 
        assert dataList != null;
 
        for (DocumentMaterialFileSmallVo archiveCategory : dataList) {
            // physcialService.mySave(physcial);
            DocumentMaterials documentMaterials = new DocumentMaterials();
            BeanUtils.copyProperties(archiveCategory, documentMaterials);
            documentMaterials.setRecordId(Long.parseLong(recordId));
            this.insertDocumentMaterials(documentMaterials);
        }
 
        return AjaxResult.success();    }
 
    @Override
    public DocumentMaterials selectByPageNumber(Long pageNumber, Integer recordId) {
        LambdaQueryWrapper<DocumentMaterials> lqw = new LambdaQueryWrapper<>();
        lqw.eq(pageNumber!=null, DocumentMaterials::getPageNumber, pageNumber);
        lqw.eq(recordId!=null, DocumentMaterials::getRecordId, recordId);
        List<DocumentMaterials> ls = list(lqw);
        if(!ls.isEmpty())
            return ls.get(0);
        else
            return null;
    }
 
    @Override
    public boolean isPageNumberIslegal(Integer recordId) {
        return this.baseMapper.isPageNumberLegal(recordId);
    }
 
    @Override
    public int getFileCount(Integer recordId) {
        return this.baseMapper.getCount(recordId);
    }
    
    @Override
    public AjaxResult addMiddleRecordsByPageNumbers(Long recordId, Long maxPageNumber) {
        try {
            // 获取指定recordId下的所有记录,按页号升序排列
            LambdaQueryWrapper<DocumentMaterials> lqw = new LambdaQueryWrapper<>();
            lqw.eq(DocumentMaterials::getRecordId, recordId);
            lqw.orderByAsc(DocumentMaterials::getPageNumber);
            List<DocumentMaterials> records = this.list(lqw);
 
            if (records == null || records.isEmpty()) {
                return AjaxResult.success("没有找到指定记录");
            }
            
            List<DocumentMaterials> middleRecords = new ArrayList<>();
            
            // 遍历记录,找出需要添加中间页号的位置
            for (int i = 0; i < records.size() - 1; i++) {
                DocumentMaterials currentRecord = records.get(i);
                DocumentMaterials nextRecord = records.get(i + 1);
                
                long currentPage = currentRecord.getPageNumber();
                long nextPage = nextRecord.getPageNumber();
                
                // 如果当前页号和下一页号之间有间隔
                if (nextPage - currentPage > 1) {
                    // 添加中间页号的记录,信息与当前记录保持一致
                    for (long page = currentPage + 1; page < nextPage; page++) {
                        DocumentMaterials newRecord = new DocumentMaterials();
                        // 复制当前记录的所有属性
                        BeanUtils.copyBeanProp(newRecord, currentRecord);
                        // 设置新的页号
                        newRecord.setPageNumber(page);
                        // 清除ID,确保是新记录
                        newRecord.setMaterialId(null);
                        // 设置创建时间
                        newRecord.setCreatedAt(new Date());
                        // 重置文件材料序号为null,让系统自动生成
                        newRecord.setFileNumber(null);
                        // 重置页次为null,让系统自动生成
                        newRecord.setPageOrder(null);
                        // 重置图像相关字段,以便上传文件时更新
                        newRecord.setSizeType(null);
                        newRecord.setWidth(null);
                        newRecord.setHeight(null);
                        newRecord.setHorizontalResolution(null);
                        newRecord.setVerticalResolution(null);
                        newRecord.setFileSize(null);
                        newRecord.setFormat(null);
                        newRecord.setUrl(null);
                        // 添加到中间记录列表
                        middleRecords.add(newRecord);
                    }
                }
            }
            
            // 处理最后一条记录到maxPageNumber之间的间隔
            DocumentMaterials lastRecord = records.get(records.size() - 1);
            long lastPage = lastRecord.getPageNumber();
            
            if (maxPageNumber > lastPage) {
                // 添加从lastPage+1到maxPageNumber之间的中间记录
                for (long page = lastPage + 1; page <= maxPageNumber; page++) {
                    DocumentMaterials newRecord = new DocumentMaterials();
                    // 复制最后一条记录的所有属性
                    BeanUtils.copyBeanProp(newRecord, lastRecord);
                    // 设置新的页号
                    newRecord.setPageNumber(page);
                    // 清除ID,确保是新记录
                    newRecord.setMaterialId(null);
                    // 设置创建时间
                    newRecord.setCreatedAt(new Date());
                    // 重置文件材料序号为null,让系统自动生成
                    newRecord.setFileNumber(null);
                    // 重置页次为null,让系统自动生成
                    newRecord.setPageOrder(null);
                    // 重置图像相关字段,以便上传文件时更新
                    newRecord.setSizeType(null);
                    newRecord.setWidth(null);
                    newRecord.setHeight(null);
                    newRecord.setHorizontalResolution(null);
                    newRecord.setVerticalResolution(null);
                    newRecord.setFileSize(null);
                    newRecord.setFormat(null);
                    newRecord.setUrl(null);
                    // 添加到中间记录列表
                    middleRecords.add(newRecord);
                }
            }
            
            // 批量保存中间记录
            if (!middleRecords.isEmpty()) {
                this.saveBatch(middleRecords);
                return AjaxResult.success("成功添加中间记录", "添加数量:" + middleRecords.size());
            } else {
                return AjaxResult.success("没有需要添加的中间记录");
            }
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error("添加中间记录失败:" + e.getMessage());
        }
    }
}