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.KwcContractLogisticsGoodsMapper;
import com.sckw.contract.model.entity.KwcContractLogisticsGoods;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

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

/**
 * @author :chenXiaoFei
 * @version :1.0
 * @description : 物流商品接口
 * @create :2025-11-06 08:32:00
 */
@Repository
public class KwcContractLogisticsGoodsRepository extends ServiceImpl<KwcContractLogisticsGoodsMapper, KwcContractLogisticsGoods> {
    public List<KwcContractLogisticsGoods> queryByContractId(Long contractId) {
        return list(Wrappers.<KwcContractLogisticsGoods>lambdaQuery()
                .eq(KwcContractLogisticsGoods::getDelFlag,0)
                .eq(KwcContractLogisticsGoods::getContractId,contractId));
    }

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

    @Transactional(rollbackFor = Exception.class)
    public void saveLogisticsGoods(List<KwcContractLogisticsGoods> goods) {
        saveOrUpdateBatch(goods);
    }

    public void deleteByContractId(long contactId) {
        remove(Wrappers.<KwcContractLogisticsGoods>lambdaQuery()
                .eq(KwcContractLogisticsGoods::getContractId,contactId));
    }

    public List<KwcContractLogisticsGoods> queryByLogIdsAndGoodsId(Set<Long> logTradeContractIds, Long goodsId) {
        return list(Wrappers.<KwcContractLogisticsGoods>lambdaQuery()
                .eq(KwcContractLogisticsGoods::getDelFlag,0)
                .eq(KwcContractLogisticsGoods::getGoodsId,goodsId)
                .in(KwcContractLogisticsGoods::getContractId,logTradeContractIds));
    }


    public KwcContractLogisticsGoods queryByContractIdAndGoodId(Long contractId, Long goodId) {
        return getOne(Wrappers.<KwcContractLogisticsGoods>lambdaQuery()
                .eq(KwcContractLogisticsGoods::getContractId, contractId)
                .eq(KwcContractLogisticsGoods::getGoodsId,goodId)
                .eq(KwcContractLogisticsGoods::getDelFlag,0)
                .last("limit 1"));
    }
}
