无关风月
2024-10-14 b60e601d62c0e2d5c4fd1df9f12fa09f2bf99987
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
423
424
425
426
package com.xinquan.order.controller.management;
 
 
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xinquan.common.core.domain.R;
import com.xinquan.common.core.utils.page.CollUtils;
import com.xinquan.common.core.utils.page.PageDTO;
import com.xinquan.common.security.utils.SecurityUtils;
import com.xinquan.course.api.domain.Course;
import com.xinquan.course.api.feign.RemoteCourseService;
import com.xinquan.meditation.api.domain.Meditation;
import com.xinquan.meditation.api.feign.RemoteMeditationService;
import com.xinquan.order.api.domain.Order;
import com.xinquan.order.api.domain.dto.OrderListDTO;
import com.xinquan.order.api.domain.vo.OrderCountVO;
import com.xinquan.order.domain.OrderPaymentRecord;
import com.xinquan.order.service.OrderPaymentRecordService;
import com.xinquan.order.service.OrderService;
import com.xinquan.system.api.RemoteUserService;
import com.xinquan.system.api.domain.AppUser;
import com.xinquan.system.api.domain.SysUser;
import com.xinquan.user.api.feign.RemoteAppUserService;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;
 
import javax.annotation.Resource;
import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;
 
/**
 * <p>
 * 订单表 前端控制器
 * </p>
 *
 * @author mitao
 * @since 2024-08-21
 */
@RestController
@RequestMapping("/mgt/order/order")
public class MgtOrderController {
    @Resource
    private OrderService orderService;
    @Resource
    private OrderPaymentRecordService orderPaymentRecordService;
    @Resource
    private RemoteAppUserService remoteAppUserService;
    @Resource
    private RemoteCourseService remoteCourseService;
    @Resource
    private RemoteMeditationService remoteMeditationService;
    @Resource
    private RemoteUserService remoteUserService;
 
