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.KwcContractTradeMapper;
import com.sckw.contract.model.entity.KwcContractTrade;
import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.NotNull;
import org.apache.commons.collections4.CollectionUtils;
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-05 09:27:00
 */
@Repository
public class KwcContractTradeRepository extends ServiceImpl<KwcContractTradeMapper, KwcContractTrade> {
    public List<KwcContractTrade> queryByPid( Long pid) {
        return list(Wrappers.<KwcContractTrade>lambdaQuery()
                .eq(KwcContractTrade::getDelFlag, 0)
                .eq(KwcContractTrade::getContractPid, pid));
    }

    public KwcContractTrade queryByContractId( Long contractId) {
        return getOne(Wrappers.<KwcContractTrade>lambdaQuery()
                .eq(KwcContractTrade::getDelFlag, 0)
                .eq(KwcContractTrade::getId, contractId)
                .last("limit 1"));
    }

    public Boolean updateByContractId(KwcContractTrade updateKwcContractTrade) {
       return update(updateKwcContractTrade, Wrappers.<KwcContractTrade>lambdaQuery()
                .eq(KwcContractTrade::getId, updateKwcContractTrade.getId()));
    }

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

    public IPage<KwcContractTrade> queryTradeListByPage( List<Long> authEntIdList,String contractCode,
                                                         String contractName, Set<Long> contractIds, Integer status, int pageNum, int pageSize) {
        return page(new Page<>(pageNum, pageSize), Wrappers.<KwcContractTrade>lambdaQuery()
                .eq(KwcContractTrade::getDelFlag, 0)
                .like(StringUtils.isNotBlank(contractCode),KwcContractTrade::getContractNo, contractCode)
                .like(StringUtils.isNotBlank(contractName),KwcContractTrade::getName, contractName)
                .eq(Objects.nonNull(status),KwcContractTrade::getStatus, status)
                .in(CollectionUtils.isNotEmpty(contractIds),KwcContractTrade::getId, contractIds)
                .in(CollectionUtils.isNotEmpty(authEntIdList),KwcContractTrade::getEntId, authEntIdList));

    }

    public List<KwcContractTrade> queryTradeListByPageList(String contractCode,
                                                         String contractName, String supplementCode,
                                                            Set<Long> contractIds, Integer status, Integer dispatchWay, int pageNum, int pageSize) {
        return list( Wrappers.<KwcContractTrade>lambdaQuery()
                .eq(KwcContractTrade::getDelFlag, 0)
                .like(StringUtils.isNotBlank(contractCode),KwcContractTrade::getContractNo, contractCode)
                .like(StringUtils.isNotBlank(contractName),KwcContractTrade::getName, contractName)
                .eq(Objects.nonNull(status),KwcContractTrade::getStatus, status)
                .like(StringUtils.isNotBlank(supplementCode),KwcContractTrade::getContractPid, supplementCode)
                .in(CollectionUtils.isNotEmpty(contractIds),KwcContractTrade::getId, contractIds)
                .eq(Objects.nonNull(dispatchWay),KwcContractTrade::getDispatchWay, dispatchWay)
                .orderByDesc(KwcContractTrade::getUpdateTime)
                .orderByDesc(KwcContractTrade::getId));

    }


    public List<KwcContractTrade> queryByContractIds(Set<Long> contractPids) {

        return list(Wrappers.<KwcContractTrade>lambdaQuery()
                .eq(KwcContractTrade::getDelFlag,0)
                .in(KwcContractTrade::getContractPid, contractPids));
    }

    public List<KwcContractTrade> findByContractIds(Set<Long> contractIdList) {
        return list(Wrappers.<KwcContractTrade>lambdaQuery()
                .eq(KwcContractTrade::getDelFlag, 0)
                .in(KwcContractTrade::getId, contractIdList));
    }

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