kongzy
2023-09-22 3124f3a5b7f45d043b228829b6b3a2e541b31574
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
package com.nms.swspkmas_standalone.service.impl;
 
import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.github.benmanes.caffeine.cache.Cache;
import com.nms.swspkmas_standalone.entity.CaptureRecord;
import com.nms.swspkmas_standalone.entity.Examinee;
import com.nms.swspkmas_standalone.entity.vo.CaptureRecordVO;
import com.nms.swspkmas_standalone.exception.ApiException;
import com.nms.swspkmas_standalone.mapper.CaptureRecordMapper;
import com.nms.swspkmas_standalone.service.CaptureRecordService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
 
import java.sql.Wrapper;
import java.util.*;
import java.util.stream.Collectors;
 
/**
 * <p>
 * 抓拍记录表 服务实现类
 * </p>
 *
 * @author kzy
 * @since 2023-09-21 08:41:38
 */
@Service
public class CaptureRecordServiceImpl extends ServiceImpl<CaptureRecordMapper, CaptureRecord> implements CaptureRecordService {
 
    @Autowired
    private Cache<String,Object> cacheService;
 
    @Override
    public void addCaptureRecord(CaptureRecordVO captureRecordVO) {
        CaptureRecord captureRecord=new CaptureRecord();
        BeanUtils.copyProperties(captureRecordVO,captureRecord);
        String nowday= DateUtil.today();
        captureRecord.setTime(nowday);
        save(captureRecord);
    }
 
    @Override
    public Map<String,List<CaptureRecord>> getCaptureRecord(String time) {
        Map<String,List<CaptureRecord>> map=new LinkedHashMap<>();
        List<CaptureRecord> captureRecordList=null;
        Long count=count(Wrappers.<CaptureRecord>lambdaQuery().eq(true,CaptureRecord::getTime,time));
        if(count==0){
            return map;
        }
        String key="capture_"+time;
        captureRecordList= (List<CaptureRecord>) cacheService.getIfPresent(key);
        if(captureRecordList==null||count!=captureRecordList.size()){
            captureRecordList=baseMapper.getCaptureRecord(time);
        }
        cacheService.put(key,captureRecordList);
        for(CaptureRecord captureRecord:captureRecordList){
            List<CaptureRecord> crList=map.get(captureRecord.getIdNumber());
            if(crList==null){
                crList=new ArrayList<>();
            }
            crList.add(captureRecord);
            map.put(captureRecord.getIdNumber(),crList);
        }
//        Map<String,List<CaptureRecord>> map=captureRecordList.stream().collect(Collectors.toMap(CaptureRecord::getIdNumber,
//                e -> new ArrayList(Arrays.asList(e)),
//                (List<CaptureRecord> oldList,List<CaptureRecord> newList) -> {oldList.addAll(newList);return oldList;}));
        return map;
    }
 
    @Override
    public void deleteCaptureRecord(Long id) {
        CaptureRecord captureRecord=getById(id);
        if(captureRecord==null){
            throw new ApiException("抓取记录不存在");
        }
        removeById(id);
    }
}