package com.muyu.server.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
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.domain.resp.FaultLogListResp;
import com.muyu.server.mapper.FaultConditionMapper;
import com.muyu.server.service.FaultConditionService;
import com.muyu.server.util.ObtainRootLogin;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
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 FaultConditionServiceImpl
        extends ServiceImpl<FaultConditionMapper, FaultCondition>
        implements FaultConditionService {

    @Autowired
    private FaultConditionMapper faultConditionMapper;



    /**
     * 故障规则
     * @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);
        return FaultConditionTotalListResp.faultConditionTotalListResp(faultConditionListResps,count);
    }


    /**
     * 故障规则添加  判断故障规则是否存在
     * @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 void saveFaultCondition(FaultCondition faultCondition) {

    }

    @Override
    public List<FaultCondition> saveFaultConditionList() {
        return this.list().stream().map(FaultCondition::faultConditionBuilder).toList();
    }

//    private void faultCache(long messageTypeId, Long faulttypeId,String faultcodeNumber) {
//        FaultCodeCache faultCodeCache = new FaultCodeCache();
//
//        //添加故障标签
//        faultCodeCache.getFaultLabels().addAll(faultLabelService.selectFaultCode(messageTypeId));
//
//        //添加车辆
//        List<CarInformation> carInformationList = carInformationService.selectCarInformation(faulttypeId);
//        faultCodeCache.getCarInformation().addAll(carInformationList);
//
//        //添加故障码
//        faultCodeCache.getFaultCode().addAll(this.list(new LambdaQueryWrapper<FaultCode>()
//                        .eq(FaultCode::getFaultcodeNumber, faultcodeNumber))
//                .stream().map(FaultCode::faultCodeBuilder)
//                .toList());
//
//        //添加围栏
//        ArrayList<CarFence> carFences1 = new ArrayList<>();
//        carInformationList.forEach(carInformation -> {
//            CarFence carFence = carFenceService.selectCarFenceList(carInformation.getCarInformationFence());
//            carFences1.add(carFence);
//        });
//        faultCodeCache.getCarFences().addAll(carFences1);
//        vehicleCacheFaultCodeAddService.put(vehicleCacheFaultCodeAddService.keyPre()
//                        + ObtainRootLogin.obtain()
//                , faultCodeCache);
//    }
}
