package com.sckw.transport.repository;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sckw.core.model.enums.CarWaybillV1Enum;
import com.sckw.transport.dao.KwtWaybillOrderNodeMapper;
import com.sckw.transport.model.KwtWaybillOrderNode;
import org.springframework.stereotype.Repository;

import java.util.Date;
import java.util.List;
import java.util.Set;

/**
 * @author PC
 */
@Repository
public class KwtWaybillOrderNodeRepository extends ServiceImpl<KwtWaybillOrderNodeMapper, KwtWaybillOrderNode> {

    /**
     * 根据运单ID查询节点轨迹列表
     *
     * @param wOrderId   运单ID
     * @param wSubtaskId 运单子单ID（可选）
     * @return 节点轨迹列表，按创建时间正序排列
     */
    public List<KwtWaybillOrderNode> queryNodesByOrderId(Long wOrderId, Long wSubtaskId) {
        LambdaQueryWrapper<KwtWaybillOrderNode> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(KwtWaybillOrderNode::getWOrderId, wOrderId);
        if (wSubtaskId != null) {
            wrapper.eq(KwtWaybillOrderNode::getWSubtaskId, wSubtaskId);
        }
        wrapper.orderByDesc(KwtWaybillOrderNode::getCreateTime);
        wrapper.orderByDesc(KwtWaybillOrderNode::getId);
        return this.list(wrapper);
    }

    /**
     * 通过运单id查询运单节点数据
     * @param wOrderId
     * @return
     */
    public KwtWaybillOrderNode queryNodesByOrderId(Long wOrderId, Integer status) {
        return getOne(Wrappers.<KwtWaybillOrderNode>lambdaQuery()
                .eq(KwtWaybillOrderNode::getWOrderId, wOrderId)
                .eq(KwtWaybillOrderNode::getOrderStatus, status)
                .orderByDesc(KwtWaybillOrderNode::getCreateTime)
                .orderByDesc(KwtWaybillOrderNode::getId)
                .last("limit 1"));
    }

    /**
     * 查询司机的运单节点数据
     * @param wOrderIds
     * @return
     */
    public List<KwtWaybillOrderNode> queryContinuousNodesByDriverId(List<Long> wOrderIds, Long driverId, Integer status) {
        return list(Wrappers.<KwtWaybillOrderNode>lambdaQuery()
                .in(KwtWaybillOrderNode::getWOrderId, wOrderIds)
                .eq(KwtWaybillOrderNode::getDriverId, driverId)
                .eq(KwtWaybillOrderNode::getOrderStatus, status)
                .orderByAsc(KwtWaybillOrderNode::getCreateTime));
    }

    /**
     * 通过运单id查询运单节点数据
     * @param wOrderIds
     * @return
     */
    public List<KwtWaybillOrderNode> queryNodesByOrderIds(List<Long> wOrderIds, Integer status) {
        return list(Wrappers.<KwtWaybillOrderNode>lambdaQuery()
                .in(KwtWaybillOrderNode::getWOrderId, wOrderIds)
                .eq(KwtWaybillOrderNode::getOrderStatus, status)
                .orderByDesc(KwtWaybillOrderNode::getCreateTime));
    }


    public List<KwtWaybillOrderNode> queryNodes(Set<Long> wayBillOrderIds, List<Integer> statusList) {
       return list(Wrappers.<KwtWaybillOrderNode>lambdaQuery()
                .in(KwtWaybillOrderNode::getWOrderId, wayBillOrderIds)
                .in(KwtWaybillOrderNode::getOrderStatus,statusList )
                .orderByAsc(KwtWaybillOrderNode::getCreateTime));
    }

    public List<KwtWaybillOrderNode> queryByOperateTime(Date startDate, Date endDate,Integer status) {
        return list(Wrappers.<KwtWaybillOrderNode>lambdaQuery()
                .eq(KwtWaybillOrderNode::getOrderStatus, status)
                .between(KwtWaybillOrderNode::getCreateTime, startDate, endDate)
                .orderByAsc(KwtWaybillOrderNode::getCreateTime));
    }

    /**
     * 查询司机的运单节点数据
     * @param nodeIds
     * @return
     */
    public List<KwtWaybillOrderNode> queryContinuousNodesByIds(List<Long> nodeIds) {
        return list(Wrappers.<KwtWaybillOrderNode>lambdaQuery()
                .in(KwtWaybillOrderNode::getId, nodeIds)
                .orderByAsc(KwtWaybillOrderNode::getCreateTime));
    }

}
