@RequestMapping(value = "/preferenceDetail/{promotionId}/{productId}", method = RequestMethod.GET) public String preferenceProductDetail( @PathVariable Long productId, @PathVariable Long promotionId, ModelMap model) { Promotion promotion = promotionService.find(promotionId); Product product = productService.find(productId); model.addAttribute("product", product); model.addAttribute("promotion", promotion); return "/shop/product/preferenceDetail"; }
/** 列表 */ @RequestMapping(value = "/list", method = RequestMethod.GET) public String list( Long brandId, Long promotionId, Long[] tagIds, BigDecimal startPrice, BigDecimal endPrice, OrderType orderType, Integer pageNumber, Integer pageSize, HttpServletRequest request, ModelMap model) { Brand brand = brandService.find(brandId); Promotion promotion = promotionService.find(promotionId); List<Tag> tags = tagService.findList(tagIds); Pageable pageable = new Pageable(pageNumber, pageSize); model.addAttribute("orderTypes", OrderType.values()); model.addAttribute("brand", brand); model.addAttribute("promotion", promotion); model.addAttribute("tags", tags); 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( null, brand, promotion, tags, null, startPrice, endPrice, true, true, null, false, null, null, orderType, pageable)); if (null != tags && tags.size() == 1 && (tags.get(0).getId() == 405 || tags.get(0).getName().equals("积分"))) { System.out.println("检查积分静态页面"); return "/shop/product/pointsList"; } return "/shop/product/list"; }
/** 生成索引 */ @RequestMapping(value = "/build", method = RequestMethod.POST) public @ResponseBody Map<String, Object> build( BuildType buildType, Boolean isPurge, Integer first, Integer count) { long startTime = System.currentTimeMillis(); if (first == null || first < 0) { first = 0; } if (count == null || count <= 0) { count = 50; } int buildCount = 0; boolean isCompleted = true; if (buildType == BuildType.article) { if (first == 0 && isPurge != null && isPurge) { searchService.purge(Article.class); } List<Article> articles = articleService.findList(null, null, null, first, count); for (Article article : articles) { searchService.index(article); buildCount++; } first += articles.size(); if (articles.size() == count) { isCompleted = false; } } else if (buildType == BuildType.product) { if (first == 0 && isPurge != null && isPurge) { searchService.purge(Product.class); } List<Product> products = productService.findList(null, null, null, first, count); for (Product product : products) { searchService.index(product); buildCount++; } first += products.size(); if (products.size() == count) { isCompleted = false; } } long endTime = System.currentTimeMillis(); Map<String, Object> map = new HashMap<String, Object>(); map.put("first", first); map.put("buildCount", buildCount); map.put("buildTime", endTime - startTime); map.put("isCompleted", isCompleted); return map; }
/** 商品选择 */ @RequestMapping(value = "/product_select", method = RequestMethod.GET) public @ResponseBody List<Map<String, Object>> productSelect(String q) { List<Map<String, Object>> data = new ArrayList<Map<String, Object>>(); if (StringUtils.isNotEmpty(q)) { List<Product> products = productService.search(q, false, 20); for (Product product : products) { Map<String, Object> map = new HashMap<String, Object>(); map.put("id", product.getId()); map.put("sn", product.getSn()); map.put("fullName", product.getFullName()); map.put("instruction", product.getInstruction()); map.put("path", product.getPath()); data.add(map); } } return data; }
/** 点击商品属性获取结果 */ @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 = "/history", method = RequestMethod.GET) public @ResponseBody List<Product> history(Long[] ids) { return productService.findList(ids); }
/** 点击数 */ @RequestMapping(value = "/hits/{id}", method = RequestMethod.GET) public @ResponseBody Long hits(@PathVariable Long id) { return productService.viewHits(id); }
/** 搜索 */ @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"; }
// 添加购物车项 @SuppressWarnings("unchecked") @Validations(requiredStrings = {@RequiredStringValidator(fieldName = "id", message = "ID不允许为空!")}) @InputConfig(resultName = "error") public String ajaxAdd() { Product product = productService.load(id); if (!product.getIsMarketable()) { return ajaxJsonErrorMessage("此商品已下架!"); } if (quantity == null || quantity < 1) { quantity = 1; } // [{\"i\":\"402880ea3189939a013189af0afc0002\"\"q\":1}]" Integer totalQuantity = 0; // 总计商品数量 BigDecimal totalPrice = new BigDecimal("0"); // 总计商品价格 Member loginMember = getLoginMember(); if (loginMember == null) { List<CartItemCookie> cartItemCookieList = new ArrayList<CartItemCookie>(); boolean isExist = false; Cookie[] cookies = getRequest().getCookies(); if (cookies != null && cookies.length > 0) { for (Cookie cookie : cookies) { if (StringUtils.equalsIgnoreCase( cookie.getName(), CartItemCookie.CART_ITEM_LIST_COOKIE_NAME)) { if (StringUtils.isNotEmpty(cookie.getValue())) { JsonConfig jsonConfig = new JsonConfig(); jsonConfig.setRootClass(CartItemCookie.class); JSONArray jsonArray = JSONArray.fromObject(cookie.getValue()); List<CartItemCookie> previousCartItemCookieList = (List<CartItemCookie>) JSONSerializer.toJava(jsonArray, jsonConfig); for (CartItemCookie previousCartItemCookie : previousCartItemCookieList) { Product cartItemCookieProduct = productService.load(previousCartItemCookie.getI()); if (StringUtils.equals(previousCartItemCookie.getI(), id)) { isExist = true; previousCartItemCookie.setQ(previousCartItemCookie.getQ() + quantity); if (product.getStore() != null && (product.getFreezeStore() + previousCartItemCookie.getQ()) > product.getStore()) { return ajaxJsonErrorMessage("添加购物车失败,商品库存不足!"); } } cartItemCookieList.add(previousCartItemCookie); totalQuantity += previousCartItemCookie.getQ(); totalPrice = cartItemCookieProduct .getPreferentialPrice(getLoginMember()) .multiply(new BigDecimal(previousCartItemCookie.getQ().toString())) .add(totalPrice); } } } } } if (!isExist) { CartItemCookie cartItemCookie = new CartItemCookie(); cartItemCookie.setI(id); cartItemCookie.setQ(quantity); cartItemCookieList.add(cartItemCookie); totalQuantity += quantity; totalPrice = product .getPreferentialPrice(getLoginMember()) .multiply(new BigDecimal(quantity.toString())) .add(totalPrice); if (product.getStore() != null && (product.getFreezeStore() + cartItemCookie.getQ()) > product.getStore()) { return ajaxJsonErrorMessage("添加购物车失败,商品库存不足!"); } } for (CartItemCookie cartItemCookie : cartItemCookieList) { if (StringUtils.equals(cartItemCookie.getI(), id)) { Product cartItemCookieProduct = productService.load(cartItemCookie.getI()); if (product.getStore() != null && (cartItemCookieProduct.getFreezeStore() + cartItemCookie.getQ()) > cartItemCookieProduct.getStore()) { return ajaxJsonErrorMessage("添加购物车失败,商品库存不足!"); } } } JSONArray jsonArray = JSONArray.fromObject(cartItemCookieList); String jsonString = jsonArray.toString().replaceAll("\"", "'"); Cookie cookie = new Cookie(CartItemCookie.CART_ITEM_LIST_COOKIE_NAME, jsonString); cookie.setPath(getRequest().getContextPath() + "/"); cookie.setMaxAge(CartItemCookie.CART_ITEM_LIST_COOKIE_MAX_AGE); getResponse().addCookie(cookie); } else { boolean isExist = false; Set<CartItem> previousCartItemList = loginMember.getCartItemSet(); if (previousCartItemList != null) { for (CartItem previousCartItem : previousCartItemList) { if (StringUtils.equals(previousCartItem.getProduct().getId(), id)) { isExist = true; previousCartItem.setQuantity(previousCartItem.getQuantity() + quantity); if (product.getStore() != null && (product.getFreezeStore() + previousCartItem.getQuantity()) > product.getStore()) { return ajaxJsonErrorMessage("添加购物车失败,商品库存不足!"); } cartItemService.update(previousCartItem); } totalQuantity += previousCartItem.getQuantity(); totalPrice = previousCartItem .getProduct() .getPreferentialPrice(getLoginMember()) .multiply(new BigDecimal(previousCartItem.getQuantity().toString())) .add(totalPrice); } } if (!isExist) { CartItem cartItem = new CartItem(); cartItem.setMember(loginMember); cartItem.setProduct(product); cartItem.setQuantity(quantity); if (product.getStore() != null && (product.getFreezeStore() + cartItem.getQuantity()) > product.getStore()) { return ajaxJsonErrorMessage("添加购物车失败,商品库存不足!"); } cartItemService.save(cartItem); totalQuantity += quantity; totalPrice = product .getPreferentialPrice(getLoginMember()) .multiply(new BigDecimal(quantity.toString())) .add(totalPrice); } } totalPrice = SystemConfigUtil.getOrderScaleBigDecimal(totalPrice); DecimalFormat decimalFormat = new DecimalFormat(getOrderUnitCurrencyFormat()); String totalPriceString = decimalFormat.format(totalPrice); Map<String, String> jsonMap = new HashMap<String, String>(); jsonMap.put(STATUS, SUCCESS); jsonMap.put(MESSAGE, "添加至购物车成功!"); jsonMap.put("totalQuantity", totalQuantity.toString()); jsonMap.put("totalPrice", totalPriceString); String person = loginMember == null ? "匿名用户" : loginMember.getUsername(); LogUtil.info("[" + person + "]将[" + product.getName() + "]加入了购物车!"); return ajaxJson(jsonMap); }
// 删除购物车项 @InputConfig(resultName = "error") @SuppressWarnings("unchecked") public String ajaxDelete() { Member loginMember = getLoginMember(); totalQuantity = 0; totalPoint = 0; totalPrice = new BigDecimal("0"); if (loginMember == null) { Cookie[] cookies = getRequest().getCookies(); if (cookies != null && cookies.length > 0) { for (Cookie cookie : cookies) { if (StringUtils.equalsIgnoreCase( cookie.getName(), CartItemCookie.CART_ITEM_LIST_COOKIE_NAME)) { if (StringUtils.isNotEmpty(cookie.getValue())) { JsonConfig jsonConfig = new JsonConfig(); jsonConfig.setRootClass(CartItemCookie.class); JSONArray previousJsonArray = JSONArray.fromObject(cookie.getValue()); List<CartItemCookie> cartItemCookieList = (List<CartItemCookie>) JSONSerializer.toJava(previousJsonArray, jsonConfig); Iterator<CartItemCookie> iterator = cartItemCookieList.iterator(); while (iterator.hasNext()) { CartItemCookie cartItemCookie = iterator.next(); if (StringUtils.equals(cartItemCookie.getI(), id)) { iterator.remove(); } else { Product product = productService.load(cartItemCookie.getI()); totalQuantity += cartItemCookie.getQ(); if (getSystemConfig().getPointType() == PointType.productSet) { totalPoint = product.getPoint() * cartItemCookie.getQ() + totalPoint; } totalPrice = product .getPreferentialPrice(getLoginMember()) .multiply(new BigDecimal(cartItemCookie.getQ().toString())) .add(totalPrice); } } JSONArray jsonArray = JSONArray.fromObject(cartItemCookieList); Cookie newCookie = new Cookie(CartItemCookie.CART_ITEM_LIST_COOKIE_NAME, jsonArray.toString()); newCookie.setPath(getRequest().getContextPath() + "/"); newCookie.setMaxAge(CartItemCookie.CART_ITEM_LIST_COOKIE_MAX_AGE); getResponse().addCookie(newCookie); } } } } } else { Set<CartItem> cartItemSet = loginMember.getCartItemSet(); if (cartItemSet != null) { for (CartItem cartItem : cartItemSet) { if (StringUtils.equals(cartItem.getProduct().getId(), id)) { cartItemService.delete(cartItem); } else { Product product = cartItem.getProduct(); totalQuantity += cartItem.getQuantity(); if (getSystemConfig().getPointType() == PointType.productSet) { totalPoint = product.getPoint() * cartItem.getQuantity() + totalPoint; } totalPrice = product .getPreferentialPrice(getLoginMember()) .multiply(new BigDecimal(cartItem.getQuantity().toString())) .add(totalPrice); } } } } totalPrice = SystemConfigUtil.getOrderScaleBigDecimal(totalPrice); if (getSystemConfig().getPointType() == PointType.orderAmount) { totalPoint = totalPrice .multiply(new BigDecimal(getSystemConfig().getPointScale().toString())) .setScale(0, RoundingMode.DOWN) .intValue(); } DecimalFormat decimalFormat = new DecimalFormat(getOrderUnitCurrencyFormat()); String totalPriceString = decimalFormat.format(totalPrice); Map<String, String> jsonMap = new HashMap<String, String>(); jsonMap.put("totalQuantity", totalQuantity.toString()); jsonMap.put("totalPoint", totalPoint.toString()); jsonMap.put("totalPrice", totalPriceString); jsonMap.put(STATUS, SUCCESS); jsonMap.put(MESSAGE, "商品删除成功!"); return ajaxJson(jsonMap); }
// 购物车项列表 @SuppressWarnings("unchecked") @InputConfig(resultName = "error") public String list() { Member loginMember = getLoginMember(); totalQuantity = 0; totalPoint = 0; totalPrice = new BigDecimal("0"); if (loginMember == null) { Cookie[] cookies = getRequest().getCookies(); if (cookies != null && cookies.length > 0) { for (Cookie cookie : cookies) { if (StringUtils.equalsIgnoreCase( cookie.getName(), CartItemCookie.CART_ITEM_LIST_COOKIE_NAME)) { if (StringUtils.isNotEmpty(cookie.getValue())) { JsonConfig jsonConfig = new JsonConfig(); jsonConfig.setRootClass(CartItemCookie.class); String jsonString = cookie.getValue().replaceAll("'", "\""); JSONArray jsonArray = JSONArray.fromObject(jsonString); List<CartItemCookie> cartItemCookieList = (List<CartItemCookie>) JSONSerializer.toJava(jsonArray, jsonConfig); for (CartItemCookie cartItemCookie : cartItemCookieList) { Product product = productService.load(cartItemCookie.getI()); if (product != null) { totalQuantity += cartItemCookie.getQ(); if (getSystemConfig().getPointType() == PointType.productSet) { totalPoint = product.getPoint() * cartItemCookie.getQ() + totalPoint; } totalPrice = product .getPreferentialPrice(getLoginMember()) .multiply(new BigDecimal(cartItemCookie.getQ().toString())) .add(totalPrice); CartItem cartItem = new CartItem(); cartItem.setProduct(product); cartItem.setQuantity(cartItemCookie.getQ()); cartItemList.add(cartItem); } } } } } } } else { Set<CartItem> cartItemSet = loginMember.getCartItemSet(); if (cartItemSet != null) { cartItemList = new ArrayList<CartItem>(cartItemSet); for (CartItem cartItem : cartItemSet) { totalQuantity += cartItem.getQuantity(); if (getSystemConfig().getPointType() == PointType.productSet) { totalPoint = cartItem.getProduct().getPoint() * cartItem.getQuantity() + totalPoint; } totalPrice = cartItem .getProduct() .getPreferentialPrice(getLoginMember()) .multiply(new BigDecimal(cartItem.getQuantity().toString())) .add(totalPrice); } } } totalPrice = SystemConfigUtil.getOrderScaleBigDecimal(totalPrice); if (getSystemConfig().getPointType() == PointType.orderAmount) { totalPoint = totalPrice .multiply(new BigDecimal(getSystemConfig().getPointScale().toString())) .setScale(0, RoundingMode.DOWN) .intValue(); } setResponseNoCache(); return "list"; }
// 购物车项列表 @InputConfig(resultName = "error") @SuppressWarnings("unchecked") public String ajaxList() { List<Map<String, String>> jsonList = new ArrayList<Map<String, String>>(); Member loginMember = getLoginMember(); totalQuantity = 0; totalPrice = new BigDecimal("0"); if (loginMember == null) { Cookie[] cookies = getRequest().getCookies(); if (cookies != null && cookies.length > 0) { for (Cookie cookie : cookies) { if (StringUtils.equalsIgnoreCase( cookie.getName(), CartItemCookie.CART_ITEM_LIST_COOKIE_NAME)) { if (StringUtils.isNotEmpty(cookie.getValue())) { JsonConfig jsonConfig = new JsonConfig(); jsonConfig.setRootClass(CartItemCookie.class); JSONArray jsonArray = JSONArray.fromObject(cookie.getValue()); List<CartItemCookie> cartItemCookieList = (List<CartItemCookie>) JSONSerializer.toJava(jsonArray, jsonConfig); for (CartItemCookie cartItemCookie : cartItemCookieList) { Product product = productService.load(cartItemCookie.getI()); if (product != null) { totalQuantity += cartItemCookie.getQ(); totalPrice = product .getPreferentialPrice(getLoginMember()) .multiply(new BigDecimal(cartItemCookie.getQ().toString())) .add(totalPrice); DecimalFormat decimalFormat = new DecimalFormat(getPriceCurrencyFormat()); String priceString = decimalFormat.format(product.getPreferentialPrice(getLoginMember())); Map<String, String> jsonMap = new HashMap<String, String>(); jsonMap.put("name", product.getName()); jsonMap.put("price", priceString); jsonMap.put("quantity", cartItemCookie.getQ().toString()); jsonMap.put("htmlFilePath", product.getHtmlFilePath()); jsonList.add(jsonMap); } } } } } } } else { Set<CartItem> cartItemSet = loginMember.getCartItemSet(); if (cartItemSet != null) { for (CartItem cartItem : cartItemSet) { Product product = cartItem.getProduct(); totalQuantity += cartItem.getQuantity(); totalPrice = product .getPreferentialPrice(getLoginMember()) .multiply(new BigDecimal(cartItem.getQuantity().toString())) .add(totalPrice); DecimalFormat decimalFormat = new DecimalFormat(getPriceCurrencyFormat()); String priceString = decimalFormat.format(cartItem.getProduct().getPreferentialPrice(getLoginMember())); Map<String, String> jsonMap = new HashMap<String, String>(); jsonMap.put("name", product.getName()); jsonMap.put("price", priceString); jsonMap.put("quantity", cartItem.getQuantity().toString()); jsonMap.put("htmlFilePath", cartItem.getProduct().getHtmlFilePath()); jsonList.add(jsonMap); } } } totalPrice = SystemConfigUtil.getOrderScaleBigDecimal(totalPrice); DecimalFormat decimalFormat = new DecimalFormat(getOrderUnitCurrencyFormat()); String totalPriceString = decimalFormat.format(totalPrice); Map<String, String> jsonMap = new HashMap<String, String>(); jsonMap.put("totalQuantity", totalQuantity.toString()); jsonMap.put("totalPrice", totalPriceString); jsonList.add(0, jsonMap); JSONArray jsonArray = JSONArray.fromObject(jsonList); String person = loginMember == null ? "匿名用户" : loginMember.getUsername(); LogUtil.info("[" + person + "]查看了购物车,总价值:" + totalPriceString); return ajaxJson(jsonArray.toString()); }