fei
7 天以前 9f5127b6c58a297104bccc75a4c5125733f5ff4b
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
package com.ruoyi.service.impl;
 
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.*;
import java.util.ArrayList;
import java.util.List;
 
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.poi.ExcelUtil;
import com.ruoyi.domain.ArchiveRecords;
import com.ruoyi.domain.Archiverecordstouser;
import com.ruoyi.domain.vo.AnalysisResult;
import com.ruoyi.domain.vo.ArchiveInfoVo;
import com.ruoyi.domain.vo.ArchiveRecordSmall;
import com.ruoyi.mapper.ArchiveRecordsMapper;
import com.ruoyi.mapper.ArchiverecordstouserMapper;
import com.ruoyi.service.IArchiveRecordsService;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import java.util.Arrays;
 
/**
 * 档案记录Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-07-12
 */
 
@Service
public class ArchiveRecordsServiceImpl extends ServiceImpl<ArchiveRecordsMapper, ArchiveRecords>  implements IArchiveRecordsService
{
//    @Autowired
//    private ArchiveRecordsMapper archiveRecordsMapper;
 
    @Autowired
    private ArchiverecordstouserMapper archiverecordstouserMapper;
 
 
 
    private LambdaQueryWrapper<ArchiveRecords> buildCondition(ArchiveRecords archiveRecords, Long userId){
        LambdaQueryWrapper<ArchiveRecords> lqw = new LambdaQueryWrapper<>();
 
        // 根据开始和结束的categoryNumber生成连续的recordId
        String categoryNumberStart = archiveRecords.getCategoryNumberStart();
        String categoryNumberEnd = archiveRecords.getCategoryNumberEnd();
        
        if (!StringUtils.isEmpty(categoryNumberStart) && !StringUtils.isEmpty(categoryNumberEnd)) {
            try {
                // 解析开始和结束的编号,提取前缀和数字部分
                // 例如:B1.3-999-2024-1 -> 前缀: B1.3-999-2024-, 数字: 1
                int lastDashIndexStart = categoryNumberStart.lastIndexOf('-');
                int lastDashIndexEnd = categoryNumberEnd.lastIndexOf('-');
                
                if (lastDashIndexStart > 0 && lastDashIndexEnd > 0) {
                    String prefixStart = categoryNumberStart.substring(0, lastDashIndexStart + 1);
                    String prefixEnd = categoryNumberEnd.substring(0, lastDashIndexEnd + 1);
                    
                    // 确保前缀相同,才生成连续编号
                    if (prefixStart.equals(prefixEnd)) {
                        int startNum = Integer.parseInt(categoryNumberStart.substring(lastDashIndexStart + 1));
                        int endNum = Integer.parseInt(categoryNumberEnd.substring(lastDashIndexEnd + 1));
                        
                        // 生成连续的recordId列表,将数字格式化为5位(如00001, 00101)
                        List<String> recordIds = new ArrayList<>();
                        for (int i = startNum; i <= endNum; i++) {
                            // 使用String.format将数字格式化为5位,不足前面补0
                            String formattedNumber = String.format("%05d", i);
                            recordIds.add(prefixStart + formattedNumber);
                        }
                        
                        // 添加到查询条件中
                        lqw.in(ArchiveRecords::getRecordId, recordIds);
                    }
                }
            } catch (Exception e) {
                // 如果解析失败,忽略此条件
                e.printStackTrace();
            }
        }
 
        System.out.println(archiveRecords.getIds());
 
        // 如果没有使用连续编号条件,才使用普通的like条件
        if (StringUtils.isEmpty(categoryNumberStart) || StringUtils.isEmpty(categoryNumberEnd)) {
            lqw.like(!StringUtils.isEmpty(archiveRecords.getRecordId()), ArchiveRecords::getRecordId, archiveRecords.getRecordId());
        }
        else {
                lqw.
            like(!StringUtils.isEmpty(archiveRecords.getInquiryNumber()), ArchiveRecords::getInquiryNumber, archiveRecords.getInquiryNumber())
                    .like(!StringUtils.isEmpty(archiveRecords.getCaseTitle()), ArchiveRecords::getCaseTitle, archiveRecords.getCaseTitle())
                    .like(!StringUtils.isEmpty(archiveRecords.getPublicAttribute()), ArchiveRecords::getPublicAttribute, archiveRecords.getPublicAttribute())
                    .like(!StringUtils.isEmpty(archiveRecords.getPreparationUnit()), ArchiveRecords::getPreparationUnit, archiveRecords.getPreparationUnit())
                    .like(!StringUtils.isEmpty(archiveRecords.getRetentionPeriod()), ArchiveRecords::getRetentionPeriod, archiveRecords.getRetentionPeriod())
                    .like(!StringUtils.isEmpty(archiveRecords.getSecurityClassification()), ArchiveRecords::getSecurityClassification, archiveRecords.getSecurityClassification())
                    .like(!StringUtils.isEmpty(archiveRecords.getFilingNumber()), ArchiveRecords::getFilingNumber, archiveRecords.getFilingNumber())
                    .like(!StringUtils.isEmpty(archiveRecords.getConstructionUnit()), ArchiveRecords::getConstructionUnit, archiveRecords.getConstructionUnit())
                    .like(!StringUtils.isEmpty(archiveRecords.getConstructionAddress()), ArchiveRecords::getConstructionAddress, archiveRecords.getConstructionAddress())
                    .like(!StringUtils.isEmpty(archiveRecords.getProjectName()), ArchiveRecords::getProjectName, archiveRecords.getProjectName())
                    .like(!StringUtils.isEmpty(archiveRecords.getProjectNumber()), ArchiveRecords::getProjectNumber, archiveRecords.getProjectNumber())
                    .like(!StringUtils.isEmpty(archiveRecords.getScanningCompany()), ArchiveRecords::getScanningCompany, archiveRecords.getScanningCompany())
                    .like(!StringUtils.isEmpty(archiveRecords.getArchiveRoomNumber()), ArchiveRecords::getArchiveRoomNumber, archiveRecords.getArchiveRoomNumber())
                    .like(!StringUtils.isEmpty(archiveRecords.getMicrofilmNumber()), ArchiveRecords::getMicrofilmNumber, archiveRecords.getMicrofilmNumber())
                    .like(!StringUtils.isEmpty(archiveRecords.getHistoricalReferenceNumber()), ArchiveRecords::getHistoricalReferenceNumber, archiveRecords.getHistoricalReferenceNumber())
                    .eq(!StringUtils.isEmpty(archiveRecords.getRecordStatus()), ArchiveRecords::getRecordStatus, archiveRecords.getRecordStatus())
                    .like(!StringUtils.isEmpty(archiveRecords.getEveryProjectName()), ArchiveRecords::getEveryProjectName, archiveRecords.getEveryProjectName())
                    .like(!StringUtils.isEmpty(archiveRecords.getRemarks()), ArchiveRecords::getRemarks, archiveRecords.getRemarks());
 
        }
 
        if(archiveRecords.getIds()!=null)
                lqw.in(ArchiveRecords::getId,new ArrayList<>(Arrays.asList(archiveRecords.getIds())));
 
 
 
        //                .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);
        if(userId==1)
 
            lqw.orderByDesc(ArchiveRecords::getCreateTime);
        System.out.println("ssssssssssssddd0000000000000000");
        return lqw;
 
    }
 
