package com.sckw.transport.repository;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
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.enums.CarWaybillEnum;
import com.sckw.transport.dao.KwtWaybillOrderMapper;
import com.sckw.transport.model.KwtWaybillOrder;
import org.springframework.stereotype.Repository;

import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;
import java.util.Objects;
import java.util.Set;

/**
 * @Author: 马超伟
 * @CreateTime: 2025-10-12
 * @Description:
 * @Version: 1.0
 */

@Repository
public class KwtWaybillOrderRepository extends ServiceImpl<KwtWaybillOrderMapper, KwtWaybillOrder> {
    public Page<KwtWaybillOrder> findPage(String truckNo,int pageNum, int pageSize, Date beforeDate, Date date) {
        Page<KwtWaybillOrder> page = new Page<>(pageNum, pageSize);
        LambdaQueryWrapper<KwtWaybillOrder> wrapper = Wrappers.<KwtWaybillOrder>lambdaQuery()
                .ge(KwtWaybillOrder::getCreateTime, beforeDate)
                .le(KwtWaybillOrder::getCreateTime, date)
                .eq(KwtWaybillOrder::getDelFlag, 0)
                .eq(StringUtils.isNotBlank(truckNo),KwtWaybillOrder::getTruckNo, truckNo)
                .orderByDesc(KwtWaybillOrder::getCreateTime);
        return page(page,wrapper);
    }

    public List<KwtWaybillOrder> findAll() {
        LambdaQueryWrapper<KwtWaybillOrder> wrapper = Wrappers.<KwtWaybillOrder>lambdaQuery()
                .eq(KwtWaybillOrder::getDelFlag, 0)
                .orderByDesc(KwtWaybillOrder::getCreateTime)
                .select(KwtWaybillOrder::getId,
                        KwtWaybillOrder::getStatus,
                        KwtWaybillOrder::getTruckNo);
        return list(wrapper);
    }


    public KwtWaybillOrder findOneByDriverId(Long driverId) {
        //获取任务中的状态
        List<Integer> tasking = CarWaybillEnum.getTasking();
        return getOne(Wrappers.<KwtWaybillOrder>lambdaQuery()
                .eq(KwtWaybillOrder::getDriverId, driverId)
                .eq(KwtWaybillOrder::getDelFlag, 0)
                .in(KwtWaybillOrder::getStatus, tasking)
                .orderByDesc(KwtWaybillOrder::getId)
                .last("limit 1"));
    }

    public KwtWaybillOrder findOneByTruckNo(String truckNo, Long entId ) {
        return getOne(Wrappers.<KwtWaybillOrder>lambdaQuery()
                .eq(KwtWaybillOrder::getDelFlag,0)
                .eq(KwtWaybillOrder::getTruckNo,truckNo)
                .eq(KwtWaybillOrder::getEntId, entId)
                .orderByDesc(KwtWaybillOrder::getId)
                .last("limit 1"));
    }

    public List<KwtWaybillOrder> queryByCreateTime(LocalDateTime startMonth,Long entId) {
        return list(Wrappers.<KwtWaybillOrder>lambdaQuery()
                .ge(KwtWaybillOrder::getCreateTime, startMonth)
                .eq(Objects.nonNull(entId),KwtWaybillOrder::getEntId, entId)
                .eq(KwtWaybillOrder::getDelFlag, 0));
    }

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

    public KwtWaybillOrder findOneByTruckId(String truckId) {
        return getOne(Wrappers.<KwtWaybillOrder>lambdaQuery()
                .eq(KwtWaybillOrder::getTruckId,truckId)
                .eq(KwtWaybillOrder::getDelFlag,0)
                .orderByDesc(KwtWaybillOrder::getId)
                .last("limit 1"));
    }

    public KwtWaybillOrder findOneByWOrderNo(String wOrderNo) {
        return getOne(Wrappers.<KwtWaybillOrder>lambdaQuery()
                .eq(KwtWaybillOrder::getWOrderNo,wOrderNo)
                .eq(KwtWaybillOrder::getDelFlag,0)
                .orderByDesc(KwtWaybillOrder::getId)
                .last("limit 1"));
    }

