“mitao”
2025-12-22 2034f95ef168280b2a355c839c5fd5a505cd6f2e
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
package com.ruoyi.web.controller.api;
 
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.framework.web.service.TokenService;
import com.ruoyi.system.dto.BigGiftPlaceOrderDTO;
import com.ruoyi.system.dto.FinalPaymentDTO;
import com.ruoyi.system.dto.PhysicalGoodsPlaceOrderDTO;
import com.ruoyi.system.model.TAppUser;
import com.ruoyi.system.query.AppletOrderListQuery;
import com.ruoyi.system.service.AppletOrderService;
import com.ruoyi.system.service.TAppUserService;
import com.ruoyi.system.service.TBigGiftPackageOrderService;
import com.ruoyi.system.service.TPhysicalGoodsOrderService;
import com.ruoyi.system.vo.AppletOrderDetailVO;
import com.ruoyi.system.vo.AppletOrderListVO;
import com.ruoyi.system.vo.PlaceOrderVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Lazy;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
 
import javax.validation.Valid;
 
/**
 * 下单接口
 *
 * @author mitao
 * @since 2025-12-16
 */
@Slf4j
@RestController
@RequestMapping("/order")
@Api(tags = "下单模块")
@Validated
@RequiredArgsConstructor(onConstructor_ = {@Lazy})
public class OrderController {
 
    private final TokenService tokenService;
    private final TAppUserService appUserService;
    private final TBigGiftPackageOrderService bigGiftPackageOrderService;
    private final TPhysicalGoodsOrderService physicalGoodsOrderService;
    private final AppletOrderService appletOrderService;
 
    /**
     * 订单列表分页查询
     *
     * @param query 查询条件
     * @return 订单列表
     */
    @ApiOperation(value = "订单列表分页查询", response = AppletOrderListVO.class)
    @PostMapping("/list")
    public R<Page<AppletOrderListVO>> getOrderList(@RequestBody AppletOrderListQuery query) {
        Integer userId = tokenService.getLoginUserApplet().getUserId();
        Page<AppletOrderListVO> page = appletOrderService.getOrderList(query, userId);
        return R.ok(page);
    }
 
    /**
     * 大礼包商品下单
     *
     * @param dto 下单请求参数
     * @return 下单结果(包含订单信息和支付参数)
     */
    @ApiOperation(value = "大礼包商品下单", response = PlaceOrderVO.class)
    @PostMapping("/big-gift/place")
    public R<PlaceOrderVO> placeBigGiftOrder(@Valid @RequestBody BigGiftPlaceOrderDTO dto) {
        // 获取当前登录用户
        Integer userId = tokenService.getLoginUserApplet().getUserId();
        TAppUser appUser = appUserService.getById(userId);
        if (appUser == null) {
            return R.fail("用户不存在");
        }
        if (appUser.getOpenid() == null || appUser.getOpenid().isEmpty()) {
            return R.fail("用户未绑定微信");
        }
 
        PlaceOrderVO result = bigGiftPackageOrderService.placeOrder(dto, userId, appUser.getOpenid());
        return R.ok(result);
    }
 
    /**
     * 实物商品下单
     *
     * @param dto 下单请求参数
     * @return 下单结果(包含订单信息和支付参数)
     */
    @ApiOperation(value = "实物商品下单", response = PlaceOrderVO.class)
    @PostMapping("/physical/place")
    public R<PlaceOrderVO> placePhysicalGoodsOrder(@Valid @RequestBody PhysicalGoodsPlaceOrderDTO dto) {
        // 获取当前登录用户
        Integer userId = tokenService.getLoginUserApplet().getUserId();
        TAppUser appUser = appUserService.getById(userId);
        if (appUser == null) {
            return R.fail("用户不存在");
        }
        if (appUser.getOpenid() == null || appUser.getOpenid().isEmpty()) {
            return R.fail("用户未绑定微信");
        }
 
        PlaceOrderVO result = physicalGoodsOrderService.placeOrder(dto, userId, appUser.getOpenid());
        return R.ok(result);
    }
 
    /**
     * 大礼包尾款支付
     *
     * @param dto 尾款支付请求参数
     * @return 支付结果(包含订单信息和支付参数)
     */
    @ApiOperation(value = "大礼包尾款支付", response = PlaceOrderVO.class)
    @PostMapping("/big-gift/pay-final")
    public R<PlaceOrderVO> payFinalPayment(@Valid @RequestBody FinalPaymentDTO dto) {
        // 获取当前登录用户
        Integer userId = tokenService.getLoginUserApplet().getUserId();
        TAppUser appUser = appUserService.getById(userId);
        if (appUser == null) {
            return R.fail("用户不存在");
        }
        if (appUser.getOpenid() == null || appUser.getOpenid().isEmpty()) {
            return R.fail("用户未绑定微信");
        }
 
        PlaceOrderVO result = bigGiftPackageOrderService.payFinalPayment(dto.getOrderId(), userId, appUser.getOpenid());
        return R.ok(result);
    }
 
    /**
     * 实物商品订单详情
     *
     * @param id 订单ID
     * @return 订单详情
     */
    @ApiOperation(value = "实物商品订单详情", response = AppletOrderDetailVO.class)
    @GetMapping("/physical/detail/{id}")
    public R<AppletOrderDetailVO> getPhysicalOrderDetail(@PathVariable Integer id) {
        Integer userId = tokenService.getLoginUserApplet().getUserId();
        AppletOrderDetailVO detail = appletOrderService.getPhysicalOrderDetail(id, userId);
        if (detail == null) {
            return R.fail("订单不存在或无权查看");
        }
        return R.ok(detail);
    }
 
    /**
     * 大礼包订单详情
     *
     * @param id 订单ID
     * @return 订单详情
     */
    @ApiOperation(value = "大礼包订单详情", response = AppletOrderDetailVO.class)
    @GetMapping("/big-gift/detail/{id}")
    public R<AppletOrderDetailVO> getBigGiftOrderDetail(@PathVariable Integer id) {
        Integer userId = tokenService.getLoginUserApplet().getUserId();
        AppletOrderDetailVO detail = appletOrderService.getBigGiftOrderDetail(id, userId);
        if (detail == null) {
            return R.fail("订单不存在或无权查看");
        }
        return R.ok(detail);
    }
 
    /**
     * 实物商品确认收货
     *
     * @param id 订单ID
     * @return 操作结果
     */
    @ApiOperation(value = "实物商品确认收货")
    @PostMapping("/physical/confirm/{id}")
    public R<Void> confirmPhysicalOrder(@PathVariable Integer id) {
        Integer userId = tokenService.getLoginUserApplet().getUserId();
        boolean success = appletOrderService.confirmPhysicalOrder(id, userId);
        return success ? R.ok() : R.fail("确认收货失败,订单不存在或状态不正确");
    }
 
    /**
     * 大礼包确认收货
     *
     * @param id 订单ID
     * @return 操作结果
     */
    @ApiOperation(value = "大礼包确认收货")
    @PostMapping("/big-gift/confirm/{id}")
    public R<Void> confirmBigGiftOrder(@PathVariable Integer id) {
        Integer userId = tokenService.getLoginUserApplet().getUserId();
        boolean success = appletOrderService.confirmBigGiftOrder(id, userId);
        return success ? R.ok() : R.fail("确认收货失败,订单不存在或状态不正确");
    }
}