diff --git a/src/main/java/com/cultural/heritage/controller/order/OrderController.java b/src/main/java/com/cultural/heritage/controller/order/OrderController.java index 7b87e69..1ab1c68 100644 --- a/src/main/java/com/cultural/heritage/controller/order/OrderController.java +++ b/src/main/java/com/cultural/heritage/controller/order/OrderController.java @@ -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 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 addOrderItem(@RequestBody List orderItemAddRequestList) { +// if (CollectionUtils.isEmpty(orderItemAddRequestList)) { +// throw new BusinessException(ErrorCode.PARAMS_ERROR); +// } +// List 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 addOrder(@RequestBody OrderAddRequest orderAddRequest) { + public BaseResponse 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 addOrderItem(@RequestBody List orderItemAddRequestList) { - if (CollectionUtils.isEmpty(orderItemAddRequestList)) { - throw new BusinessException(ErrorCode.PARAMS_ERROR); - } - List 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 orderItemList = orderAddRequest.getOrderItemList(); + List 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> listOrderItem(@RequestBody CommonRequest orderIdRequest) { + public BaseResponse> listOrderItem(@RequestBody CommonRequest orderIdRequest) { if (orderIdRequest == null || orderIdRequest.getId() <= 0) { throw new BusinessException(ErrorCode.PARAMS_ERROR); } Long id = orderIdRequest.getId(); - QueryWrapper queryWrapper = new QueryWrapper<>(); + QueryWrapper queryWrapper = new QueryWrapper<>(); queryWrapper.eq("orderId", id); - List list = orderItemService.list(queryWrapper); + List list = orderItemService.list(queryWrapper); return ResultUtils.success(list); } diff --git a/src/main/java/com/cultural/heritage/mapper/OrderItemMapper.java b/src/main/java/com/cultural/heritage/mapper/OrderItemMapper.java index af78139..648711d 100644 --- a/src/main/java/com/cultural/heritage/mapper/OrderItemMapper.java +++ b/src/main/java/com/cultural/heritage/mapper/OrderItemMapper.java @@ -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 { +public interface OrderItemMapper extends BaseMapper { } diff --git a/src/main/java/com/cultural/heritage/model/dto/order/OrderAddRequest.java b/src/main/java/com/cultural/heritage/model/dto/order/OrderAddRequest.java index 1b8c423..0b1b556 100644 --- a/src/main/java/com/cultural/heritage/model/dto/order/OrderAddRequest.java +++ b/src/main/java/com/cultural/heritage/model/dto/order/OrderAddRequest.java @@ -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 orderItemList; + + + @Serial private static final long serialVersionUID = 1L; } diff --git a/src/main/java/com/cultural/heritage/model/entity/Order.java b/src/main/java/com/cultural/heritage/model/entity/Order.java index bf38897..43549ef 100644 --- a/src/main/java/com/cultural/heritage/model/entity/Order.java +++ b/src/main/java/com/cultural/heritage/model/entity/Order.java @@ -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; + /** * 订单总金额 */ diff --git a/src/main/java/com/cultural/heritage/model/entity/OrderItem.java b/src/main/java/com/cultural/heritage/model/entity/OrderItems.java similarity index 67% rename from src/main/java/com/cultural/heritage/model/entity/OrderItem.java rename to src/main/java/com/cultural/heritage/model/entity/OrderItems.java index dac82bb..76426b0 100644 --- a/src/main/java/com/cultural/heritage/model/entity/OrderItem.java +++ b/src/main/java/com/cultural/heritage/model/entity/OrderItems.java @@ -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; diff --git a/src/main/java/com/cultural/heritage/service/order/OrderItemService.java b/src/main/java/com/cultural/heritage/service/order/OrderItemService.java index 26e7f9f..96d3df5 100644 --- a/src/main/java/com/cultural/heritage/service/order/OrderItemService.java +++ b/src/main/java/com/cultural/heritage/service/order/OrderItemService.java @@ -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 { +public interface OrderItemService extends IService { } diff --git a/src/main/java/com/cultural/heritage/service/order/impl/OrderItemServiceImpl.java b/src/main/java/com/cultural/heritage/service/order/impl/OrderItemServiceImpl.java index 7fac3db..0426d5f 100644 --- a/src/main/java/com/cultural/heritage/service/order/impl/OrderItemServiceImpl.java +++ b/src/main/java/com/cultural/heritage/service/order/impl/OrderItemServiceImpl.java @@ -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 implements OrderItemService { +public class OrderItemServiceImpl extends ServiceImpl implements OrderItemService { }