package com.muyu.server.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import com.muyu.domain.*;
import com.muyu.domain.req.FaultConditionAddReq;
import com.muyu.domain.req.FaultConditionListReq;
import com.muyu.domain.resp.FaultConditionListResp;
import com.muyu.domain.resp.FaultConditionTotalListResp;
import com.muyu.enterprise.cache.faultCode.VehicleCacheFaultCodeAddService;
import com.muyu.server.mapper.FaultConditionMapper;
import com.muyu.server.service.*;
import lombok.AllArgsConstructor;
import org.jetbrains.annotations.NotNull;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

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
@AllArgsConstructor
public class FaultConditionServiceImpl
        extends ServiceImpl<FaultConditionMapper, FaultCondition>
        implements FaultConditionService {

    @Autowired
    private FaultConditionMapper faultConditionMapper;
    private final CarInformationService carInformationService;
    private final CarFenceService carFenceService;
    private final WarnStrategyService warnStrategyService;
    private final FaultCodeService faultCodeService;
    private final FaultLabelService faultLabelService;
    private final VehicleCacheFaultCodeAddService vehicleCacheFaultCodeAddService;



    /**
     * 故障规则
     * @param faultConditionListReq
     * @return
     */
    @Override
    public FaultConditionTotalListResp getfaultrulelist(FaultConditionListReq faultConditionListReq) {
        LambdaQueryWrapper<FaultCondition> queryWrapper = new LambdaQueryWrapper<>();
        long count = this.count(queryWrapper);
        MPJLambdaWrapper<FaultCondition> wrapper = new MPJLambdaWrapper<>();
        wrapper.select(FaultCondition::getCarconditionId,
                        FaultCondition::getCarTypeId,
                        FaultCondition::getMessageTypeId,
                        FaultCondition::getFaultconditionIdentification,
                        FaultCondition::getFaultconditionParameter)
                .select(CarType::getCarTypeName)
                .select(FaultLabel::getMessageTypeName,
                        FaultLabel::getMessageTypeCode)
                .leftJoin(CarType.class,CarType::getCarTypeId,FaultCondition::getCarTypeId)
                .leftJoin(FaultLabel.class,FaultLabel::getMessageTypeId,FaultCondition::getMessageTypeId);
        if (faultConditionListReq.getCarTypeId()!=0){
            wrapper.eq(CarType::getCarTypeId, faultConditionListReq.getCarTypeId());
        }
        if (faultConditionListReq.getMessageTypeId()!=0){
            wrapper.eq(FaultLabel::getMessageTypeId, faultConditionListReq.getMessageTypeId());
        }
        wrapper.last("LIMIT "+ ((faultConditionListReq.getPageNum()-1)*faultConditionListReq.getPageSize())+", "+faultConditionListReq.getPageSize());
        List<FaultConditionListResp> faultConditionListResps = faultConditionMapper.selectJoinList(FaultConditionListResp.class, wrapper);
        //添加故障车辆缓存
        this.CacheFault(faultConditionListResps);
        return FaultConditionTotalListResp.faultConditionTotalListResp(faultConditionListResps,count);
    }

    private void CacheFault(@NotNull List<FaultConditionListResp> faultConditionListResps) {
        faultConditionListResps.forEach(faultConditionListResp -> {
            List<CarInformation> list = carInformationService
                    .list(new LambdaQueryWrapper<CarInformation>()
                            .eq(CarInformation::getCarInformationType, faultConditionListResp.getCarTypeId()));
            list.forEach(carInformation -> {
                List<FaultCode> list1 = faultCodeService.list(new LambdaQueryWrapper<FaultCode>()
                        .eq(FaultCode::getFaultcodeId, faultConditionListResp.getMessageTypeId()));
                list1.forEach(faultCode -> {
                    FaultCodeCache faultCodeCache = new FaultCodeCache();
                    faultCodeCache.setCarInformation(carInformation);
                    faultCodeCache.setFaultCode(faultCode);
                    FaultLabel faultLabel = new FaultLabel();
                    faultLabel.setMessageTypeId(faultConditionListResp.getMessageTypeId());
                    faultLabel.setMessageTypeName(faultConditionListResp.getMessageTypeName());
                    faultLabel.setMessageTypeCode(faultConditionListResp.getMessageTypeCode());
                    faultCodeCache.setFaultLabels(faultLabel);
                    vehicleCacheFaultCodeAddService.put(vehicleCacheFaultCodeAddService
                            .keyPre()+carInformation
                            .getCarInformationVIN(),faultCodeCache);
                });
            });
        });
    }


    /**
     * 故障规则添加  判断故障规则是否存在
     * @param faultConditionAddReq
     * @return
     */
    @Override
    public List<FaultCondition> selectBytypeAndlabel(FaultConditionAddReq faultConditionAddReq) {

        LambdaQueryWrapper<FaultCondition> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(FaultCondition::getCarTypeId,faultConditionAddReq.getCarTypeId())
                .eq(FaultCondition::getMessageTypeId,faultConditionAddReq.getMessageTypeId());
        List<FaultCondition> list = this.list(queryWrapper);

        return list;
    }

    @Override
    public List<FaultCondition> saveFaultConditionList() {
        List<FaultCondition> list = this.list();
        return list;
    }
}
