lisy
2023-07-11 0e36bac3643e7b44c5c316f736c6a37cab2d57fc
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
package com.dsh.course.service.impl;
 
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dsh.course.entity.*;
import com.dsh.course.feignclient.account.CoachClient;
import com.dsh.course.feignclient.account.model.Coach;
import com.dsh.course.feignclient.activity.BenefitVideoClient;
import com.dsh.course.feignclient.activity.CouponClient;
import com.dsh.course.feignclient.activity.model.BenefitsVideos;
import com.dsh.course.feignclient.activity.model.Coupon;
import com.dsh.course.feignclient.other.StoreClient;
import com.dsh.course.feignclient.other.model.Store;
import com.dsh.course.mapper.*;
import com.dsh.course.model.dto.DiscountJsonDto;
import com.dsh.course.model.vo.RegisterCourseVo;
import com.dsh.course.model.vo.request.ClasspaymentRequest;
import com.dsh.course.model.vo.request.CourseOfAfterRequest;
import com.dsh.course.model.vo.request.CourseWithDetailsRequest;
import com.dsh.course.model.vo.request.UpdateCourseVideoStatusRequest;
import com.dsh.course.model.vo.response.AppUserVideoResponse;
import com.dsh.course.model.vo.response.CourseDetailsResponse;
import com.dsh.course.model.vo.response.CourseOfVideoResponse;
import com.dsh.course.service.TCoursePackagePaymentService;
import com.dsh.course.util.PayMoneyUtil;
import com.dsh.course.util.ResultUtil;
import com.dsh.course.util.StrUtils;
import com.dsh.course.util.UUIDUtil;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
 
import javax.annotation.Resource;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
 
/**
 * <p>
 * 用户课程包购买记录 服务实现类
 * </p>
 *
 * @author administrator
 * @since 2023-06-14
 */
@Service
public class TCoursePackagePaymentServiceImpl extends ServiceImpl<TCoursePackagePaymentMapper, TCoursePackagePayment> implements TCoursePackagePaymentService {
 
 
    @Resource
    private BenefitVideoClient bfvoClient;
 
 
    @Resource
    private UserVideoDetailsMapper uvdmapper;
 
    @Resource
    private PostCourseVideoMapper pcvMapper;
 
    @Resource
    private TCoursePackageMapper tcpmapper;
 
    @Resource
    private StoreClient stoClient;
 
    @Resource
    private CoachClient coachClient;
 
    @Resource
    private CoursePackageStudentMapper cpsMapper;
 
    @Resource
    private CancelledClassesMapper cacMapper;
 
    @Resource
    private TCoursePackageDiscountMapper tcpdMapper;
 
    @Resource
    private CouponClient client;
 
    @Autowired
    private PayMoneyUtil payMoneyUtil;
 
 
    /**
     * 获取课包购买人数
     * @param coursePackageId
     * @return
     */
    @Override
    public Integer queryCountNumber(Integer coursePackageId) {
        return this.baseMapper.queryCountNumber(coursePackageId);
    }
 
    @Override
    public List<AppUserVideoResponse> queryAfterVideo(CourseOfAfterRequest search,List<Integer> courseIds) {
        List<AppUserVideoResponse> responses = new ArrayList<>();
        List<PostCourseVideo> videoList = pcvMapper.selectList(new QueryWrapper<PostCourseVideo>()
                .eq("coursePackageId", courseIds));
 
        if (videoList.size() > 0){
            List<Integer> videoIds = videoList.stream().map(PostCourseVideo::getCourseId).collect(Collectors.toList());
            List<UserVideoDetails> userVideoDetails = uvdmapper.selectList(new QueryWrapper<UserVideoDetails>()
                    .in("courseId", videoIds));
            if (userVideoDetails.size() > 0){
                for (UserVideoDetails userVideoDetail : userVideoDetails) {
                    AppUserVideoResponse response = new AppUserVideoResponse();
                    TCoursePackage coursePackage = tcpmapper.selectById(userVideoDetail.getCoursePackageId());
                    response.setPackageName(coursePackage.getName());
                    response.setCoursePackageId(userVideoDetail.getCoursePackageId());
                    BenefitsVideos videosWithIds = bfvoClient.getVideosWithIds(userVideoDetail.getCourseId());
                    response.setVideoId(userVideoDetail.getCourseId());
                    response.setCoverImage(videosWithIds.getCover());
                    if (null != search.getSearch()){
//                        根据正则模糊匹配
                        String pattern = ".*" + search.getSearch() + ".*";
                        Pattern regex = Pattern.compile(pattern);
                        Matcher matcher = regex.matcher(videosWithIds.getName());
                        if (matcher.matches()) {
                            response.setVideoName(videosWithIds.getName());
                        }
                    }else {
                        response.setVideoName(videosWithIds.getName());
                    }
                    response.setSynopsis(videosWithIds.getIntroduction());
                    response.setIntegral(videosWithIds.getIntegral());
                    response.setStudyStatus(userVideoDetail.getState());
                    responses.add(response);
                }
                Collections.sort(responses, Comparator.comparing(AppUserVideoResponse::getStudyStatus));
            }
        }
        return responses;
    }
 
