一、个人简介
💖💖作者:计算机编程果茶熊
💙💙个人简介:曾长期从事计算机专业培训教学,担任过编程老师,同时本人也热爱上课教学,擅长Java、微信小程序、Python、Golang、安卓Android等多个IT方向。会做一些项目定制化开发、代码讲解、答辩教学、文档编写、也懂一些降重方面的技巧。平常喜欢分享一些自己开发中遇到的问题的解决办法,也喜欢交流技术,大家有技术代码这一块的问题可以问我!
💛💛想说的话:感谢大家的关注与支持!
💕💕文末获取源码联系计算机编程果茶熊
二、前言
开发语言:Java+Python
数据库:MySQL
系统架构:B/S
后端框架:SpringBoot(Spring+SpringMVC+Mybatis)+Django
前端:Vue+HTML+CSS+JavaScript+jQuery
二手物品交易系统是一款基于B/S架构的综合性交易平台,采用Java/Python双版本支持,分别结合Spring Boot(Spring+SpringMVC+Mybatis)和Django框架作为后端支撑,前端采用Vue+ElementUI+HTML技术栈打造流畅用户界面,数据存储依托MySQL数据库实现高效管理。系统功能完善,包括系统首页展示热门商品与交易信息、个人中心管理用户资料与交易记录、用户管理实现账号权限控制、配送员管理确保物流高效运转、商品分类管理优化浏览体验、二手商品管理支持详细信息录入与展示、订单信息管理追踪交易全流程、取消购买管理灵活处理退款需求、收货信息管理确保准确配送、发货信息管理实时更新物品状态、配送信息管理优化物流路径、留言反馈收集用户意见建议,以及系统管理确保平台稳定运行。该系统特别适合计算机专业毕业生作为毕设项目,通过实践掌握全栈开发技能,既满足学术要求又具备实用价值,帮助学生在实际项目中应用所学知识,打造一个功能完整、技术全面的二手交易平台。
三、二手物品交易系统-视频解说
还在为毕设选题犹豫不决?二手物品交易系统已经成为计算机专业的热门方向|计算机毕业设计|SpringBoot
四、二手物品交易系统-功能介绍
五、二手物品交易系统-代码展示
// 核心功能1: 二手商品管理 - 商品上架与审核处理
@Service
public class SecondHandProductService {
@Autowired
private ProductRepository productRepository;
@Autowired
private CategoryRepository categoryRepository;
@Autowired
private UserRepository userRepository;
public ResponseEntity<Map<String, Object>> publishProduct(ProductDTO productDTO, MultipartFile[] images) {
Map<String, Object> response = new HashMap<>();
try {
// 参数校验
if (productDTO.getName() == null || productDTO.getPrice() == null || productDTO.getCategoryId() == null) {
response.put("success", false);
response.put("message", "商品信息不完整,请检查必填项");
return ResponseEntity.badRequest().body(response);
}
// 检查分类是否存在
Category category = categoryRepository.findById(productDTO.getCategoryId())
.orElseThrow(() -> new ResourceNotFoundException("商品分类不存在"));
// 获取当前用户
User currentUser = userRepository.findById(SecurityUtils.getCurrentUserId())
.orElseThrow(() -> new UnauthorizedException("用户未登录或不存在"));
// 创建商品对象
Product product = new Product();
product.setName(productDTO.getName());
product.setDescription(productDTO.getDescription());
product.setPrice(productDTO.getPrice());
product.setOriginalPrice(productDTO.getOriginalPrice());
product.setCondition(productDTO.getCondition());
product.setUsageTime(productDTO.getUsageTime());
product.setCategory(category);
product.setSeller(currentUser);
product.setStatus("PENDING"); // 待审核状态
product.setCreatedAt(new Date());
// 处理商品图片
List<ProductImage> productImages = new ArrayList<>();
if (images != null && images.length > 0) {
String uploadDir = "uploads/products/" + UUID.randomUUID().toString();
File directory = new File(uploadDir);
if (!directory.exists()) {
directory.mkdirs();
}
for (int i = 0; i < images.length; i++) {
MultipartFile image = images[i];
String fileName = StringUtils.cleanPath(image.getOriginalFilename());
String filePath = uploadDir + "/" + fileName;
// 保存图片到服务器
Files.copy(image.getInputStream(), Paths.get(filePath), StandardCopyOption.REPLACE_EXISTING);
// 创建商品图片记录
ProductImage productImage = new ProductImage();
productImage.setProduct(product);
productImage.setImagePath(filePath);
productImage.setIsPrimary(i == 0); // 第一张图片设为主图
productImages.add(productImage);
}
}
// 保存商品信息
Product savedProduct = productRepository.save(product);
// 设置响应信息
response.put("success", true);
response.put("message", "商品发布成功,等待审核");
response.put("productId", savedProduct.getId());
return ResponseEntity.ok(response);
} catch (Exception e) {
response.put("success", false);
response.put("message", "商品发布失败: " + e.getMessage());
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(response);
}
}
}
// 核心功能2: 订单信息管理 - 创建订单并处理支付
@Service
public class OrderService {
@Autowired
private OrderRepository orderRepository;
@Autowired
private ProductRepository productRepository;
@Autowired
private UserRepository userRepository;
@Autowired
private AddressRepository addressRepository;
@Autowired
private PaymentService paymentService;
@Transactional
public ResponseEntity<Map<String, Object>> createOrder(OrderCreateDTO orderDTO) {
Map<String, Object> response = new HashMap<>();
try {
// 获取当前用户
User buyer = userRepository.findById(SecurityUtils.getCurrentUserId())
.orElseThrow(() -> new UnauthorizedException("用户未登录"));
// 获取商品信息
Product product = productRepository.findById(orderDTO.getProductId())
.orElseThrow(() -> new ResourceNotFoundException("商品不存在"));
// 检查商品状态
if (!"ACTIVE".equals(product.getStatus())) {
response.put("success", false);
response.put("message", "商品当前不可购买");
return ResponseEntity.badRequest().body(response);
}
// 检查是否是自己的商品
if (product.getSeller().getId().equals(buyer.getId())) {
response.put("success", false);
response.put("message", "不能购买自己发布的商品");
return ResponseEntity.badRequest().body(response);
}
// 获取收货地址
Address shippingAddress = addressRepository.findById(orderDTO.getAddressId())
.orElseThrow(() -> new ResourceNotFoundException("收货地址不存在"));
// 验证地址是否属于当前用户
if (!shippingAddress.getUser().getId().equals(buyer.getId())) {
response.put("success", false);
response.put("message", "无效的收货地址");
return ResponseEntity.badRequest().body(response);
}
// 创建订单
Order order = new Order();
order.setBuyer(buyer);
order.setSeller(product.getSeller());
order.setProduct(product);
order.setOrderAmount(product.getPrice());
order.setShippingAddress(shippingAddress.getFullAddress());
order.setReceiverName(shippingAddress.getReceiverName());
order.setReceiverPhone(shippingAddress.getReceiverPhone());
order.setOrderStatus("PENDING_PAYMENT");
order.setCreatedAt(new Date());
// 生成订单编号
String orderNumber = generateOrderNumber();
order.setOrderNumber(orderNumber);
// 保存订单
Order savedOrder = orderRepository.save(order);
// 更新商品状态为锁定
product.setStatus("LOCKED");
productRepository.save(product);
// 创建支付记录
PaymentDTO paymentDTO = new PaymentDTO();
paymentDTO.setOrderId(savedOrder.getId());
paymentDTO.setAmount(savedOrder.getOrderAmount());
paymentDTO.setPaymentMethod(orderDTO.getPaymentMethod());
// 调用支付服务
PaymentResult paymentResult = paymentService.processPayment(paymentDTO);
// 根据支付结果更新订单状态
if (paymentResult.isSuccess()) {
savedOrder.setOrderStatus("PAID");
savedOrder.setPaidAt(new Date());
orderRepository.save(savedOrder);
// 更新商品状态为已售出
product.setStatus("SOLD");
productRepository.save(product);
response.put("success", true);
response.put("message", "订单创建成功并已支付");
response.put("orderId", savedOrder.getId());
response.put("orderNumber", orderNumber);
return ResponseEntity.ok(response);
} else {
response.put("success", true);
response.put("message", "订单创建成功,等待支付");
response.put("orderId", savedOrder.getId());
response.put("orderNumber", orderNumber);
response.put("paymentUrl", paymentResult.getPaymentUrl());
return ResponseEntity.ok(response);
}
} catch (Exception e) {
// 发生异常时回滚事务
TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
response.put("success", false);
response.put("message", "创建订单失败: " + e.getMessage());
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(response);
}
}
private String generateOrderNumber() {
// 生成订单编号: 时间戳 + 随机数
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
String timeStamp = sdf.format(new Date());
String randomNum = String.format("%04d", new Random().nextInt(10000));
return "ORD" + timeStamp + randomNum;
}
}
// 核心功能3: 配送信息管理 - 分配配送员并跟踪配送状态
@Service
public class DeliveryService {
@Autowired
private OrderRepository orderRepository;
@Autowired
private DeliveryRepository deliveryRepository;
@Autowired
private DeliveryManRepository deliveryManRepository;
@Autowired
private UserRepository userRepository;
@Autowired
private NotificationService notificationService;
@Transactional
public ResponseEntity<Map<String, Object>> assignDeliveryMan(Long orderId) {
Map<String, Object> response = new HashMap<>();
try {
// 获取订单信息
Order order = orderRepository.findById(orderId)
.orElseThrow(() -> new ResourceNotFoundException("订单不存在"));
// 检查订单状态
if (!"PAID".equals(order.getOrderStatus())) {
response.put("success", false);
response.put("message", "只有已支付的订单才能分配配送员");
return ResponseEntity.badRequest().body(response);
}
// 检查是否已经分配了配送员
if (deliveryRepository.existsByOrderId(orderId)) {
response.put("success", false);
response.put("message", "该订单已分配配送员");
return ResponseEntity.badRequest().body(response);
}
// 获取可用的配送员列表
List<DeliveryMan> availableDeliveryMen = deliveryManRepository.findByStatus("AVAILABLE");
if (availableDeliveryMen.isEmpty()) {
response.put("success", false);
response.put("message", "当前没有可用的配送员");
return ResponseEntity.status(HttpStatus.SERVICE_UNAVAILABLE).body(response);
}
// 根据配送地址和当前位置,选择最近的配送员
DeliveryMan selectedDeliveryMan = findNearestDeliveryMan(availableDeliveryMen, order.getShippingAddress());
// 创建配送记录
Delivery delivery = new Delivery();
delivery.setOrder(order);
delivery.setDeliveryMan(selectedDeliveryMan);
delivery.setStatus("ASSIGNED");
delivery.setAssignedAt(new Date());
delivery.setEstimatedDeliveryTime(calculateEstimatedDeliveryTime());
// 更新配送员状态
selectedDeliveryMan.setStatus("BUSY");
selectedDeliveryMan.setCurrentOrderCount(selectedDeliveryMan.getCurrentOrderCount() + 1);
deliveryManRepository.save(selectedDeliveryMan);
// 保存配送记录
Delivery savedDelivery = deliveryRepository.save(delivery);
// 更新订单状态
order.setOrderStatus("DELIVERING");
orderRepository.save(order);
// 发送通知给买家
NotificationDTO buyerNotification = new NotificationDTO();
buyerNotification.setUserId(order.getBuyer().getId());
buyerNotification.setTitle("订单配送通知");
buyerNotification.setContent("您的订单 " + order.getOrderNumber() + " 已分配配送员,预计送达时间: "
+ new SimpleDateFormat("MM-dd HH:mm").format(delivery.getEstimatedDeliveryTime()));
buyerNotification.setType("ORDER_DELIVERY");
buyerNotification.setRelatedId(orderId);
notificationService.sendNotification(buyerNotification);
// 发送通知给配送员
NotificationDTO deliveryManNotification = new NotificationDTO();
deliveryManNotification.setUserId(selectedDeliveryMan.getUser().getId());
deliveryManNotification.setTitle("新配送任务");
deliveryManNotification.setContent("您有一个新的配送任务,订单号: " + order.getOrderNumber()
+ ",配送地址: " + order.getShippingAddress());
deliveryManNotification.setType("DELIVERY_TASK");
deliveryManNotification.setRelatedId(savedDelivery.getId());
notificationService.sendNotification(deliveryManNotification);
// 返回成功响应
response.put("success", true);
response.put("message", "配送员分配成功");
response.put("deliveryId", savedDelivery.getId());
response.put("deliveryManId", selectedDeliveryMan.getId());
response.put("deliveryManName", selectedDeliveryMan.getUser().getUsername());
response.put("estimatedDeliveryTime", delivery.getEstimatedDeliveryTime());
return ResponseEntity.ok(response);
} catch (Exception e) {
// 发生异常时回滚事务
TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
response.put("success", false);
response.put("message", "分配配送员失败: " + e.getMessage());
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(response);
}
}
private DeliveryMan findNearestDeliveryMan(List<DeliveryMan> deliveryMen, String shippingAddress) {
// 实际应用中应该使用地理位置API计算距离
// 这里简化处理,随机选择一个配送员或根据当前订单数量选择负载最小的配送员
return deliveryMen.stream()
.min(Comparator.comparing(DeliveryMan::getCurrentOrderCount))
.orElse(deliveryMen.get(0));
}
private Date calculateEstimatedDeliveryTime() {
// 计算预计送达时间,这里简化为当前时间加2小时
Calendar calendar = Calendar.getInstance();
calendar.add(Calendar.HOUR, 2);
return calendar.getTime();
}
}
六、二手物品交易系统-文档展示
七、END
💕💕文末获取源码联系计算机编程果茶熊