liujie
2025-05-27 4f46dade49d809e1b8ef92530309dc5f19e39582
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
package com.ruoyi.web.controller.system;
 
 
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.binarywang.wxpay.bean.order.WxPayMpOrderResult;
import com.github.binarywang.wxpay.exception.WxPayException;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.framework.web.service.TokenService;
import com.ruoyi.system.dto.AddScheduleDto;
import com.ruoyi.system.dto.EditScheduleDto;
import com.ruoyi.system.model.*;
import com.ruoyi.system.query.MyOrderListQuery;
import com.ruoyi.system.service.*;
import com.ruoyi.system.vo.MyPushCompanyListVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
 
import javax.validation.Valid;
import java.io.UnsupportedEncodingException;
import java.math.BigDecimal;
import java.net.MalformedURLException;
 
@Slf4j
@RestController
@RequestMapping("/order")
@Api(tags = "订单模块")
public class OrderController {
 
 
    @Autowired
    private TbOrderService orderService;
 
    @Autowired
    private TokenService tokenService;
 
    @Autowired
    private TbCompanyService companyService;
 
    @Autowired
    private TbScheduleService scheduleService;
 
 
 
 
    @ApiOperation(value = "获取我的订单",tags = {"订单模块"})
    @GetMapping("/getMyOrderList")
    public R<Page<MyPushCompanyListVo>> getMyOrderList(MyOrderListQuery query) {
        LoginUser loginUser = tokenService.getLoginUser();
        Long userId = loginUser.getUserId();
        Page<MyPushCompanyListVo> page = orderService.getMyOrderList(query,userId);
        return R.ok(page);
    }
 
 
    @ApiOperation(value = "立即支付",tags = {"订单模块"})
    @PostMapping("/payOrder")
    public R<WxPayMpOrderResult> payOrder(String orderId) {
        LoginUser loginUser = tokenService.getLoginUser();
        Long userId = loginUser.getUserId();
        TbOrder tbOrder = orderService.getById(orderId);
        if(!tbOrder.getUserId().equals(userId.toString())){
            return R.fail("非法操作");
        }
        if(tbOrder.getStatus()!=3){
            return R.fail("订单状态错误");
        }
        TbCompany company = companyService.getById(tbOrder.getCompanyId());
        // 判断是否在支付前下架了  或者删除了
        if(company==null || company.getStatus()!=1 || company.getIsDelete()==1){
            return R.fail("该公司已下架或者删除");
        }
 
        WxPayMpOrderResult result = orderService.payOrder(tbOrder,userId);
        return R.ok(result);
    }
 
 
 
