fuliqi
2024-09-24 8f83a63bc5f046e34a1a06bcf6f1a8241c7277ac
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
package com.ycl.calculate;
 
import com.ycl.platform.domain.entity.CheckIndexVideo;
import com.ycl.platform.domain.result.UY.MonitorQualifyResult;
import com.ycl.platform.domain.vo.TMonitorVO;
import com.ycl.platform.mapper.CheckIndexVideoMapper;
import com.ycl.platform.mapper.TMonitorMapper;
import com.ycl.platform.service.ICheckIndexVideoService;
import constant.ApiConstants;
import constant.CheckThreadConstants;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import utils.DateUtils;
 
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
 
/**
 * 计算一机一档注册率、档案考核比
 * 获取分省厅、区域的map<k,v> k为deptId或者Province_deptId
 * 更新或新增
 */
@Component
@Slf4j
public class MonitorRegistrationCalculation extends IndexCalculationServe<MonitorQualifyResult, MonitorRegistrationCalculation.AreaStats> implements CalculationStrategy<MonitorQualifyResult> {
    @Autowired
    private CheckIndexVideoMapper checkIndexVideoMapper;
    @Autowired
    private ICheckIndexVideoService checkIndexVideoService;
    @Autowired
    private TMonitorMapper monitorMapper;
 
    //区域视频在线率的内部类
    protected static class AreaStats {
        //当日档案数
        int todayFiles = 0;
        //未注册的数量
        int newSites = 0;
        //全年留存数
        int allFiles = 0;
    }
    @Override
    public void calculate(List<MonitorQualifyResult> list) {
        //获取分区域的指标数量
        Map<String, MonitorRegistrationCalculation.AreaStats> areaStatsMap = getAreaStatsMap(list);
        if (areaStatsMap == null) return;
 
        //查数据库补充全年留存数
        List<TMonitorVO> monitorVOS = monitorMapper.selectMonitorVOList();
        areaStatsMap.forEach((key,areaStats)->{
            if(key.startsWith(ApiConstants.Province)){
                long count = monitorVOS.stream().filter(TMonitorVO::getProvinceTag)
                        .filter(vo -> key.split("_")[1].equals(vo.getDeptId() + "")).count();
                areaStats.allFiles = Integer.parseInt(count+"");
            }else if(key.startsWith(ApiConstants.Dept)){
                long count = monitorVOS.stream().filter(TMonitorVO::getDeptTag)
                        .filter(vo -> key.split("_")[1].equals(vo.getDeptId() + "")).count();
                areaStats.allFiles = Integer.parseInt(count+"");
            }
            else {
                long count = monitorVOS.stream().filter(vo -> key.equals(vo.getDeptId() + "")).count();
                areaStats.allFiles = Integer.parseInt(count+"");
            }
        });
 
        // 查询是否index表已经存在今日数据
        List<CheckIndexVideo> checkIndexVideoList = checkIndexVideoMapper.selectToday(DateUtils.getDate());
        List<CheckIndexVideo> checkIndexVideos = new ArrayList<>();
        areaStatsMap.forEach((key, stats) -> {
            if (stats.todayFiles > 0) {
                CheckIndexVideo checkIndexVideo = createOrUpdateCheckIndexVideo(key, stats, checkIndexVideoList);
                if (checkIndexVideo != null) {
                    checkIndexVideos.add(checkIndexVideo);
                }
            }
        });
 
        checkIndexVideoService.saveOrUpdateBatch(checkIndexVideos);
    }
 
    /**
     * 累计总点位数、在线点位数、重点点位数、重点点位在线数、指挥图像数、指挥图像在线数
     */
    @Override
    public void updateAreaStats(Map<String, AreaStats> areaStatsMap, String key, MonitorQualifyResult result) {
        //返回对象的引用,如果不存在会放入新的key,value
        AreaStats stats = areaStatsMap.computeIfAbsent(key, k -> new AreaStats());
        stats.todayFiles++;
        if (result.getNewDevice()!=null && result.getNewDevice()) {
            stats.newSites++;
        }
    }
 
    /**
     * 视频点位在线率
     */
    private CheckIndexVideo createOrUpdateCheckIndexVideo(String key, AreaStats stats, List<CheckIndexVideo> checkIndexVideoList) {
        CheckIndexVideo checkIndexVideo = getCheckIndex(key, checkIndexVideoList, CheckIndexVideo.class);
        if (checkIndexVideo == null) {
            return null;
        }
        //调用一机一档注册率
        Map<String, Object> param = new HashMap<>();
        param.put("totalSites", stats.todayFiles);
        param.put("newSites", stats.newSites);
        BigDecimal monitorRegistration = monitorRegistration(param);
        checkIndexVideo.setMonitorRegistration(monitorRegistration);
        //调用档案考核比计算
        Map<String, Object> archiveParam = new HashMap<>();
        archiveParam.put("allFiles", stats.allFiles);
        archiveParam.put("todayFiles", stats.todayFiles);
        BigDecimal archivesRate = archivesRate(archiveParam);
        //如果档案考核比低于0.9不得分
        if (new BigDecimal(CheckThreadConstants.Check_Video_Archive).compareTo(archivesRate) <= 0) {
            checkIndexVideo.setArchivesRate(archivesRate);
        } else {
            checkIndexVideo.setArchivesRate(BigDecimal.ZERO);
        }
 
        return checkIndexVideo;
    }
}