/** * * @param lightId * @param date * @param pageRequestParamDTO * @return * @throws Exception * @auth nibili 2015-3-14 */ public Page<LightsRecordModel> getLightsRecordsWithPage(final Long lightId, final String date, PageRequestParamDTO pageRequestParamDTO) throws Exception { logger.debug("##LightsRecordService.getLightsRecordsWithPage# lightId:" + lightId + " date:" + date); if (lightId == null) { throw new Exception("mac must not be empty!"); } final LightsModel lightsModel = lightsDao.findOne(lightId); if (lightsModel == null) { throw new Exception("Could not found light by lightdId : " + lightId); } return lightsRecordModelDao.findAll(new Specification<LightsRecordModel>() { public Predicate toPredicate(Root<LightsRecordModel> root, CriteriaQuery<?> query, CriteriaBuilder cb) { Path<String> macPath = root.get("mac"); Path<Date> createTimePath = root.get("createTime"); Path<Integer> statusPath = root.get("status"); List<Predicate> p = Lists.newArrayList(); // mac p.add(cb.and(cb.equal(macPath, lightsModel.getMac()))); // date if (StringUtils.isNotBlank(date) == true) { try { SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); Date startDate = sdf.parse(date); Date endDate = DateUtils.addDays(startDate, 1); p.add(cb.and(cb.between(createTimePath, startDate, endDate))); } catch (ParseException e) { logger.error("", e); } } // status p.add(cb.and(cb.notEqual(statusPath, LightsRecordModel.STATUS_NORMAL))); if (p != null) { query.where(p.toArray(new Predicate[] {})); // 這裏能夠設置任意條查詢條件 } query.orderBy(new OrderImpl(createTimePath, true)); return null; } }, pageRequestParamDTO.buildPageRequest()); }