package com.webmanage.service.impl; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import com.baomidou.mybatisplus.core.metadata.IPage; import com.baomidou.mybatisplus.extension.plugins.pagination.Page; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import com.webmanage.common.BusinessException; import com.webmanage.common.PageResult; import com.webmanage.dto.CreateOrderDTO; import com.webmanage.dto.CreateOrderItemDTO; import com.webmanage.dto.OrderQueryDTO; import com.webmanage.entity.OrderAttachment; import com.webmanage.entity.OrderDetail; import com.webmanage.entity.OrderEvaluation; import com.webmanage.entity.OrderInfo; import com.webmanage.mapper.OrderAttachmentMapper; import com.webmanage.mapper.OrderDetailMapper; import com.webmanage.mapper.OrderEvaluationMapper; import com.webmanage.mapper.OrderInfoMapper; import com.webmanage.service.OrderInfoService; import com.webmanage.service.OrderNoService; import com.webmanage.vo.OrderAttachmentVO; import com.webmanage.vo.OrderDetailItemVO; import com.webmanage.vo.OrderDetailVO; import com.webmanage.vo.OrderEvaluationVO; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.BeanUtils; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import org.springframework.util.CollectionUtils; import org.springframework.util.StringUtils; import javax.annotation.Resource; import java.math.BigDecimal; import java.time.LocalDateTime; import java.util.List; import java.util.stream.Collectors; /** * 订单信息Service实现类 */ @Slf4j @Service public class OrderInfoServiceImpl extends ServiceImpl implements OrderInfoService { @Resource private OrderDetailMapper orderDetailMapper; @Resource private OrderAttachmentMapper orderAttachmentMapper; @Resource private OrderEvaluationMapper orderEvaluationMapper; @Resource private OrderNoService orderNoService; @Override public PageResult getBuyerOrderPage(OrderQueryDTO queryDTO) { // 参数校验 if (queryDTO.getUserId() == null) { throw new BusinessException("用户ID不能为空"); } // 创建分页对象 Page page = new Page<>(queryDTO.getPageNum(), queryDTO.getPageSize()); // 执行分页查询 IPage result = baseMapper.selectBuyerOrderPage( page, queryDTO.getUserId(), queryDTO.getUnitId(), queryDTO.getOrderStatus(), queryDTO.getPaymentStatus(), queryDTO.getPaymentType(), queryDTO.getProductName(), queryDTO.getProviderName(), queryDTO.getOrderId(), queryDTO.getApplyTimeStart() != null ? queryDTO.getApplyTimeStart().toString() : null, queryDTO.getApplyTimeEnd() != null ? queryDTO.getApplyTimeEnd().toString() : null, queryDTO.getCreateTimeStart() != null ? queryDTO.getCreateTimeStart().toString() : null, queryDTO.getCreateTimeEnd() != null ? queryDTO.getCreateTimeEnd().toString() : null, queryDTO.getOrderBy(), queryDTO.getOrderDirection() ); // 构建返回结果 return new PageResult( result.getRecords(), result.getTotal(), queryDTO.getPageNum().longValue(), queryDTO.getPageSize().longValue(), result.getPages() ); } @Override public PageResult getSellerOrderPage(OrderQueryDTO queryDTO) { // 参数校验 if (queryDTO.getUserId() == null) { throw new BusinessException("用户ID不能为空"); } // 创建分页对象 Page page = new Page<>(queryDTO.getPageNum(), queryDTO.getPageSize()); // 执行分页查询 IPage result = baseMapper.selectSellerOrderPage( page, queryDTO.getUserId(), queryDTO.getOrderStatus(), queryDTO.getPaymentStatus(), queryDTO.getProductName(), queryDTO.getOrderId(), queryDTO.getApplyTimeStart() != null ? queryDTO.getApplyTimeStart().toString() : null, queryDTO.getApplyTimeEnd() != null ? queryDTO.getApplyTimeEnd().toString() : null, queryDTO.getCreateTimeStart() != null ? queryDTO.getCreateTimeStart().toString() : null, queryDTO.getCreateTimeEnd() != null ? queryDTO.getCreateTimeEnd().toString() : null, queryDTO.getOrderBy(), queryDTO.getOrderDirection() ); // 构建返回结果 return new PageResult( result.getRecords(), result.getTotal(), queryDTO.getPageNum().longValue(), queryDTO.getPageSize().longValue(), result.getPages() ); } @Override public PageResult getPendingApprovalOrderPage(OrderQueryDTO queryDTO) { // 创建分页对象 Page page = new Page<>(queryDTO.getPageNum(), queryDTO.getPageSize()); // 执行分页查询 IPage result = baseMapper.selectPendingApprovalOrderPage( page, queryDTO.getOrderStatus(), queryDTO.getProductName(), queryDTO.getProviderName(), queryDTO.getOrderId(), queryDTO.getApplyTimeStart() != null ? queryDTO.getApplyTimeStart().toString() : null, queryDTO.getApplyTimeEnd() != null ? queryDTO.getApplyTimeEnd().toString() : null, queryDTO.getOrderBy(), queryDTO.getOrderDirection() ); // 构建返回结果 return new PageResult( result.getRecords(), result.getTotal(), queryDTO.getPageNum().longValue(), queryDTO.getPageSize().longValue(), result.getPages() ); } @Override public OrderDetailVO getOrderDetail(String orderId) { // 参数校验 if (!StringUtils.hasText(orderId)) { throw new BusinessException("订单ID不能为空"); } // 查询订单基本信息 OrderInfo orderInfo = this.getById(orderId); if (orderInfo == null) { throw new BusinessException("订单不存在"); } // 构建订单详情VO OrderDetailVO orderDetailVO = new OrderDetailVO(); BeanUtils.copyProperties(orderInfo, orderDetailVO); // 查询订单详情列表 List orderDetails = orderDetailMapper.selectByOrderId(orderId); List orderDetailItemVOs = orderDetails.stream().map(detail -> { OrderDetailItemVO itemVO = new OrderDetailItemVO(); BeanUtils.copyProperties(detail, itemVO); return itemVO; }).collect(Collectors.toList()); orderDetailVO.setOrderDetails(orderDetailItemVOs); // 查询订单附件列表 List attachments = orderAttachmentMapper.selectByOrderId(orderId); List attachmentVOs = attachments.stream().map(attachment -> { OrderAttachmentVO attachmentVO = new OrderAttachmentVO(); BeanUtils.copyProperties(attachment, attachmentVO); return attachmentVO; }).collect(Collectors.toList()); orderDetailVO.setAttachments(attachmentVOs); // 查询订单评价 OrderEvaluation evaluation = orderEvaluationMapper.selectByOrderId(orderId); if (evaluation != null) { OrderEvaluationVO evaluationVO = new OrderEvaluationVO(); BeanUtils.copyProperties(evaluation, evaluationVO); orderDetailVO.setEvaluation(evaluationVO); } return orderDetailVO; } @Override @Transactional(rollbackFor = Exception.class) public OrderInfo createOrder(CreateOrderDTO createOrderDTO) { if (createOrderDTO == null || CollectionUtils.isEmpty(createOrderDTO.getItems())) { throw new BusinessException("订单信息不完整"); } // 生成订单编号 String orderId = orderNoService.generateOrderNo(); // 计算总金额 BigDecimal totalAmount = BigDecimal.ZERO; for (CreateOrderItemDTO item : createOrderDTO.getItems()) { BigDecimal unitPrice = item.getUnitPrice(); Integer quantity = item.getQuantity(); if (unitPrice == null || quantity == null || quantity <= 0) { throw new BusinessException("明细项单价或数量不合法"); } BigDecimal lineAmount = unitPrice.multiply(BigDecimal.valueOf(quantity)); totalAmount = totalAmount.add(lineAmount); } // 保存订单头 OrderInfo orderInfo = new OrderInfo(); orderInfo.setOrderId(orderId); orderInfo.setProductId(createOrderDTO.getItems().get(0).getProductId()); orderInfo.setProductName(createOrderDTO.getProductName()); orderInfo.setProviderId(createOrderDTO.getProviderId()); orderInfo.setProviderName(createOrderDTO.getProviderName()); orderInfo.setUserId(createOrderDTO.getUserId()); orderInfo.setUnitId(createOrderDTO.getUnitId()); orderInfo.setApplyTime(LocalDateTime.now()); orderInfo.setOrderStatus("待审批"); orderInfo.setTotalAmount(createOrderDTO.getTotalAmount() != null ? createOrderDTO.getTotalAmount() : totalAmount); orderInfo.setPaymentType(createOrderDTO.getPaymentType()); orderInfo.setPaymentStatus("未支付"); orderInfo.setBuyerRemarks(createOrderDTO.getBuyerRemarks()); orderInfo.setCreatedAt(LocalDateTime.now()); orderInfo.setUpdatedAt(LocalDateTime.now()); int inserted = this.baseMapper.insert(orderInfo); if (inserted <= 0) { throw new BusinessException("保存订单失败"); } // 保存订单明细 for (CreateOrderItemDTO item : createOrderDTO.getItems()) { OrderDetail detail = new OrderDetail(); detail.setOrderId(orderId); detail.setPricingId(item.getPricingId()); detail.setProductId(item.getProductId()); detail.setSuiteName(item.getSuiteName()); detail.setSalesForm(item.getSalesForm()); detail.setCustomerType(item.getCustomerType()); detail.setAccountLimit(item.getAccountLimit()); detail.setConcurrentNodes(item.getConcurrentNodes()); detail.setPriceType(item.getPriceType()); detail.setPriceUnit(item.getPriceUnit()); detail.setUnitPrice(item.getUnitPrice()); detail.setQuantity(item.getQuantity()); detail.setDuration(item.getDuration()); BigDecimal lineAmount = item.getUnitPrice().multiply(BigDecimal.valueOf(item.getQuantity())); detail.setTotalPrice(item.getTotalPrice() != null ? item.getTotalPrice() : lineAmount); detail.setProviderId(item.getProviderId()); detail.setProviderName(item.getProviderName()); detail.setRemarks(item.getRemarks()); detail.setCreatedAt(LocalDateTime.now()); detail.setUpdatedAt(LocalDateTime.now()); orderDetailMapper.insert(detail); } return orderInfo; } @Override @Transactional(rollbackFor = Exception.class) public boolean uploadOrderAttachment(String orderId, String fileName, String originalName, String fileType, Long fileSize, String fileUrl, String bucketName, String objectName, Long uploadUserId, String uploadUserName, String attachmentType, String description) { // 参数校验 if (!StringUtils.hasText(orderId)) { throw new BusinessException("订单ID不能为空"); } if (!StringUtils.hasText(fileName)) { throw new BusinessException("文件名不能为空"); } // 检查订单是否存在 OrderInfo orderInfo = this.getById(orderId); if (orderInfo == null) { throw new BusinessException("订单不存在"); } // 创建订单附件 OrderAttachment attachment = new OrderAttachment(); attachment.setOrderId(orderId); attachment.setFileName(fileName); attachment.setOriginalName(originalName); attachment.setFileType(fileType); attachment.setFileSize(fileSize); attachment.setFileUrl(fileUrl); attachment.setBucketName(bucketName); attachment.setObjectName(objectName); attachment.setUploadUserId(uploadUserId); attachment.setUploadUserName(uploadUserName); attachment.setAttachmentType(attachmentType); attachment.setDescription(description); // 保存附件 return orderAttachmentMapper.insert(attachment) > 0; } @Override @Transactional(rollbackFor = Exception.class) public boolean addOrderEvaluation(String orderId, Long evaluatorId, String evaluatorName, String evaluatorType, String content, Integer rating, Integer serviceRating, Integer qualityRating, Integer deliveryRating, Boolean isAnonymous) { // 参数校验 if (!StringUtils.hasText(orderId)) { throw new BusinessException("订单ID不能为空"); } if (evaluatorId == null) { throw new BusinessException("评价人ID不能为空"); } if (!StringUtils.hasText(content)) { throw new BusinessException("评价内容不能为空"); } if (rating == null || rating < 1 || rating > 5) { throw new BusinessException("评分必须在1-5之间"); } // 检查订单是否存在 OrderInfo orderInfo = this.getById(orderId); if (orderInfo == null) { throw new BusinessException("订单不存在"); } // 检查是否已经评价过 OrderEvaluation existingEvaluation = orderEvaluationMapper.selectByOrderId(orderId); if (existingEvaluation != null) { throw new BusinessException("该订单已经评价过了"); } // 创建订单评价 OrderEvaluation evaluation = new OrderEvaluation(); evaluation.setOrderId(orderId); evaluation.setEvaluatorId(evaluatorId); evaluation.setEvaluatorName(evaluatorName); evaluation.setEvaluatorType(evaluatorType); evaluation.setContent(content); evaluation.setRating(rating); evaluation.setServiceRating(serviceRating); evaluation.setQualityRating(qualityRating); evaluation.setDeliveryRating(deliveryRating); evaluation.setIsAnonymous(isAnonymous != null ? isAnonymous : false); // 保存评价 return orderEvaluationMapper.insert(evaluation) > 0; } @Override @Transactional(rollbackFor = Exception.class) public boolean confirmTransaction(String orderId, Long userId) { // 参数校验 if (!StringUtils.hasText(orderId)) { throw new BusinessException("订单ID不能为空"); } if (userId == null) { throw new BusinessException("用户ID不能为空"); } // 查询订单信息 OrderInfo orderInfo = this.getById(orderId); if (orderInfo == null) { throw new BusinessException("订单不存在"); } // 检查订单状态 if (!"待交易确认".equals(orderInfo.getOrderStatus())) { throw new BusinessException("订单状态不正确,无法确认交易"); } // 检查用户权限 if (!userId.equals(orderInfo.getUserId())) { throw new BusinessException("无权限操作此订单"); } // 更新订单状态 orderInfo.setOrderStatus("已完成"); orderInfo.setUpdatedAt(LocalDateTime.now()); // 如果是积分交易,需要扣除积分 if ("积分".equals(orderInfo.getPaymentType())) { // TODO: 实现积分扣除逻辑 log.info("积分交易确认,订单ID: {}, 需要扣除积分", orderId); } // 保存订单 return this.updateById(orderInfo); } @Override @Transactional(rollbackFor = Exception.class) public boolean replyEvaluation(Long evaluationId, String replyContent, Long replyUserId) { // 参数校验 if (evaluationId == null) { throw new BusinessException("评价ID不能为空"); } if (!StringUtils.hasText(replyContent)) { throw new BusinessException("回复内容不能为空"); } if (replyUserId == null) { throw new BusinessException("回复用户ID不能为空"); } // 查询评价信息 OrderEvaluation evaluation = orderEvaluationMapper.selectById(evaluationId); if (evaluation == null) { throw new BusinessException("评价不存在"); } // 更新回复信息 evaluation.setReplyContent(replyContent); evaluation.setReplyUserId(replyUserId); evaluation.setReplyTime(LocalDateTime.now()); evaluation.setUpdatedAt(LocalDateTime.now()); // 保存评价 return orderEvaluationMapper.updateById(evaluation) > 0; } }