    @Override
    public Long getMaxId() {
        //return this.
        return baseMapper.findMaxId();
    }
 
    @Override
    public AjaxResult selectDataList(ArchiveRecords archiveRecords, Integer pageNum, Integer pageSize) {
        //拿到当前用户的id
        // 获取当前用户ID
        Long userid = SecurityUtils.getUserId();
        LambdaQueryWrapper<ArchiveRecords> lqw = buildCondition(archiveRecords, userid);
 
        System.out.println("-------------");
        System.out.println(pageNum);
 
 
 
 
        Page<ArchiveRecords> page = new Page<>(pageNum, pageSize);
    
    // 使用自定义的分页查询方法,先连接再分页
    Page<ArchiveRecords> pageResult = this.baseMapper.selectJoinUserPage(page, userid, lqw);
 
 
            //selectJoinUserPage(page, lqw);
    
  
 
        List<ArchiveRecords> beanRecords = pageResult.getRecords();//得到查询出来的数据
      //  List<ArchiveRecords> beanRecords = list(lqw);
     //   log.info("从数据库中查到的为:{}", beanRecords);
    //    return markOwnData(familyId, fatherFaId, motherFaId, beanRecords);
 
 
        beanRecords.forEach(record ->{
            if(record.isArchiveRecordsId()!=null&&record.getUserId()==userid)
            {
                    record.setOwnData(true);
            }
            else
                record.setOwnData(false);
        });
        //不是管理员进行排序
       // if(userid != 1)
       //    Collections.sort(beanRecords, Comparator.comparing(ArchiveRecords::isOwnData));
        HashMap<String, Object> data = MapUtils.getResult(pageResult, beanRecords);
//        System.out.println(data.get("data"));
//        System.out.println(data.get("pageNum"));
//        System.out.println(data.get("total"));
 
        return AjaxResult.success(data);
    }
 
