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.CarType;
import com.muyu.domain.FaultCondition;
import com.muyu.domain.FaultLabel;
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.server.mapper.FaultConditionMapper;
import com.muyu.server.service.FaultConditionService;
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
public class FaultConditionServiceImpl extends ServiceImpl<FaultConditionMapper, FaultCondition> implements FaultConditionService {

    @Autowired
    private FaultConditionMapper faultConditionMapper;



    /**
     * 故障规则
     * @param faultConditionListReq
     * @return
     */
    @Override
    public FaultConditionTotalListResp getfaultrulelist(FaultConditionListReq faultConditionListReq) {
        MPJLambdaWrapper<FaultCondition> wrapper = new MPJLambdaWrapper<>();
        wrapper.selectAll(FaultCondition.class)
                .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());
        }
        Page<FaultConditionListResp> page = faultConditionMapper.selectJoinPage(new Page<>(faultConditionListReq.getPageNum(), faultConditionListReq.getPageSize()),
                FaultConditionListResp.class, wrapper);
        return FaultConditionTotalListResp.faultConditionTotalListResp(page.getRecords(),page.getTotal());
    }


    /**
     * 故障规则添加  判断故障规则是否存在
     * @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;
    }
}
