添加获取订单接口

This commit is contained in:
muhua 2024-07-13 14:54:42 +08:00
parent fb4bdbdde3
commit 677b1df832
5 changed files with 130 additions and 4 deletions

View File

@ -1,6 +1,8 @@
package com.example.takeawaysystemserver.controller; package com.example.takeawaysystemserver.controller;
import com.example.takeawaysystemserver.entity.OrderItem;
import com.example.takeawaysystemserver.entity.OrderList; import com.example.takeawaysystemserver.entity.OrderList;
import com.example.takeawaysystemserver.model.vo.OrderVO;
import com.example.takeawaysystemserver.service.OrderService; import com.example.takeawaysystemserver.service.OrderService;
import com.example.takeawaysystemserver.util.R; import com.example.takeawaysystemserver.util.R;
import io.swagger.annotations.Api; import io.swagger.annotations.Api;
@ -8,6 +10,8 @@ import io.swagger.annotations.ApiOperation;
import jakarta.annotation.Resource; import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*; import org.springframework.web.bind.annotation.*;
import java.util.List;
/** /**
* @author Ethereal * @author Ethereal
* @date 2024/7/12 * @date 2024/7/12
@ -27,4 +31,9 @@ public class OrderController {
return orderService.addOrders(token, order); return orderService.addOrders(token, order);
} }
@GetMapping("get")
@ApiOperation(value = "获取订单信息")
public R<List<OrderVO>> getOrders(@RequestHeader String token){
return orderService.getOrders(token);
}
} }

View File

@ -0,0 +1,21 @@
package com.example.takeawaysystemserver.model.vo;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
/**
* @author Ethereal
* @date 2024/7/13
* @description
*/
@Data
@AllArgsConstructor
@NoArgsConstructor
public class DishVO {
private Integer dishId;
private String dishName;
private Integer price;
private Integer count;
private Integer amount;
}

View File

@ -0,0 +1,44 @@
package com.example.takeawaysystemserver.model.vo;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import java.util.List;
/**
* @author Ethereal
* @date 2024/7/13
* @description
*/
@Data
@AllArgsConstructor
@NoArgsConstructor
public class OrderVO {
/**
* 菜品id
*/
private Integer shopId;
/**
* 菜品名
*/
private String shopName;
/**
* 订单id
*/
private Long orderId;
/**
* 总价
*/
private Integer price;
/**
* 菜品VO
*/
private List<DishVO> dishes;
}

View File

@ -2,8 +2,11 @@ package com.example.takeawaysystemserver.service;
import com.baomidou.mybatisplus.extension.service.IService; import com.baomidou.mybatisplus.extension.service.IService;
import com.example.takeawaysystemserver.entity.OrderList; import com.example.takeawaysystemserver.entity.OrderList;
import com.example.takeawaysystemserver.model.vo.OrderVO;
import com.example.takeawaysystemserver.util.R; import com.example.takeawaysystemserver.util.R;
import java.util.List;
/** /**
* @author qiushengyu * @author qiushengyu
* @description 针对表order的数据库操作Service * @description 针对表order的数据库操作Service
@ -13,7 +16,15 @@ public interface OrderService extends IService<OrderList> {
/** /**
* 添加订单 * 添加订单
* @param order 订单类 * @param order 订单类
* @param token 用户token
* @return 添加信息 * @return 添加信息
*/ */
R<String> addOrders(String token, OrderList order); R<String> addOrders(String token, OrderList order);
/**
* 获取订单
* @param token 用户token
* @return 订单信息
*/
R<List<OrderVO>> getOrders(String token);
} }

View File

@ -5,17 +5,17 @@ import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.takeawaysystemserver.constant.CommonStatusConstant; import com.example.takeawaysystemserver.constant.CommonStatusConstant;
import com.example.takeawaysystemserver.entity.*; import com.example.takeawaysystemserver.entity.*;
import com.example.takeawaysystemserver.mapper.OrderItemMapper;
import com.example.takeawaysystemserver.service.*;
import com.example.takeawaysystemserver.mapper.OrderMapper; import com.example.takeawaysystemserver.mapper.OrderMapper;
import com.example.takeawaysystemserver.model.vo.DishVO;
import com.example.takeawaysystemserver.model.vo.OrderVO;
import com.example.takeawaysystemserver.service.*;
import com.example.takeawaysystemserver.util.R; import com.example.takeawaysystemserver.util.R;
import jakarta.annotation.Resource; import jakarta.annotation.Resource;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicInteger; import java.util.concurrent.atomic.AtomicInteger;
import java.util.stream.Collectors;
/** /**
* @author qiushengyu * @author qiushengyu
@ -40,6 +40,12 @@ public class OrderServiceImpl extends ServiceImpl<OrderMapper, OrderList>
@Resource @Resource
private OrderItemService orderItemService; private OrderItemService orderItemService;
@Resource
private DishService dishService;
@Resource
private ShopService shopService;
@Override @Override
public R<String> addOrders(String token, OrderList order) { public R<String> addOrders(String token, OrderList order) {
Integer id = Integer.parseInt(userServiceImpl.getUserId(token)); Integer id = Integer.parseInt(userServiceImpl.getUserId(token));
@ -94,6 +100,41 @@ public class OrderServiceImpl extends ServiceImpl<OrderMapper, OrderList>
return R.ok("添加成功"); return R.ok("添加成功");
} }
@Override
public R<List<OrderVO>> getOrders(String token) {
Integer id = Integer.parseInt(userServiceImpl.getUserId(token));
LambdaQueryWrapper<OrderList> wrapper = new LambdaQueryWrapper<>();
wrapper.eq(OrderList::getUserId,id);
List<OrderList> orderLists = list(wrapper);
List<OrderVO> orders = orderLists.stream().map((item) -> {
OrderVO orderVO = new OrderVO();
LambdaQueryWrapper<OrderItem> wrapper1 = new LambdaQueryWrapper<>();
wrapper1.eq(OrderItem::getOrderId, item.getId());
List<OrderItem> orderItems = orderItemService.list(wrapper1);
List<DishVO> dishVos = new ArrayList<>();
int amount = 0;
for (OrderItem orderItem : orderItems) {
LambdaQueryWrapper<Dish> dishes = new LambdaQueryWrapper<>();
dishes.eq(Dish::getId, orderItem.getDishId());
String name = dishService.getOne(dishes).getName();
int i = orderItem.getPrice() * orderItem.getCount();
DishVO dishVO = new DishVO(orderItem.getDishId(),name,orderItem.getPrice(),orderItem.getCount(),i);
dishVos.add(dishVO);
amount += i;
}
String shopName = shopService.getById(item.getShopId()).getName();
orderVO.setShopName(shopName);
orderVO.setShopId(item.getShopId());
orderVO.setOrderId(item.getId());
orderVO.setDishes(dishVos);
orderVO.setPrice(amount);
return orderVO;
}).toList();
return R.ok(orders);
}
} }