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 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 List<KwfTruck> getTurkNoList(List<Long> truckIds, String truckNo) {
        return list(Wrappers.<KwfTruck>lambdaQuery()
                .eq(BaseModel::getDelFlag, 0)
                .in(Objects.nonNull(truckIds), KwfTruck::getId, truckIds)
                .like(Objects.nonNull(truckNo), KwfTruck::getTruckNo, truckNo)
                .orderByDesc(BaseModel::getCreateTime));
    }


    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 IPage<KwfTruck> queryByCondition(List<Long> truckIds,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)
                .in(Objects.nonNull(truckIds),KwfTruck::getId, truckIds)
                .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));
    }

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

    public IPage<KwfTruck> queryByPage(int pageNum, int pageSize, List<Long> truckIds, Integer businessStatus, String truckNo) {
        return page(new Page<>(pageNum, pageSize), Wrappers.<KwfTruck>lambdaQuery()
                .eq(BaseModel::getDelFlag,0)
                .in(Objects.nonNull(truckIds),KwfTruck::getId, truckIds)
                .like(StringUtils.isNotBlank(truckNo),KwfTruck::getTruckNo, truckNo)
                .eq(Objects.nonNull(businessStatus),KwfTruck::getBusinessStatus, businessStatus)
                .orderByDesc(KwfTruck::getId));
    }

    public KwfTruck queryByTruckId(Long truckId) {
        return getOne(Wrappers.<KwfTruck>lambdaQuery()
                .eq(BaseModel::getDelFlag,0)
                .eq(KwfTruck::getId, truckId)
                .last("limit 1"));
    }

    public KwfTruck findTruckByTruckNo(String truckNo) {
        return getOne(Wrappers.<KwfTruck>lambdaQuery()
                .eq(BaseModel::getDelFlag,0)
                .eq(KwfTruck::getTruckNo, truckNo)
                .last("limit 1")
        );
    }

    public List<KwfTruck> findTruckByTruckNos(List<String> truckNos) {
        return list(Wrappers.<KwfTruck>lambdaQuery()
                .eq(BaseModel::getDelFlag,0)
                .in(KwfTruck::getTruckNo, truckNos)
                .orderByDesc(BaseModel::getId));
    }
}
