package com.sckw.fleet.repository;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
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.fleet.dao.KwfTruckRouteMapper;
import com.sckw.fleet.model.KwfTruckRoute;
import com.sckw.fleet.model.dto.KwfTruckRouteDto;
import jakarta.validation.constraints.NotNull;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Repository;

import java.util.Objects;

/**
 * @author PC
 */
@Repository
public class KwfTruckRouteRepository extends ServiceImpl<KwfTruckRouteMapper, KwfTruckRoute> {
    public IPage<KwfTruckRoute> pageTruckRoute(int pageNum, int pageSize, KwfTruckRouteDto kwfTruckRoute) {
        Page<KwfTruckRoute> truckRoutePage = new Page<>(pageNum, pageSize);
        return page(truckRoutePage, Wrappers.<KwfTruckRoute>lambdaQuery()
                .in(CollectionUtils.isNotEmpty(kwfTruckRoute.getFleetIds()),KwfTruckRoute::getFleetId, kwfTruckRoute.getFleetIds())
                .in(CollectionUtils.isNotEmpty(kwfTruckRoute.getDriverIds()),KwfTruckRoute::getDriverId, kwfTruckRoute.getDriverIds())
                .eq(KwfTruckRoute::getDelFlag,0)
                .eq(KwfTruckRoute::getEntId, kwfTruckRoute.getEntId())
                .like(StringUtils.isNotBlank(kwfTruckRoute.getTruckNo()),KwfTruckRoute::getTruckNo, kwfTruckRoute.getTruckNo())
                .like(StringUtils.isNotBlank(kwfTruckRoute.getStartPoint()),KwfTruckRoute::getStartPoint, kwfTruckRoute.getStartPoint())
                .like(StringUtils.isNotBlank(kwfTruckRoute.getUnloadingPoint()),KwfTruckRoute::getUnloadingPoint, kwfTruckRoute.getUnloadingPoint()));
    }

    public Boolean deleteTruckRoute( Long id) {
        KwfTruckRoute kwfTruckRoute = new KwfTruckRoute();
        kwfTruckRoute.setId(id);
        kwfTruckRoute.setDelFlag(1);

        return updateById(kwfTruckRoute);
    }


    public KwfTruckRoute findOneByDriverId(Long driverId) {
        return getOne(Wrappers.<KwfTruckRoute>lambdaQuery()
                .eq(KwfTruckRoute::getDriverId, driverId)
                .eq(KwfTruckRoute::getDelFlag,0));
    }

    public Boolean saveTruckRoute(KwfTruckRoute kwfTruckRoute) {
        return save(kwfTruckRoute);
    }

    public Boolean updateTruckRoute(KwfTruckRoute kwfTruckRoute) {
       return updateById(kwfTruckRoute);
    }

    public KwfTruckRoute findOneByDriverIdAndPoint(Long driverId, String startPoint, String unloadingPoint) {
        return getOne(Wrappers.<KwfTruckRoute>lambdaQuery()
                .eq(KwfTruckRoute::getDriverId, driverId)
                .eq(KwfTruckRoute::getStartPoint, startPoint)
                .eq(KwfTruckRoute::getUnloadingPoint, unloadingPoint)
                .eq(KwfTruckRoute::getDelFlag,0));
    }

    /**
     * 根据司机ID和装货卸货地址查询常用路线
     * @param driverId 司机ID
     * @return 常用路线信息
     */
    public KwfTruckRoute queryByDriverIdAndAddress(Long driverId,String startLng, String startLat, String endLng ,String endLat) {
        return getOne(Wrappers.<KwfTruckRoute>lambdaQuery()
                .eq(KwfTruckRoute::getDriverId, driverId)
                .eq(KwfTruckRoute::getLoadLng, startLng)
                .eq(KwfTruckRoute::getLoadLat, startLat)
                .eq(KwfTruckRoute::getUnloadLng, endLng)
                .eq(KwfTruckRoute::getUnloadLat, endLat)
                .eq(KwfTruckRoute::getDelFlag, 0)
                .last("limit 1"));
    }
}
