package com.sckw.transport.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.core.model.constant.Global;
import com.sckw.transport.dao.KwtLogisticsOrderCirculateMapper;
import com.sckw.transport.model.KwtLogisticsOrderCirculate;
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-13 11:31:00
 */
@Repository
public class KwtLogisticsOrderCirculateRepository extends ServiceImpl<KwtLogisticsOrderCirculateMapper, KwtLogisticsOrderCirculate> {
    public IPage<KwtLogisticsOrderCirculate> queryByPage(int pageNum, int pageSize,String truckNo,
                                                         Long logisticOrderId) {
        return page(new Page<>(pageNum, pageSize), Wrappers.<KwtLogisticsOrderCirculate>lambdaQuery()
                .eq(KwtLogisticsOrderCirculate::getDelFlag,0)
                .eq(KwtLogisticsOrderCirculate::getLOrderId, logisticOrderId)
                .like(StringUtils.isNotBlank(truckNo),KwtLogisticsOrderCirculate::getTruckNo, truckNo)
                .orderByDesc(KwtLogisticsOrderCirculate::getCreateTime)
                .orderByDesc(KwtLogisticsOrderCirculate::getId));
    }

    public List<KwtLogisticsOrderCirculate> findOneByLogOrderIdsAndTruckId(List<Long> logOrderIds, Long truckId) {
        return list(Wrappers.<KwtLogisticsOrderCirculate>lambdaQuery().eq(KwtLogisticsOrderCirculate::getDelFlag,0)
                .in(KwtLogisticsOrderCirculate::getLOrderId, logOrderIds)
                .eq(Objects.nonNull(truckId),KwtLogisticsOrderCirculate::getTruckId, truckId)
        );
    }

    @Transactional
    public Boolean updateCirculates(List<KwtLogisticsOrderCirculate> updateCirculates) {
        return updateBatchById(updateCirculates);

    }

    public List<KwtLogisticsOrderCirculate> findOneByLogOrderIdsAndTruckIds(Long logOrderId, Set<String> truckIds) {
        return list(Wrappers.<KwtLogisticsOrderCirculate>lambdaQuery()
                .eq(KwtLogisticsOrderCirculate::getDelFlag,0)
                .eq(KwtLogisticsOrderCirculate::getLOrderId, logOrderId)
                .in(KwtLogisticsOrderCirculate::getTruckId, truckIds));
    }

    public Boolean insertCirculates(List<KwtLogisticsOrderCirculate> insertOrderCirculates) {
       return saveBatch(insertOrderCirculates);
    }

    public KwtLogisticsOrderCirculate findOneByLogOrderIdsAndTruckNo(Long logOrder, String truckNo, Long entId) {
        return getOne(Wrappers.<KwtLogisticsOrderCirculate>lambdaQuery()
                .eq(KwtLogisticsOrderCirculate::getDelFlag,0)
                .eq(KwtLogisticsOrderCirculate::getLOrderId, logOrder)
                .eq(Objects.nonNull(truckNo),KwtLogisticsOrderCirculate::getTruckNo, truckNo)
                .eq(KwtLogisticsOrderCirculate::getEntId, entId)
                .eq(KwtLogisticsOrderCirculate::getStatus, Global.NUMERICAL_ZERO) //0-开启，1-关闭
                .last("limit 1")
        );
    }

    public List<KwtLogisticsOrderCirculate> queryByLogOrderIdsAndTruckNo(List<Long> logOrderIds, String truckNo) {
        return list(Wrappers.<KwtLogisticsOrderCirculate>lambdaQuery()
                .eq(KwtLogisticsOrderCirculate::getDelFlag,0)
                .in(KwtLogisticsOrderCirculate::getLOrderId, logOrderIds)
                .eq(StringUtils.isNotBlank(truckNo), KwtLogisticsOrderCirculate::getTruckNo, truckNo)
        );
    }


    public List<KwtLogisticsOrderCirculate> queryEffectiveOrderByTruckNo(String truckNo) {
        return list(Wrappers.<KwtLogisticsOrderCirculate>lambdaQuery()
                .eq(KwtLogisticsOrderCirculate::getTruckNo, truckNo)
                .eq(KwtLogisticsOrderCirculate::getDelFlag,0));
    }

    public List<KwtLogisticsOrderCirculate> queryOrderCirculateByEntId(Long entId, String truckNo) {
        return list(Wrappers.<KwtLogisticsOrderCirculate>lambdaQuery()
                .eq(KwtLogisticsOrderCirculate::getEntId, entId)
                .eq(KwtLogisticsOrderCirculate::getTruckNo, truckNo)
                .eq(KwtLogisticsOrderCirculate::getDelFlag, 0)
                .orderByDesc(KwtLogisticsOrderCirculate::getCreateTime)
                .orderByDesc(KwtLogisticsOrderCirculate::getId));
    }


}
