use of cn.exrick.common.exception.XmallException in project xmall by Exrick.
the class OrderServiceImpl method createOrder.
@Override
public Long createOrder(OrderInfo orderInfo) {
TbMember member = tbMemberMapper.selectByPrimaryKey(Long.valueOf(orderInfo.getUserId()));
if (member == null) {
throw new XmallException("获取下单用户失败");
}
TbOrder order = new TbOrder();
// 生成订单ID
Long orderId = IDUtil.getRandomId();
order.setOrderId(String.valueOf(orderId));
order.setUserId(Long.valueOf(orderInfo.getUserId()));
order.setBuyerNick(member.getUsername());
order.setPayment(orderInfo.getOrderTotal());
order.setCreateTime(new Date());
order.setUpdateTime(new Date());
// 0、未付款,1、已付款,2、未发货,3、已发货,4、交易成功,5、交易关闭,6、交易失败
order.setStatus(0);
if (tbOrderMapper.insert(order) != 1) {
throw new XmallException("生成订单失败");
}
List<CartProduct> list = orderInfo.getGoodsList();
for (CartProduct cartProduct : list) {
TbOrderItem orderItem = new TbOrderItem();
// 生成订单商品ID
Long orderItemId = IDUtil.getRandomId();
orderItem.setId(String.valueOf(orderItemId));
orderItem.setItemId(String.valueOf(cartProduct.getProductId()));
orderItem.setOrderId(String.valueOf(orderId));
orderItem.setNum(Math.toIntExact(cartProduct.getProductNum()));
orderItem.setPrice(cartProduct.getSalePrice());
orderItem.setTitle(cartProduct.getProductName());
orderItem.setPicPath(cartProduct.getProductImg());
orderItem.setTotalFee(cartProduct.getSalePrice().multiply(BigDecimal.valueOf(cartProduct.getProductNum())));
if (tbOrderItemMapper.insert(orderItem) != 1) {
throw new XmallException("生成订单商品失败");
}
// 删除购物车中含该订单的商品
try {
List<String> jsonList = jedisClient.hvals(CART_PRE + ":" + orderInfo.getUserId());
for (String json : jsonList) {
CartProduct cart = new Gson().fromJson(json, CartProduct.class);
if (cart.getProductId().equals(cartProduct.getProductId())) {
jedisClient.hdel(CART_PRE + ":" + orderInfo.getUserId(), cart.getProductId() + "");
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
// 物流表
TbOrderShipping orderShipping = new TbOrderShipping();
orderShipping.setOrderId(String.valueOf(orderId));
orderShipping.setReceiverName(orderInfo.getUserName());
orderShipping.setReceiverAddress(orderInfo.getStreetName());
orderShipping.setReceiverPhone(orderInfo.getTel());
orderShipping.setCreated(new Date());
orderShipping.setUpdated(new Date());
if (tbOrderShippingMapper.insert(orderShipping) != 1) {
throw new XmallException("生成物流信息失败");
}
return orderId;
}
use of cn.exrick.common.exception.XmallException in project xmall by Exrick.
the class OrderServiceImpl method payOrder.
@Override
public int payOrder(TbThanks tbThanks) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String time = sdf.format(new Date());
tbThanks.setTime(time);
tbThanks.setDate(new Date());
TbMember tbMember = tbMemberMapper.selectByPrimaryKey(Long.valueOf(tbThanks.getUserId()));
if (tbMember != null) {
tbThanks.setUsername(tbMember.getUsername());
}
if (tbThanksMapper.insert(tbThanks) != 1) {
throw new XmallException("保存捐赠支付数据失败");
}
// 设置订单为已付款
TbOrder tbOrder = tbOrderMapper.selectByPrimaryKey(tbThanks.getOrderId());
tbOrder.setStatus(1);
tbOrder.setUpdateTime(new Date());
tbOrder.setPaymentTime(new Date());
if (tbOrderMapper.updateByPrimaryKey(tbOrder) != 1) {
throw new XmallException("更新订单失败");
}
// 发送通知确认邮件
String tokenName = UUID.randomUUID().toString();
String token = UUID.randomUUID().toString();
// 设置验证token键值对 tokenName:token
jedisClient.set(tokenName, token);
jedisClient.expire(tokenName, PAY_EXPIRE);
emailUtil.sendEmailDealThank(EMAIL_SENDER, "【XMall商城】支付待审核处理", tokenName, token, tbThanks);
return 1;
}
Aggregations