use of com.xatu.gmall.entity.OmsCartItem in project GMall by 18391713434.
the class OrderController method submitOrder.
@RequestMapping("/submitOrder")
@LoginRequired(loginSuccess = true)
public ModelAndView submitOrder(String tradeCode, String receiveAddressId, BigDecimal totalAmount, HttpServletRequest request, HttpServletResponse response, HttpSession session, ModelMap modelMap) {
String memberId = (String) request.getAttribute("memberId");
String nickname = (String) request.getAttribute("nickname");
// 检查交易码
String success = orderService.checkTradeCode(memberId, tradeCode);
if (success.equals("success")) {
// 订单项对象
List<OmsOrderItem> omsOrderItems = new ArrayList<>();
// 订单对象
OmsOrder omsOrder = new OmsOrder();
omsOrder.setAutoConfirmDay(7);
String outTradeNo = "gmall";
// 将毫秒时间戳拼接到外部订单号
outTradeNo = outTradeNo + System.currentTimeMillis();
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("YYYYMMDDHHmmss");
// 将时间字符串拼接到外部订单号
outTradeNo = outTradeNo + simpleDateFormat.format(new Date());
// 设置外部订单号
omsOrder.setOrderSn(outTradeNo);
omsOrder.setPayAmount(totalAmount);
omsOrder.setOrderType(1);
MemberReceiveAddress receiveAddressByReceiveAddressId = userService.getReceiveAddressByReceiveAddressId(receiveAddressId);
omsOrder.setReceiverDetailAddress(receiveAddressByReceiveAddressId.getDetailAddress());
omsOrder.setReceiverPhone(receiveAddressByReceiveAddressId.getPhoneNumber());
omsOrder.setReceiverPostCode(receiveAddressByReceiveAddressId.getPostCode());
omsOrder.setReceiverProvince(receiveAddressByReceiveAddressId.getProvince());
omsOrder.setReceiverRegion(receiveAddressByReceiveAddressId.getRegion());
// 当前日期加一天
Calendar calendar = Calendar.getInstance();
calendar.add(Calendar.DATE, 1);
Date date = calendar.getTime();
omsOrder.setReceiveTime(date);
omsOrder.setSourceType(0);
omsOrder.setStatus(0);
omsOrder.setTotalAmount(totalAmount);
// 根据用户id获得要购买的商品列表(购物车),和总价格
List<OmsCartItem> omsCartItems = cartService.carList(memberId);
for (OmsCartItem omsCartItem : omsCartItems) {
if (omsCartItem.getIsChecked().equals("1")) {
// 获得订单详情列表
OmsOrderItem omsOrderItem = new OmsOrderItem();
// 验价
BigDecimal price = omsCartItem.getPrice();
boolean b = skuService.checkPrice(omsCartItem.getProductSkuId(), price);
if (b == false) {
return new ModelAndView("tradeFail");
}
// 验库存
omsOrderItem.setProductPic(omsCartItem.getProductPic());
omsOrderItem.setProductName(omsCartItem.getProductName());
// 外部订单号,用来和其他系统进行交互
omsOrderItem.setOrderSn(outTradeNo);
omsOrderItem.setProductCategoryId(omsCartItem.getProductCategoryId());
omsOrderItem.setProductPrice(omsCartItem.getPrice());
omsOrderItem.setRealAmount(new BigDecimal(omsCartItem.getTotalPrice()));
omsOrderItem.setProductQuantity(omsCartItem.getQuantity());
omsOrderItem.setProductSkuCode("11111111111");
omsOrderItem.setProductSkuId(omsCartItem.getProductSkuId());
omsOrderItem.setProductId(omsCartItem.getProductId());
// 在仓库中对应的skuId
omsOrderItem.setProductSn("仓库对应的商品编号");
omsOrderItems.add(omsOrderItem);
}
}
omsOrder.setOmsOrderItems(omsOrderItems);
// 将订单和订单详情写入数据库
// 删除购物车的对应商品
orderService.saveOrder(omsOrder);
// 重定向到支付系统
ModelAndView modelAndView = new ModelAndView("redirect:http://localhost:8087");
modelAndView.addObject("outTradeNo", outTradeNo);
modelAndView.addObject("totalAmount", totalAmount);
return modelAndView;
} else {
ModelAndView modelAndView = new ModelAndView("tradeFail");
return modelAndView;
}
}
use of com.xatu.gmall.entity.OmsCartItem in project GMall by 18391713434.
the class CartServiceImpl method checkCart.
@Override
public void checkCart(String skuId, String memberId, String isChecked) {
OmsCartItem omsCartItem = new OmsCartItem();
omsCartItem.setIsChecked(isChecked);
cartMapper.updateIsChecked(skuId, memberId, isChecked);
// 缓存刷新
try {
flushCache(memberId);
} catch (Exception e) {
e.printStackTrace();
}
}
use of com.xatu.gmall.entity.OmsCartItem in project GMall by 18391713434.
the class CartServiceImpl method carList.
@Override
public List<OmsCartItem> carList(String userId) {
List<OmsCartItem> omsCartItems = new ArrayList<>();
// 在缓存中查询
Jedis jedis = null;
try {
jedis = redisUtil.getJedis();
List<String> hvals = jedis.hvals("user:" + userId + ":cart");
for (String hval : hvals) {
OmsCartItem omsCartItem = JSON.parseObject(hval, OmsCartItem.class);
omsCartItems.add(omsCartItem);
}
} catch (Exception e) {
e.printStackTrace();
return null;
} finally {
jedis.close();
}
return omsCartItems;
}
use of com.xatu.gmall.entity.OmsCartItem in project GMall by 18391713434.
the class CartServiceImpl method flushCache.
@Override
public void flushCache(String memberId) {
List<OmsCartItem> omsCartItems = cartMapper.selectList(new QueryWrapper<OmsCartItem>().eq("member_id", memberId));
// 同步到redis缓存
Map<String, String> map = new HashMap<>();
for (OmsCartItem omsCartItem : omsCartItems) {
map.put(omsCartItem.getProductSkuId().toString(), JSON.toJSONString(omsCartItem));
}
Jedis jedis = redisUtil.getJedis();
try {
jedis.del("user:" + memberId + ":cart");
jedis.hmset("user:" + memberId + ":cart", map);
} catch (Exception e) {
e.printStackTrace();
} finally {
jedis.close();
}
}
use of com.xatu.gmall.entity.OmsCartItem in project GMall by 18391713434.
the class CartController method getTotalAmount.
private BigDecimal getTotalAmount(List<OmsCartItem> omsCartItems) {
BigDecimal totalAmount = new BigDecimal("0");
for (OmsCartItem omsCartItem : omsCartItems) {
BigDecimal totalPrice;
if (StringUtils.isNotBlank(omsCartItem.getTotalPrice())) {
totalPrice = new BigDecimal(omsCartItem.getTotalPrice());
} else {
totalPrice = new BigDecimal("0");
}
totalAmount = totalAmount.add(totalPrice);
}
return totalAmount;
}
Aggregations