EquipHealthLevelJob.java 8.3 KB
package com.skua.modules.equipment.quartz;

import com.skua.core.util.ConvertUtils;
import com.skua.core.util.DateUtils;
import com.skua.core.util.DoubleOperaUtils;
import com.skua.modules.equipment.entity.EquipmentInfo;
import com.skua.modules.equipment.service.IEquipmentInfoService;
import lombok.extern.slf4j.Slf4j;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @program: skboot
 * @description: 设备健康度定时任务
 * @author: zhanglei
 * @create: 2025/03/08 16:05:05
 */
@Slf4j
@Component
public class EquipHealthLevelJob implements Job {

    @Autowired
    private IEquipmentInfoService equipmentInfoService;

    private static final String qz_screen_type = "71";//权重标识
    private static final String status_screen_type = "70";//状态标识

    @Override
    public void execute(JobExecutionContext context) throws JobExecutionException {
        log.info("设备健康度更新任务开始");
        //获取当前时间 yyyy-MM-dd
        String today = DateUtils.formatDate(new Date(), "yyyy-MM-dd");
        Map<String,Double> qzMap = initQzMap();
        Map<String,String> statusValueMap = initStatusMap();
        //workingLife 使用年限、originalValue 购置价格、installDate 安装日期
        List<EquipmentInfo> list = equipmentInfoService.getEquipmentList();
        for (EquipmentInfo equipmentInfo : list){

            if(ConvertUtils.isEmpty(equipmentInfo.getWorkingLife())||
                    ConvertUtils.isEmpty(equipmentInfo.getOriginalValue())||
                    ConvertUtils.isEmpty(equipmentInfo.getInstallDate())){
                equipmentInfo.setEquipmentHealthLevel("");
                equipmentInfo.setEquipmentHealthStatus("wfjs");
            }else{
                double performanceScore = 100.00;//性能得分
                double faultScore = 100.00;//故障得分
                double maintainScore = 100.00;//维护得分
                double useYearScore = 100.00;//使用年限得分
                //性能分数
                if(ConvertUtils.isNotEmpty(equipmentInfo.getPerformanceScore())){
                    performanceScore = Double.valueOf(equipmentInfo.getPerformanceScore());
                }
                performanceScore = DoubleOperaUtils.bigDecimalRound(qzMap.get("xnzb")/100.00 * performanceScore,2);
                //故障分数
                faultScore = getFaultScore(equipmentInfo.getGzCount(),qzMap.get("gzl")/100.00);
                //维护分数
                maintainScore = getMaintainScore(equipmentInfo.getMaintenanceCost(), equipmentInfo.getOriginalValue(),qzMap.get("whcb")/100.00);
                //使用年限分数
                useYearScore = getUseYearScore(equipmentInfo.getWorkingLife(),equipmentInfo.getInstallDate(),today,qzMap.get("synx")/100.00);
                //计算总分
                double score = DoubleOperaUtils.bigDecimalRound(performanceScore + faultScore + maintainScore + useYearScore,2);
                equipmentInfo.setEquipmentHealthLevel(String.valueOf(score));

                //判断score在statusValueMap的key值的哪个区间,返回对应的value
                String statusValue = getEquipmentHealthStatus(score,statusValueMap);
                equipmentInfo.setEquipmentHealthStatus(statusValue);
            }
            equipmentInfoService.updateById(equipmentInfo);
        }

    }

    private String getEquipmentHealthStatus(double score,Map<String,String> statusValueMap) {
        String statusValue = "";
        for (Map.Entry<String, String> entry : statusValueMap.entrySet()) {
            String key = entry.getKey();
            String value = entry.getValue();
            if (score >= Double.valueOf(key.split("-")[0]) && score <= Double.valueOf(key.split("-")[1])) {
                statusValue = value.split("-")[0];
                break;
            }
        }
        return statusValue;
    }

