package com.sckw.transport.repository;

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.core.model.enums.LogisticsOrderV1Enum;
import com.sckw.transport.dao.KwtLogisticsOrderMapper;
import com.sckw.transport.model.KwtLogisticsOrder;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
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 PC
 */
@Repository
public class KwtLogisticsOrderRepository extends ServiceImpl<KwtLogisticsOrderMapper, KwtLogisticsOrder> {
    public List<KwtLogisticsOrder> queryByCreateTime(LocalDateTime localDateTime) {
        return list(Wrappers.<KwtLogisticsOrder>lambdaQuery()
                .ge(KwtLogisticsOrder::getCreateTime, localDateTime)
                .eq(KwtLogisticsOrder::getDelFlag, 0));
    }

    public List<KwtLogisticsOrder> queryByTradeOrderIds(Set<Long> tradeOrderIds) {
        return list(Wrappers.<KwtLogisticsOrder>lambdaQuery()
                .eq(KwtLogisticsOrder::getDelFlag, 0)
                .in(KwtLogisticsOrder::getTOrderId, tradeOrderIds));
    }

    public List<KwtLogisticsOrder> queryByTradeOrderId(Long tradeId) {
        return list(Wrappers.<KwtLogisticsOrder>lambdaQuery()
                .eq(KwtLogisticsOrder::getDelFlag, 0)
                .eq(KwtLogisticsOrder::getTOrderId, tradeId));
    }

        public IPage<KwtLogisticsOrder> queryByPage(Set<Long> logisticsOrderIds, Long  tradeOrderId, String orderNo,
                                                    String tradeOrderNo,
                                                    String status,
                                                    Date startTime, Date endTime, int pageNum, int pageSize) {
        return page(new Page<>(pageNum, pageSize),
                Wrappers.<KwtLogisticsOrder>lambdaQuery()
                        .eq(KwtLogisticsOrder::getDelFlag, 0)
                        .in(CollectionUtils.isNotEmpty(logisticsOrderIds), KwtLogisticsOrder::getId, logisticsOrderIds)
                        .eq(Objects.nonNull(tradeOrderId),KwtLogisticsOrder::getTOrderId, tradeOrderId)
                        .eq(StringUtils.isNotBlank(status),KwtLogisticsOrder::getStatus, status)
                        .like(StringUtils.isNotBlank(orderNo),KwtLogisticsOrder::getLOrderNo, orderNo)
                        .like(StringUtils.isNotBlank(tradeOrderNo),KwtLogisticsOrder::getTOrderNo, tradeOrderNo)
                        .ge(Objects.nonNull(startTime),KwtLogisticsOrder::getCreateTime, startTime)
                        .le(Objects.nonNull(endTime),KwtLogisticsOrder::getCreateTime, endTime)
                        .orderByDesc(KwtLogisticsOrder::getUpdateTime)
                        .orderByDesc(KwtLogisticsOrder::getId)
        );
    }

    public KwtLogisticsOrder queryByLogisticsOrderId(Long logOrderId) {
        return getOne(Wrappers.<KwtLogisticsOrder>lambdaQuery()
                .eq(KwtLogisticsOrder::getDelFlag,0)
                .eq(KwtLogisticsOrder::getId, logOrderId));
    }

    public KwtLogisticsOrder queryByLogisticsOrderNo(String logisticsOrderNo) {
        return getOne(Wrappers.<KwtLogisticsOrder>lambdaQuery()
                .eq(KwtLogisticsOrder::getDelFlag,0)
                .like(KwtLogisticsOrder::getLOrderNo, logisticsOrderNo)
                .last("limit 1"));
    }

    public List<KwtLogisticsOrder> queryByLogisticsOrderIds(List<Long> logisOrderIds) {
        return list(Wrappers.<KwtLogisticsOrder>lambdaQuery()
                .eq(KwtLogisticsOrder::getDelFlag,0)
                .in(KwtLogisticsOrder::getId, logisOrderIds));
    }

    public List<KwtLogisticsOrder> queryByLogOrderIds(Set<Long> logOrderIds) {

        return list(Wrappers.<KwtLogisticsOrder>lambdaQuery()
                .eq(KwtLogisticsOrder::getDelFlag,0)
                .in(KwtLogisticsOrder::getId, logOrderIds));
    }

    public Boolean updateLogisticsOrder(KwtLogisticsOrder updateLogisticsOrder) {
       return updateById(updateLogisticsOrder);
    }

    public KwtLogisticsOrder queryByLogOrderIdAndStatus(Long logOrderId, Integer code) {
        return getOne(Wrappers.<KwtLogisticsOrder>lambdaQuery()
                .eq(KwtLogisticsOrder::getDelFlag,0)
                .eq(KwtLogisticsOrder::getId, logOrderId)
                .eq(KwtLogisticsOrder::getStatus, code));
    }

