puzhibing
2023-07-06 c0f6294b0be6789fddd652f89f820fcf6d5526cf
cloud-server-course/src/main/java/com/dsh/course/service/impl/TCoursePackageServiceImpl.java
@@ -3,6 +3,7 @@
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.nacos.common.utils.UuidUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dsh.course.entity.CoursePackagePaymentConfig;
@@ -26,10 +27,8 @@
import com.dsh.course.service.TCoursePackageDiscountService;
import com.dsh.course.service.TCoursePackagePaymentService;
import com.dsh.course.service.TCoursePackageService;
import com.dsh.course.util.GDMapGeocodingUtil;
import com.dsh.course.util.GeodesyUtil;
import com.dsh.course.util.ResultUtil;
import com.dsh.course.util.ToolUtil;
import com.dsh.course.util.*;
import com.dsh.course.util.httpClinet.HttpResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@@ -77,6 +76,9 @@
    @Resource
    private CouponClient couponClient;
    @Autowired
    private PayMoneyUtil payMoneyUtil;
@@ -493,21 +495,250 @@
        }
        if(paymentCourseVo.getPayType() == 1){//微信支付
            return weChatPaymentCourse(uid, price, paymentCourseVo);
        }
        if(paymentCourseVo.getPayType() == 2){//支付宝支付
            return aliPaymentCourse(uid, price, paymentCourseVo);
        }
        if(paymentCourseVo.getPayType() == 3){//玩湃币支付
            if(appUser.getPlayPaiCoins() < price.intValue()){
                return ResultUtil.error("报名失败,玩湃币不足,请充值!");
        }
        return null;
            return playPaiCoinPaymentCourse(appUser, price, paymentCourseVo);
        }
        return ResultUtil.success();
    }
    public ResultUtil weChatPaymentCourse(Integer uid, PaymentCourseVo paymentCourseVo){
    /**
     * 课程微信支付
     * @param uid
     * @param paymentPrice
     * @param paymentCourseVo
     * @return
     * @throws Exception
     */
    public ResultUtil weChatPaymentCourse(Integer uid, Double paymentPrice, PaymentCourseVo paymentCourseVo) throws Exception{
        CoursePackagePaymentConfig coursePackagePaymentConfig = coursePackagePaymentConfigService.getById(paymentCourseVo.getCoursePackagePaymentConfigId());
        String[] split = paymentCourseVo.getStudentIds().split(";");
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmssSSS");
        String code = sdf.format(new Date()) + UUIDUtil.getNumberRandom(5);
        for (String s : split) {
        TCoursePackagePayment coursePackagePayment = new TCoursePackagePayment();
//        coursePackagePayment
            coursePackagePayment.setCode(code);
            coursePackagePayment.setAppUserId(uid);
            coursePackagePayment.setStudentId(Integer.valueOf(s));
            coursePackagePayment.setCoursePackageId(paymentCourseVo.getId());
            coursePackagePayment.setPayType(paymentCourseVo.getPayType());
            coursePackagePayment.setClassHours(coursePackagePaymentConfig.getClassHours());
            coursePackagePayment.setOriginalPrice(paymentCourseVo.getPrice());
            coursePackagePayment.setUserCouponId(paymentCourseVo.getCouponId());
            coursePackagePayment.setCashPayment(new BigDecimal(paymentPrice));
            coursePackagePayment.setTotalClassHours(coursePackagePaymentConfig.getClassHours());
            coursePackagePayment.setLaveClassHours(coursePackagePaymentConfig.getClassHours());
            coursePackagePayment.setAbsencesNumber(0);
            coursePackagePayment.setPayUserType(1);
            coursePackagePayment.setPayStatus(1);
            coursePackagePayment.setPayUserId(uid);
            coursePackagePayment.setStatus(1);
            coursePackagePayment.setState(1);
            coursePackagePayment.setInsertTime(new Date());
            coursePackagePaymentService.save(coursePackagePayment);
        }
        ResultUtil weixinpay = payMoneyUtil.weixinpay("购买课程", "", code, paymentPrice.toString(), "/base/course/weChatPaymentCourseCallback", "APP");
        if(weixinpay.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);
                            List<TCoursePackagePayment> list = coursePackagePaymentService.list(new QueryWrapper<TCoursePackagePayment>().eq("state", 1).eq("code", code).eq("payType", 1));
                            TCoursePackagePayment one = list.get(0);
                            if(one.getPayStatus() == 2){
                                break;
                            }
                            ResultUtil<Map<String, String>> resultUtil = payMoneyUtil.queryWXOrder(code, "");
                            if(resultUtil.getCode() == 200 && one.getPayStatus() == 1){
                                /**
                                 * SUCCESS—支付成功,
                                 * REFUND—转入退款,
                                 * NOTPAY—未支付,
                                 * CLOSED—已关闭,
                                 * REVOKED—已撤销(刷卡支付),
                                 * USERPAYING--用户支付中,
                                 * PAYERROR--支付失败(其他原因,如银行返回失败)
                                 */
                                Map<String, String> data1 = resultUtil.getData();
                                String s = data1.get("trade_state");
                                String transaction_id = data1.get("transaction_id");
                                if("REFUND".equals(s) || "NOTPAY".equals(s) || "CLOSED".equals(s) || "REVOKED".equals(s) || "PAYERROR".equals(s) || num == 10){
                                    for (TCoursePackagePayment coursePackagePayment : list) {
                                        coursePackagePayment.setStatus(3);
                                    }
                                    coursePackagePaymentService.updateBatchById(list);
                                    break;
                                }
                                if("SUCCESS".equals(s)){
                                    for (TCoursePackagePayment coursePackagePayment : list) {
                                        coursePackagePayment.setPayStatus(2);
                                        coursePackagePayment.setOrderNumber(transaction_id);
                                    }
                                    coursePackagePaymentService.updateBatchById(list);
                                    break;
                                }
                                if("USERPAYING".equals(s)){
                                    num++;
                                }
                            }
                        }
                    }catch (Exception e){
                        e.printStackTrace();
                    }
                }
            }).start();
        }
        return weixinpay;
    }
    /**
     * 课程支付宝支付
     * @param uid
     * @param paymentPrice
     * @param paymentCourseVo
     * @return
     * @throws Exception
     */
    public ResultUtil aliPaymentCourse(Integer uid, Double paymentPrice, PaymentCourseVo paymentCourseVo) throws Exception{
        CoursePackagePaymentConfig coursePackagePaymentConfig = coursePackagePaymentConfigService.getById(paymentCourseVo.getCoursePackagePaymentConfigId());
        String[] split = paymentCourseVo.getStudentIds().split(";");
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmssSSS");
        String code = sdf.format(new Date()) + UUIDUtil.getNumberRandom(5);
        for (String s : split) {
            TCoursePackagePayment coursePackagePayment = new TCoursePackagePayment();
            coursePackagePayment.setCode(code);
            coursePackagePayment.setAppUserId(uid);
            coursePackagePayment.setStudentId(Integer.valueOf(s));
            coursePackagePayment.setCoursePackageId(paymentCourseVo.getId());
            coursePackagePayment.setPayType(paymentCourseVo.getPayType());
            coursePackagePayment.setClassHours(coursePackagePaymentConfig.getClassHours());
            coursePackagePayment.setOriginalPrice(paymentCourseVo.getPrice());
            coursePackagePayment.setUserCouponId(paymentCourseVo.getCouponId());
            coursePackagePayment.setCashPayment(new BigDecimal(paymentPrice));
            coursePackagePayment.setTotalClassHours(coursePackagePaymentConfig.getClassHours());
            coursePackagePayment.setLaveClassHours(coursePackagePaymentConfig.getClassHours());
            coursePackagePayment.setAbsencesNumber(0);
            coursePackagePayment.setPayUserType(1);
            coursePackagePayment.setPayStatus(1);
            coursePackagePayment.setPayUserId(uid);
            coursePackagePayment.setStatus(1);
            coursePackagePayment.setState(1);
            coursePackagePayment.setInsertTime(new Date());
            coursePackagePaymentService.save(coursePackagePayment);
        }
        ResultUtil alipay = payMoneyUtil.alipay("购买课程", "", code, paymentPrice.toString(), "/base/course/aliPaymentCourseCallback");
        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);
                            List<TCoursePackagePayment> list = coursePackagePaymentService.list(new QueryWrapper<TCoursePackagePayment>().eq("state", 1).eq("code", code).eq("payType", 2));
                            TCoursePackagePayment one = list.get(0);
                            if(one.getPayStatus() == 2){
                                break;
                            }
                            ResultUtil<Map<String, String>> resultUtil = payMoneyUtil.queryALIOrder(code);
                            if(resultUtil.getCode() == 200 && one.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){
                                    for (TCoursePackagePayment coursePackagePayment : list) {
                                        coursePackagePayment.setStatus(3);
                                    }
                                    coursePackagePaymentService.updateBatchById(list);
                                    break;
                                }
                                if("TRADE_SUCCESS".equals(s)){
                                    for (TCoursePackagePayment coursePackagePayment : list) {
                                        coursePackagePayment.setPayStatus(2);
                                        coursePackagePayment.setOrderNumber(tradeNo);
                                    }
                                    coursePackagePaymentService.updateBatchById(list);
                                    break;
                                }
                                if("WAIT_BUYER_PAY".equals(s)){
                                    num++;
                                }
                            }
                        }
                    }catch (Exception e){
                        e.printStackTrace();
                    }
                }
            }).start();
        }
        return alipay;
    }
    /**
     * 玩湃币支付课程
     * @param appUser
     * @param paymentPrice
     * @param paymentCourseVo
     * @return
     * @throws Exception
     */
    public ResultUtil playPaiCoinPaymentCourse(AppUser appUser, Double paymentPrice, PaymentCourseVo paymentCourseVo) throws Exception{
        CoursePackagePaymentConfig coursePackagePaymentConfig = coursePackagePaymentConfigService.getById(paymentCourseVo.getCoursePackagePaymentConfigId());
        String[] split = paymentCourseVo.getStudentIds().split(";");
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmssSSS");
        String code = sdf.format(new Date()) + UUIDUtil.getNumberRandom(5);
        for (String s : split) {
            TCoursePackagePayment coursePackagePayment = new TCoursePackagePayment();
            coursePackagePayment.setCode(code);
            coursePackagePayment.setAppUserId(appUser.getId());
            coursePackagePayment.setStudentId(Integer.valueOf(s));
            coursePackagePayment.setCoursePackageId(paymentCourseVo.getId());
            coursePackagePayment.setPayType(paymentCourseVo.getPayType());
            coursePackagePayment.setClassHours(coursePackagePaymentConfig.getClassHours());
            coursePackagePayment.setOriginalPrice(paymentCourseVo.getPrice());
            coursePackagePayment.setPlayPaiCoin(paymentPrice.intValue());
            coursePackagePayment.setTotalClassHours(coursePackagePaymentConfig.getClassHours());
            coursePackagePayment.setLaveClassHours(coursePackagePaymentConfig.getClassHours());
            coursePackagePayment.setAbsencesNumber(0);
            coursePackagePayment.setPayUserType(1);
            coursePackagePayment.setPayStatus(2);
            coursePackagePayment.setPayUserId(appUser.getId());
            coursePackagePayment.setStatus(1);
            coursePackagePayment.setState(1);
            coursePackagePayment.setInsertTime(new Date());
            coursePackagePaymentService.save(coursePackagePayment);
        }
        Integer playPaiCoins = appUser.getPlayPaiCoins();
        appUser.setPlayPaiCoins(playPaiCoins - paymentPrice.intValue());
        appUserClient.updateAppUser(appUser);
        return ResultUtil.success();
    }
}