导入地址簿——需求分析与设计
产品原型
接口设计
数据库设计
导入地址簿——代码导入
导入地址簿——功能测试
没有问题
用户下单——需求分析与设计
业务说明
业务流程
接口设计
数据库设计
用户下单——代码开发
DTO设计和VO设计
Controller层中
@RequestMapping("/user/order")
@RestController("userOrderController")
@Api(tags="用户端订单相关接口")
@Slf4j
public class orderController {
@Autowired
private OrderService orderService;
/**
* 用户下单
* @param ordersSubmitDTO
* @return
*/
@ApiOperation("用户下单")
@PostMapping("/submit")
public Result<OrderSubmitVO> submit(@RequestBody OrdersSubmitDTO ordersSubmitDTO){
log.info("用户下单:{}",ordersSubmitDTO);
OrderSubmitVO orderSubmitVO=orderService.submitOrder(ordersSubmitDTO);
return Result.success(orderSubmitVO);
}
}
Service层中
@Service
public class OrderServiceImpl implements OrderService {
@Autowired
private OrderMapper orderMapper;
@Autowired
private OrderDetailMapper orderDetailMapper;
@Autowired
private AddressBookMapper addressBookMapper;
@Autowired
private ShoppingCartMapper shoppingCartMapper;
/**
* 用户下单
* @param ordersSubmitDTO
* @return
*/
@Override
@Transactional
public OrderSubmitVO submitOrder(OrdersSubmitDTO ordersSubmitDTO) {
//处理各种业务异常(地址簿为空,购物车数据为空)
AddressBook addressBook = addressBookMapper.getById(ordersSubmitDTO.getAddressBookId());
if(addressBook==null) {
//抛出业务异常
throw new AddressBookBusinessException(MessageConstant.ADDRESS_BOOK_IS_NULL);
}
//查询当前用户的购物车数据
ShoppingCart shoppingCart = new ShoppingCart();
Long userId= BaseContext.getCurrentId();
shoppingCart.setUserId(userId);
List<ShoppingCart> list = shoppingCartMapper.list(shoppingCart);
if(list==null||list.size()==0) {
//抛出业务异常
throw new ShoppingCartBusinessException(MessageConstant.SHOPPING_CART_IS_NULL);
}
//2.向订单表插入一条数据
Orders orders=new Orders();
BeanUtils.copyProperties(ordersSubmitDTO ,orders);
orders.setOrderTime(LocalDateTime.now());
orders.setPayStatus(Orders.UN_PAID);
orders.setStatus(Orders.PENDING_PAYMENT);
orders.setNumber(String.valueOf(System.currentTimeMillis()));
orders.setPhone(addressBook.getPhone());
orders.setConsignee(addressBook.getConsignee());
orders.setUserId(userId);
orderMapper.insert(orders);
List<OrderDetail> orderDetailList=new ArrayList<>();
//3.向订单明细表插入n条数据
for(ShoppingCart cart:list){
OrderDetail orderDetail = new OrderDetail();//订单明细
BeanUtils.copyProperties(cart,orderDetail);
orderDetail.setOrderId(orders.getId()); //前面有主键返回,设置当前订单明细关联的订单id
orderDetailList.add(orderDetail);
}
orderDetailMapper.insertBatch(orderDetailList);
//4.清空当前用户的购物车数据
shoppingCartMapper.deleteById(userId);
//5.封装VO返回结果
OrderSubmitVO orderSubmitVO = OrderSubmitVO.builder()
.id(orders.getId())
.orderNumber(orders.getNumber())
.orderAmount(orders.getAmount())
.build();
return orderSubmitVO;
}
}
Mapper中
@Mapper
public interface OrderMapper {
/**
* 插入一条订单数据
* @param orders
*/
void insert(Orders orders);
}
@Mapper
public interface OrderDetailMapper {
/**
* 批量插入订单明细数据
* @param orderDetailList
*/
void insertBatch(List<OrderDetail> orderDetailList);
}
对应的两个映射文件中
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="com.sky.mapper.OrderMapper">
<insert id="insert" useGeneratedKeys="true" keyProperty="id">
insert into orders(number, status, user_id, address_book_id, order_time, checkout_time, pay_method, pay_status,
amount, remark, phone, address, consignee, estimated_delivery_time, delivery_status,
pack_amount, tableware_number, tableware_status)
values
(#{number},#{status},#{userId},#{addressBookId},#{orderTime},#{checkoutTime},#{payMethod},
#{payStatus},#{amount},#{remark},#{phone},#{address},#{consignee},#{estimatedDeliveryTime},#{deliveryStatus},
#{packAmount},#{tablewareNumber},#{tablewareStatus})
</insert>
</mapper>
<mapper namespace="com.sky.mapper.OrderDetailMapper">
<insert id="insertBatch">
insert into order_detail (name, image, order_id, dish_id, setmeal_id, dish_flavor,number, amount)
values
<foreach collection="orderDetailList" item="od" separator=",">
(#{od.name},#{od.image},#{od.orderId},#{od.dishId},#{od.setmealId},#{od.dishFlavor},#{od.number},#{od.amount})
</foreach>
</insert>
</mapper>
用户下单——功能测试
下单后数据库数据正常无误
订单支付——微信支付介绍
微信支付产品
微信支付接入流程
微信小程序支付时序图
关键步骤
5.调用微信下单接口
10.调起微信支付
订单支付—— 微信支付准备工作
获取临时域名(使用cpolar做内网穿透)
此处做完内网穿透就可以在网上访问到本地服务。
在正式部署项目到服务器上之后就可以用服务器的公网IP了。
订单支付——代码导入
相关配置
订单支付——功能测试
测个屁啊,什么东西都没有。