fuliqi
2024-09-30 3351ade035a3a027d38e0372e496a5669cd7bde2
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
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.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")
//TODO:修改score、index默认发布
public class CheckScoreTask {
    @Autowired
    private CheckTemplateMapper templateMapper;
    @Autowired
    private CheckTemplateRuleMapper checkTemplateRuleMapper;
    @Autowired
    private CheckScoreMapper scoreMapper;
    @Autowired
    private CheckIndexVideoMapper videoMapper;
    @Autowired
    private CheckIndexFaceMapper faceMapper;
    @Autowired
    private CheckIndexCarMapper carMapper;
 
    public void executeTemplate(Integer templateId) {
        CheckTemplate checkTemplate = templateMapper.selectCheckTemplateById(templateId);
        if (checkTemplate != null) {
            Short examineCategory = checkTemplate.getExamineCategory();
            Short examineTag = checkTemplate.getExamineTag();
            //查权重
            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);
                }
            } 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);
                }
            } 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);
                }
            }
            //储存分数
            if(!CollectionUtils.isEmpty(scoreList)) {
                scoreMapper.saveBatch(scoreList);
            }
        }
    }
 
    private <T extends CheckIndex> void addToList(Integer templateId, CheckTemplate checkTemplate, Short examineTag, List<CheckTemplateRule> templateRuleList, List<CheckScore> scoreList, T indexObject, Short checkCategory) {
        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);
        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) {
        //根据调整系数调整最终分数大小
        String adjustWay = checkTemplate.getAdjustWay();
        BigDecimal adjustCoefficient = checkTemplate.getAdjustCoefficient();
        if (CheckConstants.Multiply.equals(adjustWay)) {
            scoreFinal = adjustCoefficient.multiply(scoreFinal).multiply(new BigDecimal(100));
        } else if (CheckConstants.Divided.equals(adjustWay)) {
            //四舍五入保留小数后四位
            scoreFinal = scoreFinal.divide(adjustCoefficient, 4, RoundingMode.HALF_UP).multiply(new BigDecimal(100));
        }
        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());
    }
}