package com.sckw.fleet.repository;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sckw.core.model.base.BaseModel;
import com.sckw.fleet.dao.KwfTruckMapper;
import com.sckw.fleet.model.KwfTruck;
import com.sckw.fleet.model.KwfTruckRoute;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Objects;
import java.util.Set;


/**
 * @author PC
 * 车辆信息接口
 */
@Repository
public class KwfTruckRepository extends ServiceImpl<KwfTruckMapper, KwfTruck> {
    public KwfTruck getOneByEntId(Long entId) {
        return getOne(Wrappers.<KwfTruck>lambdaQuery()
                .eq(BaseModel::getDelFlag,0)
                .eq(Objects.nonNull(entId),KwfTruck::getEntId, entId)
                .orderByDesc(BaseModel::getCreateTime)
                .last("limit 1"));
    }

    public List<KwfTruck> getTurkNoList(Long entId) {
        return list(Wrappers.<KwfTruck>lambdaQuery()
                .eq(BaseModel::getDelFlag, 0)
                .eq(Objects.nonNull(entId), KwfTruck::getEntId, entId)
                .orderByDesc(BaseModel::getCreateTime));
    }



    public List<KwfTruck> findAll() {
        return list(Wrappers.<KwfTruck>lambdaQuery()
                .eq(BaseModel::getDelFlag,0));
    }

    public List<KwfTruck> queryByTruckIds(Set<Long> truckIds) {
        return list(Wrappers.<KwfTruck>lambdaQuery()
                .eq(BaseModel::getDelFlag,0)
                .in(KwfTruck::getId, truckIds));
    }

    public KwfTruck queryByTruckNo(String truckNo) {
       return getOne(Wrappers.<KwfTruck>lambdaQuery()
                .eq(BaseModel::getDelFlag,0)
                .eq(KwfTruck::getTruckNo, truckNo));

    }

    public List<KwfTruck> queryByEntIds(List<Long> entIds, String truckMark,String startTime, String endTime) {
        return list(Wrappers.<KwfTruck>lambdaQuery()
                .eq(BaseModel::getDelFlag,0)
                .in(CollectionUtils.isNotEmpty(entIds),KwfTruck::getEntId, entIds)
                .eq(StringUtils.isNotBlank(truckMark),KwfTruck::getTruckMark, truckMark)
                .ge(StringUtils.isNotBlank(startTime),KwfTruck::getCreateTime, startTime)
                .le(StringUtils.isNotBlank(endTime),KwfTruck::getCreateTime, endTime));
    }

    public IPage<KwfTruck> queryByCondition(Long entId,String truckNo, String truckMark, String startTime, String endTime, int pageNum, int pageSize) {
        Page<KwfTruck> page = new Page<>(pageNum, pageSize);
        return page(page, Wrappers.<KwfTruck>lambdaQuery()
                .eq(BaseModel::getDelFlag,0)
                .eq(Objects.nonNull(entId),KwfTruck::getEntId, entId)
                .eq(StringUtils.isNotBlank(truckNo),KwfTruck::getTruckNo, truckNo)
                .eq(StringUtils.isNotBlank(truckMark),KwfTruck::getTruckMark, truckMark)
                .ge(StringUtils.isNotBlank(startTime),KwfTruck::getCreateTime, startTime)
                .le(StringUtils.isNotBlank(endTime),KwfTruck::getCreateTime, endTime));
    }
}
