seatonwan9
2025-08-24 5fd8f535ef44ef055d91673740491b9c9177aa89
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
package com.webmanage.controller;
 
import com.webmanage.common.Result;
import com.webmanage.dto.CreateOrderDTO;
import com.webmanage.dto.FileCheckDTO;
import com.webmanage.dto.OrderQueryDTO;
import com.webmanage.entity.OrderInfo;
import com.webmanage.dto.UpdateOrderDetailDTO;
import com.webmanage.service.OrderInfoService;
import com.webmanage.service.TokenService;
import com.webmanage.service.OrderNoService;
import com.webmanage.vo.OrderDetailVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
 
import javax.annotation.Resource;
import javax.validation.Valid;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import com.webmanage.dto.OrderApprovalDTO;
 
/**
 * 订单管理Controller
 */
@Slf4j
@RestController
@RequestMapping("/api/order")
@Api(tags = "订单管理")
@Validated
public class OrderController {
    @Resource private OrderInfoService orderInfoService;
    @Resource private OrderNoService orderNoService;
    @Resource private TokenService tokenService;
 
    @PostMapping("/buyer/page")
    @ApiOperation("分页查询买家订单列表")
    public Result<Object> getBuyerOrderPage(@Valid @RequestBody OrderQueryDTO queryDTO) {
        try { return Result.success(orderInfoService.getBuyerOrderPage(queryDTO)); }
        catch (Exception e) { log.error("查询买家订单列表失败", e); return Result.error("查询买家订单列表失败:" + e.getMessage()); }
    }
 
    @PostMapping("/create")
    @ApiOperation("创建订单(包含订单详情),需在 Header 携带 Idempotency-Token 防重复提交")
    public Result<OrderInfo> createOrder(@RequestHeader(value = "Idempotency-Token", required = false) String token,
                                         @Valid @RequestBody CreateOrderDTO createOrderDTO) {
        try {
            if (!tokenService.verifyAndConsume(token)) {
                return Result.error("请求无效或重复提交,请刷新页面后重试");
            }
            OrderInfo orderInfo = orderInfoService.createOrder(createOrderDTO);
            return Result.success(orderInfo);
        } catch (Exception e) {
            log.error("创建订单失败", e);
            return Result.error("创建订单失败:" + e.getMessage());
        }
    }
 
    @GetMapping("/idempotency/token")
    @ApiOperation("获取一次性防重复提交 Token")
    public Result<Object> getIdempotencyToken(@RequestParam(required = false) Long userId) {
        try {
            String token = tokenService.generateToken(userId);
            return Result.success("token生成",token);
        } catch (Exception e) {
            log.error("生成防重复提交 Token 失败", e);
            return Result.error("生成防重复提交 Token 失败:" + e.getMessage());
        }
    }
 
    @GetMapping("/no/new")
    @ApiOperation("生成唯一订单号")
    public Result<Object> generateOrderNo() {
        try {
            String orderNo = orderNoService.generateOrderNo();
            return Result.success(orderNo);
        } catch (Exception e) {
            log.error("生成订单号失败", e);
            return Result.error("生成订单号失败:" + e.getMessage());
        }
    }
 
 
 
    @PostMapping("/status/next")
    @ApiOperation("更新订单状态到下一个状态")
    public Result<Object> updateOrderStatusToNext(
            @ApiParam("订单ID") @RequestParam @NotBlank String orderId) {
        try {
            boolean success = orderInfoService.updateOrderStatusToNext(orderId);
            return success ? Result.success("订单状态更新到下一个状态成功") : Result.error("订单状态更新失败");
        } catch (Exception e) {
            log.error("更新订单状态到下一个状态失败", e);
            return Result.error("更新订单状态到下一个状态失败:" + e.getMessage());
        }
    }
 
    @PostMapping("/status/previous")
    @ApiOperation("更新订单状态到上一个状态")
    public Result<Object> updateOrderStatusToPrevious(
            @ApiParam("订单ID") @RequestParam @NotBlank String orderId) {
        try {
            boolean success = orderInfoService.updateOrderStatusToPrevious(orderId);
            return success ? Result.success("订单状态更新到上一个状态成功") : Result.error("订单状态更新失败");
        } catch (Exception e) {
            log.error("更新订单状态到上一个状态失败", e);
            return Result.error("更新订单状态到上一个状态失败:" + e.getMessage());
        }
    }
 
    @PostMapping("/seller/page")
    @ApiOperation("分页查询卖家订单列表")
    public Result<Object> getSellerOrderPage(@Valid @RequestBody OrderQueryDTO queryDTO) {
        try {
            if (queryDTO.getProviderId() == null) {
                return Result.error("提供者ID不能为空");
            }
            return Result.success(orderInfoService.getSellerOrderPage(queryDTO));
        } catch (Exception e) {
            log.error("查询卖家订单列表失败", e);
            return Result.error("查询卖家订单列表失败:" + e.getMessage());
        }
    }
 
