@Override
 public void merge(Member member, Cart cart) {
   if (member != null && cart != null && cart.getMember() == null) {
     Cart memberCart = member.getCart();
     if (memberCart != null) {
       for (Iterator<CartItem> iterator = cart.getCartItems().iterator(); iterator.hasNext(); ) {
         CartItem cartItem = iterator.next();
         Product product = cartItem.getProduct();
         if (memberCart.contains(product)) {
           if (Cart.MAX_PRODUCT_COUNT != null
               && memberCart.getCartItems().size() > Cart.MAX_PRODUCT_COUNT) {
             continue;
           }
           CartItem item = memberCart.getCartItem(product);
           item.add(cartItem.getQuantity());
           cartItemDao.merge(item);
         } else {
           if (Cart.MAX_PRODUCT_COUNT != null
               && memberCart.getCartItems().size() >= Cart.MAX_PRODUCT_COUNT) {
             continue;
           }
           iterator.remove();
           cartItem.setCart(memberCart);
           memberCart.getCartItems().add(cartItem);
           cartItemDao.merge(cartItem);
         }
       }
       cartDao.remove(cart);
     } else {
       member.setCart(cart);
       cart.setMember(member);
       cartDao.merge(cart);
     }
   }
 }
 @Override
 public Cart getCurrent() {
   RequestAttributes requestAttributes = RequestContextHolder.currentRequestAttributes();
   if (requestAttributes != null) {
     HttpServletRequest request = ((ServletRequestAttributes) requestAttributes).getRequest();
     Principal principal =
         (Principal) request.getSession().getAttribute(Member.PRINCIPAL_ATTRIBUTE_NAME);
     Member member = principal != null ? memberDao.find(principal.getId()) : null;
     if (member != null) {
       Cart cart = member.getCart();
       if (cart != null) {
         if (!cart.hasExpired()) {
           if (!DateUtils.isSameDay(cart.getModifyDate(), new Date())) {
             cart.setModifyDate(new Date());
             cartDao.merge(cart);
           }
           return cart;
         } else {
           cartDao.remove(cart);
         }
       }
     } else {
       String id = WebUtils.getCookie(request, Cart.ID_COOKIE_NAME);
       String key = WebUtils.getCookie(request, Cart.KEY_COOKIE_NAME);
       if (StringUtils.isNotEmpty(id)
           && StringUtils.isNumeric(id)
           && StringUtils.isNotEmpty(key)) {
         Cart cart = cartDao.find(Long.valueOf(id));
         if (cart != null && cart.getMember() == null && StringUtils.equals(cart.getKey(), key)) {
           if (!cart.hasExpired()) {
             if (!DateUtils.isSameDay(cart.getModifyDate(), new Date())) {
               cart.setModifyDate(new Date());
               cartDao.merge(cart);
             }
             return cart;
           } else {
             cartDao.remove(cart);
           }
         }
       }
     }
   }
   return null;
 }
  @Override
  public Order create(
      Cart cart,
      Receiver receiver,
      PaymentMethod paymentMethod,
      ShippingMethod shippingMethod,
      CouponCode couponCode,
      boolean isInvoice,
      String invoiceTitle,
      boolean useBalance,
      String memo,
      Admin operator) {
    Assert.notNull(cart);
    Assert.notNull(cart.getMember());
    Assert.notEmpty(cart.getCartItems());
    Assert.notNull(receiver);
    Assert.notNull(paymentMethod);
    Assert.notNull(shippingMethod);

    Order order =
        build(
            cart,
            receiver,
            paymentMethod,
            shippingMethod,
            couponCode,
            isInvoice,
            invoiceTitle,
            useBalance,
            memo);

    order.setSn(snDao.generate(Sn.Type.order));
    if (paymentMethod.getMethod() == PaymentMethod.Method.online) {
      order.setLockExpire(DateUtils.addSeconds(new Date(), 20));
      order.setOperator(operator);
    }

    if (order.getCouponCode() != null) {
      couponCode.setIsUsed(true);
      couponCode.setUsedDate(new Date());
      couponCodeDao.merge(couponCode);
    }

    for (Promotion promotion : cart.getPromotions()) {
      for (Coupon coupon : promotion.getCoupons()) {
        order.getCoupons().add(coupon);
      }
    }

    Setting setting = SettingUtils.get();
    if (setting.getStockAllocationTime() == StockAllocationTime.order
        || (setting.getStockAllocationTime() == StockAllocationTime.payment
            && (order.getPaymentStatus() == PaymentStatus.partialPayment
                || order.getPaymentStatus() == PaymentStatus.paid))) {
      order.setIsAllocatedStock(true);
    } else {
      order.setIsAllocatedStock(false);
    }

    orderDao.persist(order);

    OrderLog orderLog = new OrderLog();
    orderLog.setType(Type.create);
    orderLog.setOperator(operator != null ? operator.getUsername() : null);
    orderLog.setOrder(order);
    orderLogDao.persist(orderLog);

    Member member = cart.getMember();
    if (order.getAmountPaid().compareTo(new BigDecimal(0)) > 0) {
      memberDao.lock(member, LockModeType.PESSIMISTIC_WRITE);
      member.setBalance(member.getBalance().subtract(order.getAmountPaid()));
      memberDao.merge(member);

      Deposit deposit = new Deposit();
      deposit.setType(operator != null ? Deposit.Type.adminPayment : Deposit.Type.memberPayment);
      deposit.setCredit(new BigDecimal(0));
      deposit.setDebit(order.getAmountPaid());
      deposit.setBalance(member.getBalance());
      deposit.setOperator(operator != null ? operator.getUsername() : null);
      deposit.setMember(member);
      deposit.setOrder(order);
      depositDao.persist(deposit);
    }

    if (setting.getStockAllocationTime() == StockAllocationTime.order
        || (setting.getStockAllocationTime() == StockAllocationTime.payment
            && (order.getPaymentStatus() == PaymentStatus.partialPayment
                || order.getPaymentStatus() == PaymentStatus.paid))) {
      for (OrderItem orderItem : order.getOrderItems()) {
        if (orderItem != null) {
          Product product = orderItem.getProduct();
          productDao.lock(product, LockModeType.PESSIMISTIC_WRITE);
          if (product != null && product.getStock() != null) {
            product.setAllocatedStock(
                product.getAllocatedStock()
                    + (orderItem.getQuantity() - orderItem.getShippedQuantity()));
            productDao.merge(product);
            orderDao.flush();
            staticService.build(product);
          }
        }
      }
    }

    cartDao.remove(cart);
    return order;
  }
  @Override
  @Transactional(readOnly = true)
  public Order build(
      Cart cart,
      Receiver receiver,
      PaymentMethod paymentMethod,
      ShippingMethod shippingMethod,
      CouponCode couponCode,
      boolean isInvoice,
      String invoiceTitle,
      boolean useBalance,
      String memo) {
    Assert.notNull(cart);
    Assert.notNull(cart.getMember());
    Assert.notEmpty(cart.getCartItems());

    Order order = new Order();
    order.setShippingStatus(ShippingStatus.unshipped);
    order.setFee(new BigDecimal(0));
    order.setPromotionDiscount(cart.getDiscount());
    order.setCouponDiscount(new BigDecimal(0));
    order.setOffsetAmount(new BigDecimal(0));
    order.setPoint(cart.getEffectivePoint());
    order.setMemo(memo);
    order.setMember(cart.getMember());

    if (receiver != null) {
      order.setConsignee(receiver.getConsignee());
      order.setAreaName(receiver.getAreaName());
      order.setAddress(receiver.getAddress());
      order.setZipCode(receiver.getZipCode());
      order.setPhone(receiver.getPhone());
      order.setArea(receiver.getArea());
    }

    if (!cart.getPromotions().isEmpty()) {
      StringBuffer promotionName = new StringBuffer();
      for (Promotion promotion : cart.getPromotions()) {
        if (promotion != null && promotion.getName() != null) {
          promotionName.append(" " + promotion.getName());
        }
      }
      if (promotionName.length() > 0) {
        promotionName.deleteCharAt(0);
      }
      order.setPromotion(promotionName.toString());
    }

    order.setPaymentMethod(paymentMethod);

    if (shippingMethod != null
        && paymentMethod != null
        && paymentMethod.getShippingMethods().contains(shippingMethod)) {
      BigDecimal freight = shippingMethod.calculateFreight(cart.getWeight());
      for (Promotion promotion : cart.getPromotions()) {
        if (promotion.getIsFreeShipping()) {
          freight = new BigDecimal(0);
          break;
        }
      }
      order.setFreight(freight);
      order.setShippingMethod(shippingMethod);
    } else {
      order.setFreight(new BigDecimal(0));
    }

    if (couponCode != null && cart.isCouponAllowed()) {
      couponCodeDao.lock(couponCode, LockModeType.PESSIMISTIC_WRITE);
      if (!couponCode.getIsUsed()
          && couponCode.getCoupon() != null
          && cart.isValid(couponCode.getCoupon())) {
        BigDecimal couponDiscount =
            cart.getEffectivePrice()
                .subtract(
                    couponCode
                        .getCoupon()
                        .calculatePrice(cart.getQuantity(), cart.getEffectivePrice()));
        couponDiscount =
            couponDiscount.compareTo(new BigDecimal(0)) > 0 ? couponDiscount : new BigDecimal(0);
        order.setCouponDiscount(couponDiscount);
        order.setCouponCode(couponCode);
      }
    }

    List<OrderItem> orderItems = order.getOrderItems();
    for (CartItem cartItem : cart.getCartItems()) {
      if (cartItem != null && cartItem.getProduct() != null) {
        Product product = cartItem.getProduct();
        OrderItem orderItem = new OrderItem();
        orderItem.setSn(product.getSn());
        orderItem.setName(product.getName());
        orderItem.setFullName(product.getFullName());
        orderItem.setPrice(cartItem.getPrice());
        orderItem.setWeight(product.getWeight());
        orderItem.setThumbnail(product.getThumbnail());
        orderItem.setIsGift(false);
        orderItem.setQuantity(cartItem.getQuantity());
        orderItem.setShippedQuantity(0);
        orderItem.setReturnQuantity(0);
        orderItem.setProduct(product);
        orderItem.setOrder(order);
        orderItems.add(orderItem);
      }
    }

    for (GiftItem giftItem : cart.getGiftItems()) {
      if (giftItem != null && giftItem.getGift() != null) {
        Product gift = giftItem.getGift();
        OrderItem orderItem = new OrderItem();
        orderItem.setSn(gift.getSn());
        orderItem.setName(gift.getName());
        orderItem.setFullName(gift.getFullName());
        orderItem.setPrice(new BigDecimal(0));
        orderItem.setWeight(gift.getWeight());
        orderItem.setThumbnail(gift.getThumbnail());
        orderItem.setIsGift(true);
        orderItem.setQuantity(giftItem.getQuantity());
        orderItem.setShippedQuantity(0);
        orderItem.setReturnQuantity(0);
        orderItem.setProduct(gift);
        orderItem.setOrder(order);
        orderItems.add(orderItem);
      }
    }

    Setting setting = SettingUtils.get();
    if (setting.getIsInvoiceEnabled() && isInvoice && StringUtils.isNotEmpty(invoiceTitle)) {
      order.setIsInvoice(true);
      order.setInvoiceTitle(invoiceTitle);
      order.setTax(order.calculateTax());
    } else {
      order.setIsInvoice(false);
      order.setTax(new BigDecimal(0));
    }

    if (useBalance) {
      Member member = cart.getMember();
      if (member.getBalance().compareTo(order.getAmount()) >= 0) {
        order.setAmountPaid(order.getAmount());
      } else {
        order.setAmountPaid(member.getBalance());
      }
    } else {
      order.setAmountPaid(new BigDecimal(0));
    }

    if (order.getAmountPayable().compareTo(new BigDecimal(0)) == 0) {
      order.setOrderStatus(OrderStatus.confirmed);
      order.setPaymentStatus(PaymentStatus.paid);
    } else if (order.getAmountPayable().compareTo(new BigDecimal(0)) > 0
        && order.getAmountPaid().compareTo(new BigDecimal(0)) > 0) {
      order.setOrderStatus(OrderStatus.confirmed);
      order.setPaymentStatus(PaymentStatus.partialPayment);
    } else {
      order.setOrderStatus(OrderStatus.unconfirmed);
      order.setPaymentStatus(PaymentStatus.unpaid);
    }

    if (paymentMethod != null
        && paymentMethod.getTimeout() != null
        && order.getPaymentStatus() == PaymentStatus.unpaid) {
      order.setExpire(DateUtils.addMinutes(new Date(), paymentMethod.getTimeout()));
    }

    return order;
  }