package com.sckw.contract.repository;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
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.contract.dao.KwcContractLogisticsMapper;
import com.sckw.contract.model.entity.KwcContractLogistics;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author :chenXiaoFei
 * @version :1.0
 * @description : 物流合同接口
 * @create :2025-11-06 10:24:00
 */
@Repository
public class KwcContractLogisticsRepository extends ServiceImpl<KwcContractLogisticsMapper, KwcContractLogistics> {
    public IPage<KwcContractLogistics> queryByPage(int pageNum, int pageSize, String contractNo, String contractName,
                                                   List<Long> entIdList) {
        return page(new Page<>(pageNum, pageSize), Wrappers.<KwcContractLogistics>lambdaQuery()
                .eq(KwcContractLogistics::getDelFlag, 0)
                .in(CollectionUtils.isNotEmpty(entIdList), KwcContractLogistics::getEntId, entIdList)
                .like(StringUtils.isNotBlank(contractNo), KwcContractLogistics::getContractNo, contractNo)
                .like(StringUtils.isNotBlank(contractName), KwcContractLogistics::getName, contractName));
    }

    public KwcContractLogistics queryByContractCode( String contractCode) {
        return getOne(Wrappers.<KwcContractLogistics>lambdaQuery()
                .eq(KwcContractLogistics::getDelFlag,0)
                .eq(KwcContractLogistics::getContractNo, contractCode)
                .last("limit 1"));
    }

    @Transactional
    public void saveOrUpdateLogistics(KwcContractLogistics saveContractLogistics) {
        saveOrUpdate(saveContractLogistics);
    }

    public KwcContractLogistics queryById(Long id) {
        return getOne(Wrappers.<KwcContractLogistics>lambdaQuery()
                .eq(KwcContractLogistics::getId, id));
    }
}
