package com.muyu.server.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.yulichang.query.MPJLambdaQueryWrapper;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import com.muyu.common.core.utils.StringUtils;
import com.muyu.domain.*;
import com.muyu.domain.resp.FaultConditionResp;
import com.muyu.server.mapper.CarFenceMapper;
import com.muyu.server.mapper.CarInformationMapper;
import com.muyu.server.mapper.FaultConditionMapper;
import com.muyu.server.mapper.FaultRuleMapper;
import com.muyu.server.service.CarInformationService;
import com.muyu.server.service.FaultRuleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.lang.reflect.Field;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * 故障检测业务实现层
 * @Author：weiran
 * @Package：com.muyu.cloud.faultmanage.service.impl
 * @Project：cloud-server-8
 * @name：FaultRuleServiceImpl
 * @Date：2024/9/19 22:10
 */

@Service
public class FaultRuleServiceImpl extends ServiceImpl<FaultRuleMapper, CarFaultRule> implements FaultRuleService {

    @Autowired
    private CarInformationService carInformationService;
    @Autowired
    private CarInformationMapper carInformationMapper;
    @Autowired
    private FaultConditionMapper faultConditionMapper;

    /**
     * 故障参数匹配检查
     * @param carFaultRule
     * @return
     */
    @Override
    public List<Vehicle> checkfault(CarFaultRule carFaultRule) {
        List<Vehicle> vehicles = new ArrayList<>();
        //根据车辆VIN判断它属于什么类型的车辆
        List<CarInformation> carInformationList = carInformationMapper.selectList(
                new LambdaQueryWrapper<CarInformation>()
                .eq(CarInformation::getCarInformationVIN, carFaultRule.getVin()));
        //根据车辆类型，查询表获取对应的类型的故障规则
        Long carInformationType = null;
        for (CarInformation carInformation : carInformationList) {
            carInformationType= carInformation.getCarInformationType();
        }


        List<FaultConditionResp> faultConditionResps = faultConditionMapper.selectJoinList(FaultConditionResp.class, new MPJLambdaWrapper<FaultCondition>()
                .selectAll(FaultCondition.class)
                .select(CarType::getCarTypeName)
                .select(FaultLabel::getMessageTypeName)
                .select(FaultLabel::getMessageTypeCode)
                .leftJoin(CarType.class, CarType::getCarTypeId, FaultCondition::getCarTypeId)
                .leftJoin(FaultLabel.class, FaultLabel::getMessageTypeId, FaultCondition::getMessageTypeId)
                .eq(FaultCondition::getCarTypeId,carInformationType));
        //获取当前类的所有字段，不包括继承的
        Class<? extends CarFaultRule> carFaultRuleClass = carFaultRule.getClass();
        Field[] declaredFields = carFaultRuleClass.getDeclaredFields();
        for (Field declaredField : declaredFields) {
            //确保可以访问私有字段
            declaredField.setAccessible(true);
            try {
                //获取字段的值
                Object value = declaredField.get(carFaultRule);
                if (value != null && declaredField.get(carFaultRule)!=null) {
                    // 遍历faultConditionList，查找匹配的FaultCondition
                    for (FaultConditionResp faultCondition : faultConditionResps) {
                        if (faultCondition.getMessageTypeCode().equals(declaredField.getName())){
                            String faultconditionIdentification = faultCondition.getFaultconditionIdentification();
                            BigDecimal faultconditionParameter = faultCondition.getFaultconditionParameter();
                            // 尝试将值转换为BigDecimal
                            BigDecimal bigDecimalValue = null;
                            if (value instanceof Number) {
                                Number numberValue = (Number) value;
                                if (numberValue instanceof Long || numberValue instanceof Integer) {
                                    bigDecimalValue = BigDecimal.valueOf(numberValue.longValue());
                                } else if (numberValue instanceof Double || numberValue instanceof Float) {
                                    // 注意：Double转BigDecimal可能会引入精度问题，因为Double本身可能不精确
                                    // 如果需要精确控制，可以考虑从源头使用BigDecimal
                                    bigDecimalValue = BigDecimal.valueOf(numberValue.doubleValue());
                                }
                            }

                            //判断条件选择
                            if (faultconditionIdentification.equals(">") || faultconditionIdentification.equals("&gt;") ){
                                if (bigDecimalValue.compareTo(faultconditionParameter)>0){
                                   vehicles.add(new Vehicle(carFaultRule.getVin(),declaredField.getName()+"超出规定范围"));
                                }
                            }else if(faultconditionIdentification.equals("<") || faultconditionIdentification.equals("&lt;")){
                                if (bigDecimalValue.compareTo(faultconditionParameter)<0){
                                    vehicles.add(new Vehicle(carFaultRule.getVin(),declaredField.getName()+"过低"));
                                }
                            }else if(faultconditionIdentification.equals(">=") || faultconditionIdentification.equals("&gt;=")){
                                if (bigDecimalValue.compareTo(faultconditionParameter)>=0){
                                    vehicles.add(new Vehicle(carFaultRule.getVin(),declaredField.getName()+"超出规定范围")) ;
                                }
                            }else if(faultconditionIdentification.equals("<=") || faultconditionIdentification.equals("&lt;=")){
                                if (bigDecimalValue.compareTo(faultconditionParameter)<=0){
                                    vehicles.add(new Vehicle(carFaultRule.getVin(),declaredField.getName()+"过低"));
                                }
                            }else {
                                if (bigDecimalValue.compareTo(faultconditionParameter)==0){
                                    vehicles.add(new Vehicle(carFaultRule.getVin(),declaredField.getName()+"已达到故障点"));
                                }
                            }
                        }
                    }
                }

            } catch (IllegalAccessException e) {
                e.printStackTrace();
            }

        }
        return vehicles;
    }
}
