@RequestMapping(value = "/shop/{id}/cart")
  public String cart(@PathVariable("id") String shopId, Model model, HttpServletRequest req) {
    List<CartItemVO> cartItems = new ArrayList<CartItemVO>();
    PricingResultVO prices = new PricingResultVO();

    // 购物车没有传递item参数,取用户购物车所有项目
    List<CartItemVO> items = cartService.listCartItems(shopId);
    Map<String, Integer> skuMap = new HashMap<String, Integer>();
    for (CartItem item : items) {
      CartItemVO itemVO = new CartItemVO();
      BeanUtils.copyProperties(item, itemVO);
      itemVO.setProduct(productService.load(item.getProductId()));
      Sku sku = productService.loadSku(item.getProductId(), item.getSkuId());
      if (sku == null) {
        continue;
      }
      itemVO.setSku(sku);
      cartItems.add(itemVO);
      skuMap.put(sku.getId(), item.getAmount());
    }
    prices = pricingService.calculate(skuMap, null, null);
    model.addAttribute("cartItems", cartItems);
    model.addAttribute("prices", prices);

    String nextUrl = "/shop/" + shopId + "/cart-next";
    model.addAttribute("nextUrl", nextUrl);
    return "cart/cart";
  }
  @RequestMapping(value = "/shop/{id}/cart-next")
  public String next(@PathVariable("id") String shopId, Model model, HttpServletRequest req) {
    List<CartItemVO> cartItems = new ArrayList<CartItemVO>();
    PricingResultVO prices = new PricingResultVO();

    List<CartItemVO> items = cartService.listCartItems(shopId);
    Map<String, Integer> skuMap = new HashMap<String, Integer>();
    for (CartItem item : items) {
      Sku sku = productService.loadSku(item.getProductId(), item.getSkuId());
      if (sku == null) {
        continue;
      }
      CartItemVO itemVO = new CartItemVO();
      BeanUtils.copyProperties(item, itemVO);
      itemVO.setProduct(productService.load(item.getProductId()));
      itemVO.setSku(sku);
      cartItems.add(itemVO);
      skuMap.put(sku.getId(), item.getAmount());
    }
    prices = pricingService.calculate(skuMap, null, null);

    if (cartItems.size() == 0) {
      return "redirect:/shop/" + shopId + "/cart";
    }
    model.addAttribute("cartItems", cartItems);
    model.addAttribute("prices", prices);
    model.addAttribute("shopId", shopId);

    List<AddressVO> addresses = addressService.listUserAddressesVo();
    if (addresses != null && addresses.size() > 0) {
      model.addAttribute("address", addresses.get(0));
    }
    return "cart/next";
  }
 @RequestMapping(value = "/shop/{id}/productByPage")
 public String productAll(@PathVariable("id") String shopId, Pageable page, Model model) {
   List<Product> products = productService.listProductsBySales(shopId, page, Direction.DESC);
   Long cnt = productService.countProductsByStatus(shopId, ProductStatus.ONSALE);
   model.addAttribute("prods", products);
   model.addAttribute("total", cnt);
   return "fragments/shopProducts";
 }
 @RequestMapping(value = "/shop/{id}/hotSaleProduct")
 public String listProductsBySales(@PathVariable("id") String shopId, Model model) {
   List<Product> prodsHot =
       productService.listProductsBySales(shopId, new PageRequest(0, 12), Direction.DESC);
   model.addAttribute("prods", prodsHot);
   return "fragments/shopProducts";
 }
 @RequestMapping(value = "/shop/{id}/recommendProduct")
 public String listProductsByRecommend(@PathVariable("id") String shopId, Model model) {
   List<Product> recommends =
       productService.listProductsByRecommend(shopId, new PageRequest(0, 12));
   model.addAttribute("prods", recommends);
   return "fragments/shopProducts";
 }
  /**
   * 只获取店铺信息,不获取商品列表
   *
   * @param shopId
   * @param model
   * @param req
   * @return
   */
  @RequestMapping(value = "/shop/{id}/withoutProduct")
  public String viewWithoutProduct(
      @PathVariable("id") String shopId, Model model, HttpServletRequest req) {
    Shop shop = shopService.load(shopId);
    if (shop == null) {
      throw new BizException(
          GlobalErrorCode.NOT_FOUND, new RequestContext(req).getMessage("shop.not.found"));
    }

    model.addAttribute("shop", shop);
    model.addAttribute("owner", userService.load(shop.getOwnerId()));

    ShopStyle shopStyle = shopService.loadShopStyle(shopId);
    List<String> bodyClasses = new ArrayList<String>();
    bodyClasses.add(shopStyle.getAvatarStyle());
    bodyClasses.add(shopStyle.getBackgroundColor());
    bodyClasses.add(shopStyle.getFontColor());
    bodyClasses.add(shopStyle.getFontFamily());
    bodyClasses.add(
        StringUtils.isEmpty(shopStyle.getListView()) ? "smallimg" : shopStyle.getListView());
    model.addAttribute("styles", StringUtils.join(bodyClasses, " "));
    model.addAttribute("waterfall", "waterfall".equalsIgnoreCase(shopStyle.getListView()));

    List<Category> categories = categoryService.listRootCategoriesByShop(shop.getId());
    List<CategoryVO> categoryVOs = new ArrayList<CategoryVO>();
    for (int i = 0; i < categories.size(); i++) {
      CategoryVO categoryVO = new CategoryVO();
      BeanUtils.copyProperties(categories.get(i), categoryVO);
      categoryVO.setProductCount(
          categoryService.countProductsUnderCategory((categories.get(i)).getId()));
      categoryVOs.add(categoryVO);
    }
    model.addAttribute("categories", categoryVOs);

    model.addAttribute("recommendCount", productService.countByRecommend(shopId));
    model.addAttribute("allCount", productService.countProductsBySales(shopId));

    return "shop/shopView";
  }
  @RequestMapping(value = "/shop/{id}/products")
  public String productAll(@PathVariable("id") String shopId, Model model, HttpServletRequest req) {
    Shop shop = shopService.load(shopId);
    List<Product> products = productService.listProducts(shop.getId());
    model.addAttribute("shop", shop);
    model.addAttribute("products", products);

    ShopStyle shopStyle = shopService.loadShopStyle(shopId);
    List<String> bodyClasses = new ArrayList<String>();
    bodyClasses.add(shopStyle.getAvatarStyle());
    bodyClasses.add(shopStyle.getBackgroundColor());
    bodyClasses.add(shopStyle.getFontColor());
    bodyClasses.add(shopStyle.getFontFamily());
    bodyClasses.add(
        StringUtils.isEmpty(shopStyle.getListView()) ? "smallimg" : shopStyle.getListView());
    model.addAttribute("styles", StringUtils.join(bodyClasses, " "));
    model.addAttribute("waterfall", "waterfall".equalsIgnoreCase(shopStyle.getListView()));

    return "shop/shopAll";
  }
  private void auditAP(PartnerActivityProductAudit form, ActivityTicket ticket, boolean isReAudit) {
    // 已经结束了的活动不能再次审核
    if (ActivityStatus.CLOSED.equals(ticket.getStatus())) {
      log.warn("您要审核的活动" + form.getActId() + "商品 " + form.getProductId() + " 已经结束,不能审核");
      throw new BizException(GlobalErrorCode.UNKNOWN, "已经结束了的活动不能审核");
    }

    ActivityTicket newTicket = new ActivityTicket();
    BeanUtils.copyProperties(ticket, newTicket);
    newTicket.setAuditStatus(form.getAuditState());
    newTicket.setStartTime(form.getStartTime() != null ? new Date(form.getStartTime()) : null);
    newTicket.setEndTime(form.getEndTime() != null ? new Date(form.getEndTime()) : null);
    newTicket.setAuditReason(form.getAuditReason());
    newTicket.setAuditor(form.getAuditor());

    if (isReAudit) { // 再次审核
      // 获取报名信息
      if (StringUtils.isEmpty(form.getTicketId()))
        throw new BizException(GlobalErrorCode.UNKNOWN, "再次审核,活动商品id不能为空");
      ActivityTicket at = activityTicketMapper.selectByPrimaryKey(form.getTicketId());
      if (at == null) throw new BizException(GlobalErrorCode.UNKNOWN, "活动不存在, 再次审核失败");
      if (!at.getStatus().equals(ActivityStatus.NOT_STARTED))
        throw new BizException(GlobalErrorCode.UNKNOWN, "进行中或已关闭的活动不能再次审核");
      if (!ActivityTicketAuditStatus.APPROVED.equals(at.getAuditStatus())
          && !ActivityTicketAuditStatus.REJECTED.equals(at.getAuditStatus())) {
        throw new BizException(GlobalErrorCode.UNKNOWN, "只能重审未通过或已通过状态的活动商品");
      }
      newTicket.setAuditReason(
          StringUtils.isEmpty(form.getAuditReason()) ? at.getAuditReason() : form.getAuditReason());
      newTicket.setReason(at.getReason());
      activityTicketMapper.deleteByIds(at.getId()); // 重审直接删除后新增,保留才记录方便追踪
      newTicket.setId(null);
    }

    // 查询该商品是否已在同一时点的其他活动中有出现过
    if (ActivityTicketAuditStatus.APPROVED.equals(form.getAuditState())) {
      if (activityService.existProductInRange(
          newTicket.getStartTime(), newTicket.getEndTime(), null, form.getProductId())) {
        throw new BizException(
            GlobalErrorCode.INTERNAL_ERROR, "商品重复参与活动,ID:" + form.getProductId());
      }
    }

    activityService.insertTicket(newTicket);

    activityService.auditTicketProduct(
        newTicket.getId(),
        form.getActId(),
        ticket.getId(),
        form.getProductId(),
        form.getProductBrand(),
        form.getShortName(),
        form.getSort(),
        form.getImagePc(),
        form.getImageApp());
    // 拒绝活动申请--商品可以修改
    if (!isReAudit && form.getAuditState().equals(ActivityTicketAuditStatus.REJECTED)) {
      productService.lockProduct(form.getProductId(), false);
      campaignProductMapper.deleteActivityProducts(
          form.getActId(), newTicket.getId(), form.getProductId());
      List<CampaignProduct> list = campaignProductMapper.selectByTicket(ticket.getId());
      // 如果该活动再没有商品待审核直接删除该活动申请以便重新报名
      if (list == null
          || list.size() == 0
          || (list.size() == 1 && list.get(0).getProductId().equals(form.getProductId()))) {
        activityTicketMapper.deleteByIds(ticket.getId());
      }
    }
  }
  @RequestMapping(value = "/shop/{id}")
  public String view(@PathVariable("id") String shopId, Model model, HttpServletRequest req) {
    Shop shop = shopService.load(shopId);
    if (shop == null || shop.getArchive() == Boolean.TRUE) {
      throw new BizException(
          GlobalErrorCode.NOT_FOUND, new RequestContext(req).getMessage("shop.not.found"));
    }

    model.addAttribute("shop", shop);
    model.addAttribute("owner", userService.load(shop.getOwnerId()));

    // 取推荐,和热卖商品
    List<Product> recommends =
        productService.listProductsByRecommend(shop.getId(), new PageRequest(0, 4));
    model.addAttribute("prodsRecommended", recommends);

    List<Product> prodsHot =
        productService.listProductsBySales(shop.getId(), new PageRequest(0, 12), Direction.DESC);
    model.addAttribute("prodsHot", prodsHot);

    boolean isShowAll = false;
    if (prodsHot.size() == 12) {
      Long count = productService.countProductsByStatus(shop.getId(), ProductStatus.ONSALE);
      isShowAll = (count.longValue() > 12);
    }
    model.addAttribute("isShowAll", isShowAll);

    ShopStyleVO shopStyle = shopService.loadShopStyle(shopId);
    List<String> bodyClasses = new ArrayList<String>();
    bodyClasses.add(shopStyle.getAvatarStyle());
    bodyClasses.add(shopStyle.getBackgroundColor());
    bodyClasses.add(shopStyle.getFontColor());
    bodyClasses.add(shopStyle.getFontFamily());
    bodyClasses.add(
        StringUtils.isEmpty(shopStyle.getListView()) ? "smallimg" : shopStyle.getListView());
    model.addAttribute("styles", StringUtils.join(bodyClasses, " "));
    model.addAttribute("waterfall", "waterfall".equalsIgnoreCase(shopStyle.getListView()));
    model.addAttribute("banner", shopStyle.getBannerImg());

    if (shop.getProvinceId() != null) {
      Zone province = zoneService.load(shop.getProvinceId().toString());
      model.addAttribute("province", province);
    }
    if (shop.getCityId() != null) {
      Zone city = zoneService.load(shop.getCityId().toString());
      model.addAttribute("city", city);
    }

    List<Category> categories = categoryService.listRootCategoriesByShop(shop.getId());
    List<CategoryVO> categoryVOs = new ArrayList<CategoryVO>();
    for (int i = 0; i < categories.size(); i++) {
      CategoryVO categoryVO = new CategoryVO();
      BeanUtils.copyProperties(categories.get(i), categoryVO);
      categoryVO.setProductCount(
          categoryService.countProductsUnderCategory((categories.get(i)).getId()));
      categoryVOs.add(categoryVO);
    }
    model.addAttribute("categories", categoryVOs);

    model.addAttribute("recommendCount", productService.countByRecommend(shopId));
    model.addAttribute("allCount", productService.countProductsBySales(shopId));

    return "shop/shopView";
  }