package com.sckw.fleet.repository;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
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.KwfFleetMapper;
import com.sckw.fleet.model.KwfFleet;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Repository;

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


/**
 * @author PC
 * 车队接口
 */
@Repository
public class KwfFleetRepository extends ServiceImpl<KwfFleetMapper, KwfFleet> {

    public KwfFleet queryByEntIdAndName(Long entId,String fleetName) {
        return getOne(Wrappers.<KwfFleet>lambdaQuery()
                .eq(BaseModel::getDelFlag,0)
                .eq(Objects.nonNull(entId),KwfFleet::getEntId, entId)
                .like(StringUtils.isNotBlank(fleetName),KwfFleet::getName, fleetName)
                .orderByDesc(BaseModel::getId)
                .last("limit 1"));
    }

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

    public IPage<KwfFleet> pageFleet(int pageNum, int pageSize, String truckNo) {
        Page<KwfFleet> page = new Page<>(pageNum, pageSize);

        return page(page,Wrappers.<KwfFleet>lambdaQuery()
                .eq(BaseModel::getDelFlag,0));
    }

    public List<KwfFleet> findByfleetIds(Set<Long> fleetIdList) {
        return list(Wrappers.<KwfFleet>lambdaQuery()
                .eq(BaseModel::getDelFlag,0)
                .in(CollectionUtils.isNotEmpty(fleetIdList),KwfFleet::getId, fleetIdList));
    }

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

    public KwfFleet queryByFleetId(Long id) {
        return getOne(Wrappers.<KwfFleet>lambdaQuery()
                .eq(BaseModel::getDelFlag,0)
                .eq(KwfFleet::getId, id)
                .orderByDesc(BaseModel::getId)
                .last("limit 1"));
    }

    public IPage<KwfFleet> queryByPage(Set<Long> allEnt,Long fleetId, int pageNum, int pageSize) {
        return page(new Page<>(pageNum, pageSize),
                Wrappers.<KwfFleet>lambdaQuery()
                        .eq(BaseModel::getDelFlag,0)
                        .eq(Objects.nonNull(fleetId),KwfFleet::getId, fleetId)
                        .in(CollectionUtils.isNotEmpty(allEnt),KwfFleet::getEntId, allEnt)
                        .orderByDesc(BaseModel::getId));
    }
}