    @PostMapping("/orderList")
    @ApiOperation(value = "订单列表管理列表-分页", tags = {"管理后台-订单列表管理"})
    public R<PageDTO<Order>> orderList(@RequestBody OrderListDTO courseDTO) {
        String startTime = null;
        String endTime = null;
        if (org.springframework.util.StringUtils.hasLength(courseDTO.getTime())){
            String[] split = courseDTO.getTime().split(" - ");
            startTime = split[0]+"00:00:00";
            endTime = split[1]+"23:59:59";
        }
        List<Integer> payType1 = new ArrayList<>();
        payType1.add(1);
        payType1.add(5);
        List<Integer> payType2 = new ArrayList<>();
        payType2.add(2);
        payType2.add(6);
        List<Integer> payType3 = new ArrayList<>();
        payType3.add(4);
        payType3.add(7);
        LambdaQueryWrapper<Order> courseLambdaQueryWrapper = new LambdaQueryWrapper<>();
        courseLambdaQueryWrapper.between(Order::getCreateTime, startTime, endTime);
        if (courseDTO.getPayType()!=null){
            switch (courseDTO.getPayType()){
                case 1:
                    courseLambdaQueryWrapper.in(Order::getPayType,payType1);
                    break;
                case 2:
                    courseLambdaQueryWrapper.in(Order::getPayType,payType2);
                    break;
                case 3:
                    courseLambdaQueryWrapper.eq(Order::getPayType,courseDTO.getPayType());
                    break;
                case 4:
                    courseLambdaQueryWrapper.in(Order::getPayType,payType3);
                    break;
            }
 
        }
        courseLambdaQueryWrapper.eq(courseDTO.getPaymentStatus()!=null,Order::getPaymentStatus, courseDTO.getPaymentStatus());
        courseLambdaQueryWrapper.eq(courseDTO.getOrderFrom()!=null,Order::getOrderFrom, courseDTO.getOrderFrom());
        courseLambdaQueryWrapper.eq(courseDTO.getUid()!=null&&(!courseDTO.getUid().isEmpty()),Order::getAppUserId, courseDTO.getUid());
        if (org.springframework.util.StringUtils.hasLength(courseDTO.getBuyContent())){
            // 查询购买内容
            List<Long> collect1 = orderService.lambdaQuery().like(Order::getVipType, courseDTO.getBuyContent()).list().stream()
                    .map(Order::getId).collect(Collectors.toList());
            List<Long> collect2 = orderService.lambdaQuery().like(Order::getBizOrderNo, courseDTO.getBuyContent()).list().stream()
                    .map(Order::getId).collect(Collectors.toList());
            List<Long> data = remoteCourseService.getCourseIdsByName(courseDTO.getBuyContent()).getData();
            if (!data.isEmpty()){
                List<Long> collect3 = orderService.lambdaQuery().in(Order::getBusinessId, data)
                        .eq(Order::getOrderFrom, 2)
                        .list().stream()
                        .map(Order::getId).collect(Collectors.toList());
                if (!collect3.isEmpty()){
                    collect1.addAll(collect3);
                }
            }
            List<Long> data1 = remoteMeditationService.getMeditationIdsByName(courseDTO.getBuyContent()).getData();
            if (!data1.isEmpty()){
                List<Long> collect3 = orderService.lambdaQuery().in(Order::getBusinessId, data1)
                        .eq(Order::getOrderFrom, 1)
                        .list().stream()
                        .map(Order::getId).collect(Collectors.toList());
                if (!collect3.isEmpty()){
                    collect1.addAll(collect3);
                }
            }
 
            collect1.addAll(collect2);
            List<Long> collect = collect1.stream().distinct().collect(Collectors.toList());
            if (collect.isEmpty()){
                collect.add(-1L);
            }
            courseLambdaQueryWrapper.in(Order::getId,collect);
        }
 
        courseLambdaQueryWrapper.orderByDesc(Order::getCreateTime);
        if (org.springframework.util.StringUtils.hasLength(courseDTO.getUserNameOrPhone())){
            List<Long> collect = remoteAppUserService.getAppUserByNameOrPhone(courseDTO.getUserNameOrPhone()).getData();
            if (collect.isEmpty()){
                collect.add(-1L);
            }
            courseLambdaQueryWrapper.in(Order::getAppUserId, collect);
        }
 
        Page<Order> page = orderService.page(new Page<>(courseDTO.getPageCurr(), courseDTO.getPageSize()), courseLambdaQueryWrapper);
        if (CollUtils.isEmpty(page.getRecords())) {
            return R.ok(PageDTO.empty(page));
        }
        for (Order record : page.getRecords()) {
            record.setUid(record.getId().toString());
            AppUser byId1 = remoteAppUserService.getAppUserById(record.getAppUserId()+"").getData();
            if (Objects.nonNull(byId1)){
                record.setUserName(byId1.getNickname());
                record.setCellPhone(byId1.getCellPhone());
            }
            if (record.getOrderFrom()!=null){
                switch (record.getOrderFrom()){
                    case 1:
                        Meditation data = remoteMeditationService.getMeditationById(record.getBusinessId()).getData();
                        if (data!=null){
                            record.setCategoryMeditationName(data.getCategoryName());
                            record.setMeditationTitle(data.getMeditationTitle());
                            record.setIconUrl(data.getIconUrl());
                            record.setDetailDescription(data.getDetailDescription());
                            record.setGeneralPriceMeditation(data.getGeneralPrice());
                            record.setListingStatusMeditation(data.getListingStatus());
                            record.setMeditationUid(data.getId()+"");
                        }
                        break;
                    case 2:
                        Course data1 = remoteCourseService.getCourseById(record.getBusinessId(),"").getData();
                        if (data1!=null){
                            record.setCategoryCourseName(data1.getCategoryName());
                            record.setCourseTitle(data1.getCourseTitle());
                            record.setCoverUrl(data1.getCoverUrl());
                            record.setTutor(data1.getTutor());
                            record.setCourseChapterCount(data1.getCourseChapterCount());
                            record.setGeneralPriceCourse(data1.getGeneralPrice());
                            record.setListingStatusCourse(data1.getListingStatus());
                            record.setCourseUid(data1.getId()+"");
                        }
                        break;
                    case 3:
                        record.setBuyContent(record.getVipType());
                        record.setGeneralPriceVip(record.getRealPayAmount());
                        break;
                }
            }
        }
        return R.ok(PageDTO.of(page, Order.class));
    }
    @PostMapping("/orderCount")
    @ApiOperation(value = "订单列表管理列表上方合计数据", tags = {"管理后台-订单列表管理"})
 
