完善了订单接口

This commit is contained in:
chen-xin-zhi 2024-11-05 18:59:01 +08:00
parent 3471a30fab
commit 9de330f739
7 changed files with 102 additions and 40 deletions

View File

@ -16,7 +16,7 @@ import com.cultural.heritage.model.dto.order.OrderItemAddRequest;
import com.cultural.heritage.model.dto.order.OrderQueryRequest;
import com.cultural.heritage.model.dto.order.OrderUpdateRequest;
import com.cultural.heritage.model.entity.Order;
import com.cultural.heritage.model.entity.OrderItem;
import com.cultural.heritage.model.entity.OrderItems;
import com.cultural.heritage.service.order.OrderItemService;
import com.cultural.heritage.service.order.OrderService;
import io.swagger.v3.oas.annotations.Operation;
@ -25,7 +25,6 @@ import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
@ -48,48 +47,79 @@ public class OrderController {
@Resource
private OrderItemService orderItemService;
//
// /**
// * 用户创建订单
// * @param orderAddRequest 订单创建请求体
// * @return 是否创建成功
// */
// @PostMapping("/add")
// @Operation(summary = "小程序端创建用户订单", description = "参数订单创建请求体权限所有人方法名addOrder")
// public BaseResponse<Long> addOrder(@RequestBody OrderAddRequest orderAddRequest) {
// if (orderAddRequest == null) {
// throw new BusinessException(ErrorCode.PARAMS_ERROR);
// }
// Order order = new Order();
// BeanUtils.copyProperties(orderAddRequest, order);
// boolean save = orderService.save(order);
// ThrowUtils.throwIf(!save, ErrorCode.OPERATION_ERROR);
// return ResultUtils.success(order.getId());
// }
//
//
// /**
// * 用户添加订单明细
// * @param orderItemAddRequestList 订单明细添加请求体
// * @return 是否添加成功
// */
// @PostMapping("/add/item")
// @Operation(summary = "小程序端批量添加用户订单明细", description = "参数订单明细添加请求体权限所有人方法名addOrderItem")
// public BaseResponse<Boolean> addOrderItem(@RequestBody List<OrderItemAddRequest> orderItemAddRequestList) {
// if (CollectionUtils.isEmpty(orderItemAddRequestList)) {
// throw new BusinessException(ErrorCode.PARAMS_ERROR);
// }
// List<OrderItem> orderItems = orderItemAddRequestList.stream().map(orderItemAddRequest -> {
// OrderItem orderItem = new OrderItem();
// BeanUtils.copyProperties(orderItemAddRequest, orderItem);
// return orderItem;
// }).toList();
// boolean result = orderItemService.saveBatch(orderItems);
// ThrowUtils.throwIf(!result, ErrorCode.OPERATION_ERROR);
// return ResultUtils.success(true, "添加订单明细成功");
// }
/**
* 用户创建订单
* @param orderAddRequest 订单创建请求体
* @param orderAddRequest 订单创建请求体
* @return 是否创建成功
*/
@PostMapping("/add")
@Operation(summary = "小程序端创建用户订单", description = "参数订单创建请求体权限所有人方法名addOrder")
public BaseResponse<Long> addOrder(@RequestBody OrderAddRequest orderAddRequest) {
public BaseResponse<Boolean> addOrder(@RequestBody OrderAddRequest orderAddRequest) {
if (orderAddRequest == null) {
throw new BusinessException(ErrorCode.PARAMS_ERROR);
}
// 创建一个总订单
Order order = new Order();
BeanUtils.copyProperties(orderAddRequest, order);
boolean save = orderService.save(order);
ThrowUtils.throwIf(!save, ErrorCode.OPERATION_ERROR);
return ResultUtils.success(order.getId());
}
/**
* 用户添加订单明细
* @param orderItemAddRequestList 订单明细添加请求体
* @return 是否添加成功
*/
@PostMapping("/add/item")
@Operation(summary = "小程序端批量添加用户订单明细", description = "参数订单明细添加请求体权限所有人方法名addOrderItem")
public BaseResponse<Boolean> addOrderItem(@RequestBody List<OrderItemAddRequest> orderItemAddRequestList) {
if (CollectionUtils.isEmpty(orderItemAddRequestList)) {
throw new BusinessException(ErrorCode.PARAMS_ERROR);
}
List<OrderItem> orderItems = orderItemAddRequestList.stream().map(orderItemAddRequest -> {
OrderItem orderItem = new OrderItem();
BeanUtils.copyProperties(orderItemAddRequest, orderItem);
return orderItem;
}).toList();
boolean result = orderItemService.saveBatch(orderItems);
boolean result = orderService.save(order);
ThrowUtils.throwIf(!result, ErrorCode.OPERATION_ERROR);
return ResultUtils.success(true, "添加订单明细成功");
// 创建订单明细
Long id = order.getId();
List<OrderItemAddRequest> orderItemList = orderAddRequest.getOrderItemList();
List<OrderItems> newOrderItemsList = orderItemList.stream().map(orderItem -> {
OrderItems orderItems = new OrderItems();
BeanUtils.copyProperties(orderItem, orderItems);
orderItems.setOrderId(id);
return orderItems;
}).toList();
boolean save = orderItemService.saveBatch(newOrderItemsList);
ThrowUtils.throwIf(!save, ErrorCode.OPERATION_ERROR);
return ResultUtils.success(true);
}
/**
* 分页查询所有订单
* @param orderQueryRequest 订单查询请求体
@ -119,14 +149,14 @@ public class OrderController {
@PostMapping("/list/item")
@Operation(summary = "Web端管理员查询订单明细", description = "参数:订单编号请求体,权限:管理员(admin, boss)方法名listOrderItem")
@AuthCheck(mustRole = UserConstant.ADMIN_ROLE)
public BaseResponse<List<OrderItem>> listOrderItem(@RequestBody CommonRequest orderIdRequest) {
public BaseResponse<List<OrderItems>> listOrderItem(@RequestBody CommonRequest orderIdRequest) {
if (orderIdRequest == null || orderIdRequest.getId() <= 0) {
throw new BusinessException(ErrorCode.PARAMS_ERROR);
}
Long id = orderIdRequest.getId();
QueryWrapper<OrderItem> queryWrapper = new QueryWrapper<>();
QueryWrapper<OrderItems> queryWrapper = new QueryWrapper<>();
queryWrapper.eq("orderId", id);
List<OrderItem> list = orderItemService.list(queryWrapper);
List<OrderItems> list = orderItemService.list(queryWrapper);
return ResultUtils.success(list);
}

View File

@ -1,7 +1,7 @@
package com.cultural.heritage.mapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.cultural.heritage.model.entity.OrderItem;
import com.cultural.heritage.model.entity.OrderItems;
public interface OrderItemMapper extends BaseMapper<OrderItem> {
public interface OrderItemMapper extends BaseMapper<OrderItems> {
}

View File

@ -8,10 +8,11 @@ import lombok.Data;
import java.io.Serial;
import java.io.Serializable;
import java.util.List;
@Data
@Schema(description = "订单添加请求体", requiredProperties = {"userId", "orderNumber", "addressSnapshot", "contactsSnapshot",
"couponSnapshot", "totalAmount", "orderStatus"})
"couponSnapshot", "totalAmount", "orderStatus", "orderItemList"})
public class OrderAddRequest implements Serializable {
@ -21,6 +22,13 @@ public class OrderAddRequest implements Serializable {
@Schema(description = "用户id(id > 0)", example = "2")
private Long userId;
/**
* 用户昵称
*/
@Schema(description = "用户昵称", example = "张三")
private String userName;
/**
* 订单编号
*/
@ -62,6 +70,14 @@ public class OrderAddRequest implements Serializable {
private String orderStatus;
/**
* 订单明细信息
*/
@Schema(description = "订单明细信息")
private List<OrderItemAddRequest> orderItemList;
@Serial
private static final long serialVersionUID = 1L;
}

View File

@ -9,6 +9,7 @@ import com.cultural.heritage.model.dto.snapshot.AddressSnapshot;
import com.cultural.heritage.model.dto.snapshot.ContactsSnapshot;
import com.cultural.heritage.model.dto.snapshot.CouponSnapshot;
import com.fasterxml.jackson.annotation.JsonFormat;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;
import java.io.Serial;
@ -43,6 +44,13 @@ public class Order implements Serializable {
private Long userId;
/**
* 用户昵称
*/
@Schema(description = "用户昵称", example = "张三")
private String userName;
/**
* 地址信息快照
*/
@ -63,6 +71,7 @@ public class Order implements Serializable {
private CouponSnapshot couponSnapshot;
/**
* 订单总金额
*/

View File

@ -6,6 +6,7 @@ import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import com.baomidou.mybatisplus.extension.handlers.JacksonTypeHandler;
import com.cultural.heritage.model.dto.snapshot.GoodSnapshot;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;
import java.io.Serial;
@ -17,18 +18,21 @@ import java.io.Serializable;
*/
@Data
@TableName(value = "order_item", autoResultMap = true)
public class OrderItem implements Serializable {
@Schema(description = "订单明细", requiredProperties = {"id", "orderId", "goodSnapshot", "priceSnapshot", "quantity", "itemTotalAmount"})
public class OrderItems implements Serializable {
/**
* 订单明细id
*/
@TableId(type = IdType.AUTO)
@Schema(description = "订单明细id(id > 0)", example = "2")
private Long id;
/**
* 关联的订单id
*/
@Schema(description = "关联的订单id(id > 0)", example = "3")
private Long orderId;
@ -42,18 +46,21 @@ public class OrderItem implements Serializable {
/**
* 商品单价快照
*/
@Schema(description = "商品单价", example = "15.00")
private Double priceSnapshot;
/**
* 购买数量
*/
@Schema(description = "购买数量", example = "30")
private Integer quantity;
/**
* 订单项金额单价 * 数量
*/
@Schema(description = "订单项金额(单价 * 数量)", example = "55.00")
private Double itemTotalAmount;

View File

@ -1,8 +1,8 @@
package com.cultural.heritage.service.order;
import com.baomidou.mybatisplus.extension.service.IService;
import com.cultural.heritage.model.entity.OrderItem;
import com.cultural.heritage.model.entity.OrderItems;
public interface OrderItemService extends IService<OrderItem> {
public interface OrderItemService extends IService<OrderItems> {
}

View File

@ -2,11 +2,11 @@ package com.cultural.heritage.service.order.impl;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cultural.heritage.mapper.OrderItemMapper;
import com.cultural.heritage.model.entity.OrderItem;
import com.cultural.heritage.model.entity.OrderItems;
import com.cultural.heritage.service.order.OrderItemService;
import org.springframework.stereotype.Service;
@Service
public class OrderItemServiceImpl extends ServiceImpl<OrderItemMapper, OrderItem> implements OrderItemService {
public class OrderItemServiceImpl extends ServiceImpl<OrderItemMapper, OrderItems> implements OrderItemService {
}