package com.muyu.server.util;

import com.muyu.common.core.utils.StringUtils;
import com.muyu.domain.*;
import com.muyu.enterprise.cache.faultCode.VehicleCacheFaultCodeAddService;
import com.muyu.server.service.*;
import lombok.AllArgsConstructor;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.stereotype.Component;

import java.util.AbstractList;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author：yang
 * @Package：com.muyu.server.util
 * @Project：cloud-server-8
 * @name：RunCarCondition
 * @Date：2024/10/7 19:43
 */
@Component
@AllArgsConstructor
public class RunCarCondition implements ApplicationRunner {

    private final VehicleCacheFaultCodeAddService vehicleCacheFaultCodeAddService;
    private final FaultConditionService faultConditionService;
    private final CarInformationService carInformationService;
    private final CarFenceService carFenceService;
    private final WarnStrategyService warnStrategyService;
    private final FaultCodeService faultCodeService;

    @Override
    public void run(ApplicationArguments args) throws Exception {
        List<FaultCondition> list = faultConditionService.saveFaultConditionList();
        list.forEach(faultCondition -> {
            List<CarInformation> carInformationList = carInformationService.selectCarInformation(faultCondition.getCarTypeId());
            carInformationList.forEach(carInformation -> {
                FaultCodeCache faultCodeCache = new FaultCodeCache();
                if (StringUtils.isNotNull(carInformation.getCarInformationFence())){
                    faultCodeCache.setCarFences(carFenceService.CarFenceList(carInformation.getCarInformationFence()));
                }
                if (StringUtils.isNotNull(carInformation.getCarInformationState())) {
                    faultCodeCache.setWarnStrategies(warnStrategyService
                            .selectWarnStrategyById(Long.valueOf(carInformation.getCarInformationState())));
                }
                if (StringUtils.isNotNull(faultCondition.getMessageTypeId())) {
                    faultCodeCache.setFaultCode(faultCodeService.faultCodeList(faultCondition.getMessageTypeId()));
                }
                ArrayList<CarInformation> carInformations = new ArrayList<>();
                carInformations.add(carInformation);
                faultCodeCache.setCarInformation(carInformations);
                vehicleCacheFaultCodeAddService.put(vehicleCacheFaultCodeAddService
                        .keyPre()+carInformation
                        .getCarInformationVIN(), faultCodeCache);
            });
        });
    }
}
