|
@@ -1,170 +1,72 @@
|
|
|
package com.campus.trade.service;
|
|
package com.campus.trade.service;
|
|
|
|
|
|
|
|
-import com.campus.trade.common.BusinessException;
|
|
|
|
|
-import com.campus.trade.common.OrderStatusMachine;
|
|
|
|
|
-import com.campus.trade.common.StatusCode;
|
|
|
|
|
-import com.campus.trade.entity.Item;
|
|
|
|
|
-import com.campus.trade.entity.Order;
|
|
|
|
|
-import com.campus.trade.entity.User;
|
|
|
|
|
-import com.campus.trade.mapper.ItemMapper;
|
|
|
|
|
-import com.campus.trade.mapper.OrderMapper;
|
|
|
|
|
-import com.campus.trade.mapper.UserMapper;
|
|
|
|
|
import com.campus.trade.vo.OrderVO;
|
|
import com.campus.trade.vo.OrderVO;
|
|
|
-import com.github.pagehelper.PageHelper;
|
|
|
|
|
import com.github.pagehelper.PageInfo;
|
|
import com.github.pagehelper.PageInfo;
|
|
|
import jakarta.servlet.http.HttpSession;
|
|
import jakarta.servlet.http.HttpSession;
|
|
|
-import org.springframework.beans.BeanUtils;
|
|
|
|
|
-import org.springframework.beans.factory.annotation.Autowired;
|
|
|
|
|
-import org.springframework.stereotype.Service;
|
|
|
|
|
-import org.springframework.transaction.annotation.Transactional;
|
|
|
|
|
|
|
|
|
|
-import java.util.List;
|
|
|
|
|
-import java.util.stream.Collectors;
|
|
|
|
|
-
|
|
|
|
|
-@Service
|
|
|
|
|
-public class OrderService {
|
|
|
|
|
-
|
|
|
|
|
- @Autowired
|
|
|
|
|
- private OrderMapper orderMapper;
|
|
|
|
|
-
|
|
|
|
|
- @Autowired
|
|
|
|
|
- private ItemMapper itemMapper;
|
|
|
|
|
-
|
|
|
|
|
- @Autowired
|
|
|
|
|
- private UserMapper userMapper;
|
|
|
|
|
-
|
|
|
|
|
- @Autowired
|
|
|
|
|
- private OrderStatusMachine statusMachine;
|
|
|
|
|
-
|
|
|
|
|
- // 生成订单
|
|
|
|
|
- @Transactional
|
|
|
|
|
- public Long create(Long itemId, HttpSession session) {
|
|
|
|
|
- Long buyerId = (Long) session.getAttribute("userId");
|
|
|
|
|
- Item item = itemMapper.selectById(itemId);
|
|
|
|
|
- if (item == null) {
|
|
|
|
|
- throw new BusinessException(StatusCode.NOT_FOUND, "物品不存在");
|
|
|
|
|
- }
|
|
|
|
|
- if (!"ON_SALE".equals(item.getStatus())) {
|
|
|
|
|
- throw new BusinessException(StatusCode.BAD_REQUEST, "物品已下架或已售出");
|
|
|
|
|
- }
|
|
|
|
|
- // 不能买自己的物品
|
|
|
|
|
- if (item.getSellerId().equals(buyerId)) {
|
|
|
|
|
- throw new BusinessException(StatusCode.BAD_REQUEST, "不能购买自己的物品");
|
|
|
|
|
- }
|
|
|
|
|
- // 创建订单
|
|
|
|
|
- Order order = new Order();
|
|
|
|
|
- order.setItemId(itemId);
|
|
|
|
|
- order.setBuyerId(buyerId);
|
|
|
|
|
- order.setSellerId(item.getSellerId());
|
|
|
|
|
- order.setTradeMethod(item.getTradeMethod());
|
|
|
|
|
- order.setStatus("PENDING");
|
|
|
|
|
- orderMapper.insert(order);
|
|
|
|
|
- // 物品标记为已售出
|
|
|
|
|
- item.setStatus("SOLD");
|
|
|
|
|
- itemMapper.updateStatus(item);
|
|
|
|
|
- return order.getId();
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- // 我买到的
|
|
|
|
|
- public PageInfo<OrderVO> bought(int pageNum, int pageSize, HttpSession session) {
|
|
|
|
|
- Long userId = (Long) session.getAttribute("userId");
|
|
|
|
|
- PageHelper.startPage(pageNum, pageSize);
|
|
|
|
|
- List<Order> orders = orderMapper.selectByBuyerId(userId);
|
|
|
|
|
- return toVOPageInfo(new PageInfo<>(orders));
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- // 我卖出的
|
|
|
|
|
- public PageInfo<OrderVO> sold(int pageNum, int pageSize, HttpSession session) {
|
|
|
|
|
- Long userId = (Long) session.getAttribute("userId");
|
|
|
|
|
- PageHelper.startPage(pageNum, pageSize);
|
|
|
|
|
- List<Order> orders = orderMapper.selectBySellerId(userId);
|
|
|
|
|
- return toVOPageInfo(new PageInfo<>(orders));
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- // 卖家接单
|
|
|
|
|
- @Transactional
|
|
|
|
|
- public void accept(Long orderId, HttpSession session) {
|
|
|
|
|
- Long userId = (Long) session.getAttribute("userId");
|
|
|
|
|
- Order order = orderMapper.selectById(orderId);
|
|
|
|
|
- if (order == null) throw new BusinessException(StatusCode.NOT_FOUND, "订单不存在");
|
|
|
|
|
- if (!order.getSellerId().equals(userId)) throw new BusinessException(StatusCode.FORBIDDEN, "无权操作");
|
|
|
|
|
- statusMachine.validateTransition(order.getStatus(), "CONFIRMED");
|
|
|
|
|
- order.setStatus("CONFIRMED");
|
|
|
|
|
- orderMapper.updateStatus(order);
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- // 卖家拒单
|
|
|
|
|
- @Transactional
|
|
|
|
|
- public void reject(Long orderId, HttpSession session) {
|
|
|
|
|
- Long userId = (Long) session.getAttribute("userId");
|
|
|
|
|
- Order order = orderMapper.selectById(orderId);
|
|
|
|
|
- if (order == null) throw new BusinessException(StatusCode.NOT_FOUND, "订单不存在");
|
|
|
|
|
- if (!order.getSellerId().equals(userId)) throw new BusinessException(StatusCode.FORBIDDEN, "无权操作");
|
|
|
|
|
- statusMachine.validateTransition(order.getStatus(), "CANCELLED");
|
|
|
|
|
- order.setStatus("CANCELLED");
|
|
|
|
|
- orderMapper.updateStatus(order);
|
|
|
|
|
- // 物品恢复在售
|
|
|
|
|
- Item item = itemMapper.selectById(order.getItemId());
|
|
|
|
|
- if (item != null) {
|
|
|
|
|
- item.setStatus("ON_SALE");
|
|
|
|
|
- itemMapper.updateStatus(item);
|
|
|
|
|
- }
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- // 卖家确认交货
|
|
|
|
|
- @Transactional
|
|
|
|
|
- public void deliver(Long orderId, HttpSession session) {
|
|
|
|
|
- Long userId = (Long) session.getAttribute("userId");
|
|
|
|
|
- Order order = orderMapper.selectById(orderId);
|
|
|
|
|
- if (order == null) throw new BusinessException(StatusCode.NOT_FOUND, "订单不存在");
|
|
|
|
|
- if (!order.getSellerId().equals(userId)) throw new BusinessException(StatusCode.FORBIDDEN, "无权操作");
|
|
|
|
|
- statusMachine.validateTransition(order.getStatus(), "DELIVERED");
|
|
|
|
|
- order.setStatus("DELIVERED");
|
|
|
|
|
- orderMapper.updateStatus(order);
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- // 买家确认收货
|
|
|
|
|
- @Transactional
|
|
|
|
|
- public void confirm(Long orderId, HttpSession session) {
|
|
|
|
|
- Long userId = (Long) session.getAttribute("userId");
|
|
|
|
|
- Order order = orderMapper.selectById(orderId);
|
|
|
|
|
- if (order == null) throw new BusinessException(StatusCode.NOT_FOUND, "订单不存在");
|
|
|
|
|
- if (!order.getBuyerId().equals(userId)) throw new BusinessException(StatusCode.FORBIDDEN, "无权操作");
|
|
|
|
|
- statusMachine.validateTransition(order.getStatus(), "COMPLETED");
|
|
|
|
|
- order.setStatus("COMPLETED");
|
|
|
|
|
- orderMapper.updateStatus(order);
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- // --- 私有辅助 ---
|
|
|
|
|
-
|
|
|
|
|
- private OrderVO toVO(Order order) {
|
|
|
|
|
- OrderVO vo = new OrderVO();
|
|
|
|
|
- BeanUtils.copyProperties(order, vo);
|
|
|
|
|
- Item item = itemMapper.selectById(order.getItemId());
|
|
|
|
|
- if (item != null) {
|
|
|
|
|
- vo.setItemTitle(item.getTitle());
|
|
|
|
|
- vo.setItemPrice(item.getPrice());
|
|
|
|
|
- // 取第一张图作为缩略图
|
|
|
|
|
- if (item.getImages() != null && !item.getImages().isEmpty()) {
|
|
|
|
|
- try {
|
|
|
|
|
- String[] urls = new com.fasterxml.jackson.databind.ObjectMapper()
|
|
|
|
|
- .readValue(item.getImages(), String[].class);
|
|
|
|
|
- if (urls.length > 0) vo.setItemImage(urls[0]);
|
|
|
|
|
- } catch (Exception ignored) {}
|
|
|
|
|
- }
|
|
|
|
|
- }
|
|
|
|
|
- User buyer = userMapper.selectById(order.getBuyerId());
|
|
|
|
|
- if (buyer != null) vo.setBuyerNickname(buyer.getNickname());
|
|
|
|
|
- User seller = userMapper.selectById(order.getSellerId());
|
|
|
|
|
- if (seller != null) vo.setSellerNickname(seller.getNickname());
|
|
|
|
|
- return vo;
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- private PageInfo<OrderVO> toVOPageInfo(PageInfo<Order> pageInfo) {
|
|
|
|
|
- List<OrderVO> voList = pageInfo.getList().stream().map(this::toVO).collect(Collectors.toList());
|
|
|
|
|
- PageInfo<OrderVO> voPageInfo = new PageInfo<>();
|
|
|
|
|
- BeanUtils.copyProperties(pageInfo, voPageInfo, "list");
|
|
|
|
|
- voPageInfo.setList(voList);
|
|
|
|
|
- return voPageInfo;
|
|
|
|
|
- }
|
|
|
|
|
|
|
+/**
|
|
|
|
|
+ * 订单服务接口
|
|
|
|
|
+ */
|
|
|
|
|
+public interface OrderService {
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 生成订单
|
|
|
|
|
+ *
|
|
|
|
|
+ * @param itemId 物品ID
|
|
|
|
|
+ * @param session HTTP会话
|
|
|
|
|
+ * @return 新订单ID
|
|
|
|
|
+ */
|
|
|
|
|
+ Long create(Long itemId, HttpSession session);
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 获取我买到的订单列表(分页)
|
|
|
|
|
+ *
|
|
|
|
|
+ * @param pageNum 页码
|
|
|
|
|
+ * @param pageSize 每页大小
|
|
|
|
|
+ * @param session HTTP会话
|
|
|
|
|
+ * @return 分页订单列表
|
|
|
|
|
+ */
|
|
|
|
|
+ PageInfo<OrderVO> bought(int pageNum, int pageSize, HttpSession session);
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 获取我卖出的订单列表(分页)
|
|
|
|
|
+ *
|
|
|
|
|
+ * @param pageNum 页码
|
|
|
|
|
+ * @param pageSize 每页大小
|
|
|
|
|
+ * @param session HTTP会话
|
|
|
|
|
+ * @return 分页订单列表
|
|
|
|
|
+ */
|
|
|
|
|
+ PageInfo<OrderVO> sold(int pageNum, int pageSize, HttpSession session);
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 卖家接单
|
|
|
|
|
+ *
|
|
|
|
|
+ * @param orderId 订单ID
|
|
|
|
|
+ * @param session HTTP会话
|
|
|
|
|
+ */
|
|
|
|
|
+ void accept(Long orderId, HttpSession session);
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 卖家拒单
|
|
|
|
|
+ *
|
|
|
|
|
+ * @param orderId 订单ID
|
|
|
|
|
+ * @param session HTTP会话
|
|
|
|
|
+ */
|
|
|
|
|
+ void reject(Long orderId, HttpSession session);
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 卖家确认交货
|
|
|
|
|
+ *
|
|
|
|
|
+ * @param orderId 订单ID
|
|
|
|
|
+ * @param session HTTP会话
|
|
|
|
|
+ */
|
|
|
|
|
+ void deliver(Long orderId, HttpSession session);
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 买家确认收货
|
|
|
|
|
+ *
|
|
|
|
|
+ * @param orderId 订单ID
|
|
|
|
|
+ * @param session HTTP会话
|
|
|
|
|
+ */
|
|
|
|
|
+ void confirm(Long orderId, HttpSession session);
|
|
|
}
|
|
}
|