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.base.BaseModel;
import com.sckw.core.model.enums.CarWaybillV1Enum;
import com.sckw.transport.dao.KwtWaybillOrderSubtaskMapper;
import com.sckw.transport.model.KwtWaybillOrderSubtask;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Repository;

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

/**
 * @author PC
 */
@Repository
public class KwtWaybillOrderSubtaskRepository extends ServiceImpl<KwtWaybillOrderSubtaskMapper, KwtWaybillOrderSubtask> {
    public KwtWaybillOrderSubtask findOneByWOrderNo(String wOrderNo) {
       return getOne(Wrappers.<KwtWaybillOrderSubtask>lambdaQuery()
                .eq(BaseModel::getDelFlag,0)
                .eq(KwtWaybillOrderSubtask::getWOrderNo,wOrderNo)
                .orderByDesc(KwtWaybillOrderSubtask::getId)
                 .last("limit 1"));
    }

    public List<KwtWaybillOrderSubtask> findOneByWOrderIds(Set<Long> wOrderIds) {
        return list(Wrappers.<KwtWaybillOrderSubtask>lambdaQuery()
                .eq(BaseModel::getDelFlag,0)
                .in(KwtWaybillOrderSubtask::getWOrderId,wOrderIds));
    }

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

    public List<KwtWaybillOrderSubtask> queryByWOrderIds(List<Long> wOrderIds) {

        return list(Wrappers.<KwtWaybillOrderSubtask>lambdaQuery()
                .eq(BaseModel::getDelFlag,0)
                .in(KwtWaybillOrderSubtask::getWOrderId,wOrderIds));
    }

    public KwtWaybillOrderSubtask queryByWOrderId(Long wOrderId) {
        return getOne(Wrappers.<KwtWaybillOrderSubtask>lambdaQuery()
                .eq(KwtWaybillOrderSubtask::getWOrderId,wOrderId)
                .eq(BaseModel::getDelFlag,0));
    }

    public List<KwtWaybillOrderSubtask> queryByLogIds(List<Long> logIds) {
        return list(Wrappers.<KwtWaybillOrderSubtask>lambdaQuery()
                .eq(BaseModel::getDelFlag,0)
                .in(KwtWaybillOrderSubtask::getLOrderId,logIds));
    }

    public List<KwtWaybillOrderSubtask> queryValidByLogisticsOrderIds(List<Long> logIds) {
        if (CollectionUtils.isEmpty(logIds)) {
            return Collections.emptyList();
        }
        return list(Wrappers.<KwtWaybillOrderSubtask>lambdaQuery()
                .select(KwtWaybillOrderSubtask::getWOrderId, KwtWaybillOrderSubtask::getStatus, KwtWaybillOrderSubtask::getUpdateTime)
                .eq(BaseModel::getDelFlag, 0)
                .in(KwtWaybillOrderSubtask::getLOrderId, logIds)
                .ne(KwtWaybillOrderSubtask::getStatus, CarWaybillV1Enum.CANCELLED.getCode()));
    }

    public List<KwtWaybillOrderSubtask> queryByLogId(Long logOrderId) {
        return list(Wrappers.<KwtWaybillOrderSubtask>lambdaQuery()
                .eq(BaseModel::getDelFlag,0)
                .eq(KwtWaybillOrderSubtask::getLOrderId,logOrderId));
    }

    public IPage<KwtWaybillOrderSubtask> queryByPage( Set<Long> logOrderIds, Set<Long> billOrderIds,
                                                     Integer status,
                                                      List<Integer> waybillOrderStatus,
                                                     int pageNum,
                                                     int pageSize) {
        return page(new Page<>(pageNum, pageSize),
                Wrappers.<KwtWaybillOrderSubtask>lambdaQuery()
                        .eq(BaseModel::getDelFlag,0)
                        .eq(Objects.nonNull( status),KwtWaybillOrderSubtask::getStatus, status)
                        .notIn(CollectionUtils.isNotEmpty( waybillOrderStatus),KwtWaybillOrderSubtask::getStatus, waybillOrderStatus)
                        .in(CollectionUtils.isNotEmpty(logOrderIds),KwtWaybillOrderSubtask::getLOrderId, logOrderIds)
                        .in(CollectionUtils.isNotEmpty(billOrderIds),KwtWaybillOrderSubtask::getWOrderId, billOrderIds)
                        .orderByDesc(BaseModel::getUpdateTime)
                        .orderByDesc(KwtWaybillOrderSubtask::getId));
    }

    public KwtWaybillOrderSubtask queryByBillOrderId(Long id) {
        return getOne(Wrappers.<KwtWaybillOrderSubtask>lambdaQuery()
                .eq(KwtWaybillOrderSubtask::getWOrderId,id)
                .eq(BaseModel::getDelFlag,0)
                .last("limit 1"));
    }

    public List<KwtWaybillOrderSubtask> findByList(Integer status, Set<Long> billOrderIds, Set<Long> logOrderIds) {
        return list(Wrappers.<KwtWaybillOrderSubtask>lambdaQuery()
                .eq(BaseModel::getDelFlag,0)
                .eq(Objects.nonNull( status),KwtWaybillOrderSubtask::getStatus, status)
                .in(CollectionUtils.isNotEmpty(logOrderIds),KwtWaybillOrderSubtask::getLOrderId, logOrderIds)
                .in(CollectionUtils.isNotEmpty(billOrderIds),KwtWaybillOrderSubtask::getWOrderId, billOrderIds)
        );
    }

    public List<KwtWaybillOrderSubtask> queryWaybillOrderSubByEntIdAndStatus(Set<Long> logOrderIds, Integer status, LocalDateTime startTime, LocalDateTime endTime) {
        return list(Wrappers.<KwtWaybillOrderSubtask>lambdaQuery()
                .eq(KwtWaybillOrderSubtask::getDelFlag,0)
                .in(CollectionUtils.isNotEmpty(logOrderIds),KwtWaybillOrderSubtask::getLOrderId,logOrderIds)
                .eq(Objects.nonNull(status),KwtWaybillOrderSubtask::getStatus,status)
                .ge(Objects.nonNull(startTime), KwtWaybillOrderSubtask::getUpdateTime, startTime)
                .le(Objects.nonNull(endTime), KwtWaybillOrderSubtask::getUpdateTime, endTime)
                .orderByDesc(KwtWaybillOrderSubtask::getId));
    }
}
