package com.muyu.enterprise.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.common.core.web.domain.BaseEntity;

import com.muyu.domain.Fault;
import com.muyu.enterprise.mapper.FaultMapper;
import com.muyu.enterprise.service.FaultService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 * @Author：ChenYan
 * @Project：2112-car-cloud-server
 * @Package：com.muyu.fault.service
 * @Filename：IFaultServiceImpl
 * @Description TODO
 * @Date：2024/9/18 11:14
 */
@Service
public class FaultServiceImpl extends ServiceImpl<FaultMapper, Fault> implements FaultService {


    @Autowired
    private FaultMapper faultMapper;

    /**
     *
     * @param fault 车辆故障管理主键
     * @return  fault
     */
    @Override
    public List<Fault> pageQuery(Fault fault) {
        LambdaQueryWrapper<Fault> queryWrapper = new LambdaQueryWrapper<>();
        if (StringUtils.isNotEmpty(fault.getFaultCode())) {
            queryWrapper.like(Fault::getFaultCode, fault.getFaultCode());
        }
        if (StringUtils.isNotEmpty(fault.getFaultType())) {
            queryWrapper.like(Fault::getFaultType, fault.getFaultType());
        }
        if (StringUtils.isNotEmpty(fault.getFaultName())) {
            queryWrapper.like(Fault::getFaultName, fault.getFaultName());
        }


        Object beginTime = fault.getParams().get("beginTime");
        if (Objects.nonNull(beginTime) && beginTime instanceof Date beginDate) {
            queryWrapper.gt(BaseEntity::getCreateTime, beginDate);
        }
        Object endTime = fault.getParams().get("endTime");
        if (Objects.nonNull(endTime) && endTime instanceof Date endDate) {
            queryWrapper.lt(BaseEntity::getCreateTime, endDate);
        }
        return this.list(queryWrapper);
    }

}
