package com.muyu.breakdown.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.muyu.breakdown.domain.FaultLog;
import com.muyu.breakdown.mapper.FaultLogMapper;
import com.muyu.breakdown.service.IFaultLogService;
import com.muyu.common.core.utils.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import java.util.List;

/**
 * 故障日志Service业务层处理
 *
 * @author muyu
 * @date 2024-09-20
 */
@Service
public class FaultLogServiceImpl
        extends ServiceImpl<FaultLogMapper, FaultLog>
        implements IFaultLogService {

    /**
     * 精确查询故障日志
     *
     * @param id 故障日志主键
     * @return 故障日志
     */
    @Override
    public FaultLog selectFaultLogById(Long id)
    {
        LambdaQueryWrapper<FaultLog> queryWrapper = new LambdaQueryWrapper<>();
        Assert.notNull(id, "id不可为空");
        queryWrapper.eq(FaultLog::getId, id);
        return this.getOne(queryWrapper);
    }


    /**
     * 查询故障日志列表
     *
     * @param faultLog 故障日志
     * @return 故障日志
     */
    @Override
    public List<FaultLog> selectFaultLogList(FaultLog faultLog)
    {
        LambdaQueryWrapper<FaultLog> queryWrapper = new LambdaQueryWrapper<>();
        if (StringUtils.isNotEmpty(faultLog.getFaultCode())){
            queryWrapper.eq(FaultLog::getFaultCode, faultLog.getFaultCode());
        }
        if (StringUtils.isNotEmpty(faultLog.getCarVin())){
            queryWrapper.eq(FaultLog::getCarVin, faultLog.getCarVin());
        }
        if (faultLog.getStartTime()!=null){
            queryWrapper.eq(FaultLog::getStartTime, faultLog.getStartTime());
        }
        if (faultLog.getEndTime()!=null){
            queryWrapper.eq(FaultLog::getEndTime, faultLog.getEndTime());
        }
        return this.list(queryWrapper);
    }

    /**
    * 唯一 判断
    * @param faultLog 故障日志
     * @return 故障日志
    */
    @Override
    public Boolean checkIdUnique(FaultLog faultLog) {
        LambdaQueryWrapper<FaultLog> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(FaultLog::getId, faultLog.getId());
        return this.count(queryWrapper) > 0;
    }

}
