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.KwcContractLogisticsUnitMapper;
import com.sckw.contract.model.entity.KwcContractLogistics;
import com.sckw.contract.model.entity.KwcContractLogisticsUnit;
import com.sckw.contract.model.entity.KwcContractTradeUnit;
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-05 17:31:00
 */
@Repository
public class KwcContractLogisticsUnitRepository extends ServiceImpl<KwcContractLogisticsUnitMapper, KwcContractLogisticsUnit> {
    public List<KwcContractLogisticsUnit> queryByContractId(Long contractId) {
        return list(Wrappers.<KwcContractLogisticsUnit>lambdaQuery()
                .eq(KwcContractLogisticsUnit::getContractId, contractId)
                .eq(KwcContractLogisticsUnit::getDelFlag,0));
    }

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

    public List<KwcContractLogisticsUnit> queryByContractIds(List<Long> contractIds,Integer entType) {
        return list(Wrappers.<KwcContractLogisticsUnit>lambdaQuery()
                .in(KwcContractLogisticsUnit::getContractId, contractIds)
                .eq(KwcContractLogisticsUnit::getUnitType, entType)
                .eq(KwcContractLogisticsUnit::getDelFlag,0));

    }

    @Transactional
    public void saveUnits(List<KwcContractLogisticsUnit> units) {
        saveBatch( units);
    }

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

    public List<KwcContractLogisticsUnit> queryByEntIdAndEntType(Long entId, Integer entType) {
        return list(Wrappers.<KwcContractLogisticsUnit>lambdaQuery()
                .eq(KwcContractLogisticsUnit::getEntId, entId)
                .eq(KwcContractLogisticsUnit::getUnitType, entType)
                .eq(KwcContractLogisticsUnit::getDelFlag,0));
    }

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

    public List<KwcContractLogisticsUnit> queryByEntIds(List<Long> entIdList,Integer entType) {
       return list(Wrappers.<KwcContractLogisticsUnit>lambdaQuery()
                .eq(KwcContractLogisticsUnit::getDelFlag,0)
                .eq(Objects.nonNull(entType),KwcContractLogisticsUnit::getUnitType, entType)
                .in(KwcContractLogisticsUnit::getEntId, entIdList));
    }


    public List<KwcContractLogisticsUnit> queryByEntIdAndType(Long entId) {
        return list(Wrappers.<KwcContractLogisticsUnit>lambdaQuery()
                .eq(KwcContractLogisticsUnit::getEntId, entId)
                .eq(KwcContractLogisticsUnit::getUnitType, 3)
                .eq(KwcContractLogisticsUnit::getDelFlag, 0));
    }

    public KwcContractLogisticsUnit queryByLogContractIdUnit(Long tradeContractId) {
        return getOne(Wrappers.<KwcContractLogisticsUnit>lambdaQuery()
                .eq(KwcContractLogisticsUnit::getDelFlag, 0)
                .eq(KwcContractLogisticsUnit::getUnitType, 4)
                .eq(KwcContractLogisticsUnit::getContractId, tradeContractId)
                .last("limit 1"));
    }

    public KwcContractLogisticsUnit queryByContractIdAndEntType(Long contractId, Integer entType) {
        return getOne(Wrappers.<KwcContractLogisticsUnit>lambdaQuery()
                .eq(KwcContractLogisticsUnit::getContractId, contractId)
                .eq(KwcContractLogisticsUnit::getUnitType, entType)
                .eq(KwcContractLogisticsUnit::getDelFlag,0)
                .last("limit 1"));
    }

}
