seatonwan9
2025-08-14 e4f9152bcacf02be0cb376dcb225eaf444b8951b
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
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<OrderInfoMapper, OrderInfo> implements OrderInfoService {
 
    @Resource
    private OrderDetailMapper orderDetailMapper;
 
    @Resource
    private OrderAttachmentMapper orderAttachmentMapper;
 
    @Resource
    private OrderEvaluationMapper orderEvaluationMapper;
 
    @Resource
    private OrderNoService orderNoService;
 
    @Override
    public PageResult<OrderInfo> getBuyerOrderPage(OrderQueryDTO queryDTO) {
        // 参数校验
        if (queryDTO.getUserId() == null) {
            throw new BusinessException("用户ID不能为空");
        }
 
        // 创建分页对象
        Page<OrderInfo> page = new Page<>(queryDTO.getPageNum(), queryDTO.getPageSize());
 
        // 执行分页查询
        IPage<OrderInfo> 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<OrderInfo>(
            result.getRecords(),
            result.getTotal(),
            queryDTO.getPageNum().longValue(),
            queryDTO.getPageSize().longValue(),
            result.getPages()
        );
    }
 
    @Override
    public PageResult<OrderInfo> getSellerOrderPage(OrderQueryDTO queryDTO) {
        // 参数校验
        if (queryDTO.getUserId() == null) {
            throw new BusinessException("用户ID不能为空");
        }
 
        // 创建分页对象
        Page<OrderInfo> page = new Page<>(queryDTO.getPageNum(), queryDTO.getPageSize());
 
        // 执行分页查询
        IPage<OrderInfo> 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<OrderInfo>(
            result.getRecords(),
            result.getTotal(),
            queryDTO.getPageNum().longValue(),
            queryDTO.getPageSize().longValue(),
            result.getPages()
        );
    }
 
    @Override
    public PageResult<OrderInfo> getPendingApprovalOrderPage(OrderQueryDTO queryDTO) {
        // 创建分页对象
        Page<OrderInfo> page = new Page<>(queryDTO.getPageNum(), queryDTO.getPageSize());
 
        // 执行分页查询
        IPage<OrderInfo> 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<OrderInfo>(
            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<OrderDetail> orderDetails = orderDetailMapper.selectByOrderId(orderId);
        List<OrderDetailItemVO> orderDetailItemVOs = orderDetails.stream().map(detail -> {
            OrderDetailItemVO itemVO = new OrderDetailItemVO();
            BeanUtils.copyProperties(detail, itemVO);
            return itemVO;
        }).collect(Collectors.toList());
        orderDetailVO.setOrderDetails(orderDetailItemVOs);
 
        // 查询订单附件列表
        List<OrderAttachment> attachments = orderAttachmentMapper.selectByOrderId(orderId);
        List<OrderAttachmentVO> 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 String 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 orderId;
    }
 
    @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;
    }
}