    /**
     * 查询档案记录
     * 
     * @param id 档案记录主键
     * @return 档案记录
     */
    @Override
    public ArchiveRecords selectArchiveRecordsById(Long id)
    {
        System.out.println(id);
        System.out.println("-------------ssssssssssssssssss");
        LambdaQueryWrapper<ArchiveRecords> lqw = new LambdaQueryWrapper<>();
        lqw.eq(id!=null, ArchiveRecords::getId, id);
        List<ArchiveRecords> records = list(lqw);
        if(!records.isEmpty())
            return records.get(0);
        else
            return null;
    }
 
    /**
     * 查询档案记录列表
     * 
     * @param archiveRecords 档案记录
     * @return 档案记录
     */
    @Override
    public List<ArchiveRecords> selectArchiveRecordsList(ArchiveRecords archiveRecords)
    {
        //拿到当前用户的id
        // 获取当前用户ID
        Long userid = SecurityUtils.getUserId();
        LambdaQueryWrapper<ArchiveRecords> lambdaQueryWrapper = buildCondition(archiveRecords,userid);
        List<ArchiveRecords> beanRecords = list(lambdaQueryWrapper);
 
        return beanRecords;
    }
    
    @Override
    public List<ArchiveRecords> selectArchiveRecordsByIds(Long[] ids)
    {
        // 获取当前用户ID
        Long userid = SecurityUtils.getUserId();
        
        // 创建查询条件
        LambdaQueryWrapper<ArchiveRecords> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        
        // 根据ids查询
        lambdaQueryWrapper.in(ArchiveRecords::getId, Arrays.asList(ids));
        
        // 如果不是管理员,需要考虑权限过滤(可根据实际权限需求调整)
        if (userid != 1) {
            // 这里可以添加权限相关的过滤条件
        }
        
        List<ArchiveRecords> beanRecords = list(lambdaQueryWrapper);
        return beanRecords;
    }
 
    /**
     * 新增档案记录
     * 
     * @param archiveRecords 档案记录
     * @return 结果
     */
    @Override
    public int insertArchiveRecords(ArchiveRecords archiveRecords)
    {
        LocalDateTime time = LocalDateTime.now();
 
        Date date = Date.from(time.atZone(ZoneId.systemDefault()).toInstant());
        archiveRecords.setCreateTime(date);
        System.out.println(archiveRecords.getRecordId());
        //根据档号查询,是否已经有档号,有的话,就不让插入
        LambdaQueryWrapper<ArchiveRecords> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(!StringUtils.isEmpty(archiveRecords.getRecordId()), ArchiveRecords::getRecordId,
                archiveRecords.getRecordId());
        List<ArchiveRecords> lis = list(lambdaQueryWrapper);
        if(!lis.isEmpty())
        {
            return 0;
        }
        archiveRecords.setRecordStatus("未录入");
      //  archiveRecords
        boolean res = this.save(archiveRecords);
 
        //0表示失败,1表示成功
        if(res)
            return 1;
        else
            return 0;
    }
 
