fuliqi
2024-09-24 8f83a63bc5f046e34a1a06bcf6f1a8241c7277ac
ycl-server/src/main/java/com/ycl/task/CheckScoreTask.java
@@ -4,15 +4,15 @@
import com.google.common.base.CaseFormat;
import com.ycl.platform.base.CheckIndex;
import com.ycl.platform.domain.entity.*;
import com.ycl.platform.mapper.CheckScoreMapper;
import com.ycl.platform.mapper.CheckTemplateMapper;
import com.ycl.platform.mapper.CheckTemplateRuleMapper;
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;
@@ -25,6 +25,9 @@
@Slf4j
@Component("checkScoreTask")
//TODO:增加考核标签字典:公安部
//TODO:修改点位状态字典:在线、离线
//TODO:修改score、index默认发布
public class CheckScoreTask {
    @Autowired
    private CheckTemplateMapper templateMapper;
@@ -33,18 +36,15 @@
    @Autowired
    private CheckScoreMapper scoreMapper;
    @Autowired
    private MongoTemplate mongoTemplate;
    private CheckIndexVideoMapper videoMapper;
    @Autowired
    private ICheckIndexVideoService videoService;
    private CheckIndexFaceMapper faceMapper;
    @Autowired
    private ICheckIndexFaceService faceService;
    @Autowired
    private ICheckIndexCarService carService;
    private CheckIndexCarMapper carMapper;
    public void executeTemplate(Integer templateId) {
        CheckTemplate checkTemplate = templateMapper.selectCheckTemplateById(templateId);
        if (checkTemplate != null) {
            log.info("执行考核模板---------->{}", checkTemplate.getTemplateName());
            Short examineCategory = checkTemplate.getExamineCategory();
            Short examineTag = checkTemplate.getExamineTag();
            //查权重
@@ -63,54 +63,54 @@
                checkIndexVideo.setExamineTag(examineTag);
                checkIndexVideo.setDay(day);
                checkIndexVideo.setDeptIds(JSONArray.parseArray(checkTemplate.getDeptId(), Integer.class));
                //根据模板的考核标签查各区县对应省厅或市局视频数据
                List<CheckIndexVideo> checkIndexVideos = videoService.selectCheckIndexVideoList(checkIndexVideo);
                //根据模板的考核标签查各区县对应省厅或市局或公安部视频数据
                List<CheckIndexVideo> checkIndexVideos = videoMapper.getCheckIndexVideoList(checkIndexVideo);
                for (CheckIndexVideo indexVideo : checkIndexVideos) {
                    addToList(templateId, checkTemplate, examineTag, templateRuleList, scoreList, indexVideo,CheckConstants.Rule_Category_Video);
                    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 = carService.selectCheckIndexCarList(checkIndexCar);
                //根据模板的考核标签查各区县对应省厅或市局或公安部车辆数据
                List<CheckIndexCar> checkIndexCars = carMapper.getCheckIndexCarList(checkIndexCar);
                for (CheckIndexCar indexCar : checkIndexCars) {
                    addToList(templateId, checkTemplate, examineTag, templateRuleList, scoreList, indexCar,CheckConstants.Rule_Category_Car);
                    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 = faceService.selectCheckIndexFaceList(checkIndexFace);
                //根据模板的考核标签查各区县对应省厅或市局或公安部人脸数据
                List<CheckIndexFace> checkIndexFaces = faceMapper.getCheckIndexFaceList(checkIndexFace);
                for (CheckIndexFace indexFace : checkIndexFaces) {
                    addToList(templateId, checkTemplate, examineTag, templateRuleList, scoreList, indexFace,CheckConstants.Rule_Category_Face);
                    addToList(templateId, checkTemplate, examineTag, templateRuleList, scoreList, indexFace, CheckConstants.Rule_Category_Face);
                }
            }
            //储存分数
            scoreMapper.saveBatch(scoreList);
            //TODO:根据报警分数阈值,添加报警信息
            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) {
    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);
        fillCheckScore(templateId, checkTemplate, examineTag, indexObject, checkScore, scoreFinal, checkCategory);
        scoreList.add(checkScore);
    }
    //通用方法计算分数
    private <T>BigDecimal getScoreFinal(T object, BigDecimal scoreFinal, CheckTemplateRule templateRule) {
    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);
@@ -123,37 +123,28 @@
            BigDecimal score = index.multiply(templateRule.getWeight());
            scoreFinal = scoreFinal.add(score);
        } catch (Exception e) {
            log.info("反射异常",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) {
    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)){
        if (CheckConstants.Multiply.equals(adjustWay)) {
            scoreFinal = adjustCoefficient.multiply(scoreFinal).multiply(new BigDecimal(100));
        }else if(CheckConstants.Divided.equals(adjustWay)){
        } 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).multiply(new BigDecimal(100));
        }
        checkScore.setCreateTime(new Date());
        checkScore.setExamineTag(Integer.parseInt(examineTag +""));
        checkScore.setExamineTag(Integer.parseInt(examineTag + ""));
        checkScore.setExamineCategory(checkCategory);
        checkScore.setDeptId(checkIndex.getDeptId());
        checkScore.setTemplateId(templateId);
        checkScore.setScore(scoreFinal);
    }
    //查mongo数据归档到mysql
    public void dataArchiving() {
        //TODO:归档check_index_car,区分省厅市局,每个区县一条数据
        CheckIndexCar checkIndexCar = new CheckIndexCar();
        //TODO:归档check_index_face
        CheckIndexFace checkIndexFace = new CheckIndexFace();
        //TODO:归档check_index_video
        CheckIndexVideo checkIndexVideo = new CheckIndexVideo();
        checkScore.setPublish(PublishType.PUBLISHED.getCode());
    }
}