package com.muyu.server.service.impl;

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;

import java.util.List;

/**
 * 故障日志业务实现层
 * @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) {
        LambdaQueryWrapper<FaultLog> queryWrapper = new LambdaQueryWrapper<>();
        long count = this.count(queryWrapper);
        MPJLambdaWrapper<FaultLog> wrapper = new MPJLambdaWrapper<>();
        wrapper.select(FaultLog::getLogId,
                        FaultLog::getFaultcodeId,
                        FaultLog::getCarInformationId,
                        FaultLog::getCarVin,
                        FaultLog::getStartwarningTime,
                        FaultLog::getEndwarningTime)
                .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());
                }
                wrapper.last("LIMIT "+ ((faultLogListReq.getPageNum()-1)*faultLogListReq.getPageSize())+", "+faultLogListReq.getPageSize());
        List<FaultLogListResp> faultLogListResps = faultLogMapper.selectJoinList(FaultLogListResp.class, wrapper);
        return FaultLogTotalListResp.faultLogTotalListResp(faultLogListResps,count);
    }
}