    @Override
    public CourseOfVideoResponse queryVideoDetails(CourseWithDetailsRequest detailsRequest,Integer appUserId) {
        CourseOfVideoResponse response = new CourseOfVideoResponse();
        BenefitsVideos videosWithIds = bfvoClient.getVideosWithIds(detailsRequest.getVideoId());
        TCoursePackage coursePackage = tcpmapper.selectById(detailsRequest.getCoursePackageId());
        if (null != videosWithIds && null != coursePackage){
            response.setCoursePackageId(coursePackage.getId());
            response.setVideoId(videosWithIds.getId());
            response.setVideoURL(videosWithIds.getVideos());
            response.setVideoName(videosWithIds.getName());
            response.setIntegral(videosWithIds.getIntegral());
            UserVideoDetails userVideoDetails = uvdmapper.selectOne(new QueryWrapper<UserVideoDetails>()
                    .eq("appUserId",appUserId )
                    .eq("coursePackageId",coursePackage.getId())
                    .eq("courseId",videosWithIds.getId()));
 
            response.setStudyStatus(userVideoDetails.getState());
            response.setPackageName(coursePackage.getName());
            response.setSynopsis(videosWithIds.getIntroduction());
            response.setDetailedDiagram(coursePackage.getIntroduceDrawing());
        }
 
        return response;
    }
 
    @Override
    public String updateVideoStatus(UpdateCourseVideoStatusRequest detailsRequest, Integer appUserId) {
        UserVideoDetails userVideoDetails = uvdmapper.selectOne(new QueryWrapper<UserVideoDetails>()
                .eq("appUserId",appUserId )
                .eq("coursePackageId",detailsRequest.getCoursePackageId())
                .eq("courseId",detailsRequest.getVideoId())
                );
        if (null != userVideoDetails && userVideoDetails.getState() ==1 && detailsRequest.getIsOver() == 1){
            userVideoDetails.setState(2);
            userVideoDetails.setUpdateTime(new Date());
            uvdmapper.updateById(userVideoDetails);
            return "SUCCESS";
        }
        return null;
    }
 
    @Override
    public List<RegisterCourseVo> queryRegisteredCourseList(CourseOfAfterRequest courseTypeId, Integer appUserId) {
        List<RegisterCourseVo> courseVos = new ArrayList<>();
        List<TCoursePackagePayment> tCoursePackagePayments = this.list(new QueryWrapper<TCoursePackagePayment>()
                .eq("appUserId",appUserId ));
        if (tCoursePackagePayments.size() > 0){
            for (TCoursePackagePayment tCoursePackagePayment : tCoursePackagePayments) {
                TCoursePackage coursePackage = tcpmapper.selectById(tCoursePackagePayment.getCoursePackageId());
                Store store = stoClient.queryStoreById(coursePackage.getStoreId());
                RegisterCourseVo registerCourseVo = new RegisterCourseVo();
                registerCourseVo.setCoursePayId(tCoursePackagePayment.getId());
                registerCourseVo.setCoursePackageId(tCoursePackagePayment.getCoursePackageId());
                registerCourseVo.setPackageImg(coursePackage.getCoverDrawing());
                String storeAndCourse = coursePackage.getName()+"("+ store.getName() +")";
                registerCourseVo.setCourseNameStore(storeAndCourse);
                registerCourseVo.setCourseTime(coursePackage.getClassStartTime()+"-"+coursePackage.getClassEndTime());
                Coach coach = coachClient.queryCoachById(coursePackage.getCoachId());
                registerCourseVo.setCourseTeacher(coach.getName());
                List<CoursePackageStudent> coursePackageStudents = cpsMapper.selectList(new QueryWrapper<CoursePackageStudent>()
                        .eq("coursePackageId",coursePackage.getId())
                        .eq("studentId",appUserId ));
                registerCourseVo.setCourseNums(coursePackageStudents.size() * 2);
                registerCourseVo.setPayStatus(tCoursePackagePayment.getPayStatus());
                courseVos.add(registerCourseVo);
            }
        }
        return courseVos;
    }
 
