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;
/**
*
* 抓拍记录表 服务实现类
*
*
* @author kzy
* @since 2023-09-21 08:41:38
*/
@Service
public class CaptureRecordServiceImpl extends ServiceImpl implements CaptureRecordService {
@Autowired
private Cache 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> getCaptureRecord(String time) {
Map> map=new LinkedHashMap<>();
List captureRecordList=null;
Long count=count(Wrappers.lambdaQuery().eq(true,CaptureRecord::getTime,time));
if(count==0){
return map;
}
String key="capture_"+time;
captureRecordList= (List) cacheService.getIfPresent(key);
if(captureRecordList==null||count!=captureRecordList.size()){
captureRecordList=baseMapper.getCaptureRecord(time);
}
cacheService.put(key,captureRecordList);
for(CaptureRecord captureRecord:captureRecordList){
List crList=map.get(captureRecord.getIdNumber());
if(crList==null){
crList=new ArrayList<>();
}
crList.add(captureRecord);
map.put(captureRecord.getIdNumber(),crList);
}
// Map> map=captureRecordList.stream().collect(Collectors.toMap(CaptureRecord::getIdNumber,
// e -> new ArrayList(Arrays.asList(e)),
// (List oldList,List 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);
}
}