zxl
2025-02-19 002372c5cfd2c9b9881fda5e527c2b6c4ee4d599
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
package com.ycl.task;
 
import com.alibaba.fastjson2.JSONArray;
import com.google.common.base.CaseFormat;
import com.ycl.platform.base.CheckIndex;
import com.ycl.platform.domain.entity.*;
import com.ycl.platform.domain.vo.YwPointVO;
import com.ycl.platform.mapper.*;
import com.ycl.platform.service.*;
import constant.CheckConstants;
import enumeration.general.PublishType;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;
 
import java.lang.reflect.Field;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
 
 
@Slf4j
@Component("checkScoreTask")
public class CheckScoreTask {
    @Autowired
    private CheckTemplateMapper templateMapper;
    @Autowired
    private CheckTemplateRuleMapper checkTemplateRuleMapper;
    @Autowired
    private CheckScoreMapper scoreMapper;
    @Autowired
    private ICheckScoreService checkScoreService;
    @Autowired
    private CheckIndexVideoMapper videoMapper;
    @Autowired
    private CheckIndexFaceMapper faceMapper;
    @Autowired
    private CheckIndexCarMapper carMapper;
    @Autowired
    private YwPointMapper pointMapper;
    //公安部只有视频考核
    public void executeTemplate(Integer templateId) {
        CheckTemplate checkTemplate = templateMapper.selectCheckTemplateById(templateId);
        if (checkTemplate != null) {
            Short examineCategory = checkTemplate.getExamineCategory();
            Short examineTag = checkTemplate.getExamineTag();
            //用于补充数量
            List<YwPointVO> pointVOS= pointMapper.selectToCount(examineCategory, examineTag);
            //查权重
            CheckTemplateRule checkTemplateRule = new CheckTemplateRule();
            checkTemplateRule.setCheckTemplateId(templateId);
            List<CheckTemplateRule> templateRuleList = checkTemplateRuleMapper.selectListByTemplateId(templateId);
            //查今天的index指标
            SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
            String day = dateFormat.format(new Date());
            //创建score集合方便最后批量存储
            List<CheckScore> scoreList = new ArrayList<>();
            //根据考核类别和考核标签,查不同index表(区分省厅区县)
            if (CheckConstants.Rule_Category_Video.equals(examineCategory)) {
                CheckIndexVideo checkIndexVideo = new CheckIndexVideo();
                checkIndexVideo.setExamineTag(examineTag);
                checkIndexVideo.setDay(day);
                checkIndexVideo.setDeptIds(JSONArray.parseArray(checkTemplate.getDeptId(), Integer.class));
                //根据模板的考核标签查各区县对应省厅或市局或公安部视频数据
                List<CheckIndexVideo> checkIndexVideos = videoMapper.getCheckIndexVideoList(checkIndexVideo);
                for (CheckIndexVideo indexVideo : checkIndexVideos) {
                    addToList(templateId, checkTemplate, examineTag, templateRuleList, scoreList, indexVideo, CheckConstants.Rule_Category_Video,pointVOS);
                }
            } else if (CheckConstants.Rule_Category_Car.equals(examineCategory)) {
                CheckIndexCar checkIndexCar = new CheckIndexCar();
                checkIndexCar.setDay(day);
                checkIndexCar.setExamineTag(examineTag);
                checkIndexCar.setDeptIds(JSONArray.parseArray(checkTemplate.getDeptId(), Integer.class));
                //根据模板的考核标签查各区县对应省厅或市局或公安部车辆数据
                List<CheckIndexCar> checkIndexCars = carMapper.getCheckIndexCarList(checkIndexCar);
                for (CheckIndexCar indexCar : checkIndexCars) {
                    addToList(templateId, checkTemplate, examineTag, templateRuleList, scoreList, indexCar, CheckConstants.Rule_Category_Car,pointVOS);
                }
            } else if (CheckConstants.Rule_Category_Face.equals(examineCategory)) {
                CheckIndexFace checkIndexFace = new CheckIndexFace();
                checkIndexFace.setDay(day);
                checkIndexFace.setExamineTag(examineTag);
                checkIndexFace.setDeptIds(JSONArray.parseArray(checkTemplate.getDeptId(), Integer.class));
                //根据模板的考核标签查各区县对应省厅或市局或公安部人脸数据
                List<CheckIndexFace> checkIndexFaces = faceMapper.getCheckIndexFaceList(checkIndexFace);
                for (CheckIndexFace indexFace : checkIndexFaces) {
                    addToList(templateId, checkTemplate, examineTag, templateRuleList, scoreList, indexFace, CheckConstants.Rule_Category_Face,pointVOS);
                }
            }
 
            //查出今天生成score
            List<CheckScore> todays = scoreMapper.selectToday(day);
            //遍历scoreList集合,如果今天生成过补充id
            for (CheckScore result : scoreList) {
                for (CheckScore today : todays) {
                    //根据考核标签、考核种类、部门id查询是否存在今日数据
                    if(result.getExamineTag().equals(today.getExamineTag()) && result.getExamineCategory().equals(today.getExamineCategory()) && result.getDeptId().equals(today.getDeptId())){
                        //补充id,后续根据id saveOrUpdate
                        result.setId(today.getId());
                        result.setPublish(today.getPublish());
                    }
                }
            }
            //储存分数
            if (!CollectionUtils.isEmpty(scoreList)) {
                checkScoreService.saveOrUpdateBatch(scoreList);
            }
        }
    }
 
