小程序(及后端)订单详情页完成;后端获取用户订单列表接口完成
This commit is contained in:
@@ -1,19 +1,34 @@
|
||||
package com.cxyxiaomo.epp.common.enums;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.Optional;
|
||||
|
||||
public enum OrderStatus {
|
||||
PENDING("Pending"),
|
||||
PROCESSING("Processing"),
|
||||
SHIPPED("Shipped"),
|
||||
DELIVERED("Delivered"),
|
||||
CANCELLED("Cancelled");
|
||||
PENDING("Pending", "等待确认"),
|
||||
PROCESSING("Processing", "已支付,正在处理"),
|
||||
SHIPPED("Shipped", "已发货,等待确认收货"),
|
||||
DELIVERED("Delivered", "已送达"),
|
||||
CANCELLED("Cancelled", "已取消");
|
||||
|
||||
private final String value;
|
||||
private final String description;
|
||||
|
||||
private OrderStatus(String value) {
|
||||
private OrderStatus(String value, String description) {
|
||||
this.value = value;
|
||||
this.description = description;
|
||||
}
|
||||
|
||||
public static OrderStatus get(String code) {
|
||||
Optional<OrderStatus> first = Arrays.stream(OrderStatus.values()).filter(e -> e.getValue().equals(code)).findFirst();
|
||||
OrderStatus orderStatus = first.orElse(null);
|
||||
return orderStatus;
|
||||
}
|
||||
|
||||
public String getValue() {
|
||||
return value;
|
||||
}
|
||||
|
||||
public String toString() {
|
||||
return description;
|
||||
}
|
||||
}
|
||||
|
@@ -1,6 +1,5 @@
|
||||
package com.cxyxiaomo.epp.common.pojo;
|
||||
|
||||
import com.cxyxiaomo.epp.common.enums.OrderStatus;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
import lombok.experimental.Accessors;
|
||||
@@ -14,5 +13,5 @@ public class Order {
|
||||
private Long id;
|
||||
private Integer userId;
|
||||
private LocalDateTime orderDate;
|
||||
private OrderStatus orderStatus;
|
||||
private String orderStatus;
|
||||
}
|
||||
|
@@ -17,7 +17,7 @@ import java.util.stream.Collectors;
|
||||
@Accessors(chain = true) // 链式写法
|
||||
// 微服务必须要实现Serializable
|
||||
public class GoodVO implements Serializable {
|
||||
Long id;
|
||||
String id;
|
||||
String goodsName;
|
||||
Integer categoryId;
|
||||
String brand;
|
||||
@@ -38,6 +38,7 @@ public class GoodVO implements Serializable {
|
||||
}
|
||||
GoodVO goodVO = new GoodVO();
|
||||
BeanUtils.copyProperties(good, goodVO);
|
||||
goodVO.setId(String.valueOf(good.getId()));
|
||||
return goodVO;
|
||||
}
|
||||
|
||||
|
@@ -4,6 +4,7 @@ import com.cxyxiaomo.epp.common.pojo.OrderDetail;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
import lombok.experimental.Accessors;
|
||||
import org.springframework.beans.BeanUtils;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.util.List;
|
||||
@@ -17,8 +18,8 @@ import java.util.stream.Collectors;
|
||||
// 微服务必须要实现Serializable
|
||||
public class OrderDetailVO implements Serializable {
|
||||
private Long id;
|
||||
private Long orderId;
|
||||
private Long goodId;
|
||||
private String orderId;
|
||||
private String goodId;
|
||||
private Integer goodCount;
|
||||
private Double unitPrice;
|
||||
|
||||
@@ -27,7 +28,9 @@ public class OrderDetailVO implements Serializable {
|
||||
return null;
|
||||
}
|
||||
OrderDetailVO orderDetailVO = new OrderDetailVO();
|
||||
// BeanUtils.copyProperties(orderDetail, orderDetailVO);
|
||||
BeanUtils.copyProperties(orderDetail, orderDetailVO);
|
||||
orderDetailVO.setOrderId(String.valueOf(orderDetail.getOrderId()));
|
||||
orderDetailVO.setGoodId(String.valueOf(orderDetail.getGoodId()));
|
||||
return orderDetailVO;
|
||||
}
|
||||
|
||||
|
@@ -8,7 +8,6 @@ import lombok.experimental.Accessors;
|
||||
import org.springframework.beans.BeanUtils;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.time.LocalDate;
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
@@ -20,10 +19,10 @@ import java.util.stream.Collectors;
|
||||
@Accessors(chain = true) // 链式写法
|
||||
// 微服务必须要实现Serializable
|
||||
public class OrderVO implements Serializable {
|
||||
private Long id;
|
||||
private String id;
|
||||
private Integer userId;
|
||||
private LocalDateTime orderDate;
|
||||
private OrderStatus orderStatus;
|
||||
private String orderStatus;
|
||||
|
||||
public static OrderVO convertFrom(Order order) {
|
||||
if (order == null) {
|
||||
@@ -31,6 +30,10 @@ public class OrderVO implements Serializable {
|
||||
}
|
||||
OrderVO orderVO = new OrderVO();
|
||||
BeanUtils.copyProperties(order, orderVO);
|
||||
|
||||
orderVO.setId(String.valueOf(order.getId())); // 转成字符串 避免前端丢失精度
|
||||
OrderStatus orderStatus = OrderStatus.get(order.getOrderStatus());
|
||||
orderVO.setOrderStatus(orderStatus.toString());
|
||||
return orderVO;
|
||||
}
|
||||
|
||||
|
@@ -109,6 +109,12 @@
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-websocket</artifactId>
|
||||
</dependency>
|
||||
|
||||
<!-- 热部署 -->
|
||||
<!--<dependency>-->
|
||||
<!-- <groupId>org.springframework.boot</groupId>-->
|
||||
<!-- <artifactId>spring-boot-devtools</artifactId>-->
|
||||
<!--</dependency>-->
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
|
@@ -2,14 +2,22 @@ package com.cxyxiaomo.epp.shop.controller;
|
||||
|
||||
import com.alibaba.fastjson2.JSONArray;
|
||||
import com.alibaba.fastjson2.JSONObject;
|
||||
import com.cxyxiaomo.epp.common.pojo.Order;
|
||||
import com.cxyxiaomo.epp.common.pojo.OrderDetail;
|
||||
import com.cxyxiaomo.epp.common.response.Res;
|
||||
import com.cxyxiaomo.epp.common.vo.GoodVO;
|
||||
import com.cxyxiaomo.epp.common.vo.OrderDetailVO;
|
||||
import com.cxyxiaomo.epp.common.vo.OrderVO;
|
||||
import com.cxyxiaomo.epp.shop.service.GoodsService;
|
||||
import com.cxyxiaomo.epp.shop.service.OrderService;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import java.util.HashMap;
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
@RestController
|
||||
@RequestMapping("/shop/order")
|
||||
@@ -18,12 +26,14 @@ public class OrderController {
|
||||
@Resource
|
||||
OrderService orderService;
|
||||
|
||||
@Resource
|
||||
GoodsService goodsService;
|
||||
|
||||
/**
|
||||
* 小程序端创建订单
|
||||
*
|
||||
* @param userId 下单用户
|
||||
* @param orderList 下单商品 [ { goodId, count }, ... ]
|
||||
* @param orderList 下单商品 [ { goodId, count }, ... ]
|
||||
* @return
|
||||
*/
|
||||
@PostMapping("/miniprogram/createOrder")
|
||||
@@ -58,7 +68,7 @@ public class OrderController {
|
||||
|
||||
try {
|
||||
long orderId = orderService.createOrder(userId, orderDetailList);
|
||||
return Res.success(orderId);
|
||||
return Res.success(String.valueOf(orderId)); // 转为 String 避免前端丢失精度
|
||||
} catch (Exception e) {
|
||||
String message = e.getMessage();
|
||||
if (message.contains("Exception")) {
|
||||
@@ -69,4 +79,63 @@ public class OrderController {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取指定订单的详情
|
||||
*
|
||||
* @param orderId
|
||||
* @return
|
||||
*/
|
||||
@GetMapping("/miniprogram/orderDetail")
|
||||
@ResponseBody
|
||||
public Res orderDetail(@RequestParam Long orderId) {
|
||||
if (orderId == null) {
|
||||
return Res.error("订单不存在");
|
||||
}
|
||||
|
||||
try {
|
||||
OrderVO orderVO = orderService.getOrderById(orderId);
|
||||
if (orderVO == null) {
|
||||
return Res.error("订单不存在");
|
||||
}
|
||||
List<OrderDetailVO> orderDetailVOList = orderService.getOrderDetailById(orderId);
|
||||
|
||||
List<Long> goodIdList = orderDetailVOList.stream().map(orderDetailVO -> {
|
||||
String goodId = orderDetailVO.getGoodId();
|
||||
return Long.parseLong(goodId);
|
||||
}).collect(Collectors.toList());
|
||||
|
||||
List<GoodVO> goodVOList = goodsService.listByIds(goodIdList);
|
||||
HashMap<String, Object> resultMap = new HashMap<>();
|
||||
resultMap.put("order", orderVO);
|
||||
resultMap.put("orderItem", orderDetailVOList);
|
||||
resultMap.put("goods", goodVOList);
|
||||
return Res.success(resultMap);
|
||||
} catch (Exception e) {
|
||||
String message = e.getMessage();
|
||||
if (message.contains("Exception")) {
|
||||
// 内部异常不打印出去
|
||||
return Res.error("查询订单失败,请重试");
|
||||
} else {
|
||||
return Res.error(message);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取用户的订单列表
|
||||
*
|
||||
* @param userId
|
||||
* @return
|
||||
*/
|
||||
@GetMapping("/miniprogram/listUserOrder")
|
||||
@ResponseBody
|
||||
public Res listUserOrder(@RequestParam Integer userId) {
|
||||
if (userId == null) {
|
||||
return Res.error("参数错误");
|
||||
}
|
||||
List<Order> orders = orderService.listUserOrder(userId);
|
||||
List<OrderVO> orderVOS = OrderVO.convertFrom(orders);
|
||||
return Res.success(orderVOS);
|
||||
}
|
||||
}
|
||||
|
@@ -17,6 +17,8 @@ public interface GoodsDao {
|
||||
|
||||
Good selectById(Long id);
|
||||
|
||||
List<Good> listByIds(List<Long> id);
|
||||
|
||||
Integer insert(Good good);
|
||||
|
||||
Integer update(Good good);
|
||||
|
@@ -18,7 +18,15 @@ public interface OrderDao {
|
||||
* @param orderId
|
||||
* @return
|
||||
*/
|
||||
Order getOrderById(Integer orderId);
|
||||
Order getOrderById(Long orderId);
|
||||
|
||||
/**
|
||||
* 根据订单 ID 查询订单详情
|
||||
*
|
||||
* @param orderId
|
||||
* @return
|
||||
*/
|
||||
List<OrderDetail> getOrderDetailById(Long orderId);
|
||||
|
||||
/**
|
||||
* 根据用户 ID 查询订单列表
|
||||
@@ -26,7 +34,7 @@ public interface OrderDao {
|
||||
* @param userId
|
||||
* @return
|
||||
*/
|
||||
List<Order> listOrdersByUserId(Integer userId);
|
||||
List<Order> listOrderByUserId(Integer userId);
|
||||
|
||||
/**
|
||||
* 创建订单
|
||||
@@ -48,5 +56,5 @@ public interface OrderDao {
|
||||
int updateOrderStatus(@Param("orderId") Integer orderId, @Param("status") String status);
|
||||
|
||||
// 根据订单 ID 删除订单信息及订单详情信息
|
||||
int deleteOrderById(Integer orderId);
|
||||
int deleteOrderById(Long orderId);
|
||||
}
|
||||
|
@@ -9,6 +9,7 @@ import com.cxyxiaomo.epp.shop.dao.GoodsDao;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
@Service
|
||||
@@ -37,4 +38,13 @@ public class GoodsService {
|
||||
List<GoodCategoryVO> goodCategoryVOList = GoodCategoryVO.convertFrom(list);
|
||||
return goodCategoryVOList;
|
||||
}
|
||||
|
||||
public List<GoodVO> listByIds(List<Long> goodIdList) {
|
||||
if (goodIdList == null || goodIdList.size() == 0) {
|
||||
return new ArrayList<>();
|
||||
}
|
||||
List<Good> list = goodsDao.listByIds(goodIdList);
|
||||
List<GoodVO> goodVOList = GoodVO.convertFrom(list);
|
||||
return goodVOList;
|
||||
}
|
||||
}
|
||||
|
@@ -5,6 +5,8 @@ import com.cxyxiaomo.epp.common.pojo.Good;
|
||||
import com.cxyxiaomo.epp.common.pojo.Order;
|
||||
import com.cxyxiaomo.epp.common.pojo.OrderDetail;
|
||||
import com.cxyxiaomo.epp.common.utils.SnowflakeManager;
|
||||
import com.cxyxiaomo.epp.common.vo.OrderDetailVO;
|
||||
import com.cxyxiaomo.epp.common.vo.OrderVO;
|
||||
import com.cxyxiaomo.epp.shop.dao.GoodsDao;
|
||||
import com.cxyxiaomo.epp.shop.dao.OrderDao;
|
||||
import org.springframework.stereotype.Service;
|
||||
@@ -43,7 +45,7 @@ public class OrderService {
|
||||
order.setId(orderId);
|
||||
order.setUserId(userId);
|
||||
order.setOrderDate(LocalDateTime.now());
|
||||
order.setOrderStatus(OrderStatus.PENDING);
|
||||
order.setOrderStatus(OrderStatus.PENDING.getValue());
|
||||
orderDao.insertOrder(order);
|
||||
|
||||
// 创建订单详情
|
||||
@@ -68,4 +70,43 @@ public class OrderService {
|
||||
orderDao.batchInsertOrderDetail(orderItemList);
|
||||
return orderId;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取订单
|
||||
*
|
||||
* @param orderId
|
||||
*/
|
||||
public OrderVO getOrderById(Long orderId) {
|
||||
Order order = orderDao.getOrderById(orderId);
|
||||
if (order == null) {
|
||||
return null;
|
||||
}
|
||||
OrderVO orderVO = OrderVO.convertFrom(order);
|
||||
return orderVO;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取订单详情
|
||||
*
|
||||
* @param orderId
|
||||
*/
|
||||
public List<OrderDetailVO> getOrderDetailById(Long orderId) {
|
||||
List<OrderDetail> orderDetails = orderDao.getOrderDetailById(orderId);
|
||||
if (orderDetails == null) {
|
||||
return null;
|
||||
}
|
||||
List<OrderDetailVO> orderDetailVOS = OrderDetailVO.convertFrom(orderDetails);
|
||||
return orderDetailVOS;
|
||||
}
|
||||
|
||||
/**
|
||||
* 通过用户 id 获取用户订单列表
|
||||
*
|
||||
* @param userId
|
||||
* @return
|
||||
*/
|
||||
public List<Order> listUserOrder(Integer userId) {
|
||||
List<Order> orders = orderDao.listOrderByUserId(userId);
|
||||
return orders;
|
||||
}
|
||||
}
|
||||
|
@@ -50,6 +50,20 @@
|
||||
WHERE id = #{id}
|
||||
</select>
|
||||
|
||||
<select id="listByIds" parameterType="java.util.List" resultType="com.cxyxiaomo.epp.common.pojo.Good">
|
||||
SELECT *
|
||||
FROM goods
|
||||
<where>
|
||||
<if test="list != null and list.size > 0">
|
||||
AND id IN
|
||||
<foreach collection="list" item="id" open="(" separator="," close=")">
|
||||
#{id}
|
||||
</foreach>
|
||||
</if>
|
||||
AND deleted = 0
|
||||
</where>
|
||||
</select>
|
||||
|
||||
<!--<select id="selectByCategoryId" resultMap="GoodsResultMap">-->
|
||||
<!-- SELECT *-->
|
||||
<!-- FROM goods-->
|
||||
|
@@ -11,29 +11,40 @@
|
||||
<result column="order_status" property="orderStatus"/>
|
||||
</resultMap>
|
||||
<resultMap id="OrderDetailResultMap" type="com.cxyxiaomo.epp.common.pojo.OrderDetail">
|
||||
<id property="id" column="id" />
|
||||
<result property="orderId" column="order_id" />
|
||||
<result property="goodId" column="good_id" />
|
||||
<result property="goodCount" column="good_count" />
|
||||
<result property="unitPrice" column="unit_price" />
|
||||
<id property="id" column="id"/>
|
||||
<result property="orderId" column="order_id"/>
|
||||
<result property="goodId" column="good_id"/>
|
||||
<result property="goodCount" column="good_count"/>
|
||||
<result property="unitPrice" column="unit_price"/>
|
||||
</resultMap>
|
||||
|
||||
<!-- 根据订单 ID 查询订单信息 -->
|
||||
<select id="getOrderById" resultMap="OrderResultMap">
|
||||
SELECT * FROM order WHERE order_id=#{orderId}
|
||||
SELECT *
|
||||
FROM `order`
|
||||
WHERE id = #{orderId}
|
||||
</select>
|
||||
|
||||
<!-- 根据订单 ID 查询订单详情 -->
|
||||
<select id="getOrderDetailById" resultMap="OrderDetailResultMap">
|
||||
SELECT *
|
||||
FROM `order_detail`
|
||||
WHERE order_id = #{orderId}
|
||||
</select>
|
||||
|
||||
<!-- 根据用户 ID 查询订单列表 -->
|
||||
<select id="listOrdersByUserId" resultType="com.cxyxiaomo.epp.common.pojo.Order">
|
||||
<select id="listOrderByUserId" resultType="com.cxyxiaomo.epp.common.pojo.Order">
|
||||
SELECT *
|
||||
FROM order
|
||||
FROM `order`
|
||||
WHERE user_id = #{userId}
|
||||
order by order_date desc
|
||||
</select>
|
||||
|
||||
<!-- 创建订单 -->
|
||||
<insert id="insertOrder" parameterType="com.cxyxiaomo.epp.common.pojo.Order" useGeneratedKeys="true" keyProperty="orderId">
|
||||
INSERT INTO `order` (id, user_id, order_date, order_status) VALUES
|
||||
(#{id}, #{userId}, #{orderDate,javaType=java.time.LocalDateTime,jdbcType=TIMESTAMP}, #{orderStatus})
|
||||
<insert id="insertOrder" parameterType="com.cxyxiaomo.epp.common.pojo.Order" useGeneratedKeys="true"
|
||||
keyProperty="orderId">
|
||||
INSERT INTO `order` (id, user_id, order_date, order_status)
|
||||
VALUES (#{id}, #{userId}, #{orderDate,javaType=java.time.LocalDateTime,jdbcType=TIMESTAMP}, #{orderStatus})
|
||||
</insert>
|
||||
|
||||
<!-- 批量插入订单详情 -->
|
||||
@@ -51,12 +62,18 @@
|
||||
</insert>
|
||||
|
||||
<update id="updateOrderStatus" parameterType="com.cxyxiaomo.epp.common.pojo.Order">
|
||||
UPDATE order SET order_status=#{orderStatus} WHERE order_id=#{orderId}
|
||||
UPDATE order
|
||||
SET order_status=#{orderStatus}
|
||||
WHERE order_id = #{orderId}
|
||||
</update>
|
||||
|
||||
<!-- 根据订单 ID 删除订单信息及订单详情信息 -->
|
||||
<delete id="deleteOrderById" parameterType="java.lang.Integer">
|
||||
DELETE FROM order WHERE order_id = #{orderId};
|
||||
DELETE FROM goods_order_details WHERE order_id = #{orderId};
|
||||
DELETE
|
||||
FROM order
|
||||
WHERE order_id = #{orderId};
|
||||
DELETE
|
||||
FROM goods_order_details
|
||||
WHERE order_id = #{orderId};
|
||||
</delete>
|
||||
</mapper>
|
@@ -207,6 +207,13 @@
|
||||
<version>${lombok.version}</version>
|
||||
<optional>true</optional>
|
||||
</dependency>
|
||||
|
||||
<!-- 热部署 -->
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-devtools</artifactId>
|
||||
<version>2.7.5</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</dependencyManagement>
|
||||
|
||||
|
Reference in New Issue
Block a user