努力好了,时间会给你答案。--------magic_guo
订单模块的流程以及用户操作如下:
1.当用户选定购物车需要结算的商品并单击结算按钮时,会跳转到订单页面,此时会将选定的购物车信息展示在订单界面,并且还会显示用户的地址信息以供用户选择;
2.用户选择了地址,点击提交订单按钮,此时生成订单数据,并将插入数据库,同时清除购物车信息,然后跳转到支付页面;
3.根据订单id查询订单
5.根据用户id查询所有订单;
6.更新订单状态的操作;
7.取消订单,用户跳转到支付页面而未支付;此时订单处于待支付状态;
取消订单分为两种操作:①在订单未超时的情况下用户主动取消、②订单超时,被动取消;
看代码:
Maven配置:
com.guo shop-common com.guo shop-feign org.springframework.boot spring-boot-starter-web org.springframework.cloud spring-cloud-starter-config org.springframework.cloud spring-cloud-starter-netflix-eureka-client mysql mysql-connector-java runtime org.springframework.boot spring-boot-starter-test test
订单表t_order:
订单详情表t_order_detail:
订单实体类:
@Data
@TableName(value = "t_order")
public class Order {
// 根据用户id后四位生成订单,这里使用字符串
@TableId(type = IdType.INPUT)
private String id;
private Integer uid;
private Date createTime;
private String address;
private String phone;
private String username;
private BigDecimal totalPrice;
// 支付方式:1:支付宝,2:微信,3:银行卡支付
private Integer payType;
// 订单状态:1:未支付,2:已支付,3:已取消,4:已超时,5:已发货,6:已签收
private Integer orderStatus;
@TableField(exist = false)
private List orderDetailList;
}
订单详情实体类:
@Data
@TableName(value = "t_order_detail")
public class OrderDetail {
@TableId(type = IdType.AUTO)
private Integer id;
private String oid;
private Integer gid;
private Integer gcount;
private BigDecimal gprice;
private BigDecimal subtotal;
private String gname;
private String gdesc;
private String gpng;
}
CarGoods实体类:
@Data
public class CarGoods {
private Integer gid;
private Integer count;
private String gname;
private BigDecimal gprice;
private String gdesc;
private Integer gtype;
private List goodsPicList;
}
控制层controller:
@RequestMapping("/order")
@RestController
public class OrderController {
@Autowired
private ICarService carService;
@Autowired
private IAddressService addressService;
@Autowired
private IOrderService orderService;
@Autowired
private OrderUtils orderUtils;
@RequestMapping("/settlement")
@LoginUser
public ResultEntity settlement(User user) {
if (user.getId() == null) {
return ResultEntity.error("你还未登录,请登录!");
}
// 从购物车中查询出商品信息
List carGoodsList = carService.getCarGoodsList(user.getId());
// 查询用户的地址信息
List userAllAddress = addressService.getUserAllAddress(user.getId());
// 计算总价
BigDecimal totalPrice = orderUtils.getTotalPrice(carGoodsList);
Map map = new HashMap<>();
map.put("totalPrice", totalPrice);
map.put("carGoodsList", carGoodsList);
map.put("userAllAddress", userAllAddress);
return ResultEntity.success(map);
}
@RequestMapping("/submitOrder")
@LoginUser
public ResultEntity submitOrder(User user, Integer addressId) {
if (user.getId() == null) {
return ResultEntity.error("你还未登录,请登录!");
}
// 将数据插入数据库
String orderId = orderService.insertOrder(user.getId(), addressId);
// 清空购物车信息
carService.clearUserCar(user.getId());
// 跳转到第三方支付平台
return ResultEntity.success(orderId);
}
@RequestMapping("/getOrderByOrderId")
public Order getOrderByOrderId(@Param("id") String orderId) {
return orderService.selectById(orderId);
}
@RequestMapping("/getOrderListByUserId")
public List getOrderListByUserId(@Param("id") String userId) {
EntityWrapper orderEntityWrapper = new EntityWrapper<>();
orderEntityWrapper.eq("uid", userId);
return orderService.selectList(orderEntityWrapper);
}
@RequestMapping("/updateOrderStatus")
public ResultEntity updateOrderStatus(String orderId, Integer status) {
orderService.updateOrderStatus(orderId, status);
return ResultEntity.success();
}
}
Order service层:
public interface IOrderService extends IService{ String insertOrder(Integer uid, Integer addressId); void updateOrderStatus(String orderId, Integer status); } @Service public class OrderServiceImpl extends ServiceImpl implements IOrderService { @Autowired private ICarService carService; @Autowired private IAddressService addressService; @Autowired private IOrderDetailService orderDetailService; @Autowired private OrderUtils orderUtils; @Override public String insertOrder(Integer uid, Integer addressId) { // 根据用户id查询购物车信息 List carGoodsList = carService.getCarGoodsList(uid); // 根据地址Id,查询出用户选择的地址 Address address = addressService.getAddressById(addressId); // 通过工具类获取订单Id String orderId = orderUtils.createOrderId(uid); // 获取订单总价 BigDecimal totalPrice = orderUtils.getTotalPrice(carGoodsList); // 封装订单实体类 Order order = new Order(); order.setId(orderId); order.setUid(uid); order.setAddress(address.getAddress()); order.setPhone(address.getPhone()); order.setUsername(address.getUsername()); order.setCreateTime(new Date()); order.setOrderStatus(1); order.setPayType(1); order.setTotalPrice(totalPrice); // 插入订单表 Integer insert = baseMapper.insert(order); // 封装订单详情并插入订单详情表 List orderDetailList = new ArrayList<>(); for (CarGoods carGoods : carGoodsList) { OrderDetail orderDetail = new OrderDetail(); orderDetail.setGcount(carGoods.getCount()); orderDetail.setGdesc(carGoods.getGdesc()); orderDetail.setGname(carGoods.getGname()); orderDetail.setGpng(carGoods.getGoodsPicList().get(0).getPng()); orderDetail.setGprice(carGoods.getGprice()); orderDetail.setGid(uid); orderDetail.setOid(orderId); orderDetail.setSubtotal(carGoods.getGprice().multiply(BigDecimal.valueOf(carGoods.getCount()))); orderDetailList.add(orderDetail); } boolean b = orderDetailService.insertBatch(orderDetailList); return orderId; } @Override public void updateOrderStatus(String orderId, Integer status) { baseMapper.updateOrderStatus(orderId, status); } }
OrderDetail service层:
public interface IOrderDetailService extends IService{ } @Service public class OrderDetailServiceImpl extends ServiceImpl implements IOrderDetailService { }
OrderMapper层:
public interface OrderMapper extends baseMapper{ @Update("update t_order set order_status = #{status} where id = #{orderId}") void updateOrderStatus(@Param("orderId") String orderId, @Param("status") Integer status); }
OrderDetailMapper层:
public interface OrderDetailMapper extends baseMapper{ }
OrderUtil工具类:
@Component
public class OrderUtils {
@Autowired
private StringRedisTemplate redisTemplate;
public String createOrderId(Integer userId) {
// 年月日
String date = new SimpleDateFormat("yyyyMMdd").format(new Date());
// 用户id后四位
String userIdSuffix = getUserIdSuffix(userId);
// 随机四位数
String random = RandomStringUtils.random(4, false, true);
// 流水号
int orderIndex = 1;
//
if (redisTemplate.hasKey(ShopConstants.ORDER_INDEX)) {
// 如果存在,就自增
orderIndex = redisTemplate.opsForValue().increment(ShopConstants.ORDER_INDEX).intValue();
} else {
// 不存在就插入
redisTemplate.opsForValue().set(ShopConstants.ORDER_INDEX, String.valueOf(orderIndex));
}
// 组合起来, 返回订单编号
return date + userIdSuffix + random + orderIndex;
}
public String getUserIdSuffix(Integer userId) {
StringBuffer stringBuffer = new StringBuffer(String.valueOf(userId));
// 判断用户id的长度是否大于四位
if (stringBuffer.length() < 4) {
for (int i = stringBuffer.length(); i < 4; i++) {
stringBuffer.insert(0, "0");
}
return stringBuffer.toString();
} else {
return stringBuffer.substring((stringBuffer.length() - 4));
}
}
public BigDecimal getTotalPrice(List carGoodsList) {
BigDecimal totalPrice = new BigDecimal(0);
for (CarGoods carGoods : carGoodsList) {
// 商品数量
Integer count = carGoods.getCount();
// 商品单价
BigDecimal gprice = carGoods.getGprice();
// 商品的小计
BigDecimal subTotal = gprice.multiply(BigDecimal.valueOf(count));
totalPrice = totalPrice.add(subTotal);
}
return totalPrice;
}
}
就这样吧。
本文章教学视频来自:https://www.bilibili.com/video/BV1tb4y1Q74E?p=3&t=125
静下心,慢慢来,会很快!



