关于spring:提交商品订单-SysResult

63次阅读

共计 3397 个字符,预计需要花费 9 分钟才能阅读完成。

SysResult
个别后端业务解决实现之后,须要给客户端返回无效信息,告知客户端程序执行是否正确

@Data
@Accessors(chain = true)
@NoArgsConstructor
@AllArgsConstructor
public class SysResult {
 private Integer status;
 private String msp;// 服务器提示信息 胜利 失败
 private Object data;// 服务器返回值数据
 // 利用 static 的静态方法 将数据动静返回
 public static SysResult fail(){return new SysResult(201,"业务执行失败",null);
 }
 public static SysResult success(){return new SysResult(200,"业务执行胜利",null);
 }
 public static SysResult success(Object data){return new SysResult(200,"业务执行胜利",data);
 }
 public static SysResult success(String msp,Object data){return new SysResult(200,msp,data);
 }
}

订单 pojo

import java.util.Date;
import java.util.List;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.Data;
import lombok.experimental.Accessors;
@TableName("tb_order")
@Data
@Accessors(chain=true)
public class Order extends BasePojo{@TableField(exist=false) // 入库操作疏忽该字段
 private OrderShipping orderShipping;
 // 封装订单商品信息  一对多
 @TableField(exist=false) // 入库操作疏忽该字段
 private List<OrderItem> orderItems;
 @TableId
 private String orderId;
 private String payment;
 private Integer paymentType;
 private String postFee;
 private Integer status;
 private Date paymentTime;
 private Date consignTime;
 private Date endTime;
 private Date closeTime;
 private String shippingName;
 private String shippingCode;
 private Long userId;
 private String buyerMessage;
 private String buyerNick;
 private Integer buyerRate;
}

订单商品 pojo

import java.util.Date;
import java.util.List;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.Data;
import lombok.experimental.Accessors;
@TableName("tb_order_item")
@Data
@Accessors(chain=true)
public class OrderItem extends BasePojo{@TableId(type = IdType.AUTO)
 private String itemId;
  
 private String orderId;
 private Integer num;
 private String title;
 private Long price;
 private Long totalFee;
 private String picPath;
}

订单物流 pojo

import java.util.Date;
import java.util.List;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.Data;
import lombok.experimental.Accessors;
@TableName("tb_order_shipping")
@Data
@Accessors(chain=true)
public class OrderShipping extends BasePojo{
 @TableId
 private String orderId;
 private String receiverName;
 private String receiverPhone;
 private String receiverMobile;
 private String receiverState;
 private String receiverCity;
 private String receiverDistrict;
 private String receiverAddress;
 private String receiverZip;
  }

实现提交订单

 @RequestMapping("submit")
 @ResponseBody
public SysResult submit(Order order){Long userId = UserLocalThread.get().getId();
 String orderId=dubboOrderService.saveOrder(order);
 if(StringUtils.isEmpty(orderId)){return SysResult.fail();
 }else {return SysResult.success(orderId);
 }
 }
@Autowired
private OrderMapper orderMapper;
@Autowired
private OrderShippingMapper orderShippingMapper;
@Autowired
private OrderItemMapper orderItemMapper;
 @Override
 public String saveOrder(Order order) {
 //1. 拼接 orderID = 用户 id 加工夫戳
 String orderId=order.getUserId()+""+System.currentTimeMillis();
 order.setOrderId(orderId).setStatus(1);
 int i = orderMapper.insert(order);// 订单入库
 // 实现订单物流入库
 OrderShipping orderShipping = order.getOrderShipping();
 orderShipping.setOrderId(orderId);// 订单物流增加订单 id
 orderShippingMapper.insert(orderShipping);
 // 实现订单商品入库
 List<OrderItem>orderItems=order.getOrderItems();
 for (OrderItem orderItem:orderItems){orderItem.setOrderId(orderId);
 orderItemMapper.insert(orderItem);
 System.out.println("商品入库胜利");
 }
 return orderId;
 }

正文完
 0