    /**
     * 修改档案记录
     * 
     * @param archiveRecords 档案记录
     * @return 结果
     */
    @Override
    public int updateArchiveRecords(ArchiveRecords archiveRecords)
    {
        boolean result = updateById(archiveRecords);
        if(result)
        return 1;
        else
            return 0;
    }
 
    /**
     * 批量删除档案记录
     * 
     * @param ids 需要删除的档案记录主键
     * @return 结果
     */
    @Override
    public int deleteArchiveRecordsByIds(Long[] ids)
    {
        Long res = 0L;
        for(Long id: ids)
        {
           this.baseMapper.updateAllInfoById(id);
 
        }
        return 1;
    }
 
    /**
     * 删除档案记录信息
     * 
     * @param id 档案记录主键
     * @return 结果
     */
    @Override
    public int deleteArchiveRecordsById(Long id)
    {
        return 0;
    }
 
    @Override
    public ArchiveInfoVo selectByRecordId(Long id) {
        return this.baseMapper.findByRecordId(id);
    }
 
    @Override
    public AjaxResult importExcel(MultipartFile file) {
        ExcelUtil<ArchiveRecords> util = new ExcelUtil<>(ArchiveRecords.class);
        List<ArchiveRecords> dataList = null;
        try {
            dataList = util.importExcel(file.getInputStream());
        } catch (Exception e) {
            throw new RuntimeException("没有按照规则导入数据");
        }
 
        assert dataList != null;
 
        for (ArchiveRecords archiveRecords : dataList) {
           // physcialService.mySave(physcial);
            archiveRecords.setRecordStatus("未录入");
            LocalDateTime time = LocalDateTime.now();
 
            Date date = Date.from(time.atZone(ZoneId.systemDefault()).toInstant());
            archiveRecords.setCreateTime(date);
            this.mySave(archiveRecords);
        }
 
        return AjaxResult.success();
    }
 
    @Override
    public List<AnalysisResult> statisticAya() {
        return this.baseMapper.statisticAyasis();
    }
 
    @Override
    public int updateArchiveById(String status, Long id) {
        this.baseMapper.updateStatusById(status, id);
        return 0;
    }
 
    @Override
    public int updateStatusByIds(Long[] ids) {
        for(Long id : ids)
            this.baseMapper.updateStatusById("录入完成",id);
        return 0;
    }
 
    @Override
    public List<ArchiveRecordSmall> findByIds(ArchiveRecords archiveRecords) {
 
        LambdaQueryWrapper<ArchiveRecords> lqw = new LambdaQueryWrapper<>();
        if(archiveRecords.getIds()!=null)
            lqw.in(ArchiveRecords::getId,new ArrayList<>(Arrays.asList(archiveRecords.getIds())));
        return this.baseMapper.selectByIds(lqw);
    }
 
 
    public AjaxResult mySave(ArchiveRecords archiveRecords) {
 
 
        //检查是否有重复数据插入
        LambdaQueryWrapper<ArchiveRecords> lqw = new LambdaQueryWrapper<>();
        lqw.eq(!StringUtils.isEmpty(archiveRecords.getRecordId()), ArchiveRecords::getRecordId,archiveRecords.getRecordId());
        List<ArchiveRecords> list = list(lqw);
        if (list.size() > 0) {
          //  throw new RuntimeException("请勿新增重复数据");
            //如果有重复数据,则根据recordId进行数据修改
         //   if()
 
                this.baseMapper.update(archiveRecords, lqw);
                return AjaxResult.success();
 
 
        }
        else {
        Long userid = SecurityUtils.getUserId();
        if(userid==1) {
            if (save(archiveRecords)) {
                return AjaxResult.success();
            } else {
                return AjaxResult.error();
            }
        }
             else
            return AjaxResult.error("档案号不存在!");
        }
 
    }
    @Override
    public boolean whether(@Param("recordId") Long recordId){
        return this.baseMapper.whether(recordId);
    }
}