package com.muyu.server.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import com.muyu.common.core.utils.StringUtils;
import com.muyu.domain.CarInformation;
import com.muyu.domain.FaultCode;
import com.muyu.domain.FaultLog;
import com.muyu.domain.req.FaultLogListReq;
import com.muyu.domain.resp.FaultLogListResp;
import com.muyu.domain.resp.FaultLogTotalListResp;
import com.muyu.server.mapper.FaultLogMapper;
import com.muyu.server.service.FaultLogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
/**
 * 故障日志业务实现层
 * @Author：weiran
 * @Package：com.muyu.cloud.faultmanage.service.impl
 * @Project：cloud-server-8
 * @name：FaultLogServiceImpl
 * @Date：2024/9/20 9:34
 */
@Service
public class FaultLogServiceImpl extends ServiceImpl<FaultLogMapper, FaultLog> implements FaultLogService {
    @Autowired
    private FaultLogMapper faultLogMapper;

    /**
     * 故障日志列表
     * @param faultLogListReq
     * @return
     */
    @Override
    public FaultLogTotalListResp selectfaultlog(FaultLogListReq faultLogListReq) {

        MPJLambdaWrapper<FaultLog> wrapper = new MPJLambdaWrapper<>();
        wrapper.selectAll(FaultLog.class)
                .select(FaultCode::getFaultcodeNumber)
                .select(CarInformation::getCarInformationVin)
                .leftJoin(FaultCode.class,FaultCode::getFaultcodeId,FaultLog::getFaultcodeId)
                .leftJoin(CarInformation.class,CarInformation::getCarInformationId,FaultLog::getCarInformationId)
                .eq(StringUtils.isNotEmpty(faultLogListReq.getCarVin()),
                        FaultLog::getCarVin,faultLogListReq.getCarVin());
                if (faultLogListReq.getStartwarningTime()!=null){
                    wrapper.eq(FaultLog::getStartwarningTime,faultLogListReq.getStartwarningTime());
                }
                if (faultLogListReq.getEndwarningTime()!=null){
                    wrapper.eq(FaultLog::getEndwarningTime,faultLogListReq.getEndwarningTime());
                }
        Page<FaultLogListResp> ipage = faultLogMapper.selectJoinPage(new Page<>(faultLogListReq.getPageNum(),
                faultLogListReq.getPageSize()), FaultLogListResp.class, wrapper);
        return FaultLogTotalListResp.faultLogTotalListResp(ipage.getRecords(),ipage.getTotal());
    }
}
