package com.muyu.server.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.muyu.common.core.utils.StringUtils;
import com.muyu.domain.FaultCode;
import com.muyu.domain.FaultLabel;
import com.muyu.domain.req.FaultCodeAddReq;
import com.muyu.server.mapper.FaultLabelMapper;
import com.muyu.server.service.FaultLabelService;
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：FaultTypeServiceImpl
 * @Date：2024/9/18 21:00
 */
@Service
public class FaultLabelServiceImpl extends ServiceImpl<FaultLabelMapper, FaultLabel> implements FaultLabelService {
    @Autowired
    private FaultLabelMapper faultLabelMapper;


    /**
     * 故障信息查询
     * @return
     */
    @Override
    public List<FaultLabel> select() {
        LambdaQueryWrapper<FaultLabel> queryWrapper = new LambdaQueryWrapper<>();
        return this.list(queryWrapper);
    }

    /**
     * 判断故障名称是否存在
     * @param messageTypeName
     * @return
     */
    @Override
    public FaultLabel selectfaultName(String messageTypeName) {
        LambdaQueryWrapper<FaultLabel> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(StringUtils.isNotEmpty(messageTypeName),
                FaultLabel::getMessageTypeName, messageTypeName);
        List<FaultLabel> list = this.list(queryWrapper);
        if (!list.isEmpty()){
            return list.get(0);
        }
        return null;
    }

    /**
     * 添加故障项表
     * @param faultLabel
     * @return
     */
    @Override
    public Integer insertfaultlabel(FaultLabel faultLabel) {
       return this.save(faultLabel)?1:0;
    }

    /**
     * 删除对应的故障名称表信息
     * @param messageTypeId
     * @return
     */
    @Override
    public Integer delfaultlabel(Integer messageTypeId) {
        // 使用LambdaQueryWrapper来构建查询条件
        LambdaQueryWrapper<FaultLabel> queryWrapper = new LambdaQueryWrapper<>();
        // 假设FaultCode实体类中有一个getMessageTypeId方法
        queryWrapper.eq(FaultLabel::getMessageTypeId, messageTypeId);
        return faultLabelMapper.delete(queryWrapper);
    }

    /**
     * 查询报文
     * @param messageTypeId
     */
    @Override
    public FaultLabel faultLabelList(long messageTypeId) {
        List<FaultLabel> list = this.list(new LambdaQueryWrapper<FaultLabel>().eq(StringUtils.isNotNull(messageTypeId),
                FaultLabel::getMessageTypeId, messageTypeId)).stream().toList();
        FaultLabel faultLabel1 = new FaultLabel();
        if (!list.isEmpty()){
            list.forEach(faultLabel -> {
                faultLabel1.setMessageTypeId(faultLabel.getMessageTypeId());
                faultLabel1.setMessageTypeName(faultLabel.getMessageTypeName());
                faultLabel1.setMessageTypeCode(faultLabel.getMessageTypeCode());
                faultLabel1.setMessageTypeBelongs(faultLabel.getMessageTypeBelongs());
            });
        }
        return faultLabel1;
    }

}