    @Override
    public CourseDetailsResponse queryRegisteredCourseDetails(Integer coursePayId, Integer appUserId) {
        CourseDetailsResponse response = new CourseDetailsResponse();
        TCoursePackagePayment tCoursePackagePayment = this.baseMapper.selectById(coursePayId);
 
        if (null != tCoursePackagePayment){
            TCoursePackage coursePackage = tcpmapper.selectById(tCoursePackagePayment.getCoursePackageId());
            response.setCoursePackageId(tCoursePackagePayment.getCoursePackageId());
            response.setCoverDrawing(coursePackage.getCoverDrawing());
            response.setCoursePackageName(coursePackage.getName());
            response.setCoursePayId(tCoursePackagePayment.getId());
            List<Integer> integers = StrUtils.dealStrToList(coursePackage.getClassWeeks());
            if (integers.size() > 0){
                StringBuilder courWeeks = new StringBuilder("每");
                for (Integer integer : integers) {
                    switch (integer){
                        case 1:
                            courWeeks.append("周一、");
                            break;
                        case 2:
                            courWeeks.append("周二、");
                            break;
                        case 3:
                            courWeeks.append("周三、");
                            break;
                        case 4:
                            courWeeks.append("周四、");
                            break;
                        case 5:
                            courWeeks.append("周五、");
                            break;
                        case 6:
                            courWeeks.append("周六、");
                            break;
                        case 7:
                            courWeeks.append("周末、");
                            break;
                        default:
                            break;
                    }
                }
                if (courWeeks.length() > 0 && courWeeks.charAt(courWeeks.length() - 1) == ','){
                    courWeeks.deleteCharAt(courWeeks.length() - 1);
                }
                response.setWeeks(courWeeks.toString());
            }
            response.setCourseTimeFrame(coursePackage.getClassStartTime()+"-"+coursePackage.getClassEndTime());
            response.setIntroduceDrawing(coursePackage.getIntroduceDrawing());
 
            Integer payType = tCoursePackagePayment.getPayType();
            BigDecimal cashPayment = tCoursePackagePayment.getCashPayment();
            double cashPaymentValue = cashPayment.doubleValue();
            Integer playPaiCoin = tCoursePackagePayment.getPlayPaiCoin();
            TCoursePackageDiscount coursePackageDiscount = tcpdMapper.selectOne(new QueryWrapper<TCoursePackageDiscount>()
                    .eq("coursePackageId",coursePackage.getId() )
                    .eq("type",1)
                    .eq("auditStatus",2));
            ObjectMapper objectMapper = new ObjectMapper();
            String content = coursePackageDiscount.getContent();
            double discountMember = 0.0;
            DiscountJsonDto discountJsonDto = null;
            try {
                discountJsonDto = objectMapper.readValue(content, DiscountJsonDto.class);
                discountMember = discountJsonDto.getDiscountMember();
            } catch (JsonProcessingException e) {
                throw new RuntimeException(e);
            }
            switch (payType) {
                case 1:
                case 2:
                    response.setAmount(cashPaymentValue);
                    response.setVipAmount(discountMember);
                    break;
                case 3:
                    response.setWpGold(playPaiCoin);
                    break;
            }
            response.setPayStatus(tCoursePackagePayment.getPayStatus());
        }
        return response;
    }
 
