|
|
|
@ -0,0 +1,227 @@
|
|
|
|
|
/*
|
|
|
|
|
* Copyright (c) 2018-2999 广州市蓝海创新科技有限公司 All rights reserved.
|
|
|
|
|
*
|
|
|
|
|
* https://www.mall4j.com/
|
|
|
|
|
*
|
|
|
|
|
* 未经允许,不可做商业用途!
|
|
|
|
|
*
|
|
|
|
|
* 版权所有,侵权必究!
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
package com.yami.shop.api.controller; // 定义类所在的包
|
|
|
|
|
|
|
|
|
|
import cn.hutool.core.bean.BeanUtil; // 引入Hutool工具类库中的BeanUtil工具类
|
|
|
|
|
import com.baomidou.mybatisplus.core.metadata.IPage; // 引入MyBatis-Plus的分页接口
|
|
|
|
|
import com.yami.shop.bean.app.dto.*; // 引入各种DTO类
|
|
|
|
|
import com.yami.shop.bean.enums.OrderStatus; // 引入订单状态枚举
|
|
|
|
|
import com.yami.shop.bean.model.Order; // 引入订单模型
|
|
|
|
|
import com.yami.shop.bean.model.OrderItem; // 引入订单项模型
|
|
|
|
|
import com.yami.shop.bean.model.ShopDetail; // 引入店铺详情模型
|
|
|
|
|
import com.yami.shop.bean.model.UserAddrOrder; // 引入用户地址订单模型
|
|
|
|
|
import com.yami.shop.common.exception.YamiShopBindException; // 引入自定义异常类
|
|
|
|
|
import com.yami.shop.common.response.ServerResponseEntity; // 引入服务器响应实体类
|
|
|
|
|
import com.yami.shop.common.util.Arith; // 引入算术工具类
|
|
|
|
|
import com.yami.shop.common.util.PageParam; // 引入分页参数工具类
|
|
|
|
|
import com.yami.shop.security.api.util.SecurityUtils; // 引入安全工具类
|
|
|
|
|
import com.yami.shop.service.*; // 引入各种服务类
|
|
|
|
|
import io.swagger.v3.oas.annotations.Operation; // 引入Swagger的Operation注解
|
|
|
|
|
import io.swagger.v3.oas.annotations.Parameter; // 引入Swagger的Parameter注解
|
|
|
|
|
import io.swagger.v3.oas.annotations.Parameters; // 引入Swagger的Parameters注解
|
|
|
|
|
import io.swagger.v3.oas.annotations.tags.Tag; // 引入Swagger的Tag注解
|
|
|
|
|
import lombok.AllArgsConstructor; // 引入Lombok的@AllArgsConstructor注解
|
|
|
|
|
import org.springframework.web.bind.annotation.*;
|
|
|
|
|
|
|
|
|
|
import java.util.Collections; // 引入Java的Collections工具类
|
|
|
|
|
import java.util.List; // 引入Java的List接口
|
|
|
|
|
import java.util.Objects; // 引入Java的Objects工具类
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* MyOrderController类,用于管理用户的订单操作。
|
|
|
|
|
* 该类包含获取订单详情、获取订单列表、取消订单、确认收货、删除订单和获取订单数量的方法。
|
|
|
|
|
* @作者 lanhai
|
|
|
|
|
*/
|
|
|
|
|
@RestController // 标注这是一个控制器类,并且其返回结果直接写入HTTP响应体中,而不是视图名称
|
|
|
|
|
@RequestMapping("/p/myOrder") // 定义请求路径的根地址为/p/myOrder
|
|
|
|
|
@Tag(name = "我的订单接口") // 给API文档添加标签,描述这个控制器的功能
|
|
|
|
|
@AllArgsConstructor // 使用Lombok注解生成全参构造函数
|
|
|
|
|
public class MyOrderController {
|
|
|
|
|
|
|
|
|
|
private final OrderService orderService; // 注入订单服务类
|
|
|
|
|
|
|
|
|
|
private final UserAddrOrderService userAddrOrderService; // 注入用户地址订单服务类
|
|
|
|
|
|
|
|
|
|
private final ProductService productService; // 注入商品服务类
|
|
|
|
|
|
|
|
|
|
private final SkuService skuService; // 注入SKU服务类
|
|
|
|
|
|
|
|
|
|
private final MyOrderService myOrderService; // 注入我的订单服务类
|
|
|
|
|
|
|
|
|
|
private final ShopDetailService shopDetailService; // 注入店铺详情服务类
|
|
|
|
|
|
|
|
|
|
private final OrderItemService orderItemService; // 注入订单项服务类
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 订单详情信息接口
|
|
|
|
|
* @param orderNumber 订单号
|
|
|
|
|
* @return 服务器响应实体,包含订单详情信息
|
|
|
|
|
*/
|
|
|
|
|
@GetMapping("/orderDetail")
|
|
|
|
|
@Operation(summary = "订单详情信息", description = "根据订单号获取订单详情信息")
|
|
|
|
|
@Parameter(name = "orderNumber", description = "订单号", required = true)
|
|
|
|
|
public ServerResponseEntity<OrderShopDto> orderDetail(@RequestParam(value = "orderNumber") String orderNumber) {
|
|
|
|
|
|
|
|
|
|
String userId = SecurityUtils.getUser().getUserId();
|
|
|
|
|
OrderShopDto orderShopDto = new OrderShopDto();
|
|
|
|
|
|
|
|
|
|
Order order = orderService.getOrderByOrderNumber(orderNumber);
|
|
|
|
|
|
|
|
|
|
if (order == null) {
|
|
|
|
|
throw new RuntimeException("该订单不存在");
|
|
|
|
|
}
|
|
|
|
|
if (!Objects.equals(order.getUserId(), userId)) {
|
|
|
|
|
throw new RuntimeException("你没有权限获取该订单信息");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ShopDetail shopDetail = shopDetailService.getShopDetailByShopId(order.getShopId());
|
|
|
|
|
UserAddrOrder userAddrOrder = userAddrOrderService.getById(order.getAddrOrderId());
|
|
|
|
|
UserAddrDto userAddrDto = BeanUtil.copyProperties(userAddrOrder, UserAddrDto.class);
|
|
|
|
|
List<OrderItem> orderItems = orderItemService.getOrderItemsByOrderNumber(orderNumber);
|
|
|
|
|
List<OrderItemDto> orderItemList = BeanUtil.copyToList(orderItems, OrderItemDto.class);
|
|
|
|
|
|
|
|
|
|
orderShopDto.setShopId(shopDetail.getShopId());
|
|
|
|
|
orderShopDto.setShopName(shopDetail.getShopName());
|
|
|
|
|
orderShopDto.setActualTotal(order.getActualTotal());
|
|
|
|
|
orderShopDto.setUserAddrDto(userAddrDto);
|
|
|
|
|
orderShopDto.setOrderItemDtos(orderItemList);
|
|
|
|
|
orderShopDto.setTransfee(order.getFreightAmount());
|
|
|
|
|
orderShopDto.setReduceAmount(order.getReduceAmount());
|
|
|
|
|
orderShopDto.setCreateTime(order.getCreateTime());
|
|
|
|
|
orderShopDto.setRemarks(order.getRemarks());
|
|
|
|
|
orderShopDto.setStatus(order.getStatus());
|
|
|
|
|
|
|
|
|
|
double total = 0.0;
|
|
|
|
|
Integer totalNum = 0;
|
|
|
|
|
for (OrderItemDto orderItem : orderShopDto.getOrderItemDtos()) {
|
|
|
|
|
total = Arith.add(total, orderItem.getProductTotalAmount());
|
|
|
|
|
totalNum += orderItem.getProdCount();
|
|
|
|
|
}
|
|
|
|
|
orderShopDto.setTotal(total);
|
|
|
|
|
orderShopDto.setTotalNum(totalNum);
|
|
|
|
|
|
|
|
|
|
return ServerResponseEntity.success(orderShopDto);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 订单列表接口
|
|
|
|
|
* @param status 订单状态
|
|
|
|
|
* @param page 分页参数
|
|
|
|
|
* @return 服务器响应实体,包含订单列表信息
|
|
|
|
|
*/
|
|
|
|
|
@GetMapping("/myOrder")
|
|
|
|
|
@Operation(summary = "订单列表信息", description = "根据订单状态获取订单列表信息,状态为0时获取所有订单")
|
|
|
|
|
@Parameters({
|
|
|
|
|
@Parameter(name = "status", description = "订单状态 1:待付款 2:待发货 3:待收货 4:待评价 5:成功 6:失败")
|
|
|
|
|
})
|
|
|
|
|
public ServerResponseEntity<IPage<MyOrderDto>> myOrder(@RequestParam(value = "status") Integer status, PageParam<MyOrderDto> page) {
|
|
|
|
|
String userId = SecurityUtils.getUser().getUserId();
|
|
|
|
|
IPage<MyOrderDto> myOrderDtoIpage = myOrderService.pageMyOrderByUserIdAndStatus(page, userId, status);
|
|
|
|
|
return ServerResponseEntity.success(myOrderDtoIpage);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 取消订单
|
|
|
|
|
* @param orderNumber 订单号
|
|
|
|
|
* @return 服务器响应实体
|
|
|
|
|
*/
|
|
|
|
|
@PutMapping("/cancel/{orderNumber}")
|
|
|
|
|
@Operation(summary = "根据订单号取消订单", description = "根据订单号取消订单")
|
|
|
|
|
@Parameter(name = "orderNumber", description = "订单号", required = true)
|
|
|
|
|
public ServerResponseEntity<String> cancel(@PathVariable("orderNumber") String orderNumber) {
|
|
|
|
|
String userId = SecurityUtils.getUser().getUserId();
|
|
|
|
|
Order order = orderService.getOrderByOrderNumber(orderNumber);
|
|
|
|
|
if (!Objects.equals(order.getUserId(), userId)) {
|
|
|
|
|
throw new YamiShopBindException("你没有权限获取该订单信息");
|
|
|
|
|
}
|
|
|
|
|
if (!Objects.equals(order.getStatus(), OrderStatus.UNPAY.value())) {
|
|
|
|
|
throw new YamiShopBindException("订单已支付,无法取消订单");
|
|
|
|
|
}
|
|
|
|
|
List<OrderItem> orderItems = orderItemService.getOrderItemsByOrderNumber(orderNumber);
|
|
|
|
|
order.setOrderItems(orderItems);
|
|
|
|
|
// 取消订单
|
|
|
|
|
orderService.cancelOrders(Collections.singletonList(order));
|
|
|
|
|
|
|
|
|
|
// 清除缓存
|
|
|
|
|
for (OrderItem orderItem : orderItems) {
|
|
|
|
|
productService.removeProductCacheByProdId(orderItem.getProdId());
|
|
|
|
|
skuService.removeSkuCacheBySkuId(orderItem.getSkuId(), orderItem.getProdId());
|
|
|
|
|
}
|
|
|
|
|
return ServerResponseEntity.success();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 确认收货
|
|
|
|
|
* @param orderNumber 订单号
|
|
|
|
|
* @return 服务器响应实体
|
|
|
|
|
*/
|
|
|
|
|
@PutMapping("/receipt/{orderNumber}")
|
|
|
|
|
@Operation(summary = "根据订单号确认收货", description = "根据订单号确认收货")
|
|
|
|
|
public ServerResponseEntity<String> receipt(@PathVariable("orderNumber") String orderNumber) {
|
|
|
|
|
String userId = SecurityUtils.getUser().getUserId();
|
|
|
|
|
Order order = orderService.getOrderByOrderNumber(orderNumber);
|
|
|
|
|
if (!Objects.equals(order.getUserId(), userId)) {
|
|
|
|
|
throw new YamiShopBindException("你没有权限获取该订单信息");
|
|
|
|
|
}
|
|
|
|
|
if (!Objects.equals(order.getStatus(), OrderStatus.CONSIGNMENT.value())) {
|
|
|
|
|
throw new YamiShopBindException("订单未发货,无法确认收货");
|
|
|
|
|
}
|
|
|
|
|
List<OrderItem> orderItems = orderItemService.getOrderItemsByOrderNumber(orderNumber);
|
|
|
|
|
order.setOrderItems(orderItems);
|
|
|
|
|
// 确认收货
|
|
|
|
|
orderService.confirmOrder(Collections.singletonList(order));
|
|
|
|
|
|
|
|
|
|
for (OrderItem orderItem : orderItems) {
|
|
|
|
|
productService.removeProductCacheByProdId(orderItem.getProdId());
|
|
|
|
|
skuService.removeSkuCacheBySkuId(orderItem.getSkuId(), orderItem.getProdId());
|
|
|
|
|
}
|
|
|
|
|
return ServerResponseEntity.success();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 删除订单
|
|
|
|
|
* @param orderNumber 订单号
|
|
|
|
|
* @return 服务器响应实体
|
|
|
|
|
*/
|
|
|
|
|
@DeleteMapping("/{orderNumber}")
|
|
|
|
|
@Operation(summary = "根据订单号删除订单", description = "根据订单号删除订单")
|
|
|
|
|
@Parameter(name = "orderNumber", description = "订单号", required = true)
|
|
|
|
|
public ServerResponseEntity<String> delete(@PathVariable("orderNumber") String orderNumber) {
|
|
|
|
|
String userId = SecurityUtils.getUser().getUserId();
|
|
|
|
|
|
|
|
|
|
Order order = orderService.getOrderByOrderNumber(orderNumber);
|
|
|
|
|
if (order == null) {
|
|
|
|
|
throw new YamiShopBindException("该订单不存在");
|
|
|
|
|
}
|
|
|
|
|
if (!Objects.equals(order.getUserId(), userId)) {
|
|
|
|
|
throw new YamiShopBindException("你没有权限获取该订单信息");
|
|
|
|
|
}
|
|
|
|
|
if (!Objects.equals(order.getStatus(), OrderStatus.SUCCESS.value()) && !Objects.equals(order.getStatus(), OrderStatus.CLOSE.value())) {
|
|
|
|
|
throw new YamiShopBindException("订单未完成或未关闭,无法删除订单");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 删除订单
|
|
|
|
|
orderService.deleteOrders(Collections.singletonList(order));
|
|
|
|
|
|
|
|
|
|
return ServerResponseEntity.success("删除成功");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 获取我的订单订单数量
|
|
|
|
|
*/
|
|
|
|
|
@GetMapping("/orderCount")
|
|
|
|
|
@Operation(summary = "获取我的订单订单数量", description = "获取我的订单订单数量")
|
|
|
|
|
public ServerResponseEntity<OrderCountData> getOrderCount() {
|
|
|
|
|
String userId = SecurityUtils.getUser().getUserId();
|
|
|
|
|
OrderCountData orderCountMap = orderService.getOrderCount(userId);
|
|
|
|
|
return ServerResponseEntity.success(orderCountMap);
|
|
|
|
|
}
|
|
|
|
|
}
|