小程序(及后端)订单详情页完成;后端获取用户订单列表接口完成
This commit is contained in:
		@@ -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>
 | 
			
		||||
		Reference in New Issue
	
	Block a user