💎一站式轻松地调用各大LLM模型接口,支持GPT4、智谱、星火、月之暗面及文生图 广告
# 数据库设计 ![](https://box.kancloud.cn/a1ed19f8a3cbc7aaffd1fc0f8565d4ca_615x635.png) 包括订单主表、订单信息表、商品信息表、商品类型表 # 服务环境设计 ![](https://box.kancloud.cn/b3f49a47a44fcdf30e93af9e56ff1543_1752x900.png) # Dao设计 以订单接口为例 订单dao层示例 ~~~ public interface OrderDetailRepository extends JpaRepository<OrderDetail, String>{ List<OrderDetail> findByOrderId(String orderId); } ~~~ 订餐实现层示例 ~~~ public interface OrderService { //创建订单 OrderDTO create(OrderDTO orderDTO); //查询单个订单详情 OrderDTO findOne(String orderId); //查询订单总列表(买家用) Page<OrderDTO> findList(String buyerOpenid, Pageable pageable); //取消订单 OrderDTO cancel(OrderDTO orderDTO); //完结订单 OrderDTO finish(OrderDTO orderDTO); //支付订单 OrderDTO paid(OrderDTO orderDTO); //查询订单列表(卖家管理系统用的) Page<OrderDTO> findList(Pageable pageable); } ~~~ 实现层细节: ~~~ @Service @Slf4j public class OrderServiceImpl implements OrderService { @Autowired private ProductService productService; @Autowired private OrderDetailRepository orderDetailRepository; @Autowired private OrderMasterRepository orderMasterRepository; @Autowired private PayService payService; @Autowired private WebSocket webSocket; @Override @Transactional//事务管理,一旦失败就回滚 public OrderDTO create(OrderDTO orderDTO) { //设置下订单id(是个随机,这里调用了根据时间产生6位随机数的方法) String orderId = KeyUtil.genUniqueKey(); //给总价赋值 BigDecimal orderAmount = new BigDecimal(BigInteger.ZERO); //List<CartDTO> cartDTOList = new ArrayList<>(); //1.查询商品(数量,价格) for (OrderDetail orderDetail : orderDTO.getOrderDetailList()){ ProductInfo productInfo = productService.findOne(orderDetail.getProductId()); if(productInfo == null){ throw new SellException(ResultEnum.PRODUCT_NOT_EXIST); } //2.计算总价=单价*数量+orderAmount orderAmount = productInfo.getProductPrice() .multiply(new BigDecimal(orderDetail.getProductQuantity())) .add(orderAmount); //3.订单详情入库(OrderMaster和orderDetail) //利用BeanUtils方法把前端查找出来的productInfo商品信息复制给订单详情 BeanUtils.copyProperties(productInfo, orderDetail);//先复制,再赋值 orderDetail.setDetailId(KeyUtil.genUniqueKey()); orderDetail.setOrderId(orderId); orderDetailRepository.save(orderDetail); /* CartDTO cartDTO = new CartDTO(orderDetail.getProductId(), orderDetail.getProductQuantity()); cartDTOList.add(cartDTO);*/ } //3.订单总表入库(OrderMaster和orderDetail) OrderMaster orderMaster = new OrderMaster(); orderDTO.setOrderId(orderId); BeanUtils.copyProperties(orderDTO, orderMaster); orderMaster.setOrderAmount(orderAmount);//是一个整个订单的总价,所以在foe循环之外设置 orderMaster.setOrderStatus(OrderStatusEnum.New.getCode()); orderMaster.setPayStatus(PayStatusEnum.WAIT.getCode()); orderMasterRepository.save(orderMaster); //4.扣库存 List<CartDTO> cartDTOList = orderDTO.getOrderDetailList().stream().map(e -> new CartDTO(e.getProductId(), e.getProductQuantity()) ).collect(Collectors.toList()); productService.decreaseStock(cartDTOList); //发送websocket消息 webSocket.sendMessage(orderDTO.getOrderId()); return orderDTO; } ~~~ # 控制层示例 ~~~ @RestController @RequestMapping("/buyer/order") @Slf4j public class BuyerOrderController { @Autowired private OrderService orderService; @Autowired private BuyerService buyerService; //创建订单 @PostMapping(value = "/create") public ResultVO<Map<String, String>> creat(@Valid OrderForm orderForm, BindingResult bindingResult){ if(bindingResult.hasErrors()){ log.error("【创建订单】 参数不正确, orderForm={}", orderForm); throw new SellException(ResultEnum.PARAM_ERROR.getCode(), bindingResult.getFieldError().getDefaultMessage()); } OrderDTO orderDTO = OrderFormZOrderDTOConverter.convert(orderForm); if(CollectionUtils.isEmpty(orderDTO.getOrderDetailList())){ log.error("【创建订单】 购物车不能为空"); throw new SellException(ResultEnum.CART_EMPTY); } OrderDTO createResult = orderService.create(orderDTO); Map<String, String> map = new HashMap<>(); map.put("orderId", createResult.getOrderId()); return ResultVOUtil.success(map); } //订单列表 @GetMapping(value = "/list") public ResultVO<List<OrderDTO>> list(@RequestParam("openid") String openid, @RequestParam(value = "page", defaultValue = "0") Integer page, @RequestParam(value = "size", defaultValue = "10") Integer size){ if(StringUtils.isNullOrEmpty(openid)){ log.error("【查询订单列表】 openid为空"); throw new SellException(ResultEnum.PARAM_ERROR); } PageRequest request = new PageRequest(page, size); Page<OrderDTO> orderDTOPage = orderService.findList(openid, request); //只用返回当前页面的数据集合就行了,因为前端传过来的就是第几页和每一页的size(一般都会定好) return ResultVOUtil.success(orderDTOPage.getContent()); } //订单详情 @GetMapping("/detail") public ResultVO<OrderDTO> detail(@RequestParam("openid") String openid, @RequestParam("orderId") String orderId){ /* //TODO 不安全的做法,改进 OrderDTO orderDTO = orderService.findOne(orderId);*/ OrderDTO orderDTO = buyerService.findOrderOne(openid, orderId); return ResultVOUtil.success(orderDTO); } //取消订单 @PostMapping("/cancel") public ResultVO cancel(@RequestParam("openid") String openid, @RequestParam("orderId") String orderId){ /* //TODO 不安全的做法,改进 OrderDTO orderDTO = orderService.findOne(orderId); orderService.cancle(orderDTO);*/ buyerService.cancelOrder(openid, orderId); return ResultVOUtil.success(); } } ~~~