完善了订单接口

This commit is contained in:
chen-xin-zhi 2024-11-03 01:12:07 +08:00
parent e11512cdaf
commit ddecad324e
12 changed files with 334 additions and 13 deletions

View File

@ -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<Boolean> addOrder(@RequestBody OrderAddRequest orderAddRequest) {
public BaseResponse<Long> 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<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 orderQueryRequest 订单查询请求体
* @return 订单列表信息
*/
@PostMapping("/list/page")
public BaseResponse<Page<Order>> listOrder(@RequestBody OrderQueryRequest orderQueryRequest) {
if (orderQueryRequest == null) {
throw new BusinessException(ErrorCode.PARAMS_ERROR);
}
long current = orderQueryRequest.getCurrent();
long pageSize = orderQueryRequest.getPageSize();
Page<Order> page = orderService.page(new Page<>(current, pageSize),
orderService.getQueryWrapper(orderQueryRequest));
return ResultUtils.success(page);
}
/**
* 查询当前订单的订单明细
* @param orderIdRequest 订单编号请求体
* @return 订单明细列表信息
*/
@PostMapping("/list/item")
public BaseResponse<List<OrderItem>> 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.eq("orderId", id);
List<OrderItem> list = orderItemService.list(queryWrapper);
return ResultUtils.success(list);
}
/**
* 更新用户订单状态
* @param orderUpdateRequest 订单状态更新请求体
* @return 是否更新成功
*/
@PostMapping("/update")
public BaseResponse<Boolean> 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<Order> 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<List<Order>> listOrder() {
List<Order> list = orderService.list();
return ResultUtils.success(list);
}
}

View File

@ -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;
}

View File

@ -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;
}

View File

@ -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;
}

View File

@ -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;
/**

View File

@ -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;

View File

@ -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<OrderItem> {
}

View File

@ -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<Order> {
/**
* 获取查询条件
*/
QueryWrapper<Order> getQueryWrapper(OrderQueryRequest orderQueryRequest);
}

View File

@ -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<OrderItemMapper, OrderItem> implements OrderItemService {
}

View File

@ -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<OrderMapper, Order> implements OrderService {
/**
* 获取查询条件
*/
@Override
public QueryWrapper<Order> 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<Order> 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;
}
}

View File

@ -0,0 +1,7 @@
<?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.cultural.heritage.mapper.OrderItemMapper">
</mapper>

View File

@ -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<Order> 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);
}
}