예제 #1
0
  @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);
  }
예제 #2
0
 /** 查看 */
 @RequestMapping(value = "/view", method = RequestMethod.GET)
 public String view(Type type, Date beginDate, Date endDate, Model model) {
   if (type == null) {
     type = Type.month;
   }
   if (beginDate == null) {
     beginDate = DateUtils.addMonths(new Date(), -11);
   }
   if (endDate == null) {
     endDate = new Date();
   }
   Map<Date, BigDecimal> salesAmountMap = new LinkedHashMap<Date, BigDecimal>();
   Map<Date, Integer> salesVolumeMap = new LinkedHashMap<Date, Integer>();
   Calendar beginCalendar = DateUtils.toCalendar(beginDate);
   Calendar endCalendar = DateUtils.toCalendar(endDate);
   int beginYear = beginCalendar.get(Calendar.YEAR);
   int endYear = endCalendar.get(Calendar.YEAR);
   int beginMonth = beginCalendar.get(Calendar.MONTH);
   int endMonth = endCalendar.get(Calendar.MONTH);
   for (int year = beginYear; year <= endYear; year++) {
     if (salesAmountMap.size() >= MAX_SIZE) {
       break;
     }
     Calendar calendar = Calendar.getInstance();
     calendar.set(Calendar.YEAR, year);
     if (type == Type.year) {
       calendar.set(Calendar.MONTH, calendar.getActualMinimum(Calendar.MONTH));
       calendar.set(Calendar.DATE, calendar.getActualMinimum(Calendar.DATE));
       calendar.set(Calendar.HOUR_OF_DAY, calendar.getActualMinimum(Calendar.HOUR_OF_DAY));
       calendar.set(Calendar.MINUTE, calendar.getActualMinimum(Calendar.MINUTE));
       calendar.set(Calendar.SECOND, calendar.getActualMinimum(Calendar.SECOND));
       Date begin = calendar.getTime();
       calendar.set(Calendar.MONTH, calendar.getActualMaximum(Calendar.MONTH));
       calendar.set(Calendar.DATE, calendar.getActualMaximum(Calendar.DATE));
       calendar.set(Calendar.HOUR_OF_DAY, calendar.getActualMaximum(Calendar.HOUR_OF_DAY));
       calendar.set(Calendar.MINUTE, calendar.getActualMaximum(Calendar.MINUTE));
       calendar.set(Calendar.SECOND, calendar.getActualMaximum(Calendar.SECOND));
       Date end = calendar.getTime();
       BigDecimal salesAmount = orderService.getSalesAmount(begin, end);
       Integer salesVolume = orderService.getSalesVolume(begin, end);
       salesAmountMap.put(begin, salesAmount != null ? salesAmount : BigDecimal.ZERO);
       salesVolumeMap.put(begin, salesVolume != null ? salesVolume : 0);
     } else {
       for (int month = year == beginYear ? beginMonth : calendar.getActualMinimum(Calendar.MONTH);
           month <= (year == endYear ? endMonth : calendar.getActualMaximum(Calendar.MONTH));
           month++) {
         if (salesAmountMap.size() >= MAX_SIZE) {
           break;
         }
         calendar.set(Calendar.MONTH, month);
         calendar.set(Calendar.DATE, calendar.getActualMinimum(Calendar.DATE));
         calendar.set(Calendar.HOUR_OF_DAY, calendar.getActualMinimum(Calendar.HOUR_OF_DAY));
         calendar.set(Calendar.MINUTE, calendar.getActualMinimum(Calendar.MINUTE));
         calendar.set(Calendar.SECOND, calendar.getActualMinimum(Calendar.SECOND));
         Date begin = calendar.getTime();
         calendar.set(Calendar.DATE, calendar.getActualMaximum(Calendar.DATE));
         calendar.set(Calendar.HOUR_OF_DAY, calendar.getActualMaximum(Calendar.HOUR_OF_DAY));
         calendar.set(Calendar.MINUTE, calendar.getActualMaximum(Calendar.MINUTE));
         calendar.set(Calendar.SECOND, calendar.getActualMaximum(Calendar.SECOND));
         Date end = calendar.getTime();
         BigDecimal salesAmount = orderService.getSalesAmount(begin, end);
         Integer salesVolume = orderService.getSalesVolume(begin, end);
         salesAmountMap.put(begin, salesAmount != null ? salesAmount : BigDecimal.ZERO);
         salesVolumeMap.put(begin, salesVolume != null ? salesVolume : 0);
       }
     }
   }
   model.addAttribute("types", Type.values());
   model.addAttribute("type", type);
   model.addAttribute("beginDate", beginDate);
   model.addAttribute("endDate", endDate);
   model.addAttribute("salesAmountMap", salesAmountMap);
   model.addAttribute("salesVolumeMap", salesVolumeMap);
   return "/admin/sales/view";
 }