实现订单系统
1 订单系统实现
1.1 系统架构
1.2 订单表结构
订单表:
订单商品表:
物流表:
1.3 接口
参见:淘淘商城-订单系统接口.docx
1.4 创建订单系统taotao-order
系统配置参考taotao-sso系统。
1.5 实现创建订单接口
1.5.1 service
- @Service
- public class OrderServiceImpl implements OrderService {
- @Autowired
- private TbOrderMapper orderMapper;
- @Autowired
- private TbOrderItemMapper orderItemMapper;
- @Autowired
- private TbOrderShippingMapper orderShippingMapper;
- //redis中的订单key
- @Value("${ORDER_ID_KEY}")
- private String ORDER_ID_KEY;
- @Value("${ORDER_BEGIN_ID}")
- private Long ORDER_BEGIN_ID;
- @Autowired
- private JedisCluster jedisCluster;
- @Override
- public TaotaoResult createOrder(TbOrder order, List<TbOrderItem> itemList, TbOrderShipping orderShipping) {
- //订单表
- //生成订单号
- String orderIdStr = jedisCluster.get(ORDER_ID_KEY);
- Long orderId = null;
- if (StringUtils.isBlank(orderIdStr)) {
- //如果redis中没有订单号使用初始订单号初始化
- jedisCluster.set(ORDER_ID_KEY, ORDER_BEGIN_ID.toString());
- orderId = ORDER_BEGIN_ID;
- } else {
- //生成订单号
- orderId = jedisCluster.incr(ORDER_ID_KEY);
- }
- //设置订单号
- order.setOrderId(orderId.toString());
- Date date = new Date();
- //订单创建时间
- order.setCreateTime(date);
- //订单更新时间
- order.setUpdateTime(date);
- //插入订单表
- orderMapper.insert(order);
- //插入订单商品表
- for (TbOrderItem tbOrderItem : itemList) {
- //取订单商品id
- Long orderItemId = jedisCluster.incr("ORDER_ITEM_ID");
- tbOrderItem.setId(orderItemId.toString());
- tbOrderItem.setOrderId(orderId.toString());
- //添加到订单商品表
- orderItemMapper.insert(tbOrderItem);
- }
- //插入物流表
- orderShipping.setOrderId(orderId.toString());
- orderShipping.setCreated(date);
- orderShipping.setUpdated(date);
- orderShippingMapper.insert(orderShipping);
- return TaotaoResult.ok(orderId.toString());
- }
- }
1.5.2 Controller
1.5.2.1 Pojo
- public class Order extends TbOrder{
- private List<TbOrderItem> orderItems;
- private TbOrderShipping orderShipping;
- }
1.5.2.2 controller
- @Controller
- @RequestMapping("/order")
- public class OrderController {
- @Autowired
- private OrderService orderService;
- @RequestMapping(value = "/create", method=RequestMethod.POST)
- @ResponseBody
- public TaotaoResult createOrder(@RequestBody Order order) {
- TaotaoResult result = null;
- try {
- result = orderService.createOrder(order, order.getOrderItems(), order.getOrderShipping());
- } catch (Exception e) {
- e.printStackTrace();
- return TaotaoResult.build(500, ExceptionUtil.getStackTrace(e));
- }
- return result;
- }
- }
1.6 其他接口
....
2 前端系统实现下单功能
2.1 下单流程
2.2 订单结算页面
2.2.1 Controller
@Controller @RequestMapping("/order") public class OrderController {
@Autowired private OrderService orderService; @Autowired private CartService cartService; /** * 显示订单确认画面 * <p>Title: showOrderCart</p> * <p>Description: </p> * @param request * @param model * @return */ @RequestMapping("/order-cart") public String showOrderCart(HttpServletRequest request, TbUser user, Model model) { //根据用户信息,取出用户的收货地址列表 //本项目中使用静态数据模拟。。。。 //从cookie中把商品列表取出来 List<Item> itemsList = cartService.getCartItemsList(request); model.addAttribute("cartList", itemsList); return "order-cart"; } } |
2.3 提交订单处理
2.3.1 创建订单Service
- @Srvice
- public class OrderServiceImpl implements OrderService {
- @Value("${ORDER_BASE_URL}")
- private String ORDER_BASE_URL;
- @Value("${ORDER_CREATE_URL}")
- private String ORDER_CREATE_URL;
- @Override
- public TaotaoResult createService(Order order) {
- //把pojo转换成json数据
- String json = JsonUtils.objectToJson(order);
- //调用订单系统服务提交订单
- String resultStr = HttpClientUtil.doPostJson(ORDER_BASE_URL + ORDER_CREATE_URL, json);
- //转换成java对象
- TaotaoResult taotaoResult = TaotaoResult.format(resultStr);
- return taotaoResult;
- }
- }
2.3.2 Controller
- @RequestMapping(value="/create", method=RequestMethod.POST)
- public String createOrder(HttpServletRequest request, Order order, Model model) {
- //从request中取用户信息
- TbUser user = (TbUser)request.getAttribute("user");
- //把用户信息补充到order对象中
- order.setUserId(user.getId());
- order.setBuyerNick(user.getUsername());
- //提交订单
- TaotaoResult result = null;
- try {
- result = orderService.createService(order);
- //订单创建成功
- if(result.getStatus() == 200) {
- model.addAttribute("orderId", result.getData());
- model.addAttribute("payment", order.getPayment());
- //两天后送达
- DateTime dateTime = new DateTime();
- dateTime = dateTime.plusDays(2);
- model.addAttribute("date", dateTime.toString("yyyy-MM-dd"));
- return "success";
- }
- } catch (Exception e) {
- e.printStackTrace();
- result = TaotaoResult.build(500, ExceptionUtil.getStackTrace(e));
- }
- //订单创建失败
- model.addAttribute("message", result.getMsg());
- return "error/exception";
- }