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;
import java.util.Objects;
import java.util.Set;

/**
 * @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,
                                                   String status,
                                                   Long entId,Set<Long> contractIds ) {
        return page(new Page<>(pageNum, pageSize), Wrappers.<KwcContractLogistics>lambdaQuery()
                .eq(KwcContractLogistics::getDelFlag, 0)
                .eq(Objects.nonNull(entId), KwcContractLogistics::getEntId, entId)
                .like(StringUtils.isNotBlank(contractNo), KwcContractLogistics::getContractNo, contractNo)
                .like(StringUtils.isNotBlank(contractName), KwcContractLogistics::getName, contractName)
                .eq(StringUtils.isNotBlank(status), KwcContractLogistics::getStatus, status)
                .in(CollectionUtils.isNotEmpty(contractIds), KwcContractLogistics::getId, contractIds)
                .orderByDesc(KwcContractLogistics::getUpdateTime)
                .orderByDesc(KwcContractLogistics::getId));
    }

    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));
    }

    public List<KwcContractLogistics> queryByLogisticContractIds(Set<Long> logContractIds) {
        return list(Wrappers.<KwcContractLogistics>lambdaQuery()
                .eq(KwcContractLogistics::getDelFlag, 0)
                .in(KwcContractLogistics::getId, logContractIds));
    }

    public Boolean updateLogistics(KwcContractLogistics updateLogistics) {
       return update(updateLogistics,
                Wrappers.<KwcContractLogistics>lambdaQuery()
                        .eq(KwcContractLogistics::getId, updateLogistics.getId()));
    }

    public List<KwcContractLogistics> queryByEntId(Long entId) {
        return list(Wrappers.<KwcContractLogistics>lambdaQuery()
                .eq(KwcContractLogistics::getEntId, entId)
                .eq(KwcContractLogistics::getDelFlag, 0));
    }

    public List<KwcContractLogistics> queryList(Long entId, Set<Long> contractIdList, String contractNo, String contractName, String status) {
        return list(Wrappers.<KwcContractLogistics>lambdaQuery()
                .eq(KwcContractLogistics::getDelFlag, 0)
                .eq(Objects.nonNull(status),KwcContractLogistics::getStatus, status)
                .in(CollectionUtils.isNotEmpty(contractIdList),KwcContractLogistics::getId, contractIdList)
                .eq(Objects.nonNull(entId),KwcContractLogistics::getEntId, entId)
                .like(org.apache.commons.lang3.StringUtils.isNotBlank(contractNo),KwcContractLogistics::getContractNo, contractNo)
                .like(org.apache.commons.lang3.StringUtils.isNotBlank(contractName),KwcContractLogistics::getName, contractName));
    }
}
