package com.sckw.order.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.order.dao.KwoTradeOrderMapper;
import com.sckw.order.model.KwoTradeOrder;
import jakarta.validation.constraints.NotBlank;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Repository;

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

/**
 * @author :chenXiaoFei
 * @version :1.0
 * @description : 贸易订单接口
 * @create :2025-11-07 10:14:00
 */
@Repository
public class KwoTradeOrderRepository extends ServiceImpl<KwoTradeOrderMapper, KwoTradeOrder> {
    public IPage<KwoTradeOrder> queryByPage(Set<Long> entIds,List<Long> orderIds, String orderNo, String status,
                                            String orderStartTime,
                                            String orderEndTime,
                                            int pageNum,
                                            int pageSize) {
        Page<KwoTradeOrder> orderPage = new Page<>(pageNum, pageSize);
        return page(orderPage, Wrappers.<KwoTradeOrder>lambdaQuery()
                .like(StringUtils.isNotBlank(orderNo),KwoTradeOrder::getTOrderNo, orderNo)
                .in(CollectionUtils.isNotEmpty(entIds),KwoTradeOrder::getEntId, entIds)
                .in(CollectionUtils.isNotEmpty(orderIds),KwoTradeOrder::getId, orderIds)
                .eq(KwoTradeOrder::getDelFlag, 0)
                .eq(StringUtils.isNotBlank(status),KwoTradeOrder::getStatus, status)
                .ge(StringUtils.isNotBlank(orderStartTime),KwoTradeOrder::getCreateTime, orderStartTime)
                .le(StringUtils.isNotBlank(orderEndTime),KwoTradeOrder::getCreateTime, orderEndTime));
    }

    public KwoTradeOrder queryByTradeOrderId(Long tradeOrderId) {
        return getOne(Wrappers.<KwoTradeOrder>lambdaQuery()
                .eq(KwoTradeOrder::getId, tradeOrderId)
                .eq(KwoTradeOrder::getDelFlag, 0));
    }

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