    @ApiOperation(value = "下单预定",tags = {"订单模块"})
    @PostMapping("/placeOrder")
    public synchronized R<?> placeOrder(String companyId) {
        if(StringUtils.isEmpty(companyId)){
            R.fail("id不能为空");
        }
        LoginUser loginUser = tokenService.getLoginUser();
        Long userId = loginUser.getUserId();
        // 查看现在是否可以单 判断这个公司是否上架  是否已经产生的了订单
        TbCompany company = companyService.getById(companyId);
        if(company.getStatus()!=1){
            return R.fail("公司未上架");
        }
        long count = orderService.count(new LambdaQueryWrapper<TbOrder>().eq(TbOrder::getCompanyId, companyId).ne(TbOrder::getStatus, -1));
        if (count > 0) {
            return R.fail("改公司已被预定");
        }
        orderService.placeOrder(companyId,company,userId);
        return R.ok();
    }
 
 
    @ApiOperation(value = "买家完成订单",tags = {"订单模块"})
    @PostMapping("/buyerSuccessOrder")
    public synchronized R<?> buyerSuccessOrder(String orderId) {
        if(StringUtils.isEmpty(orderId)){
            R.fail("订单id不能为空");
        }
        LoginUser loginUser = tokenService.getLoginUser();
        Long userId = loginUser.getUserId();
        TbOrder order = orderService.getById(orderId);
        if(order==null){
            return R.fail("订单不存在");
        }
        TbCompany company = companyService.getById(order.getCompanyId());
        if (!order.getUserId().equals(userId.toString())) {
            return R.fail("非法操作");
        }
        if(order.getStatus()!=5){
            return R.fail("该订单状态不能完成");
        }
        // 订单完成  商品已售卖
        order.setStatus(6);
        order.updateById();
        company.setStatus(2);
 
        // 分佣
        orderService.commission(order,company.getUserId());
 
 
        company.updateById();
        return R.ok();
 
    }
 
 
    @ApiOperation(value = "买家回复进度",tags = {"订单模块"})
    @PostMapping("/buyerAddSchedule")
    public synchronized R<?> buyerAddSchedule(@RequestBody @Valid AddScheduleDto dto) {
        LoginUser loginUser = tokenService.getLoginUser();
        Long userId = loginUser.getUserId();
        TbOrder order = orderService.getById(dto.getOrderId());
        if(order==null){
            return R.fail("订单不存在");
        }
        if (!order.getUserId().equals(userId.toString())) {
            return R.fail("非法操作");
        }
        if(order.getStatus()!=4){
            return R.fail("该订单状态不能回复");
        }
        scheduleService.buyerAddSchedule(dto,userId);
        return R.ok();
 
    }
 
 
    @ApiOperation(value = "买家取消订单",tags = {"订单模块"})
    @PostMapping("/buyerCancelOrder")
    public synchronized R<?> buyerCancelOrder(String orderId) {
        if(StringUtils.isEmpty(orderId)){
            R.fail("订单id不能为空");
        }
        LoginUser loginUser = tokenService.getLoginUser();
        Long userId = loginUser.getUserId();
        TbOrder order = orderService.getById(orderId);
        if(order==null){
            return R.fail("订单不存在");
        }
        if (!order.getUserId().equals(userId.toString())) {
            return R.fail("非法操作");
        }
        if(order.getStatus()!=3){
            return R.fail("该订单状态不能取消");
        }
        order.setStatus(-1);
        order.updateById();
        return R.ok();
 
    }
 
 
 
    @ApiOperation(value = "编辑办理进度",tags = {"发布模块"})
    @PostMapping("/updateSchedule")
    public R<?> updateSchedule(@RequestBody @Valid EditScheduleDto dto) {
        LoginUser loginUser = tokenService.getLoginUser();
        Long userId = loginUser.getUserId();
        TbSchedule schedule = scheduleService.getById(dto.getScheduleId());
        if(schedule==null){
            return R.fail("进度不存在");
        }
        if(!schedule.getUserId().equals(userId.toString())){
            return R.fail("非法操作");
        }
        schedule.setText(dto.getText());
        schedule.setImg(dto.getImg());
        schedule.updateById();
        return R.ok();
    }
 
 
    @ApiOperation(value = "删除办理进度",tags = {"发布模块"})
    @DeleteMapping("/delSchedule/{scheduleId}")
    public R<?> delSchedule(@PathVariable("scheduleId") String scheduleId) {
        LoginUser loginUser = tokenService.getLoginUser();
        Long userId = loginUser.getUserId();
        TbSchedule schedule = scheduleService.getById(scheduleId);
        if(schedule==null){
            return R.fail("进度不存在");
        }
        if(!schedule.getUserId().equals(userId.toString())){
            return R.fail("非法操作");
        }
        scheduleService.removeById(scheduleId);
        return R.ok();
    }
 
 
 
 
 
    /**
     * 微信小程序支付成功回调函数
     */
    @PostMapping(value = "/WX/zxsCallback")
    public String wxNotify(@RequestBody String xmlData) {
        try {
            System.out.println("微信支付回调!!!!!!!!!!");
            return orderService.weAppletChatNotice(xmlData);
        } catch (WxPayException e) {
            throw new RuntimeException(e);
        }
    }
 
 
 
}