public int getItemCountInUncloseOrders(long userId, int itemId) { List<OrderItemsModel> orderItems = orderItemsDAO.getListByUserIdAndItemId(userId, itemId); if (org.apache.commons.collections.CollectionUtils.isEmpty(orderItems)) { return 0; } Set<Long> orderIds = new HashSet<>(); for (OrderItemsModel orderItem : orderItems) { orderIds.add(orderItem.getOrderid()); } if (org.apache.commons.collections.CollectionUtils.isEmpty(orderIds)) { return 0; } List<OrdersModel> orders = ordersDAO.getsByIds(orderIds); Map<Long, OrdersModel> ordersMap = new HashMap<>(); for (OrdersModel order : orders) { ordersMap.put(order.getId(), order); } int itemCount = 0; for (OrderItemsModel orderItem : orderItems) { OrdersModel ordersModel = ordersMap.get(orderItem.getOrderid()); if (ordersModel == null) { continue; } if (ordersModel.getStatus() != OrdersModel.STATUS_CLOSED) { itemCount += orderItem.getQuantity(); } } return itemCount; }
private UserAccessInfo genUserAccessInfo(OrdersModel order) { UserAccessInfo accessInfo = new UserAccessInfo(); accessInfo.setTm(DateUtils.formatDateTime(new Date())); Baby baby = babyService.getBabyByUidxUid(order.getUserid()); accessInfo.setPreganancy(baby.getStatus()); accessInfo.setBabyGender(baby.getGender()); if (baby.getBirthday() != null) { accessInfo.setBabyBirthday(DateUtils.formatDate(baby.getBirthday())); } if (baby.getEdc() != null) { accessInfo.setEdc(DateUtils.formatDate(baby.getEdc())); } accessInfo.setUid(order.getUserid()); accessInfo.setEvent("payreturn"); accessInfo.setStatus(UserAccessInfo.STATUS_SUCCESS); List<OrderItemsModel> orderItems = orderItemsDAO.getListByOrderId(order.getId()); accessInfo.setCartSelected(orderItems.size()); int skuQuantity = 0; Set<Long> itemids = new HashSet<>(); for (OrderItemsModel orderItem : orderItems) { itemids.add(orderItem.getItemid()); skuQuantity += orderItem.getQuantity(); } accessInfo.setItemQuantity(itemids.size()); accessInfo.setSkuQuantity(skuQuantity); accessInfo.setOrderid(order.getId()); accessInfo.setTotolFee(DecimalUtil.toStandard(order.getTotalfee()).doubleValue()); accessInfo.setDeliveryFee(DecimalUtil.toStandard(order.getDeliveryfee()).doubleValue()); return accessInfo; }
public boolean buy(NotifyModel notify) { Logger log = Logger.getLogger(PayNotifyListener.class.getName()); OrdersModel order = ordersDAO.getById(notify.getObjid()); if (order == null) { log.error("order not exists:" + notify.toString()); return false; } UserAccessInfo userAccessInfo = this.genUserAccessInfo(order); LogService flumeEventLogger = LogService.getInstance(); if (order.getStatus() > OrdersModel.STATUS_CLOSED) { log.error("order has payed, orderid=" + order.getId() + ", notifylog:" + notify.toString()); userAccessInfo.setStatus(OrdersErrorEnum.ORDER_STATUS_ERROR.getCode()); flumeEventLogger.info(userAccessInfo); return false; } CreditModel credit = creditDAO.getByUserid(order.getUserid()); if (credit == null) { log.error("credit not exists:" + notify.toString()); userAccessInfo.setStatus(OrdersErrorEnum.CREDIT_NOT_EXISTS.getCode()); flumeEventLogger.info(userAccessInfo); return false; } // 设置订单本次支付金额 order.setPaytype(notify.getPaytype()); order.setPaytime((int) (System.currentTimeMillis() / 1000L)); order.setDirect(notify.getMoney().add(order.getDirect())); order.setPayed(notify.getMoney().add(order.getPayed())); order.setOutmoney(notify.getMoney().add(order.getOutmoney())); BigDecimal toPay = order.getTotalfee().subtract(order.getReduce()); // 账户余额不足,需再次支付,基本不会走到这个分支 if (credit.getValue().subtract(toPay).doubleValue() < 0) { log.error("credit less, orderid=" + order.getId() + ", notifylog:" + notify.toString()); ordersDAO.updatePayed(order); userAccessInfo.setStatus(OrdersErrorEnum.CREDIT_LESS_ERROR.getCode()); flumeEventLogger.info(userAccessInfo); return false; } // 账户余额足以支付,减去账户余额,优先级:返利 > 充值卡 > 充值 > 支付 BigDecimal rebate = new BigDecimal(0); BigDecimal giftcard = new BigDecimal(0); BigDecimal recharge = new BigDecimal(0); BigDecimal direct = new BigDecimal(0); if (toPay.doubleValue() > 0.001 && credit.getRebate().doubleValue() > 0.001) { if (credit.getRebate().subtract(toPay).doubleValue() > 0.001) { rebate = toPay; } else { rebate = credit.getRebate(); } toPay = toPay.subtract(rebate); } if (toPay.doubleValue() > 0.001 && credit.getGiftcard().doubleValue() > 0.001) { if (credit.getGiftcard().subtract(toPay).doubleValue() > 0.001) { giftcard = toPay; } else { giftcard = credit.getGiftcard(); } toPay = toPay.subtract(giftcard); } if (toPay.doubleValue() > 0.001 && credit.getRecharge().doubleValue() > 0.001) { if (credit.getRecharge().subtract(toPay).doubleValue() > 0.001) { recharge = toPay; } else { recharge = credit.getRecharge(); } } if (toPay.doubleValue() > 0.001 && credit.getDirect().doubleValue() > 0.001) { if (credit.getDirect().subtract(toPay).doubleValue() > 0.001) { direct = toPay; } else { direct = credit.getDirect(); } } BigDecimal balance = null; // 减去余额 try { balance = creditService.updateUpdate( order.getUserid(), toPay.negate(), rebate.negate(), giftcard.negate(), recharge.negate(), direct.negate()); } catch (CreditException e) { log.error("sql error:", e); return false; } CreditLogModel creditlog = new CreditLogModel(); creditlog.setUserid(order.getUserid()); creditlog.setType(CreditLogModel.TYPE_BUY); creditlog.setObjid(order.getId()); creditlog.setValue(toPay.negate()); creditlog.setBalance(balance); creditlog.setRebate(rebate.negate()); creditlog.setGiftcard(giftcard.negate()); creditlog.setRecharge(recharge.negate()); creditlog.setDirect(direct.negate()); creditlog.setAddtime((int) (System.currentTimeMillis() / 1000L)); if (creditLogDAO.add(creditlog) != 1) { log.error("sql error:" + creditlog.toString()); return false; } // 更新订单状态 if (ordersDAO.setPayed(order) != 1) { log.error("sql error:" + creditlog.toString()); return false; } flumeEventLogger.info(userAccessInfo); return true; }