    private <T extends CheckIndex> void addToList(Integer templateId, CheckTemplate checkTemplate, Short examineTag, List<CheckTemplateRule> templateRuleList, List<CheckScore> scoreList, T indexObject, Short checkCategory, List<YwPointVO> pointVOS) {
        CheckScore checkScore = new CheckScore();
        checkScore.setIndexId(indexObject.getId());
        BigDecimal scoreFinal = BigDecimal.ZERO;
        for (CheckTemplateRule templateRule : templateRuleList) {
            //计算分数
            scoreFinal = getScoreFinal(indexObject, scoreFinal, templateRule);
        }
        //补充checkScore
        fillCheckScore(templateId, checkTemplate, examineTag, indexObject, checkScore, scoreFinal, checkCategory,pointVOS);
        scoreList.add(checkScore);
    }
 
    //通用方法计算分数
    private <T> BigDecimal getScoreFinal(T object, BigDecimal scoreFinal, CheckTemplateRule templateRule) {
        String ruleIndex = templateRule.getRuleIndex();
        //将a_b_c转换为aBC
        String camelRuleIndex = CaseFormat.LOWER_UNDERSCORE.to(CaseFormat.LOWER_CAMEL, ruleIndex);
        try {
            //反射获取属性值
            Class<T> clazz = (Class<T>) object.getClass();
            Field field = clazz.getDeclaredField(camelRuleIndex);
            field.setAccessible(true);
            BigDecimal index = (BigDecimal) field.get(object);
            BigDecimal score = index.multiply(templateRule.getWeight());
            scoreFinal = scoreFinal.add(score);
        } catch (Exception e) {
            log.error("反射异常", e.getMessage());
        }
        return scoreFinal;
    }
 
    //设置checkScore对象
    private void fillCheckScore(Integer templateId, CheckTemplate checkTemplate, Short examineTag, CheckIndex checkIndex, CheckScore checkScore, BigDecimal scoreFinal, Short checkCategory, List<YwPointVO> pointVOS) {
        //根据调整系数调整最终分数大小
        String adjustWay = checkTemplate.getAdjustWay();
        BigDecimal adjustCoefficient = checkTemplate.getAdjustCoefficient();
        if (CheckConstants.Multiply.equals(adjustWay)) {
//            scoreFinal = adjustCoefficient.multiply(scoreFinal).multiply(new BigDecimal(100));
            scoreFinal = adjustCoefficient.multiply(scoreFinal);
        } else if (CheckConstants.Divided.equals(adjustWay)) {
            //四舍五入保留小数后四位
//            scoreFinal = scoreFinal.divide(adjustCoefficient, 4, RoundingMode.HALF_UP).multiply(new BigDecimal(100));
            scoreFinal = scoreFinal.divide(adjustCoefficient, 4, RoundingMode.HALF_UP);
        }
        checkScore.setCreateTime(new Date());
        checkScore.setExamineTag(Integer.parseInt(examineTag + ""));
        checkScore.setExamineCategory(checkCategory);
        checkScore.setDeptId(checkIndex.getDeptId());
        checkScore.setTemplateId(templateId);
        checkScore.setScore(scoreFinal);
        checkScore.setPublish(PublishType.PUBLISHED.getCode());
        if(!CollectionUtils.isEmpty(pointVOS)) {
            long count = pointVOS.stream().filter(ywPointVO -> checkIndex.getDeptId().equals(ywPointVO.getDeptId())).count();
            checkScore.setDeviceCount((int) count);
        }
    }
}