    @PostMapping("/approval/page")
    @ApiOperation("分页查询待审批订单列表")
    public Result<Object> getPendingApprovalOrderPage(@Valid @RequestBody OrderQueryDTO queryDTO) {
        try {
            return Result.success(orderInfoService.getPendingApprovalOrderPage(queryDTO));
        } catch (Exception e) {
            log.error("查询待审批订单列表失败", e);
            return Result.error("查询待审批订单列表失败:" + e.getMessage());
        }
    }
 
    @GetMapping("/detail/{orderId}")
    @ApiOperation("获取订单详情")
    public Result<OrderDetailVO> getOrderDetail(
            @ApiParam("订单ID") @PathVariable @NotBlank String orderId) {
        try {
            OrderDetailVO orderDetail = orderInfoService.getOrderDetail(orderId);
            return Result.success(orderDetail);
        } catch (Exception e) {
            log.error("获取订单详情失败,订单ID: {}", orderId, e);
            return Result.error("获取订单详情失败:" + e.getMessage());
        }
    }
 
    @PostMapping("/attachment/upload")
    @ApiOperation("上传订单附件")
    public Result<Long> uploadOrderAttachment(
            @ApiParam("订单ID") @RequestParam @NotBlank String orderId,
            @ApiParam("文件名") @RequestParam @NotBlank String fileName,
            @ApiParam("原始文件名") @RequestParam String originalName,
            @ApiParam("附件类型") @RequestParam @NotBlank String fileType,
            @ApiParam("附件大小") @RequestParam @NotNull Long fileSize,
            @ApiParam("附件地址") @RequestParam @NotBlank String fileUrl,
            @ApiParam("存储桶名称") @RequestParam String bucketName,
            @ApiParam("对象名称") @RequestParam String objectName,
            @ApiParam("上传用户ID") @RequestParam @NotNull Long uploadUserId,
            @ApiParam("上传用户名") @RequestParam @NotBlank String uploadUserName,
            @ApiParam("附件类型") @RequestParam String attachmentType,
            @ApiParam("附件描述") @RequestParam String description) {
        try {
            Long attachmentId = orderInfoService.uploadOrderAttachment(
                orderId, fileName, originalName, fileType, fileSize, fileUrl,
                bucketName, objectName, uploadUserId, uploadUserName, attachmentType, description
            );
            return Result.success("上传订单附件成功", attachmentId);
        } catch (Exception e) {
            log.error("上传订单附件失败,订单ID: {}", orderId, e);
            return Result.error("上传订单附件失败:" + e.getMessage());
        }
    }
 
    @PostMapping("/evaluation/add")
    @ApiOperation("添加订单评价")
    public Result<Boolean> addOrderEvaluation(
            @ApiParam("订单ID") @RequestParam @NotBlank String orderId,
            @ApiParam("评价人ID") @RequestParam @NotNull Long evaluatorId,
            @ApiParam("评价人姓名") @RequestParam @NotBlank String evaluatorName,
            @ApiParam("评价人类型") @RequestParam @NotBlank String evaluatorType,
            @ApiParam("评价内容") @RequestParam @NotBlank String content,
            @ApiParam("评分") @RequestParam @NotNull Integer rating,
            @ApiParam("服务评分") @RequestParam Integer serviceRating,
            @ApiParam("质量评分") @RequestParam Integer qualityRating,
            @ApiParam("交付评分") @RequestParam Integer deliveryRating,
            @ApiParam("是否匿名") @RequestParam Boolean isAnonymous) {
        try {
            boolean result = orderInfoService.addOrderEvaluation(
                orderId, evaluatorId, evaluatorName, evaluatorType, content, rating,
                serviceRating, qualityRating, deliveryRating, isAnonymous
            );
            return result ? Result.success(true) : Result.error("添加订单评价失败");
        } catch (Exception e) {
            log.error("添加订单评价失败,订单ID: {}", orderId, e);
            return Result.error("添加订单评价失败:" + e.getMessage());
        }
    }
 
    @PostMapping("/transaction/confirm")
    @ApiOperation("交易确认")
    public Result<Boolean> confirmTransaction(
            @ApiParam("订单ID") @RequestParam @NotBlank String orderId,
            @ApiParam("用户ID") @RequestParam @NotNull Long userId) {
        try {
            boolean result = orderInfoService.confirmTransaction(orderId, userId);
            return result ? Result.success(true) : Result.error("交易确认失败");
        } catch (Exception e) {
            log.error("交易确认失败,订单ID: {}", orderId, e);
            return Result.error("交易确认失败:" + e.getMessage());
        }
    }
 