    @Override
    public ResultUtil ContinuationOrpaymentCourse(Integer userIdFormRedis, ClasspaymentRequest request) {
        TCoursePackagePayment tCoursePackagePayment = this.baseMapper.selectById(request.getCoursePayId());
        String code = "";
        if (tCoursePackagePayment.getPayStatus() == 1){
            code = tCoursePackagePayment.getCode();
        }else {
            TCoursePackagePayment newPayment = new TCoursePackagePayment();
            SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmssSSS");
            newPayment.setCode(sdf.format(new Date()) + UUIDUtil.getNumberRandom(5));
            newPayment.setAppUserId(userIdFormRedis);
            newPayment.setStudentId(request.getStuId());
            newPayment.setCoursePackageId(request.getLessonId());
            newPayment.setClassHours(tCoursePackagePayment.getClassHours());
            newPayment.setOriginalPrice(tCoursePackagePayment.getOriginalPrice());
            newPayment.setTotalClassHours(tCoursePackagePayment.getTotalClassHours());
            newPayment.setLaveClassHours(tCoursePackagePayment.getTotalClassHours());
            newPayment.setAbsencesNumber(0);
            newPayment.setPayUserType(1);
            newPayment.setPayStatus(1);
            newPayment.setStatus(1);
            newPayment.setState(1);
            newPayment.setInsertTime(new Date());
            this.baseMapper.insert(newPayment);
            code = newPayment.getCode();
        }
        switch (request.getPayType()){
            case 1:
                WeChatPayment(code,request);
                break;
            case 2:
                AlipayPayment(code,request);
                break;
            case 3:
                PlaypaiGoldPayment(code,request);
                break;
            default:
                break;
        }
        return ResultUtil.success();
    }
 
 
    public void WeChatPayment(String code, ClasspaymentRequest request){
 
    }
 
 
    public ResultUtil AlipayPayment(String code, ClasspaymentRequest request){
        BigDecimal amount = BigDecimal.ZERO;
        if (request.getUseConpon() == 1){
            Coupon coupon = client.queryCouponById(request.getConponId());
            // TODO: 2023/7/11 课包购买的微信支付
        }
 
        ResultUtil alipay = payMoneyUtil.alipay("课包购买", "", "", code, amount.toString(),
                "/base/coursePackage/alipayNewPaymentCallback");
//        if(alipay.getCode() == 200){
//            new Thread(new Runnable() {
//                @Override
//                public void run() {
//                    try {
//                        int num = 1;
//                        int wait = 0;
//                        while (num <= 10){
//                            int min = 5000;
//                            wait += (min * num);
//                            Thread.sleep(wait);
//                            TCoursePackagePayment coursePackagePayment = couPayClient.getCoursePackagePaymentByCode(code);
//                            if(coursePackagePayment.getPayStatus() == 2){
//                                break;
//                            }
//                            ResultUtil<Map<String, String>> resultUtil = payMoneyUtil.queryALIOrder(code);
//                            if(resultUtil.getCode() == 200 && coursePackagePayment.getPayStatus() == 1){
//                                /**
//                                 * WAIT_BUYER_PAY(交易创建,等待买家付款)、
//                                 * TRADE_CLOSED(未付款交易超时关闭,或支付完成后全额退款)、
//                                 * TRADE_SUCCESS(交易支付成功)、
//                                 * TRADE_FINISHED(交易结束,不可退款)
//                                 */
//                                Map<String, String> data1 = resultUtil.getData();
//                                String s = data1.get("tradeStatus");
//                                String tradeNo = data1.get("tradeNo");
//                                if("TRADE_CLOSED".equals(s) || "TRADE_FINISHED".equals(s) || num == 10){
//                                    coursePackagePayment.setState(3);
//                                    couPayClient.delPaymentCoursePackage(coursePackagePayment.getId());
//                                    break;
//                                }
//                                if("TRADE_SUCCESS".equals(s)){
//                                    coursePackagePayment.setPayStatus(2);
//                                    coursePackagePayment.setOrderNumber(tradeNo);
//                                    couPayClient.updatePaymentCoursePackage(coursePackagePayment);
//                                    break;
//                                }
//                                if("WAIT_BUYER_PAY".equals(s)){
//                                    num++;
//                                }
//                            }
//                        }
//                    }catch (Exception e){
//                        e.printStackTrace();
//                    }
//                }
//            }).start();
//        }
        return alipay;
    }
 
    public void PlaypaiGoldPayment(String code, ClasspaymentRequest request){
 
    }
 
}