/** * 可進行查詢的分頁列表 */ public Page<MessageConsumeFailLog> list(LogQueryParam param){ Sort sort = new Sort( Sort.Direction.DESC ,"createTime"); Pageable pageable = new PageRequest( param.getPageNo(), param.getPageSize(), sort ); return messageConsumeFailLogRepository.findAll( (root, query, cb) -> listSpecification(root, cb, param), pageable ); }
/** * 分頁列表的查詢構成器 */ private Predicate listSpecification(Root<MessageConsumeFailLog> root, CriteriaBuilder cb, LogQueryParam param){ List<Predicate> predicates = Lists.newArrayListWithExpectedSize(10); //消息隊列名稱 if (!StringUtils.isEmpty(param.getQueueName())){ predicates.add( cb.like( root.get( "queueName" ).as( String.class ), "%"+param.getQueueName()+"%")); } //關鍵詞 if (!StringUtils.isEmpty(param.getKeyWord())){ predicates.add( cb.like( root.get( "keyWord" ).as( String.class ), "%"+param.getKeyWord()+"%")); } //關鍵內容 if (!StringUtils.isEmpty(param.getKeyWordValue())){ predicates.add( cb.like( root.get( "keyWordValue" ).as( String.class ), "%"+param.getKeyWordValue()+"%")); } //日誌狀態 if (!Objects.isNull(param.getState())){ predicates.add( cb.equal( root.get( "state" ).as( Integer.class ), param.getState())); } //開始時間 結束時間 if (!Objects.isNull(param.getStartTime())&&!Objects.isNull(param.getEndTime())){ //時間戳轉Date Date startTime = new Date( param.getStartTime() ); Date entTime = new Date( param.getEndTime() ); predicates.add( cb.between( root.get( "createTime" ).as( Date.class ), startTime, entTime)); } int size = predicates.size(); return cb.and( predicates.toArray(new Predicate[size]) ); }