    @PostMapping("/evaluation/reply")
    @ApiOperation("回复评价")
    public Result<Boolean> replyEvaluation(
            @ApiParam("评价ID") @RequestParam @NotNull Long evaluationId,
            @ApiParam("回复内容") @RequestParam @NotBlank String replyContent,
            @ApiParam("回复用户ID") @RequestParam @NotNull Long replyUserId) {
        try {
            boolean result = orderInfoService.replyEvaluation(evaluationId, replyContent, replyUserId);
            return result ? Result.success(true) : Result.error("回复评价失败");
        } catch (Exception e) {
            log.error("回复评价失败,评价ID: {}", evaluationId, e);
            return Result.error("回复评价失败:" + e.getMessage());
        }
    }
 
    @PostMapping("/detail/update")
    @ApiOperation("更新订单详情")
    public Result<Boolean> updateOrderDetail(@Valid @RequestBody UpdateOrderDetailDTO updateOrderDetailDTO) {
        try {
            boolean result = orderInfoService.updateOrderDetail(updateOrderDetailDTO);
            return result ? Result.success(true) : Result.error("更新订单详情失败");
        } catch (Exception e) {
            log.error("更新订单详情失败,订单ID: {}", updateOrderDetailDTO.getOrderId(), e);
            return Result.error("更新订单详情失败:" + e.getMessage());
        }
    }
 
    @PostMapping("/detail/remarks/update")
    @ApiOperation("只更新订单详情的备注信息")
    public Result<Boolean> updateOrderDetailRemarksOnly(@Valid @RequestBody UpdateOrderDetailDTO.UpdateOrderDetailRemarksOnlyDTO updateOrderDetailDTO) {
        try {
            boolean result = orderInfoService.updateOrderDetailRemarksOnly(updateOrderDetailDTO);
            return result ? Result.success(true) : Result.error("更新订单详情备注失败");
        } catch (Exception e) {
            log.error("更新订单详情备注失败,订单ID: {}", updateOrderDetailDTO.getOrderId(), e);
            return Result.error("更新订单详情备注失败:" + e.getMessage());
        }
    }
 
    @PostMapping("/trade/checkFiles")
    @ApiOperation("文件核查")
    public Result<Boolean> checkFiles(@Valid @RequestBody FileCheckDTO fileCheckDTO) {
        try {
            boolean result = orderInfoService.checkFiles(fileCheckDTO);
            return result ? Result.success(true) : Result.error("文件核查失败");
        } catch (Exception e) {
            log.error("文件核查失败,订单ID: {}", fileCheckDTO.getOrderId(), e);
            return Result.error("文件核查失败:" + e.getMessage());
        }
    }
 
    @DeleteMapping("/attachment/delete/{attachmentId}")
    @ApiOperation("删除订单附件")
    public Result<Boolean> deleteOrderAttachment(@ApiParam("附件ID") @PathVariable @NotNull Long attachmentId) {
        try {
            boolean result = orderInfoService.deleteOrderAttachment(attachmentId);
            return result ? Result.success(true) : Result.error("删除订单附件失败");
        } catch (Exception e) {
            log.error("删除订单附件失败,附件ID: {}", attachmentId, e);
            return Result.error("删除订单附件失败:" + e.getMessage());
        }
    }
 
    @PostMapping("/trade/approve")
    @ApiOperation("审批通过")
    public Result<Boolean> approveOrder(@Valid @RequestBody OrderApprovalDTO orderApprovalDTO) {
        try {
            boolean result = orderInfoService.approveOrder(orderApprovalDTO);
            return result ? Result.success(true) : Result.error("审批通过失败");
        } catch (Exception e) {
            log.error("审批通过失败,订单ID: {}", orderApprovalDTO.getOrderId(), e);
            return Result.error("审批通过失败:" + e.getMessage());
        }
    }
 
    @GetMapping("/agreement/check/{orderId}")
    @ApiOperation("检查订单是否包含协议类型的子订单")
    public Result<Boolean> checkAgreementPriceType(
            @ApiParam("订单ID") @PathVariable @NotBlank String orderId) {
        try {
            boolean hasAgreement = orderInfoService.hasAgreementPriceType(orderId);
            return Result.success(hasAgreement);
        } catch (Exception e) {
            log.error("检查订单协议类型失败,订单ID: {}", orderId, e);
            return Result.error("检查订单协议类型失败:" + e.getMessage());
        }
    }
 
    @DeleteMapping("/cancel/{orderId}")
    @ApiOperation("取消订单")
    public Result<Boolean> cancelOrder(@ApiParam("订单ID") @PathVariable @NotBlank String orderId) {
        try {
            boolean result = orderInfoService.cancelOrder(orderId);
            return result ? Result.success(true) : Result.error("取消订单失败");
        } catch (Exception e) {
            log.error("取消订单失败,订单ID: {}", orderId, e);
            return Result.error("取消订单失败:" + e.getMessage());
        }
    }
}