package com.muyu.enterpise.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.muyu.domain.FaultRule;
import com.muyu.enterpise.mapper.FaultRuleMapper;
import com.muyu.enterpise.service.FaultRuleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 故障检测
 *  * @className: FaultLog  ️✈️
 *  * @author: Yang 鹏  🦅
 *  * @date: 2024/9/23 21:06 ⏰
 *  * @Version: 1.0
 *  * @description: 故障检测
 */
@Service
public class FaultRuleServiceImpl extends ServiceImpl<FaultRuleMapper, FaultRule> implements FaultRuleService {

    @Autowired
    private FaultRuleMapper faultRuleMapper;

    /**
     * 车辆故障检测
     * @return
     */
    @Override
    public String checkfaults(FaultRule faultRule) {
        //获取触发条件
        LambdaQueryWrapper<FaultRule> queryWrapper = new LambdaQueryWrapper<>();
        if (Long.valueOf(faultRule.getFaultcodeId())!=null && faultRule.getFaultcodeId()!=0){
            queryWrapper.eq(FaultRule::getFaultcodeId, faultRule.getFaultcodeId());
        }
        List<FaultRule> faultRuleList = this.list(queryWrapper);
        for (FaultRule rule : faultRuleList) {
            //单个值比较
            if (faultRule.getSingleThreshold()!=null && faultRule.getMaxThreshold()==null && faultRule.getMinThreshold()==null){
                //大于阈值
                if (faultRule.getConditionContent().contains(">") || faultRule.getConditionContent().contains("大于")){
                    if (faultRule.getThreshold().compareTo(rule.getSingleThreshold())>0){
                        return "数据超过阈值，出现异常";
                    }
                }
                //小于阈值
                if (faultRule.getConditionContent().contains("<") || faultRule.getConditionContent().contains("小于")){
                    if (faultRule.getThreshold().compareTo(rule.getSingleThreshold())<0){
                        return "数据过低，出现异常";
                    }
                }
            }else { //区间值比较
                if (faultRule.getThreshold().compareTo(rule.getMinThreshold())<0 || faultRule.getThreshold().compareTo(rule.getMaxThreshold())>0){
                    return "数据不在可控范围内，出现异常";
                }
            }
        }
        return "判断出现异常";
    }


}
