package com.sckw.transport.repository;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sckw.transport.dao.KwtLogisticsOrderUnitMapper;
import com.sckw.transport.model.KwtLogisticsOrderUnit;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Repository;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Set;

/**
 * @author PC
 */
@Repository
public class KwtLogisticsOrderUnitRepository extends ServiceImpl<KwtLogisticsOrderUnitMapper, KwtLogisticsOrderUnit> {
    public KwtLogisticsOrderUnit queryByLOrderIdAndUnitType(Long lOrderId, int unitType) {
        return getOne(Wrappers.<KwtLogisticsOrderUnit>lambdaQuery()
                .eq(KwtLogisticsOrderUnit::getDelFlag,0)
                .eq(KwtLogisticsOrderUnit::getLOrderId,lOrderId)
                .eq(KwtLogisticsOrderUnit::getUnitType,unitType)
                .orderByDesc(KwtLogisticsOrderUnit::getId)
                .last("limit 1"));
    }

    public List<KwtLogisticsOrderUnit> queryByLOrderIdsAndUnitType(Set<Long> lOrderIds, int unitType) {
        return list(Wrappers.<KwtLogisticsOrderUnit>lambdaQuery()
                .eq(KwtLogisticsOrderUnit::getDelFlag,0)
                .in(KwtLogisticsOrderUnit::getLOrderId,lOrderIds)
                .eq(KwtLogisticsOrderUnit::getUnitType,unitType));
    }

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

    public List<KwtLogisticsOrderUnit> queryByEntIds(Set<Long> entIds) {
        return list(Wrappers.<KwtLogisticsOrderUnit>lambdaQuery()
                .eq(KwtLogisticsOrderUnit::getDelFlag,0)
                .in(KwtLogisticsOrderUnit::getEntId,entIds)
                .select(KwtLogisticsOrderUnit::getId,KwtLogisticsOrderUnit::getLOrderId,KwtLogisticsOrderUnit::getEntId));
    }

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

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

    /**
     * 根据物流订单ID列表查询
     */
    public List<KwtLogisticsOrderUnit> queryByLOrderIds(List<Long> lOrderIds) {
        return list(Wrappers.<KwtLogisticsOrderUnit>lambdaQuery()
                .eq(KwtLogisticsOrderUnit::getDelFlag, 0)
                .in(KwtLogisticsOrderUnit::getLOrderId, lOrderIds));
    }

    /**
     * 根据报表企业范围查询对应物流订单单位。
     * <p>
     * 该报表为免登录公开展示，企业范围由业务固定指定：
     * 查询单位企业在指定企业集合内，或顶级企业在指定企业集合内的物流订单。
     * </p>
     *
     * @param entIds 报表企业ID集合
     * @return 报表企业范围内的物流订单单位集合
     */
    public List<KwtLogisticsOrderUnit> queryByReportEntIds(List<Long> entIds) {
        if (CollectionUtils.isEmpty(entIds)) {
            return Collections.emptyList();
        }
        return list(Wrappers.<KwtLogisticsOrderUnit>lambdaQuery()
                .select(KwtLogisticsOrderUnit::getId, KwtLogisticsOrderUnit::getLOrderId)
                .eq(KwtLogisticsOrderUnit::getDelFlag, 0)
                .and(wrapper -> wrapper.in(KwtLogisticsOrderUnit::getEntId, entIds)
                        .or()
                        .in(KwtLogisticsOrderUnit::getTopEntId, entIds)));
    }

    public List<KwtLogisticsOrderUnit> queryListByLOrderId(Long lOrderId) {
        return list(Wrappers.<KwtLogisticsOrderUnit>lambdaQuery()
                        .eq(KwtLogisticsOrderUnit::getDelFlag, 0)
                        .eq(KwtLogisticsOrderUnit::getLOrderId, lOrderId)
                        .in(KwtLogisticsOrderUnit::getUnitType, 1, 2)
                        .orderByDesc(KwtLogisticsOrderUnit::getCreateTime)
        );
    }
}