    public List<KwtLogisticsOrder> queryByEntIds(Long entId) {
        return list(Wrappers.<KwtLogisticsOrder>lambdaQuery()
                .eq(KwtLogisticsOrder::getDelFlag,0)
                .eq(KwtLogisticsOrder::getEntId, entId));
    }

    public List<KwtLogisticsOrder> queryByBillMethod(String billingMethod) {
        return list(Wrappers.<KwtLogisticsOrder>lambdaQuery()
                .eq(KwtLogisticsOrder::getDelFlag,0)
                .eq(KwtLogisticsOrder::getBillingMode, billingMethod));
    }

    public List<KwtLogisticsOrder> queryList( Set<Long> logOrderIds, Long tradeOrderId, String orderNo, String orderStatus, String startTime, String endTime) {
        return list(Wrappers.<KwtLogisticsOrder>lambdaQuery()
                .eq(KwtLogisticsOrder::getDelFlag,0)
                .in(CollectionUtils.isNotEmpty(logOrderIds), KwtLogisticsOrder::getId, logOrderIds)
                .eq(Objects.nonNull(tradeOrderId),KwtLogisticsOrder::getTOrderId, tradeOrderId)
                .eq(StringUtils.isNotBlank(orderStatus),KwtLogisticsOrder::getStatus, orderStatus)
                .ge(StringUtils.isNotBlank(startTime),KwtLogisticsOrder::getLoadTime, startTime)
                .le(StringUtils.isNotBlank(startTime),KwtLogisticsOrder::getUnloadTime, endTime)
                .like(StringUtils.isNotBlank(orderNo),KwtLogisticsOrder::getLOrderNo, orderNo)
        );
    }

    public KwtLogisticsOrder queryByLogisticOrderId(String lOrderNo) {
        return getOne(Wrappers.<KwtLogisticsOrder>lambdaQuery()
                .eq(KwtLogisticsOrder::getDelFlag,0)
                .eq(KwtLogisticsOrder::getLOrderNo, lOrderNo)
                .last("limit 1"));
    }

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

    public List<KwtLogisticsOrder> selectWaybillOrderOptions(Set<Long> logOrderIds, String logisticOrderNo) {
        return list(Wrappers.<KwtLogisticsOrder>lambdaQuery().eq(KwtLogisticsOrder::getDelFlag, 0)
                .in( KwtLogisticsOrder::getId, logOrderIds)
                .like(StringUtils.isNotBlank(logisticOrderNo), KwtLogisticsOrder::getLOrderNo, logisticOrderNo));
    }


    public List<KwtLogisticsOrder> queryByLogOrderIds(List<Long> logOrderIdList) {
        return list(Wrappers.<KwtLogisticsOrder>lambdaQuery()
                .in(KwtLogisticsOrder::getId, logOrderIdList)
                .eq(KwtLogisticsOrder::getDelFlag,0)
                .eq(KwtLogisticsOrder::getStatus, LogisticsOrderV1Enum.IN_TRANSIT.getCode())
                .orderByDesc(KwtLogisticsOrder::getCreateTime)
                .orderByDesc(KwtLogisticsOrder::getId));
    }

    public List<KwtLogisticsOrder> queryByLOrderIdsAndStatus(List<Long> logOrderIds) {
        return list(Wrappers.<KwtLogisticsOrder>lambdaQuery()
                .eq(KwtLogisticsOrder::getDelFlag,0)
                .in(KwtLogisticsOrder::getId, logOrderIds)
                .eq(KwtLogisticsOrder::getStatus, 10));
    }


    public List<KwtLogisticsOrder> queryTradeOrderNo(String tradeOrderNo) {
        return list(Wrappers.<KwtLogisticsOrder>lambdaQuery()
                .eq(KwtLogisticsOrder::getDelFlag,0)
                .like(KwtLogisticsOrder::getTOrderNo, tradeOrderNo));
    }

    /**
     * 根据贸易订单号精确查询物流订单。
     *
     * @param tradeOrderNo 贸易订单号
     * @return 物流订单列表
     */
    public List<KwtLogisticsOrder> queryByTradeOrderNo(String tradeOrderNo) {
        return list(Wrappers.<KwtLogisticsOrder>lambdaQuery()
                .eq(KwtLogisticsOrder::getDelFlag, 0)
                .eq(KwtLogisticsOrder::getTOrderNo, tradeOrderNo)
                .orderByDesc(KwtLogisticsOrder::getCreateTime)
                .orderByDesc(KwtLogisticsOrder::getId));
    }
}
