@Override public void update(Order order, Admin operator) { Assert.notNull(order); Order pOrder = orderDao.find(order.getId()); if (pOrder.getIsAllocatedStock()) { for (OrderItem orderItem : pOrder.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); } } } 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); productDao.flush(); staticService.build(product); } } } } orderDao.merge(order); OrderLog orderLog = new OrderLog(); orderLog.setType(Type.modify); orderLog.setOperator(operator != null ? operator.getUsername() : null); orderLog.setOrder(order); orderLogDao.persist(orderLog); }
@Override @Transactional @CacheEvict( value = {"product", "productCategory", "review", "consultation"}, allEntries = true) public void save(Review review) { super.save(review); Product product = review.getProduct(); if (product != null) { reviewDao.flush(); long totalScore = reviewDao.calculateTotalScore(product); long scoreCount = reviewDao.calculateScoreCount(product); product.setTotalScore(totalScore); product.setScoreCount(scoreCount); productDao.merge(product); reviewDao.flush(); staticService.build(product); } }
@Override public void cancel(Order order, Admin operator) { Assert.notNull(order); CouponCode couponCode = order.getCouponCode(); if (couponCode != null) { couponCode.setIsUsed(false); couponCode.setUsedDate(null); couponCodeDao.merge(couponCode); order.setCouponCode(null); orderDao.merge(order); } if (order.getIsAllocatedStock()) { 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); } } } order.setIsAllocatedStock(false); } order.setOrderStatus(OrderStatus.cancelled); order.setExpire(null); orderDao.merge(order); OrderLog orderLog = new OrderLog(); orderLog.setType(Type.cancel); orderLog.setOperator(operator != null ? operator.getUsername() : null); orderLog.setOrder(order); orderLogDao.persist(orderLog); }
@Override public void delete(Order order) { if (order.getIsAllocatedStock()) { 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); } } } } super.delete(order); }
@Override public void shipping(Order order, Shipping shipping, Admin operator) { Assert.notNull(order); Assert.notNull(shipping); Assert.notEmpty(shipping.getShippingItems()); orderDao.lock(order, LockModeType.PESSIMISTIC_WRITE); Setting setting = SettingUtils.get(); if (!order.getIsAllocatedStock() && setting.getStockAllocationTime() == StockAllocationTime.ship) { 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); } } } order.setIsAllocatedStock(true); } shipping.setOrder(order); shippingDao.persist(shipping); for (ShippingItem shippingItem : shipping.getShippingItems()) { OrderItem orderItem = order.getOrderItem(shippingItem.getSn()); if (orderItem != null) { Product product = orderItem.getProduct(); productDao.lock(product, LockModeType.PESSIMISTIC_WRITE); if (product != null) { if (product.getStock() != null) { product.setStock(product.getStock() - shippingItem.getQuantity()); if (order.getIsAllocatedStock()) { product.setAllocatedStock(product.getAllocatedStock() - shippingItem.getQuantity()); } } productDao.merge(product); orderDao.flush(); staticService.build(product); } orderItemDao.lock(orderItem, LockModeType.PESSIMISTIC_WRITE); orderItem.setShippedQuantity(orderItem.getShippedQuantity() + shippingItem.getQuantity()); } } if (order.getShippedQuantity() >= order.getQuantity()) { order.setShippingStatus(ShippingStatus.shipped); order.setIsAllocatedStock(false); } else if (order.getShippedQuantity() > 0) { order.setShippingStatus(ShippingStatus.partialShipment); } order.setExpire(null); orderDao.merge(order); OrderLog orderLog = new OrderLog(); orderLog.setType(Type.shipping); orderLog.setOperator(operator != null ? operator.getUsername() : null); orderLog.setOrder(order); orderLogDao.persist(orderLog); }
@Override public void payment(Order order, Payment payment, Admin operator) { Assert.notNull(order); Assert.notNull(payment); orderDao.lock(order, LockModeType.PESSIMISTIC_WRITE); payment.setOrder(order); paymentDao.merge(payment); if (payment.getMethod() == Payment.Method.deposit) { Member member = order.getMember(); memberDao.lock(member, LockModeType.PESSIMISTIC_WRITE); member.setBalance(member.getBalance().subtract(payment.getAmount())); memberDao.merge(member); Deposit deposit = new Deposit(); deposit.setType(operator != null ? Deposit.Type.adminPayment : Deposit.Type.memberPayment); deposit.setCredit(new BigDecimal(0)); deposit.setDebit(payment.getAmount()); deposit.setBalance(member.getBalance()); deposit.setOperator(operator != null ? operator.getUsername() : null); deposit.setMember(member); deposit.setOrder(order); depositDao.persist(deposit); } Setting setting = SettingUtils.get(); if (!order.getIsAllocatedStock() && setting.getStockAllocationTime() == StockAllocationTime.payment) { 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); } } } order.setIsAllocatedStock(true); } order.setAmountPaid(order.getAmountPaid().add(payment.getAmount())); order.setFee(payment.getFee()); order.setExpire(null); if (order.getAmountPaid().compareTo(order.getAmount()) >= 0) { order.setOrderStatus(OrderStatus.confirmed); order.setPaymentStatus(PaymentStatus.paid); } else if (order.getAmountPaid().compareTo(new BigDecimal(0)) > 0) { order.setOrderStatus(OrderStatus.confirmed); order.setPaymentStatus(PaymentStatus.partialPayment); } orderDao.merge(order); OrderLog orderLog = new OrderLog(); orderLog.setType(Type.payment); orderLog.setOperator(operator != null ? operator.getUsername() : null); orderLog.setOrder(order); orderLogDao.persist(orderLog); }
@Override public void complete(Order order, Admin operator) { Assert.notNull(order); Member member = order.getMember(); memberDao.lock(member, LockModeType.PESSIMISTIC_WRITE); if (order.getShippingStatus() == ShippingStatus.partialShipment || order.getShippingStatus() == ShippingStatus.shipped) { member.setPoint(member.getPoint() + order.getPoint()); for (Coupon coupon : order.getCoupons()) { couponCodeDao.build(coupon, member); } } if (order.getShippingStatus() == ShippingStatus.unshipped || order.getShippingStatus() == ShippingStatus.returned) { CouponCode couponCode = order.getCouponCode(); if (couponCode != null) { couponCode.setIsUsed(false); couponCode.setUsedDate(null); couponCodeDao.merge(couponCode); order.setCouponCode(null); orderDao.merge(order); } } member.setAmount(member.getAmount().add(order.getAmountPaid())); if (!member.getMemberRank().getIsSpecial()) { MemberRank memberRank = memberRankDao.findByAmount(member.getAmount()); if (memberRank != null && memberRank.getAmount().compareTo(member.getMemberRank().getAmount()) > 0) { member.setMemberRank(memberRank); } } memberDao.merge(member); if (order.getIsAllocatedStock()) { 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); } } } order.setIsAllocatedStock(false); } for (OrderItem orderItem : order.getOrderItems()) { if (orderItem != null) { Product product = orderItem.getProduct(); productDao.lock(product, LockModeType.PESSIMISTIC_WRITE); if (product != null) { Integer quantity = orderItem.getQuantity(); Calendar nowCalendar = Calendar.getInstance(); Calendar weekSalesCalendar = DateUtils.toCalendar(product.getWeekSalesDate()); Calendar monthSalesCalendar = DateUtils.toCalendar(product.getMonthSalesDate()); if (nowCalendar.get(Calendar.YEAR) != weekSalesCalendar.get(Calendar.YEAR) || nowCalendar.get(Calendar.WEEK_OF_YEAR) > weekSalesCalendar.get(Calendar.WEEK_OF_YEAR)) { product.setWeekSales((long) quantity); } else { product.setWeekSales(product.getWeekSales() + quantity); } if (nowCalendar.get(Calendar.YEAR) != monthSalesCalendar.get(Calendar.YEAR) || nowCalendar.get(Calendar.MONTH) > monthSalesCalendar.get(Calendar.MONTH)) { product.setMonthSales((long) quantity); } else { product.setMonthSales(product.getMonthSales() + quantity); } product.setSales(product.getSales() + quantity); product.setWeekSalesDate(new Date()); product.setMonthSalesDate(new Date()); productDao.merge(product); orderDao.flush(); staticService.build(product); } } } order.setOrderStatus(OrderStatus.completed); order.setExpire(null); orderDao.merge(order); OrderLog orderLog = new OrderLog(); orderLog.setType(Type.complete); orderLog.setOperator(operator != null ? operator.getUsername() : null); orderLog.setOrder(order); orderLogDao.persist(orderLog); }
@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; }