package com.sckw.system.repository;

import com.baomidou.mybatisplus.core.metadata.IPage;
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.system.dao.KwsWeighbridgeRecordDao;
import com.sckw.system.model.KwsWeighbridgeRecord;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Repository;

import java.math.BigDecimal;
import java.util.Collection;
import java.util.Date;
import java.util.List;

@Repository
public class KwsWeighbridgeRecordRepository extends ServiceImpl<KwsWeighbridgeRecordDao, KwsWeighbridgeRecord> {

    public IPage<KwsWeighbridgeRecord> pageQuery(int pageNum, int pageSize, Collection<Long> weighbridgeIds,
                                                 String truckNo, BigDecimal minWeight, BigDecimal maxWeight,
                                                 Date createStartTime, Date createEndTime,
                                                 Date receiveStartTime, Date receiveEndTime) {
        return page(new Page<>(pageNum, pageSize), Wrappers.<KwsWeighbridgeRecord>lambdaQuery()
                .eq(KwsWeighbridgeRecord::getDelFlag, 0)
                .in(weighbridgeIds != null && !weighbridgeIds.isEmpty(), KwsWeighbridgeRecord::getWeighbridgeId, weighbridgeIds)
                .like(StringUtils.isNotBlank(truckNo), KwsWeighbridgeRecord::getTruckNo, truckNo)
                .ge(minWeight != null, KwsWeighbridgeRecord::getWeight, minWeight)
                .le(maxWeight != null, KwsWeighbridgeRecord::getWeight, maxWeight)
                .ge(createStartTime != null, KwsWeighbridgeRecord::getCreateTime, createStartTime)
                .le(createEndTime != null, KwsWeighbridgeRecord::getCreateTime, createEndTime)
                .ge(receiveStartTime != null, KwsWeighbridgeRecord::getReceiveTime, receiveStartTime)
                .le(receiveEndTime != null, KwsWeighbridgeRecord::getReceiveTime, receiveEndTime)
                .orderByDesc(KwsWeighbridgeRecord::getCreateTime)
                .orderByDesc(KwsWeighbridgeRecord::getId));
    }

    public List<KwsWeighbridgeRecord> listQuery(Collection<Long> weighbridgeIds,
                                                String truckNo, BigDecimal minWeight, BigDecimal maxWeight,
                                                Date createStartTime, Date createEndTime,
                                                Date receiveStartTime, Date receiveEndTime) {
        return list(Wrappers.<KwsWeighbridgeRecord>lambdaQuery()
                .eq(KwsWeighbridgeRecord::getDelFlag, 0)
                .in(weighbridgeIds != null && !weighbridgeIds.isEmpty(), KwsWeighbridgeRecord::getWeighbridgeId, weighbridgeIds)
                .like(StringUtils.isNotBlank(truckNo), KwsWeighbridgeRecord::getTruckNo, truckNo)
                .ge(minWeight != null, KwsWeighbridgeRecord::getWeight, minWeight)
                .le(maxWeight != null, KwsWeighbridgeRecord::getWeight, maxWeight)
                .ge(createStartTime != null, KwsWeighbridgeRecord::getCreateTime, createStartTime)
                .le(createEndTime != null, KwsWeighbridgeRecord::getCreateTime, createEndTime)
                .ge(receiveStartTime != null, KwsWeighbridgeRecord::getReceiveTime, receiveStartTime)
                .le(receiveEndTime != null, KwsWeighbridgeRecord::getReceiveTime, receiveEndTime)
                .orderByDesc(KwsWeighbridgeRecord::getCreateTime)
                .orderByDesc(KwsWeighbridgeRecord::getId));
    }
}