    public R<OrderCountVO> orderCount(@RequestBody OrderListDTO courseDTO) {
        OrderCountVO orderCountVO = new OrderCountVO();
        String startTime = null;
        String endTime = null;
        if (org.springframework.util.StringUtils.hasLength(courseDTO.getTime())){
            String[] split = courseDTO.getTime().split(" - ");
            startTime = split[0]+"00:00:00";
            endTime = split[1]+"23:59:59";
        }
        List<Integer> payType1 = new ArrayList<>();
        payType1.add(1);
        payType1.add(5);
        List<Integer> payType2 = new ArrayList<>();
        payType2.add(2);
        payType2.add(6);
        List<Integer> payType3 = new ArrayList<>();
        payType3.add(4);
        payType3.add(7);
        LambdaQueryWrapper<Order> courseLambdaQueryWrapper = new LambdaQueryWrapper<>();
        courseLambdaQueryWrapper.between(Order::getCreateTime, startTime, endTime);
        if (courseDTO.getPayType()!=null){
            switch (courseDTO.getPayType()){
                case 1:
                    courseLambdaQueryWrapper.in(Order::getPayType,payType1);
                    break;
                case 2:
                    courseLambdaQueryWrapper.in(Order::getPayType,payType2);
                    break;
                case 3:
                    courseLambdaQueryWrapper.eq(Order::getPayType,courseDTO.getPayType());
                    break;
                case 4:
                    courseLambdaQueryWrapper.in(Order::getPayType,payType3);
                    break;
            }
 
        }
        courseLambdaQueryWrapper.eq(courseDTO.getPaymentStatus()!=null,Order::getPaymentStatus, courseDTO.getPaymentStatus());
        courseLambdaQueryWrapper.eq(courseDTO.getOrderFrom()!=null,Order::getOrderFrom, courseDTO.getOrderFrom());
        if (org.springframework.util.StringUtils.hasLength(courseDTO.getBuyContent())){
            // 查询购买内容
            List<Long> collect1 = orderService.lambdaQuery().like(Order::getVipType, courseDTO.getBuyContent()).list().stream()
                    .map(Order::getId).collect(Collectors.toList());
            List<Long> collect2 = orderService.lambdaQuery().like(Order::getBizOrderNo, courseDTO.getBuyContent()).list().stream()
                    .map(Order::getId).collect(Collectors.toList());
            List<Long> data = remoteCourseService.getCourseIdsByName(courseDTO.getBuyContent()).getData();
            if (!data.isEmpty()){
                List<Long> collect3 = orderService.lambdaQuery().in(Order::getBusinessId, data)
                        .eq(Order::getOrderFrom, 2)
                        .list().stream()
                        .map(Order::getId).collect(Collectors.toList());
                if (!collect3.isEmpty()){
                    collect1.addAll(collect3);
                }
            }
            List<Long> data1 = remoteMeditationService.getMeditationIdsByName(courseDTO.getBuyContent()).getData();
            if (!data1.isEmpty()){
                List<Long> collect3 = orderService.lambdaQuery().in(Order::getBusinessId, data1)
                        .eq(Order::getOrderFrom, 1)
                        .list().stream()
                        .map(Order::getId).collect(Collectors.toList());
                if (!collect3.isEmpty()){
                    collect1.addAll(collect3);
                }
            }
 
            collect1.addAll(collect2);
            List<Long> collect = collect1.stream().distinct().collect(Collectors.toList());
            if (collect.isEmpty()){
                collect.add(-1L);
            }
            courseLambdaQueryWrapper.in(Order::getId,collect);
        }
        courseLambdaQueryWrapper.orderByDesc(Order::getCreateTime);
        if (org.springframework.util.StringUtils.hasLength(courseDTO.getUserNameOrPhone())){
            List<Long> collect = remoteAppUserService.getAppUserByNameOrPhone(courseDTO.getUserNameOrPhone()).getData();
            if (collect.isEmpty()){
                collect.add(-1L);
            }
            courseLambdaQueryWrapper.in(Order::getAppUserId, collect);
        }
        List<Order> page = orderService.list(courseLambdaQueryWrapper);
        Integer payCount = 0;
        Integer completeCount = 0;
        Integer cancelCount = 0;
        BigDecimal totalMoney = new BigDecimal("0");
        for (Order record : page) {
            if (record.getPaymentStatus()!=null){
                switch (record.getPaymentStatus()){
                    case 1:
                        payCount++;
                        break;
                    case 2:
                        completeCount++;
                        break;
                    case 3:
                        cancelCount++;
                        break;
                }
            }
            if (record.getRealPayAmount()!=null && record.getRefundStatus()!=3){
                totalMoney = totalMoney.add(record.getRealPayAmount());
            }
        }
        orderCountVO.setTotalCount(page.size());
        orderCountVO.setPayCount(payCount);
        orderCountVO.setCompleteCount(completeCount);
        orderCountVO.setCancelCount(cancelCount);
        orderCountVO.setTotalMoney(totalMoney);
        return R.ok(orderCountVO);
    }
 
