package com.sckw.contract.repository;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sckw.contract.dao.KwcContractTradeGoodsMapper;
import com.sckw.contract.model.entity.KwcContractTradeGoods;
import org.springframework.stereotype.Repository;

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

/**
 * @author :chenXiaoFei
 * @version :1.0
 * @description :贸易合同接口
 * @create :2025-11-04 09:52:00
 */
@Repository
public class KwcContractTradeGoodsRepository extends ServiceImpl<KwcContractTradeGoodsMapper, KwcContractTradeGoods> {
    public List<KwcContractTradeGoods> queryByConTractIdGoodsIds(Long pid,List<Long> goodsIds) {
        return list(Wrappers.<KwcContractTradeGoods>lambdaQuery()
                .eq(KwcContractTradeGoods::getDelFlag,0)
                .eq(KwcContractTradeGoods::getContractId, pid)
                .in(KwcContractTradeGoods::getGoodsId, goodsIds));
    }

    public List<KwcContractTradeGoods> queryByContractIds(Set<Long> contractIds) {
        return list(Wrappers.<KwcContractTradeGoods>lambdaQuery()
                .eq(KwcContractTradeGoods::getDelFlag,0)
                .in(KwcContractTradeGoods::getContractId, contractIds));
    }

    public List<KwcContractTradeGoods> queryByConTractIdsGoodsIds(Set<Long> pids,List<Long> goodsIds) {
        return list(Wrappers.<KwcContractTradeGoods>lambdaQuery()
                .eq(KwcContractTradeGoods::getDelFlag,0)
                .in(KwcContractTradeGoods::getContractId, pids)
                .in(KwcContractTradeGoods::getGoodsId, goodsIds));
    }

    public List<KwcContractTradeGoods> queryByContractId(Long contractId) {
        return list(Wrappers.<KwcContractTradeGoods>lambdaQuery()
                .eq(KwcContractTradeGoods::getDelFlag,0)
                .eq(KwcContractTradeGoods::getContractId, contractId));
    }

    public List<KwcContractTradeGoods> queryByContractIdsAndGoodsId(Set<Long> contractIds, Long goodsId) {
        return list(Wrappers.<KwcContractTradeGoods>lambdaQuery()
                .eq(KwcContractTradeGoods::getDelFlag,0)
                .in(KwcContractTradeGoods::getContractId, contractIds)
                .eq(Objects.nonNull(goodsId),KwcContractTradeGoods::getGoodsId, goodsId));
    }

    public KwcContractTradeGoods queryByContractIdAndGoodsId(Long contractId, Long goodsId) {
        return getOne(Wrappers.<KwcContractTradeGoods>lambdaQuery()
                .eq(KwcContractTradeGoods::getDelFlag,0)
                .eq(KwcContractTradeGoods::getContractId, contractId)
                .eq(KwcContractTradeGoods::getGoodsId, goodsId)
                .orderByDesc(KwcContractTradeGoods::getId)
                .last("limit 1"));
    }

    public List<KwcContractTradeGoods> queryByContractIdsAndGoodsIds(Set<Long> contractIds, Set<Long> goodsIds) {
        return list(Wrappers.<KwcContractTradeGoods>lambdaQuery()
                .eq(KwcContractTradeGoods::getDelFlag,0)
                .in(KwcContractTradeGoods::getContractId, contractIds)
                .in(KwcContractTradeGoods::getGoodsId, goodsIds)
        );
    }
}
