public List<Goods> findList( ProductCategory productCategory, Boolean isMarketable, Goods.GenerateMethod generateMethod, Date beginDate, Date endDate, Integer first, Integer count) { CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder(); CriteriaQuery<Goods> criteriaQuery = criteriaBuilder.createQuery(Goods.class); Root<Goods> root = criteriaQuery.from(Goods.class); criteriaQuery.select(root); Predicate restrictions = criteriaBuilder.conjunction(); if (productCategory != null) { Subquery<ProductCategory> subquery = criteriaQuery.subquery(ProductCategory.class); Root<ProductCategory> subqueryRoot = subquery.from(ProductCategory.class); subquery.select(subqueryRoot); subquery.where( criteriaBuilder.or( criteriaBuilder.equal(subqueryRoot, productCategory), criteriaBuilder.like( subqueryRoot.<String>get("treePath"), "%" + ProductCategory.TREE_PATH_SEPARATOR + productCategory.getId() + ProductCategory.TREE_PATH_SEPARATOR + "%"))); restrictions = criteriaBuilder.and( restrictions, criteriaBuilder.in(root.get("productCategory")).value(subquery)); } if (isMarketable != null) { restrictions = criteriaBuilder.and( restrictions, criteriaBuilder.equal(root.get("isMarketable"), isMarketable)); } if (generateMethod != null) { restrictions = criteriaBuilder.and( restrictions, criteriaBuilder.equal(root.get("generateMethod"), generateMethod)); } if (beginDate != null) { restrictions = criteriaBuilder.and( restrictions, criteriaBuilder.greaterThanOrEqualTo(root.<Date>get("createDate"), beginDate)); } if (endDate != null) { restrictions = criteriaBuilder.and( restrictions, criteriaBuilder.lessThanOrEqualTo(root.<Date>get("createDate"), endDate)); } criteriaQuery.where(restrictions); return super.findList(criteriaQuery, first, count, null, null); }
/** 获取参数组 */ @RequestMapping(value = "/parameter_groups", method = RequestMethod.GET) public @ResponseBody Set<ParameterGroup> parameterGroups(Long id) { Set<ParameterGroup> set = null; ProductCategory productCategory = productCategoryService.find(id); if (productCategory.getParameterGroups() == null || productCategory.getParameterGroups().size() <= 0) { List<ProductCategory> list = productCategoryService.findParents(productCategory); for (Iterator iterator = list.iterator(); iterator.hasNext(); ) { ProductCategory p = (ProductCategory) iterator.next(); if (p.getAttributes() != null && p.getParameterGroups().size() > 0) { set = p.getParameterGroups(); } } } else { set = productCategory.getParameterGroups(); } return set; }
/** 点击商品属性获取结果 */ @RequestMapping(value = "/slist/{productCategoryId}", method = RequestMethod.GET) public String slist( @PathVariable Long productCategoryId, Long brandId, Long promotionId, Long[] tagIds, BigDecimal startPrice, BigDecimal endPrice, OrderType orderType, Boolean isOutOfStock, Integer pageNumber, Integer pageSize, HttpServletRequest request, ModelMap model) { ProductCategory productCategory = productCategoryService.find(productCategoryId); List<ProductCategory> childrenpCategory = productCategoryService.findChildrenByParent(productCategoryId); if (productCategory == null) { throw new ResourceNotFoundException(); } Brand brand = brandService.find(brandId); Promotion promotion = promotionService.find(promotionId); List<Tag> tags = tagService.findList(tagIds); Map<Attribute, String> attributeValue = new HashMap<Attribute, String>(); if (productCategory != null) { Set<Attribute> attributes = productCategory.getAttributes(); for (Attribute attribute : attributes) { String value = request.getParameter("attribute_" + attribute.getId()); if (StringUtils.isNotEmpty(value) && attribute.getOptions().contains(value)) { attributeValue.put(attribute, value); } } } Pageable pageable = new Pageable(pageNumber, pageSize); model.addAttribute("orderTypes", OrderType.values()); model.addAttribute("productCategory", productCategory); model.addAttribute("childrenpCategory", childrenpCategory); model.addAttribute("brand", brand); model.addAttribute("promotion", promotion); model.addAttribute("tags", tags); model.addAttribute("isOutOfStock", isOutOfStock); model.addAttribute("attributeValue", attributeValue); model.addAttribute("startPrice", startPrice); model.addAttribute("endPrice", endPrice); model.addAttribute("orderType", orderType); model.addAttribute("pageNumber", pageNumber); model.addAttribute("pageSize", pageSize); model.addAttribute( "page", productService.findPage( productCategory, brand, promotion, tags, attributeValue, startPrice, endPrice, true, true, null, false, isOutOfStock, null, orderType, pageable)); return "/gw/product/slist"; }
/** 搜索 */ @RequestMapping(value = "/search", method = RequestMethod.GET) public String search( String keyword, BigDecimal startPrice, BigDecimal endPrice, OrderType orderType, Integer pageNumber, Integer pageSize, ModelMap model, HttpServletRequest request) { System.out.println("gwSearch"); System.out.println("keyword=" + keyword); System.out.println("keyword=" + keyword); System.out.println("keyword=" + keyword); ProductCategory productCategory = productCategoryService.findProductCategoryByKeyword(keyword); Map<Attribute, String> attributeValue = new HashMap<Attribute, String>(); if (productCategory != null) { System.out.println("productCategory=" + productCategory.getName()); System.out.println("productCategory=" + productCategory.getId()); Set<Attribute> attributes = productCategory.getAttributes(); for (Attribute attribute : attributes) { String value = request.getParameter("attribute_" + attribute.getId()); if (StringUtils.isNotEmpty(value) && attribute.getOptions().contains(value)) { attributeValue.put(attribute, value); } } } if (StringUtils.isEmpty(keyword)) { return ERROR_VIEW; } Pageable pageable = null; if (pageSize != null) { pageable = new Pageable(pageNumber, pageSize); } else { pageable = new Pageable(pageNumber, DEFAULT_PAGE_SIZE); } Page<Product> page = searchService.search(keyword, startPrice, endPrice, orderType, pageable); List<Product> products = page.getContent(); System.out.println("page.zlh->" + products); model.addAttribute("orderTypes", OrderType.values()); model.addAttribute("productKeyword", keyword); model.addAttribute("startPrice", startPrice); model.addAttribute("endPrice", endPrice); model.addAttribute("orderType", orderType); pageable.setSearchValue(keyword); pageable.setSearchProperty("name"); model.addAttribute( "page1", productService.findPage( productCategory, null, null, null, attributeValue, startPrice, endPrice, true, true, null, false, null, null, orderType, pageable)); System.out.println( productService .findPage( productCategory, null, null, null, attributeValue, startPrice, endPrice, true, true, null, false, null, null, orderType, pageable) .getTotal()); model.addAttribute( "page", productService.findPageByEntcode( null, null, null, null, null, null, null, null, true, null, null, null, null, OrderType.dateDesc, pageable)); System.out.println( productService .findPageByEntcode( null, null, null, null, null, null, null, null, true, null, null, null, null, OrderType.dateDesc, pageable) .getTotal()); return "gw/product/search"; }
public List<Goods> findList( Goods.Type type, ProductCategory productCategory, Brand brand, Promotion promotion, Tag tag, Map<Attribute, String> attributeValueMap, BigDecimal startPrice, BigDecimal endPrice, Boolean isMarketable, Boolean isList, Boolean isTop, Boolean isOutOfStock, Boolean isStockAlert, Boolean hasPromotion, Goods.OrderType orderType, Integer count, List<Filter> filters, List<Order> orders) { CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder(); CriteriaQuery<Goods> criteriaQuery = criteriaBuilder.createQuery(Goods.class); Root<Goods> root = criteriaQuery.from(Goods.class); criteriaQuery.select(root); Predicate restrictions = criteriaBuilder.conjunction(); if (type != null) { restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.equal(root.get("type"), type)); } if (productCategory != null) { Subquery<ProductCategory> subquery = criteriaQuery.subquery(ProductCategory.class); Root<ProductCategory> subqueryRoot = subquery.from(ProductCategory.class); subquery.select(subqueryRoot); subquery.where( criteriaBuilder.or( criteriaBuilder.equal(subqueryRoot, productCategory), criteriaBuilder.like( subqueryRoot.<String>get("treePath"), "%" + ProductCategory.TREE_PATH_SEPARATOR + productCategory.getId() + ProductCategory.TREE_PATH_SEPARATOR + "%"))); restrictions = criteriaBuilder.and( restrictions, criteriaBuilder.in(root.get("productCategory")).value(subquery)); } if (brand != null) { restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.equal(root.get("brand"), brand)); } if (promotion != null) { restrictions = criteriaBuilder.and( restrictions, criteriaBuilder.equal(root.join("promotions"), promotion)); } if (tag != null) { restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.equal(root.join("tags"), tag)); } if (attributeValueMap != null) { for (Map.Entry<Attribute, String> entry : attributeValueMap.entrySet()) { String propertyName = Goods.ATTRIBUTE_VALUE_PROPERTY_NAME_PREFIX + entry.getKey().getPropertyIndex(); restrictions = criteriaBuilder.and( restrictions, criteriaBuilder.equal(root.get(propertyName), entry.getValue())); } } if (startPrice != null && endPrice != null && startPrice.compareTo(endPrice) > 0) { BigDecimal temp = startPrice; startPrice = endPrice; endPrice = temp; } if (startPrice != null && startPrice.compareTo(BigDecimal.ZERO) >= 0) { restrictions = criteriaBuilder.and( restrictions, criteriaBuilder.ge(root.<Number>get("price"), startPrice)); } if (endPrice != null && endPrice.compareTo(BigDecimal.ZERO) >= 0) { restrictions = criteriaBuilder.and( restrictions, criteriaBuilder.le(root.<Number>get("price"), endPrice)); } if (isMarketable != null) { restrictions = criteriaBuilder.and( restrictions, criteriaBuilder.equal(root.get("isMarketable"), isMarketable)); } if (isList != null) { restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.equal(root.get("isList"), isList)); } if (isTop != null) { restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.equal(root.get("isTop"), isTop)); } if (isOutOfStock != null) { Subquery<Product> subquery = criteriaQuery.subquery(Product.class); Root<Product> subqueryRoot = subquery.from(Product.class); subquery.select(subqueryRoot); Path<Integer> stock = subqueryRoot.get("stock"); Path<Integer> allocatedStock = subqueryRoot.get("allocatedStock"); if (isOutOfStock) { subquery.where( criteriaBuilder.equal(subqueryRoot.get("goods"), root), criteriaBuilder.lessThanOrEqualTo(stock, allocatedStock)); } else { subquery.where( criteriaBuilder.equal(subqueryRoot.get("goods"), root), criteriaBuilder.greaterThan(stock, allocatedStock)); } restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.exists(subquery)); } if (isStockAlert != null) { Subquery<Product> subquery = criteriaQuery.subquery(Product.class); Root<Product> subqueryRoot = subquery.from(Product.class); subquery.select(subqueryRoot); Path<Integer> stock = subqueryRoot.get("stock"); Path<Integer> allocatedStock = subqueryRoot.get("allocatedStock"); Setting setting = SystemUtils.getSetting(); if (isStockAlert) { subquery.where( criteriaBuilder.equal(subqueryRoot.get("goods"), root), criteriaBuilder.lessThanOrEqualTo( stock, criteriaBuilder.sum(allocatedStock, setting.getStockAlertCount()))); } else { subquery.where( criteriaBuilder.equal(subqueryRoot.get("goods"), root), criteriaBuilder.greaterThan( stock, criteriaBuilder.sum(allocatedStock, setting.getStockAlertCount()))); } restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.exists(subquery)); } if (hasPromotion != null) { restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.isNotNull(root.join("promotions"))); } criteriaQuery.where(restrictions); if (orderType != null) { switch (orderType) { case topDesc: criteriaQuery.orderBy( criteriaBuilder.desc(root.get("isTop")), criteriaBuilder.desc(root.get("createDate"))); break; case priceAsc: criteriaQuery.orderBy( criteriaBuilder.asc(root.get("price")), criteriaBuilder.desc(root.get("createDate"))); break; case priceDesc: criteriaQuery.orderBy( criteriaBuilder.desc(root.get("price")), criteriaBuilder.desc(root.get("createDate"))); break; case salesDesc: criteriaQuery.orderBy( criteriaBuilder.desc(root.get("sales")), criteriaBuilder.desc(root.get("createDate"))); break; case scoreDesc: criteriaQuery.orderBy( criteriaBuilder.desc(root.get("score")), criteriaBuilder.desc(root.get("createDate"))); break; case dateDesc: criteriaQuery.orderBy(criteriaBuilder.desc(root.get("createDate"))); break; } } else if (CollectionUtils.isEmpty(orders)) { criteriaQuery.orderBy( criteriaBuilder.desc(root.get("isTop")), criteriaBuilder.desc(root.get("createDate"))); } return super.findList(criteriaQuery, null, count, filters, orders); }