    @GetMapping("/detailOrder")
    @ApiOperation(value = "查看详情订单列表管理", notes = "管理后台-订单列表管理")
    public R<Order> detailOrder(String uid) {
        Order record = orderService.getById(uid);
        if (record.getOrderFrom()!=null){
            switch (record.getOrderFrom()){
                case 1:
                    Meditation data = remoteMeditationService.getMeditationById(record.getBusinessId()).getData();
                    if (data!=null){
                        record.setCategoryMeditationName(data.getCategoryName());
                        record.setMeditationTitle(data.getMeditationTitle());
                        record.setIconUrl(data.getIconUrl());
                        record.setDetailDescription(data.getDetailDescription());
                        record.setGeneralPriceMeditation(data.getGeneralPrice());
                        record.setListingStatusMeditation(data.getListingStatus());
                        record.setMeditationUid(data.getId()+"");
                    }
                    break;
                case 2:
                    Course data1 = remoteCourseService.getCourseById(record.getBusinessId(),"").getData();
                    if (data1!=null){
                        record.setCategoryCourseName(data1.getCategoryName());
                        record.setCourseTitle(data1.getCourseTitle());
                        record.setCoverUrl(data1.getCoverUrl());
                        record.setTutor(data1.getTutor());
                        record.setCourseChapterCount(data1.getCourseChapterCount());
                        record.setGeneralPriceCourse(data1.getGeneralPrice());
                        record.setListingStatusCourse(data1.getListingStatus());
                        record.setCourseUid(data1.getId()+"");
                    }
                    break;
                case 3:
                    record.setBuyContent(record.getVipType());
                    record.setGeneralPriceVip(record.getRealPayAmount());
                    break;
            }
        }
        record.setUid(record.getId().toString());
        AppUser byId2 = remoteAppUserService.getAppUserById(record.getAppUserId()+"").getData();
        AppUser byId3 = remoteAppUserService.getAppUserById(record.getGiveUserId()+"").getData();
        if (Objects.nonNull(byId2)){
            record.setUserName(byId2.getNickname());
            record.setAvatar(byId2.getAvatar());
            record.setCellPhone(byId2.getCellPhone());
        }
        if (Objects.nonNull(byId3)){
            record.setUserNameGive(byId3.getNickname());
            record.setAvatarGive(byId3.getAvatar());
            record.setCellPhoneGive(byId3.getCellPhone());
        }
        if (record.getChangePriceOperator()!=null){
            SysUser data = remoteUserService.getSysUserById(record.getChangePriceOperator() + "").getData();
            record.setChangePriceOperatorName(data.getNickName()+"("+data.getUserName()+")");
        }
        if (record.getPayType()!=null){
            List<OrderPaymentRecord> list = orderPaymentRecordService.list(new LambdaQueryWrapper<OrderPaymentRecord>()
                    .eq(OrderPaymentRecord::getOrderId, record.getId())
                    .orderByDesc(OrderPaymentRecord::getPaymentType)
            );
            if (record.getPayType() ==5 || record.getPayType()==6 || record.getPayType()==7){
                if(record.getRealPayAmount()!=null){
                    switch (record.getPayType()){
                        case 5:
                            if (!list.isEmpty()){
                                OrderPaymentRecord orderPaymentRecord = list.get(1);
                                OrderPaymentRecord orderPaymentRecord1 = list.get(0);
                                switch (orderPaymentRecord.getPaymentType()){
                                    case 1:
                                        record.setPaymentType("微信(¥"+(orderPaymentRecord.getPayAmount()==null?"":orderPaymentRecord.getPayAmount())+")"
                                        +" + 余额(¥"+orderPaymentRecord1.getPayAmount()+")");
                                        break;
                                    case 2:
                                        record.setPaymentType("支付宝(¥"+(orderPaymentRecord.getPayAmount()==null?"":orderPaymentRecord.getPayAmount())+")"
                                                +" + 余额(¥"+orderPaymentRecord1.getPayAmount()+")");
 
                                        break;
                                    case 3:
                                        record.setPaymentType("内购(¥"+(orderPaymentRecord.getPayAmount()==null?"":orderPaymentRecord.getPayAmount())+")"
                                                +" + 余额(¥"+orderPaymentRecord1.getPayAmount()+")");
                                        break;
                                }
                            }
                            break;
                        case 6:
                            record.setPaymentType("支付宝(¥"+record.getRealPayAmount()+")");
                            break;
                        case 7:
                            record.setPaymentType("内购(¥"+record.getRealPayAmount()+")");
                            break;
                    }
                }
            }else{
                if(record.getRealPayAmount()!=null){
                    switch (record.getPayType()){
                        case 1:
                            record.setPaymentType("微信(¥"+record.getRealPayAmount()+")");
                            break;
                        case 2:
                            record.setPaymentType("支付宝(¥"+record.getRealPayAmount()+")");
                            break;
                        case 3:
                            record.setPaymentType("余额(¥"+record.getRealPayAmount()+")");
                            break;
                        case 4:
                            record.setPaymentType("内购(¥"+record.getRealPayAmount()+")");
                            break;
                    }
                }
            }
        }
        return R.ok(record);
    }
    @GetMapping("/cancel")
    @ApiOperation(value = "取消订单", notes = "管理后台-订单列表管理")
    public R updateState(String uid) {
        Order byId = orderService.getById(uid);
        byId.setPaymentStatus(3);
        orderService.updateById(byId);
        return R.ok();
    }
}