    //初始化权重
    private Map<String,Double> initQzMap() {
        Map<String,Double> qzMap = new HashMap<>();
        qzMap.put("xnzb",40.00);//性能指标
        qzMap.put("gzl",20.00);//故障率
        qzMap.put("whcb",20.00);//维护成本
        qzMap.put("synx",20.00);//使用年限
        //获取权重及分值信息
        List<Map<String,Object>> weightList = equipmentInfoService.getScreenList(qz_screen_type);
        for (Map<String,Object> map : weightList){
            if("xnzb".equals(map.get("code"))){
                qzMap.put("xnzb",Double.valueOf(map.get("value").toString()));
            }
            if("gzl".equals(map.get("code"))){
                qzMap.put("gzl",Double.valueOf(map.get("value").toString()));
            }
            if("whcb".equals(map.get("code"))){
                qzMap.put("whcb",Double.valueOf(map.get("value").toString()));
            }
            if("synx".equals(map.get("code"))){
                qzMap.put("synx",Double.valueOf(map.get("value").toString()));
            }
        }
        return qzMap;
    }

    //初始化状态字典
    private Map<String,String> initStatusMap() {
        Map<String,String> statusValueMap = new HashMap<>();
        statusValueMap.put("90-100","wdyx-稳定运行");//稳定运行
        statusValueMap.put("60-90","qwth-轻微退化");//轻微退化
        statusValueMap.put("40-60","mxxj-明显下降");//明显下降
        statusValueMap.put("0-40","jjbf-接近报废");//接近报废
        List<Map<String,Object>> statusList = equipmentInfoService.getScreenList(status_screen_type);
        for (Map<String,Object> map : statusList){
            if("wdyx".equals(map.get("code"))){
                statusValueMap.put(map.get("value").toString(),"wdyx-"+map.get("name").toString());//稳定运行
            }
            if("qwth".equals(map.get("code"))){
                statusValueMap.put(map.get("value").toString(),"qwth-"+map.get("name").toString());//轻微退化
            }
            if("mxxj".equals(map.get("code"))){
                statusValueMap.put(map.get("value").toString(),"mxxj-"+map.get("name").toString());//明显下降
            }
            if("jjbf".equals(map.get("code"))){
                statusValueMap.put(map.get("value").toString(),"jjbf-"+map.get("name").toString());//接近报废
            }
        }
        return statusValueMap;
    }

    /**
     * 计算故障评分
     * @param gzCount 设备信息
     * @param qz 权重因子
     * @return 返回计算得到的故障评分
     */
    private double getFaultScore(String gzCount, double qz) {
        double score = 100.00;
        if(Double.valueOf(gzCount) >= 4){
            score = 0.00;
        }else{
            score = 100 - 100*Double.valueOf(gzCount)/4;
        }
        return DoubleOperaUtils.bigDecimalRound(qz * score,2);
    }

    /**
     * 计算维护得分
     * @param maintenanceCost   设备维护费用
     * @param originalValue     购置金额
     * @param qz                权重
     * @return 返回计算得到的维护得分
     */
    private double getMaintainScore(String maintenanceCost, String originalValue, double qz) {
        double score = 100.00;
        if(ConvertUtils.isNotEmpty(originalValue)){
            double gzfy = Double.valueOf(originalValue);
            double whfy = Double.valueOf(maintenanceCost);
            score = 100*(gzfy-whfy)/gzfy;
        }
        return DoubleOperaUtils.bigDecimalRound(qz * score,2);
    }

    /**
     * 计算使用寿命得分
     * @param workingLife 使用年限
     * @param installDate 安装日期
     * @param today       当前日期
     * @param qz          权重因子
     * @return 返回使用寿命得分,固定为100分的20%
     */
    private double getUseYearScore(String workingLife, String installDate, String today, double qz) {
        double score = 100.00;
        if(ConvertUtils.isNotEmpty(installDate)){
            if(ConvertUtils.isNotEmpty(workingLife)){
                double sjsy = Double.valueOf(DateUtils.dayDiff(installDate,today));
                double sm = 365*Double.valueOf(workingLife);
                score = 100 * (sm-sjsy)/sm;
            }
        }
        return DoubleOperaUtils.bigDecimalRound(qz * score,2);
    }

}