    public List<KwtWaybillOrder> queryWaybillOrderByEntId(Long entId) {
        return list(Wrappers.<KwtWaybillOrder>lambdaQuery()
                .eq(KwtWaybillOrder::getDelFlag,0)
                .eq(Objects.nonNull(entId),KwtWaybillOrder::getEntId,entId)
                .orderByDesc(KwtWaybillOrder::getId));
    }

    public IPage<KwtWaybillOrder> queryPageByBillOrderIds(Set<Long> billOrderIds, int pageNum, int pageSize) {
        return page(new Page<>(pageNum, pageSize),
                Wrappers.<KwtWaybillOrder>lambdaQuery()
                        .eq(KwtWaybillOrder::getDelFlag,0)
                        .in(KwtWaybillOrder::getId, billOrderIds)
                        .orderByDesc(KwtWaybillOrder::getCreateTime)
                        .orderByDesc(KwtWaybillOrder::getId));
    }

    public List<KwtWaybillOrder> queryByCondition(KwtWaybillOrder order) {
        return list(Wrappers.<KwtWaybillOrder>lambdaQuery()
                .eq(KwtWaybillOrder::getDelFlag,0)
                .like(StringUtils.isNotBlank(order.getWOrderNo()),KwtWaybillOrder::getWOrderNo, order.getWOrderNo())
                .like(StringUtils.isNotBlank(order.getTruckNo()),KwtWaybillOrder::getTruckNo, order.getTruckNo())
                .like(StringUtils.isNotBlank(order.getDriverName()),KwtWaybillOrder::getDriverName, order.getDriverName())
                .like(StringUtils.isNotBlank(order.getDriverPhone()),KwtWaybillOrder::getDriverPhone, order.getDriverPhone())
        );
    }

    public List<KwtWaybillOrder> queryByBillOrderIds(Set<Long> wayBillOrderIds) {

        return list(Wrappers.<KwtWaybillOrder>lambdaQuery()
                .eq(KwtWaybillOrder::getDelFlag,0)
                .in(KwtWaybillOrder::getId, wayBillOrderIds)
                .orderByDesc(KwtWaybillOrder::getUpdateTime)
                .orderByDesc(KwtWaybillOrder::getId)
        );
    }

    public List<KwtWaybillOrder> queryByBillOrderIds(Set<Long> billOrderIds, Long entId) {
        return list(Wrappers.<KwtWaybillOrder>lambdaQuery()
                .eq(KwtWaybillOrder::getDelFlag,0)
                .eq(KwtWaybillOrder::getEntId,entId)
                .in(KwtWaybillOrder::getId, billOrderIds)
                .orderByDesc(KwtWaybillOrder::getUpdateTime)
        );
    }

    public KwtWaybillOrder queryByBillOrderId(Long orderId) {
        return getOne(Wrappers.<KwtWaybillOrder>lambdaQuery()
                .eq(KwtWaybillOrder::getDelFlag,0)
                .eq(KwtWaybillOrder::getId,orderId)
                .last("limit 1")
        );
    }

    public List<KwtWaybillOrder> findWbOrderByTruckNoAndStatus(String truckNo,List<Integer> status) {
        return list(Wrappers.<KwtWaybillOrder>lambdaQuery()
                .eq(KwtWaybillOrder::getDelFlag,0)
                .eq(KwtWaybillOrder::getTruckNo,truckNo)
                .in(KwtWaybillOrder::getStatus,status)
                .orderByDesc(KwtWaybillOrder::getId));
    }

    public List<KwtWaybillOrder> findList(Set<Long> allEnt, Integer status, Set<Long> billOrderIds,Set<Long> logOrderIds) {
        return list(Wrappers.<KwtWaybillOrder>lambdaQuery()
                .eq(KwtWaybillOrder::getDelFlag,0)
                .in(CollectionUtils.isNotEmpty(allEnt),KwtWaybillOrder::getEntId,allEnt)
                .eq(Objects.nonNull( status),KwtWaybillOrder::getStatus,status)
                .in(CollectionUtils.isNotEmpty(billOrderIds),KwtWaybillOrder::getId,billOrderIds)
                .in(CollectionUtils.isNotEmpty(logOrderIds),KwtWaybillOrder::getId,logOrderIds)
                .orderByDesc(KwtWaybillOrder::getId));
    }
}
