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.Date;
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,Set<Long> contractIds ) {
        return page(new Page<>(pageNum, pageSize), Wrappers.<KwcContractLogistics>lambdaQuery()
                .eq(KwcContractLogistics::getDelFlag, 0)
                .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 updateById(updateLogistics);
    }

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

    /**
     * 查询供应商有效的物流合同
     * @param entId
     * @return
     */
    public List<KwcContractLogistics> queryValidByEntId(Long entId) {
        return list(Wrappers.<KwcContractLogistics>lambdaQuery()
                .eq(KwcContractLogistics::getEntId, entId)
                .eq(KwcContractLogistics::getDispatchWay, 2)
                .eq(KwcContractLogistics::getDelFlag, 0)
                .eq(KwcContractLogistics::getStatus, 0)
                .and(queryWrapper -> queryWrapper.apply("{0} between start_time and end_time",new Date())
                        .or(queryWrapper1->queryWrapper1.le(KwcContractLogistics::getStartTime, new Date()).isNull(KwcContractLogistics::getEndTime))
                ));
    }


    public List<KwcContractLogistics> queryList(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)
                .like(org.apache.commons.lang3.StringUtils.isNotBlank(contractNo),KwcContractLogistics::getContractNo, contractNo)
                .like(org.apache.commons.lang3.StringUtils.isNotBlank(contractName),KwcContractLogistics::getName, contractName));
    }

    /**
     * 查询时间范围内的指定派车方式的物流合同
     * @param contractIdList
     * @param startTime
     * @param endTime
     * @param dispatchingType
     * @return
     */
    public KwcContractLogistics queryBy(Set<Long> contractIdList, Date startTime, Date endTime, Integer dispatchingType) {
        return getOne(Wrappers.<KwcContractLogistics>lambdaQuery()
                .eq(KwcContractLogistics::getDelFlag, 0)
                .in(KwcContractLogistics::getId, contractIdList)
                .eq(KwcContractLogistics::getDispatchWay, dispatchingType)
                .eq(KwcContractLogistics::getStatus,0)
                .and(queryWrapper -> queryWrapper.between(KwcContractLogistics::getStartTime, startTime, endTime)
                        //结束日期不为空
                        .or().between(KwcContractLogistics::getEndTime, startTime, endTime)
                        .or(queryWrapper1->queryWrapper1.le(KwcContractLogistics::getStartTime, startTime).ge(KwcContractLogistics::getEndTime, endTime))
                        // 结束日期为空，代表长期有效
                        .or(queryWrapper2->queryWrapper2.ge(KwcContractLogistics::getStartTime, endTime).isNull(KwcContractLogistics::getEndTime))
                ));
    }


    public List<KwcContractLogistics> queryByIdsAndDispatch(Set<Long> ids) {
        return list(Wrappers.<KwcContractLogistics>lambdaQuery()
                .in(KwcContractLogistics::getId, ids)
                .eq(KwcContractLogistics::getDispatchWay, 2)
                .eq(KwcContractLogistics::getStatus, 0)
                .eq(KwcContractLogistics::getDelFlag, 0));
    }

    public List<KwcContractLogistics> queryBycontractIdList(Set<Long> ids) {
        return list(Wrappers.<KwcContractLogistics>lambdaQuery()
                .in(KwcContractLogistics::getId, ids)
                .eq(KwcContractLogistics::getDispatchWay, 1)
                .eq(KwcContractLogistics::getStatus, 0)
                .eq(KwcContractLogistics::getDelFlag, 0));
    }
}
