From ddecad324eeca822b4dd1898dc08d8e59e0ad654 Mon Sep 17 00:00:00 2001 From: chen-xin-zhi <3588068430@qq.com> Date: Sun, 3 Nov 2024 01:12:07 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=8C=E5=96=84=E4=BA=86=E8=AE=A2=E5=8D=95?= =?UTF-8?q?=E6=8E=A5=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../controller/order/OrderController.java | 112 ++++++++++++++++-- .../model/dto/order/OrderItemAddRequest.java | 44 +++++++ .../model/dto/order/OrderQueryRequest.java | 65 ++++++++++ .../model/dto/order/OrderUpdateRequest.java | 26 ++++ .../model/dto/snapshot/CouponSnapshot.java | 3 + .../heritage/model/entity/OrderItem.java | 5 +- .../service/order/OrderItemService.java | 8 ++ .../heritage/service/order/OrderService.java | 8 ++ .../order/impl/OrderItemServiceImpl.java | 12 ++ .../service/order/impl/OrderServiceImpl.java | 40 +++++++ src/main/resources/mapper/OrderItemMapper.xml | 7 ++ .../java/com/cultural/heritage/test/Test.java | 17 ++- 12 files changed, 334 insertions(+), 13 deletions(-) create mode 100644 src/main/java/com/cultural/heritage/model/dto/order/OrderItemAddRequest.java create mode 100644 src/main/java/com/cultural/heritage/model/dto/order/OrderQueryRequest.java create mode 100644 src/main/java/com/cultural/heritage/model/dto/order/OrderUpdateRequest.java create mode 100644 src/main/java/com/cultural/heritage/service/order/OrderItemService.java create mode 100644 src/main/java/com/cultural/heritage/service/order/impl/OrderItemServiceImpl.java create mode 100644 src/main/resources/mapper/OrderItemMapper.xml 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 9f2d3f1..dd52e36 100644 --- a/src/main/java/com/cultural/heritage/controller/order/OrderController.java +++ b/src/main/java/com/cultural/heritage/controller/order/OrderController.java @@ -1,18 +1,29 @@ package com.cultural.heritage.controller.order; +import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; +import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper; +import com.baomidou.mybatisplus.extension.plugins.pagination.Page; import com.cultural.heritage.common.BaseResponse; import com.cultural.heritage.common.ErrorCode; import com.cultural.heritage.common.ResultUtils; +import com.cultural.heritage.constant.CommonConstant; import com.cultural.heritage.exception.BusinessException; import com.cultural.heritage.exception.ThrowUtils; +import com.cultural.heritage.model.dto.CommonRequest; import com.cultural.heritage.model.dto.order.OrderAddRequest; +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.service.order.OrderItemService; import com.cultural.heritage.service.order.OrderService; import io.swagger.v3.oas.annotations.tags.Tag; import jakarta.annotation.Resource; import lombok.extern.slf4j.Slf4j; -import org.aspectj.weaver.ast.Or; 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; @@ -24,19 +35,25 @@ import java.util.List; @RequestMapping("/order") @Slf4j @Tag(name = "订单接口") +@Transactional(rollbackFor = Exception.class) public class OrderController { @Resource private OrderService orderService; + + @Resource + private OrderItemService orderItemService; + + /** * 用户创建订单 * @param orderAddRequest 订单创建请求体 * @return 是否创建成功 */ @PostMapping("/add") - public BaseResponse addOrder(@RequestBody OrderAddRequest orderAddRequest) { + public BaseResponse addOrder(@RequestBody OrderAddRequest orderAddRequest) { if (orderAddRequest == null) { throw new BusinessException(ErrorCode.PARAMS_ERROR); } @@ -44,16 +61,89 @@ public class OrderController { 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") + 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 orderQueryRequest 订单查询请求体 + * @return 订单列表信息 + */ + @PostMapping("/list/page") + public BaseResponse> listOrder(@RequestBody OrderQueryRequest orderQueryRequest) { + if (orderQueryRequest == null) { + throw new BusinessException(ErrorCode.PARAMS_ERROR); + } + long current = orderQueryRequest.getCurrent(); + long pageSize = orderQueryRequest.getPageSize(); + Page page = orderService.page(new Page<>(current, pageSize), + orderService.getQueryWrapper(orderQueryRequest)); + return ResultUtils.success(page); + } + + + + /** + * 查询当前订单的订单明细 + * @param orderIdRequest 订单编号请求体 + * @return 订单明细列表信息 + */ + @PostMapping("/list/item") + 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.eq("orderId", id); + List list = orderItemService.list(queryWrapper); + return ResultUtils.success(list); + } + + + + + /** + * 更新用户订单状态 + * @param orderUpdateRequest 订单状态更新请求体 + * @return 是否更新成功 + */ + @PostMapping("/update") + public BaseResponse updateOrderStatus(@RequestBody OrderUpdateRequest orderUpdateRequest) { + if (orderUpdateRequest == null || orderUpdateRequest.getId() <= 0) { + throw new BusinessException(ErrorCode.PARAMS_ERROR); + } + Long id = orderUpdateRequest.getId(); + String orderStatus = orderUpdateRequest.getOrderStatus(); + UpdateWrapper updateWrapper = new UpdateWrapper<>(); + updateWrapper.eq("id", id); + updateWrapper.set("orderStatus", orderStatus); + boolean result = orderService.update(updateWrapper); + ThrowUtils.throwIf(!result, ErrorCode.OPERATION_ERROR); return ResultUtils.success(true); } - /** - * 查询所有订单 - * @return - */ - @PostMapping("/list") - public BaseResponse> listOrder() { - List list = orderService.list(); - return ResultUtils.success(list); - } + } diff --git a/src/main/java/com/cultural/heritage/model/dto/order/OrderItemAddRequest.java b/src/main/java/com/cultural/heritage/model/dto/order/OrderItemAddRequest.java new file mode 100644 index 0000000..62684a1 --- /dev/null +++ b/src/main/java/com/cultural/heritage/model/dto/order/OrderItemAddRequest.java @@ -0,0 +1,44 @@ +package com.cultural.heritage.model.dto.order; + +import com.cultural.heritage.model.dto.snapshot.GoodSnapshot; +import lombok.Data; + +import java.io.Serial; +import java.io.Serializable; + +@Data +public class OrderItemAddRequest implements Serializable { + + + /** + * 关联的订单id + */ + private Long orderId; + + + /** + * 商品信息快照 + */ + private GoodSnapshot goodSnapshot; + + + /** + * 商品单价快照 + */ + private Double priceSnapshot; + + + /** + * 购买数量 + */ + private Integer quantity; + + + /** + * 订单项金额(单价 * 数量) + */ + private Double itemTotalAmount; + + @Serial + private static final long serialVersionUID = 1L; +} diff --git a/src/main/java/com/cultural/heritage/model/dto/order/OrderQueryRequest.java b/src/main/java/com/cultural/heritage/model/dto/order/OrderQueryRequest.java new file mode 100644 index 0000000..e301a0a --- /dev/null +++ b/src/main/java/com/cultural/heritage/model/dto/order/OrderQueryRequest.java @@ -0,0 +1,65 @@ +package com.cultural.heritage.model.dto.order; + +import com.baomidou.mybatisplus.annotation.IdType; +import com.baomidou.mybatisplus.annotation.TableField; +import com.baomidou.mybatisplus.annotation.TableId; +import com.baomidou.mybatisplus.extension.handlers.JacksonTypeHandler; +import com.cultural.heritage.common.PageRequest; +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 lombok.Data; + +import java.io.Serial; +import java.io.Serializable; +import java.util.Date; + +@Data +public class OrderQueryRequest extends PageRequest implements Serializable { + + + /** + * 用户id + */ + private Long userId; + + + /** + * 订单最小金额 + */ + private Double minTotalAmount; + + + /** + * 订单最大金额 + */ + private Double maxTotalAmount; + + + /** + * 订单状态 + */ + private String orderStatus; + + + /** + * 开始时间 + */ + @JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8") + private Date startTime; + + + + /** + * 结束时间 + */ + @JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8") + private Date endTime; + + + + + @Serial + private static final long serialVersionUID = 1L; +} diff --git a/src/main/java/com/cultural/heritage/model/dto/order/OrderUpdateRequest.java b/src/main/java/com/cultural/heritage/model/dto/order/OrderUpdateRequest.java new file mode 100644 index 0000000..3e1f738 --- /dev/null +++ b/src/main/java/com/cultural/heritage/model/dto/order/OrderUpdateRequest.java @@ -0,0 +1,26 @@ +package com.cultural.heritage.model.dto.order; + +import lombok.Data; + +import java.io.Serial; +import java.io.Serializable; + +@Data +public class OrderUpdateRequest implements Serializable { + + /** + * 订单id + */ + private Long id; + + + /** + * 订单状态 + */ + private String orderStatus; + + + + @Serial + private static final long serialVersionUID = 1L; +} diff --git a/src/main/java/com/cultural/heritage/model/dto/snapshot/CouponSnapshot.java b/src/main/java/com/cultural/heritage/model/dto/snapshot/CouponSnapshot.java index 2fe5743..5d098e0 100644 --- a/src/main/java/com/cultural/heritage/model/dto/snapshot/CouponSnapshot.java +++ b/src/main/java/com/cultural/heritage/model/dto/snapshot/CouponSnapshot.java @@ -1,5 +1,6 @@ package com.cultural.heritage.model.dto.snapshot; +import com.fasterxml.jackson.annotation.JsonFormat; import lombok.Data; import java.io.Serial; @@ -34,12 +35,14 @@ public class CouponSnapshot implements Serializable { /** * 有效开始日期 */ + @JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8") private Date startTime; /** * 有效截止日期 */ + @JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8") private Date endTime; /** diff --git a/src/main/java/com/cultural/heritage/model/entity/OrderItem.java b/src/main/java/com/cultural/heritage/model/entity/OrderItem.java index a39dcf0..dac82bb 100644 --- a/src/main/java/com/cultural/heritage/model/entity/OrderItem.java +++ b/src/main/java/com/cultural/heritage/model/entity/OrderItem.java @@ -1,8 +1,10 @@ package com.cultural.heritage.model.entity; import com.baomidou.mybatisplus.annotation.IdType; +import com.baomidou.mybatisplus.annotation.TableField; 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 lombok.Data; @@ -14,7 +16,7 @@ import java.io.Serializable; * @TableName order_item */ @Data -@TableName("order_item") +@TableName(value = "order_item", autoResultMap = true) public class OrderItem implements Serializable { /** @@ -33,6 +35,7 @@ public class OrderItem implements Serializable { /** * 商品信息快照 */ + @TableField(typeHandler = JacksonTypeHandler.class) private GoodSnapshot goodSnapshot; diff --git a/src/main/java/com/cultural/heritage/service/order/OrderItemService.java b/src/main/java/com/cultural/heritage/service/order/OrderItemService.java new file mode 100644 index 0000000..26e7f9f --- /dev/null +++ b/src/main/java/com/cultural/heritage/service/order/OrderItemService.java @@ -0,0 +1,8 @@ +package com.cultural.heritage.service.order; + +import com.baomidou.mybatisplus.extension.service.IService; +import com.cultural.heritage.model.entity.OrderItem; + +public interface OrderItemService extends IService { + +} diff --git a/src/main/java/com/cultural/heritage/service/order/OrderService.java b/src/main/java/com/cultural/heritage/service/order/OrderService.java index c43f11a..d577d80 100644 --- a/src/main/java/com/cultural/heritage/service/order/OrderService.java +++ b/src/main/java/com/cultural/heritage/service/order/OrderService.java @@ -1,7 +1,15 @@ package com.cultural.heritage.service.order; +import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import com.baomidou.mybatisplus.extension.service.IService; +import com.cultural.heritage.model.dto.order.OrderQueryRequest; import com.cultural.heritage.model.entity.Order; public interface OrderService extends IService { + + + /** + * 获取查询条件 + */ + QueryWrapper getQueryWrapper(OrderQueryRequest orderQueryRequest); } 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 new file mode 100644 index 0000000..7fac3db --- /dev/null +++ b/src/main/java/com/cultural/heritage/service/order/impl/OrderItemServiceImpl.java @@ -0,0 +1,12 @@ +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.service.order.OrderItemService; +import org.springframework.stereotype.Service; + +@Service +public class OrderItemServiceImpl extends ServiceImpl implements OrderItemService { + +} diff --git a/src/main/java/com/cultural/heritage/service/order/impl/OrderServiceImpl.java b/src/main/java/com/cultural/heritage/service/order/impl/OrderServiceImpl.java index 2e11143..3b06b51 100644 --- a/src/main/java/com/cultural/heritage/service/order/impl/OrderServiceImpl.java +++ b/src/main/java/com/cultural/heritage/service/order/impl/OrderServiceImpl.java @@ -1,11 +1,51 @@ package com.cultural.heritage.service.order.impl; +import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; +import com.cultural.heritage.common.ErrorCode; +import com.cultural.heritage.constant.CommonConstant; +import com.cultural.heritage.exception.BusinessException; import com.cultural.heritage.mapper.OrderMapper; +import com.cultural.heritage.model.dto.order.OrderQueryRequest; import com.cultural.heritage.model.entity.Order; import com.cultural.heritage.service.order.OrderService; +import com.cultural.heritage.utils.SqlUtils; +import io.netty.util.internal.StringUtil; +import org.apache.commons.lang3.ObjectUtils; +import org.apache.commons.lang3.StringUtils; import org.springframework.stereotype.Service; +import java.util.Date; + @Service public class OrderServiceImpl extends ServiceImpl implements OrderService { + + + /** + * 获取查询条件 + */ + @Override + public QueryWrapper getQueryWrapper(OrderQueryRequest orderQueryRequest) { + if (orderQueryRequest == null) { + throw new BusinessException(ErrorCode.PARAMS_ERROR); + } + Long userId = orderQueryRequest.getUserId(); + Double minTotalAmount = orderQueryRequest.getMinTotalAmount(); + Double maxTotalAmount = orderQueryRequest.getMaxTotalAmount(); + String orderStatus = orderQueryRequest.getOrderStatus(); + Date startTime = orderQueryRequest.getStartTime(); + Date endTime = orderQueryRequest.getEndTime(); + String sortOrder = orderQueryRequest.getSortOrder(); + String sortField = orderQueryRequest.getSortField(); + + QueryWrapper queryWrapper = new QueryWrapper<>(); + queryWrapper.eq(ObjectUtils.isNotEmpty(userId), "userId", userId); + queryWrapper.ge(ObjectUtils.isNotEmpty(minTotalAmount), "totalAmount", minTotalAmount); + queryWrapper.le(ObjectUtils.isNotEmpty(maxTotalAmount), "totalAmount", maxTotalAmount); + queryWrapper.eq(StringUtils.isNotBlank(orderStatus), "orderStatus", orderStatus); + queryWrapper.between("createTime", startTime, endTime); + queryWrapper.orderBy(SqlUtils.validSortField(sortField), sortOrder.equals(CommonConstant.SORT_ORDER_ASC), + sortField); + return queryWrapper; + } } diff --git a/src/main/resources/mapper/OrderItemMapper.xml b/src/main/resources/mapper/OrderItemMapper.xml new file mode 100644 index 0000000..23ab152 --- /dev/null +++ b/src/main/resources/mapper/OrderItemMapper.xml @@ -0,0 +1,7 @@ + + + + + \ No newline at end of file diff --git a/src/test/java/com/cultural/heritage/test/Test.java b/src/test/java/com/cultural/heritage/test/Test.java index d9a6df6..6790bbe 100644 --- a/src/test/java/com/cultural/heritage/test/Test.java +++ b/src/test/java/com/cultural/heritage/test/Test.java @@ -1,13 +1,28 @@ package com.cultural.heritage.test; +import com.cultural.heritage.model.entity.Order; +import org.apache.commons.lang3.ObjectUtils; +import org.springframework.beans.BeanUtils; + import java.text.SimpleDateFormat; +import java.util.ArrayList; +import java.util.Collection; import java.util.Date; +import java.util.List; public class Test { public static void main(String[] args) { +// Date date = new Date(); +// SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); +// String format = sdf.format(date); +// System.out.println(format); + + List list = new ArrayList<>(); + System.out.println(ObjectUtils.isEmpty(list)); Date date = new Date(); - SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); + SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); String format = sdf.format(date); System.out.println(format); + } }