List of usage examples for javax.persistence.criteria CriteriaBuilder sum
<N extends Number> Expression<N> sum(Expression<N> x);
From source file:net.groupbuy.dao.impl.OrderDaoImpl.java
public Integer getSalesVolume(Date beginDate, Date endDate) { CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder(); CriteriaQuery<Integer> criteriaQuery = criteriaBuilder.createQuery(Integer.class); Root<Order> root = criteriaQuery.from(Order.class); criteriaQuery.select(criteriaBuilder.sum(root.join("orderItems").<Integer>get("shippedQuantity"))); Predicate restrictions = criteriaBuilder.conjunction(); restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.equal(root.get("orderStatus"), OrderStatus.completed)); if (beginDate != null) { restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.greaterThanOrEqualTo(root.<Date>get("createDate"), beginDate)); }/*from w ww. j a v a 2 s .co m*/ if (endDate != null) { restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.lessThanOrEqualTo(root.<Date>get("createDate"), endDate)); } criteriaQuery.where(restrictions); return entityManager.createQuery(criteriaQuery).setFlushMode(FlushModeType.COMMIT).getSingleResult(); }
From source file:net.groupbuy.dao.impl.OrderDaoImpl.java
public BigDecimal getSalesAmount(Date beginDate, Date endDate) { CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder(); CriteriaQuery<BigDecimal> criteriaQuery = criteriaBuilder.createQuery(BigDecimal.class); Root<Order> root = criteriaQuery.from(Order.class); criteriaQuery.select(criteriaBuilder.sum(root.<BigDecimal>get("amountPaid"))); Predicate restrictions = criteriaBuilder.conjunction(); restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.equal(root.get("orderStatus"), OrderStatus.completed)); if (beginDate != null) { restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.greaterThanOrEqualTo(root.<Date>get("createDate"), beginDate)); }/*from ww w.j a v a 2s . co m*/ if (endDate != null) { restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.lessThanOrEqualTo(root.<Date>get("createDate"), endDate)); } criteriaQuery.where(restrictions); return entityManager.createQuery(criteriaQuery).setFlushMode(FlushModeType.COMMIT).getSingleResult(); }
From source file:net.shopxx.dao.impl.StatisticDaoImpl.java
public List<Statistic> analyze(Statistic.Period period, Date beginDate, Date endDate) { Assert.notNull(period);/* w w w . java 2s. c om*/ CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder(); CriteriaQuery<Statistic> criteriaQuery = criteriaBuilder.createQuery(Statistic.class); Root<Statistic> root = criteriaQuery.from(Statistic.class); switch (period) { case year: criteriaQuery.select(criteriaBuilder.construct(Statistic.class, root.get("year"), criteriaBuilder.sum(root.<Long>get("registerMemberCount")), criteriaBuilder.sum(root.<Long>get("createOrderCount")), criteriaBuilder.sum(root.<Long>get("completeOrderCount")), criteriaBuilder.sum(root.<BigDecimal>get("createOrderAmount")), criteriaBuilder.sum(root.<BigDecimal>get("completeOrderAmount")))); criteriaQuery.groupBy(root.get("year")); break; case month: criteriaQuery.select(criteriaBuilder.construct(Statistic.class, root.get("year"), root.get("month"), criteriaBuilder.sum(root.<Long>get("registerMemberCount")), criteriaBuilder.sum(root.<Long>get("createOrderCount")), criteriaBuilder.sum(root.<Long>get("completeOrderCount")), criteriaBuilder.sum(root.<BigDecimal>get("createOrderAmount")), criteriaBuilder.sum(root.<BigDecimal>get("completeOrderAmount")))); criteriaQuery.groupBy(root.get("year"), root.get("month")); break; case day: criteriaQuery.select(criteriaBuilder.construct(Statistic.class, root.get("year"), root.get("month"), root.get("day"), root.<Long>get("registerMemberCount"), root.<Long>get("createOrderCount"), root.<Long>get("completeOrderCount"), root.<BigDecimal>get("createOrderAmount"), root.<BigDecimal>get("completeOrderAmount"))); break; } Predicate restrictions = criteriaBuilder.conjunction(); if (beginDate != null) { Calendar calendar = DateUtils.toCalendar(beginDate); int year = calendar.get(Calendar.YEAR); int month = calendar.get(Calendar.MONTH); int day = calendar.get(Calendar.DAY_OF_MONTH); restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.or(criteriaBuilder.greaterThan(root.<Integer>get("year"), year), criteriaBuilder.and(criteriaBuilder.equal(root.<Integer>get("year"), year), criteriaBuilder.greaterThan(root.<Integer>get("month"), month)), criteriaBuilder.and(criteriaBuilder.equal(root.<Integer>get("year"), year), criteriaBuilder.equal(root.<Integer>get("month"), month), criteriaBuilder.greaterThanOrEqualTo(root.<Integer>get("day"), day)))); } if (endDate != null) { Calendar calendar = DateUtils.toCalendar(endDate); int year = calendar.get(Calendar.YEAR); int month = calendar.get(Calendar.MONTH); int day = calendar.get(Calendar.DAY_OF_MONTH); restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.or(criteriaBuilder.lessThan(root.<Integer>get("year"), year), criteriaBuilder.and(criteriaBuilder.equal(root.<Integer>get("year"), year), criteriaBuilder.lessThan(root.<Integer>get("month"), month)), criteriaBuilder.and(criteriaBuilder.equal(root.<Integer>get("year"), year), criteriaBuilder.equal(root.<Integer>get("month"), month), criteriaBuilder.lessThanOrEqualTo(root.<Integer>get("day"), day)))); } criteriaQuery.where(restrictions); return entityManager.createQuery(criteriaQuery).getResultList(); }
From source file:net.groupbuy.dao.impl.MemberDaoImpl.java
public List<Object[]> findPurchaseList(Date beginDate, Date endDate, Integer count) { CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder(); CriteriaQuery<Object[]> criteriaQuery = criteriaBuilder.createQuery(Object[].class); Root<Member> member = criteriaQuery.from(Member.class); Join<Product, Order> orders = member.join("orders"); criteriaQuery.multiselect(member.get("id"), member.get("username"), member.get("email"), member.get("point"), member.get("amount"), member.get("balance"), criteriaBuilder.sum(orders.<BigDecimal>get("amountPaid"))); Predicate restrictions = criteriaBuilder.conjunction(); if (beginDate != null) { restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.greaterThanOrEqualTo(orders.<Date>get("createDate"), beginDate)); }/*from ww w .j a v a 2 s . c o m*/ if (endDate != null) { restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.lessThanOrEqualTo(orders.<Date>get("createDate"), endDate)); } restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.equal(orders.get("orderStatus"), OrderStatus.completed), criteriaBuilder.equal(orders.get("paymentStatus"), PaymentStatus.paid)); criteriaQuery.where(restrictions); criteriaQuery.groupBy(member.get("id"), member.get("username"), member.get("email"), member.get("point"), member.get("amount"), member.get("balance")); criteriaQuery.orderBy(criteriaBuilder.desc(criteriaBuilder.sum(orders.<BigDecimal>get("amountPaid")))); TypedQuery<Object[]> query = entityManager.createQuery(criteriaQuery).setFlushMode(FlushModeType.COMMIT); if (count != null && count >= 0) { query.setMaxResults(count); } return query.getResultList(); }
From source file:net.shopxx.dao.impl.OrderDaoImpl.java
public BigDecimal createOrderAmount(Date beginDate, Date endDate) { CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder(); CriteriaQuery<BigDecimal> criteriaQuery = criteriaBuilder.createQuery(BigDecimal.class); Root<Order> root = criteriaQuery.from(Order.class); criteriaQuery.select(criteriaBuilder.sum(root.<BigDecimal>get("amount"))); Predicate restrictions = criteriaBuilder.conjunction(); if (beginDate != null) { restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.greaterThanOrEqualTo(root.<Date>get("createDate"), beginDate)); }/*from w ww .j a va 2 s .co m*/ if (endDate != null) { restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.lessThanOrEqualTo(root.<Date>get("createDate"), endDate)); } criteriaQuery.where(restrictions); BigDecimal result = entityManager.createQuery(criteriaQuery).getSingleResult(); return result != null ? result : BigDecimal.ZERO; }
From source file:net.shopxx.dao.impl.OrderDaoImpl.java
public BigDecimal completeOrderAmount(Date beginDate, Date endDate) { CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder(); CriteriaQuery<BigDecimal> criteriaQuery = criteriaBuilder.createQuery(BigDecimal.class); Root<Order> root = criteriaQuery.from(Order.class); criteriaQuery.select(criteriaBuilder.sum(root.<BigDecimal>get("amount"))); Predicate restrictions = criteriaBuilder.conjunction(); if (beginDate != null) { restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.greaterThanOrEqualTo(root.<Date>get("completeDate"), beginDate)); }/*from www . j a va2 s.co m*/ if (endDate != null) { restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.lessThanOrEqualTo(root.<Date>get("completeDate"), endDate)); } criteriaQuery.where(restrictions); BigDecimal result = entityManager.createQuery(criteriaQuery).getSingleResult(); return result != null ? result : BigDecimal.ZERO; }
From source file:net.osxx.dao.impl.ProductDaoImpl.java
public List<Object[]> findSalesList(Date beginDate, Date endDate, Integer count) { CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder(); CriteriaQuery<Object[]> criteriaQuery = criteriaBuilder.createQuery(Object[].class); Root<Product> product = criteriaQuery.from(Product.class); Join<Product, OrderItem> orderItems = product.join("orderItems"); Join<Product, net.osxx.entity.Order> order = orderItems.join("order"); criteriaQuery.multiselect(product.get("id"), product.get("sn"), product.get("name"), product.get("fullName"), product.get("price"), criteriaBuilder.sum(orderItems.<Integer>get("quantity")), criteriaBuilder.sum(criteriaBuilder .prod(orderItems.<Integer>get("quantity"), orderItems.<BigDecimal>get("price")))); Predicate restrictions = criteriaBuilder.conjunction(); if (beginDate != null) { restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.greaterThanOrEqualTo(order.<Date>get("createDate"), beginDate)); }/*from w w w . j av a2 s .com*/ if (endDate != null) { restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.lessThanOrEqualTo(order.<Date>get("createDate"), endDate)); } restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.equal(order.get("orderStatus"), OrderStatus.completed), criteriaBuilder.equal(order.get("paymentStatus"), PaymentStatus.paid)); criteriaQuery.where(restrictions); criteriaQuery.groupBy(product.get("id"), product.get("sn"), product.get("name"), product.get("fullName"), product.get("price")); criteriaQuery.orderBy(criteriaBuilder.desc(criteriaBuilder.sum( criteriaBuilder.prod(orderItems.<Integer>get("quantity"), orderItems.<BigDecimal>get("price"))))); TypedQuery<Object[]> query = entityManager.createQuery(criteriaQuery).setFlushMode(FlushModeType.COMMIT); if (count != null && count >= 0) { query.setMaxResults(count); } return query.getResultList(); }
From source file:com.dp2345.dao.impl.ProductDaoImpl.java
public List<Object[]> findSalesList(Date beginDate, Date endDate, Integer count) { CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder(); CriteriaQuery<Object[]> criteriaQuery = criteriaBuilder.createQuery(Object[].class); Root<Product> product = criteriaQuery.from(Product.class); Join<Product, OrderItem> orderItems = product.join("orderItems"); Join<Product, com.dp2345.entity.Order> order = orderItems.join("order"); criteriaQuery.multiselect(product.get("id"), product.get("sn"), product.get("name"), product.get("fullName"), product.get("price"), criteriaBuilder.sum(orderItems.<Integer>get("quantity")), criteriaBuilder.sum(criteriaBuilder .prod(orderItems.<Integer>get("quantity"), orderItems.<BigDecimal>get("price")))); Predicate restrictions = criteriaBuilder.conjunction(); if (beginDate != null) { restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.greaterThanOrEqualTo(order.<Date>get("createDate"), beginDate)); }/* ww w . j ava 2s .c o m*/ if (endDate != null) { restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.lessThanOrEqualTo(order.<Date>get("createDate"), endDate)); } restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.equal(order.get("orderStatus"), OrderStatus.completed), criteriaBuilder.equal(order.get("paymentStatus"), PaymentStatus.paid)); criteriaQuery.where(restrictions); criteriaQuery.groupBy(product.get("id"), product.get("sn"), product.get("name"), product.get("fullName"), product.get("price")); criteriaQuery.orderBy(criteriaBuilder.desc(criteriaBuilder.sum( criteriaBuilder.prod(orderItems.<Integer>get("quantity"), orderItems.<BigDecimal>get("price"))))); TypedQuery<Object[]> query = entityManager.createQuery(criteriaQuery).setFlushMode(FlushModeType.COMMIT); if (count != null && count >= 0) { query.setMaxResults(count); } return query.getResultList(); }
From source file:net.shopxx.dao.impl.ProductDaoImpl.java
public List<Object[]> findSalesList(Date beginDate, Date endDate, Integer count) { CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder(); CriteriaQuery<Object[]> criteriaQuery = criteriaBuilder.createQuery(Object[].class); Root<Product> product = criteriaQuery.from(Product.class); Join<Product, OrderItem> orderItems = product.join("orderItems"); Join<Product, net.shopxx.entity.Order> order = orderItems.join("order"); criteriaQuery.multiselect(product.get("id"), product.get("sn"), product.get("name"), product.get("fullName"), product.get("price"), criteriaBuilder.sum(orderItems.<Integer>get("quantity")), criteriaBuilder.sum(criteriaBuilder .prod(orderItems.<Integer>get("quantity"), orderItems.<BigDecimal>get("price")))); Predicate restrictions = criteriaBuilder.conjunction(); if (beginDate != null) { restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.greaterThanOrEqualTo(order.<Date>get("createDate"), beginDate)); }/*w w w . jav a2 s.c o m*/ if (endDate != null) { restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.lessThanOrEqualTo(order.<Date>get("createDate"), endDate)); } restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.equal(order.get("orderStatus"), OrderStatus.completed), criteriaBuilder.equal(order.get("paymentStatus"), PaymentStatus.paid)); criteriaQuery.where(restrictions); criteriaQuery.groupBy(product.get("id"), product.get("sn"), product.get("name"), product.get("fullName"), product.get("price")); criteriaQuery.orderBy(criteriaBuilder.desc(criteriaBuilder.sum( criteriaBuilder.prod(orderItems.<Integer>get("quantity"), orderItems.<BigDecimal>get("price"))))); TypedQuery<Object[]> query = entityManager.createQuery(criteriaQuery).setFlushMode(FlushModeType.COMMIT); if (count != null && count >= 0) { query.setMaxResults(count); } return query.getResultList(); }
From source file:net.groupbuy.dao.impl.ProductDaoImpl.java
public List<Object[]> findSalesList(Date beginDate, Date endDate, Integer count) { CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder(); CriteriaQuery<Object[]> criteriaQuery = criteriaBuilder.createQuery(Object[].class); Root<Product> product = criteriaQuery.from(Product.class); Join<Product, OrderItem> orderItems = product.join("orderItems"); Join<Product, net.groupbuy.entity.Order> order = orderItems.join("order"); criteriaQuery.multiselect(product.get("id"), product.get("sn"), product.get("name"), product.get("fullName"), product.get("price"), criteriaBuilder.sum(orderItems.<Integer>get("quantity")), criteriaBuilder.sum(criteriaBuilder .prod(orderItems.<Integer>get("quantity"), orderItems.<BigDecimal>get("price")))); Predicate restrictions = criteriaBuilder.conjunction(); if (beginDate != null) { restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.greaterThanOrEqualTo(order.<Date>get("createDate"), beginDate)); }// w ww .j av a 2 s .co m if (endDate != null) { restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.lessThanOrEqualTo(order.<Date>get("createDate"), endDate)); } restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.equal(order.get("orderStatus"), OrderStatus.completed), criteriaBuilder.equal(order.get("paymentStatus"), PaymentStatus.paid)); criteriaQuery.where(restrictions); criteriaQuery.groupBy(product.get("id"), product.get("sn"), product.get("name"), product.get("fullName"), product.get("price")); criteriaQuery.orderBy(criteriaBuilder.desc(criteriaBuilder.sum( criteriaBuilder.prod(orderItems.<Integer>get("quantity"), orderItems.<BigDecimal>get("price"))))); TypedQuery<Object[]> query = entityManager.createQuery(criteriaQuery).setFlushMode(FlushModeType.COMMIT); if (count != null && count >= 0) { query.setMaxResults(count); } return query.getResultList(); }