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.KwsPrinterDao;
import com.sckw.system.model.KwsPrinter;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Repository;

import java.util.Collection;
import java.util.List;

@Repository
public class KwsPrinterRepository extends ServiceImpl<KwsPrinterDao, KwsPrinter> {

    public IPage<KwsPrinter> pageQuery(int pageNum, int pageSize, String printerName, Collection<Long> entIds) {
        return page(new Page<>(pageNum, pageSize), Wrappers.<KwsPrinter>lambdaQuery()
                .eq(KwsPrinter::getDelFlag, 0)
                .like(StringUtils.isNotBlank(printerName), KwsPrinter::getPrinterName, printerName)
                .in(entIds != null && !entIds.isEmpty(), KwsPrinter::getEntId, entIds)
                .orderByDesc(KwsPrinter::getCreateTime)
                .orderByDesc(KwsPrinter::getId));
    }

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

    public KwsPrinter findByEntIdAndName(Long entId, String printerName) {
        return getOne(Wrappers.<KwsPrinter>lambdaQuery()
                .eq(KwsPrinter::getDelFlag, 0)
                .eq(KwsPrinter::getEntId, entId)
                .eq(KwsPrinter::getPrinterName, printerName)
                .last("limit 1"));
    }

    public List<KwsPrinter> listByEntId(Long entId) {
        return list(Wrappers.<KwsPrinter>lambdaQuery()
                .eq(KwsPrinter::getDelFlag, 0)
                .eq(KwsPrinter::getStatus, 0)
                .eq(KwsPrinter::getEntId, entId)
                .orderByDesc(KwsPrinter::getCreateTime));
    }
}
