package com.sckw.order.repository;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sckw.core.model.base.BaseModel;
import com.sckw.order.dao.KwoTradeOrderUnitMapper;
import com.sckw.order.model.KwoTradeOrderUnit;
import org.springframework.stereotype.Repository;

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

/**
 * @author :chenXiaoFei
 * @version :1.0
 * @description : 贸易订单公司信息
 * @create :2025-11-07 10:57:00
 */
@Repository
public class KwoTradeOrderUnitRepository extends ServiceImpl<KwoTradeOrderUnitMapper, KwoTradeOrderUnit> {
    public List<KwoTradeOrderUnit> queryByOrderIds(Set<Long> tradeOrderIds) {
        return list(Wrappers.<KwoTradeOrderUnit>lambdaQuery()
                .eq(BaseModel::getDelFlag,0)
                .in(KwoTradeOrderUnit::getTOrderId, tradeOrderIds));
    }

    public List<KwoTradeOrderUnit> queryByOrderId(Long traderOrderId) {
        return list(Wrappers.<KwoTradeOrderUnit>lambdaQuery()
                .eq(BaseModel::getDelFlag,0)
                .eq(KwoTradeOrderUnit::getTOrderId, traderOrderId));
    }

    public List<KwoTradeOrderUnit> queryByEntIds(List<Object> entIds) {
        return list(Wrappers.<KwoTradeOrderUnit>lambdaQuery()
                .eq(BaseModel::getDelFlag,0)
                .in(KwoTradeOrderUnit::getEntId, entIds));
    }

    public List<KwoTradeOrderUnit> queryByReportEntIds(List<Long> entIds) {
        if (entIds == null || entIds.isEmpty()) {
            return Collections.emptyList();
        }
        return list(Wrappers.<KwoTradeOrderUnit>lambdaQuery()
                .select(KwoTradeOrderUnit::getTOrderId)
                .eq(BaseModel::getDelFlag, 0)
                .and(wrapper -> wrapper.in(KwoTradeOrderUnit::getEntId, entIds)
                        .or()
                        .in(KwoTradeOrderUnit::getTopEntId, entIds)));
    }

    public List<KwoTradeOrderUnit> queryByEntIdAndUnitType(Long entId, String unitType) {
        if (entId == null) {
            return Collections.emptyList();
        }
        return list(Wrappers.<KwoTradeOrderUnit>lambdaQuery()
                .eq(BaseModel::getDelFlag, 0)
                .eq(KwoTradeOrderUnit::getEntId, entId)
                .eq(KwoTradeOrderUnit::getUnitType, unitType));
    }

    public List<KwoTradeOrderUnit> queryByEntName(String entName) {
        return list(
                Wrappers.<KwoTradeOrderUnit>lambdaQuery()
                        .eq(BaseModel::getDelFlag, 0)
                        .eq(KwoTradeOrderUnit::getUnitType, 1)
                        .like(KwoTradeOrderUnit::getFirmName, entName)
        );
    }
}
