lisy
2023-07-20 c678894e37d15cca116d962eba5c107c99176945
cloud-server-course/src/main/java/com/dsh/course/service/impl/TCoursePackageServiceImpl.java
@@ -3,15 +3,21 @@
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;
import com.dsh.course.entity.TCoursePackage;
import com.dsh.course.entity.TCoursePackageDiscount;
import com.dsh.course.entity.TCoursePackagePayment;
import com.dsh.course.feignclient.account.AppUserClient;
import com.dsh.course.feignclient.account.StudentClient;
import com.dsh.course.feignclient.account.model.AppUser;
import com.dsh.course.feignclient.account.model.Student;
import com.dsh.course.feignclient.activity.CouponClient;
import com.dsh.course.feignclient.activity.UserCouponClient;
import com.dsh.course.feignclient.activity.model.Coupon;
import com.dsh.course.feignclient.activity.model.UserCoupon;
import com.dsh.course.feignclient.other.StoreClient;
import com.dsh.course.feignclient.other.model.Store;
import com.dsh.course.mapper.TCoursePackageMapper;
@@ -21,9 +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.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;
@@ -65,6 +70,15 @@
    @Resource
    private StudentClient studentClient;
    @Resource
    private UserCouponClient userCouponClient;
    @Resource
    private CouponClient couponClient;
    @Autowired
    private PayMoneyUtil payMoneyUtil;
@@ -112,7 +126,7 @@
            Store store = storeClient.queryStoreById(coursePackage.getStoreId());
            Integer integer = coursePackagePaymentService.queryCountNumber(coursePackage.getId());
            CoursePackagePaymentConfig coursePackagePaymentConfig = coursePackagePaymentConfigService.getOne(new QueryWrapper<CoursePackagePaymentConfig>()
                    .eq("coursePackageId", coursePackage.getId()).orderByAsc("classHours limit 0, 1"));
                    .eq("coursePackageId", coursePackage.getId()).orderByAsc("classHours").last(" limit 0, 1"));
            CoursePackageListVo coursePackageListVo = new CoursePackageListVo();
            coursePackageListVo.setId(coursePackage.getId());
            coursePackageListVo.setName(coursePackage.getName());
@@ -273,9 +287,11 @@
        coursePackageInfo.setStoreAddress(store.getAddress());
        coursePackageInfo.setLon(store.getLon());
        coursePackageInfo.setLat(store.getLat());
        Map<String, Double> distance = GeodesyUtil.getDistance(lon + "," + lat, store.getLon() + "," + store.getLat());
        double wgs84 = new BigDecimal(distance.get("WGS84")).divide(new BigDecimal(1000)).setScale(2, RoundingMode.HALF_EVEN).doubleValue();
        coursePackageInfo.setDistance(wgs84);
        if(ToolUtil.isNotEmpty(lon) && ToolUtil.isNotEmpty(lat)){
            Map<String, Double> distance = GeodesyUtil.getDistance(lon + "," + lat, store.getLon() + "," + store.getLat());
            double wgs84 = new BigDecimal(distance.get("WGS84")).divide(new BigDecimal(1000)).setScale(2, RoundingMode.HALF_EVEN).doubleValue();
            coursePackageInfo.setDistance(wgs84);
        }
        String[] split = coursePackage.getClassWeeks().split(";");
        coursePackageInfo.setWeeks(Arrays.asList(split));
@@ -290,7 +306,7 @@
            coursePackagePaymentConfigVo.setClassHours(coursePackagePaymentConfig.getClassHours());
            coursePackagePaymentConfigVo.setPayType(coursePackage.getPayType());
            coursePackagePaymentConfigVo.setPlayPaiCoin(coursePackagePaymentConfig.getPlayPaiCoin());
            //会员显示原价和会员价(最低)。非会员显示会员价和支付价(最低)
            //会员显示原价和支付价(会员价)。非会员显示会员价和支付价(最低)
            if(appUser.getIsVip() == 0){//非会员
                List<TCoursePackageDiscount> list2 = coursePackageDiscountService.list(new QueryWrapper<TCoursePackageDiscount>().eq("coursePackagePaymentConfigId", coursePackagePaymentConfig.getId())
                        .eq("type", 1).eq("auditStatus", 2));
@@ -399,11 +415,333 @@
        Student student = studentClient.queryDefaultStudent(uid);
        StudentVo studentVo = new StudentVo();
        studentVo.setId(student.getId());
        studentVo.setPhone(student.getPhone());
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy");
        studentVo.setAge(Integer.valueOf(sdf.format(new Date())) - Integer.valueOf(sdf.format(student.getBirthday())));
        if(null != student){
            studentVo.setId(student.getId());
            studentVo.setPhone(student.getPhone());
            studentVo.setName(student.getName());
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy");
            studentVo.setAge(Integer.valueOf(sdf.format(new Date())) - Integer.valueOf(sdf.format(student.getBirthday())));
        }
        coursePackageInfo.setStudent(studentVo);
        return coursePackageInfo;
    }
    /**
     * 课程包名支付
     * @param uid
     * @param paymentCourseVo
     * @return
     * @throws Exception
     */
    @Override
    public ResultUtil paymentCourse(Integer uid, PaymentCourseVo paymentCourseVo) throws Exception {
        AppUser appUser = appUserClient.queryAppUser(uid);
        CoursePackageInfo coursePackageInfo = queryCourseInfo(uid, paymentCourseVo.getId(), null, null);
        List<CoursePackagePaymentConfigVo> list = coursePackageInfo.getList();
        String[] students = paymentCourseVo.getStudentIds().split(";");
        for (CoursePackagePaymentConfigVo coursePackagePaymentConfigVo : list) {
            if(paymentCourseVo.getCoursePackagePaymentConfigId().compareTo(coursePackagePaymentConfigVo.getId()) == 0){
                Integer payType = coursePackagePaymentConfigVo.getPayType();
                if(payType == 1 && paymentCourseVo.getPayType() == 3){//现金支付
                    return ResultUtil.error("支付方式错误,请刷新后重试");
                }
                if(payType == 2 && paymentCourseVo.getPayType() != 3){//玩湃币支付
                    return ResultUtil.error("支付方式错误,请刷新后重试");
                }
                Double paymentPrice = coursePackagePaymentConfigVo.getPaymentPrice();//支付价
                paymentPrice = new BigDecimal(students.length).multiply(new BigDecimal(paymentPrice)).setScale(2, RoundingMode.HALF_EVEN).doubleValue();
                Double playPaiCoin = coursePackagePaymentConfigVo.getPlayPaiCoin().doubleValue();//支付币
                playPaiCoin = new BigDecimal(students.length).multiply(new BigDecimal(playPaiCoin)).setScale(2, RoundingMode.HALF_EVEN).doubleValue();
                if(payType == 1 && paymentPrice.compareTo(paymentCourseVo.getPrice()) != 0){
                    return ResultUtil.error("支付金额异常,请刷新后重试");
                }
                if(payType == 2 && playPaiCoin.compareTo(paymentCourseVo.getPrice()) != 0){
                    return ResultUtil.error("支付金额异常,请刷新后重试");
                }
            }
        }
        Double price = paymentCourseVo.getPrice();//支付金额
        //校验优惠券
        Long couponId = paymentCourseVo.getCouponId();
        if(null != couponId && paymentCourseVo.getPayType() != 3){
            UserCoupon userCoupon = userCouponClient.queryUserCouponById(couponId);
            if(userCoupon.getStatus() == 2){
                return ResultUtil.error("优惠券已被核销");
            }
            Coupon coupon = couponClient.queryCouponById(userCoupon.getCouponId());
            long time = coupon.getEndTime().getTime();
            if(System.currentTimeMillis() >= time){
                return ResultUtil.error("优惠券已过期");
            }
            if(coupon.getType() == 1){//满减
                JSONObject jsonObject = JSON.parseObject(coupon.getContent());
                Double num1 = jsonObject.getDouble("num1");
                Double num2 = jsonObject.getDouble("num2");
                if(price.compareTo(num1) <= 0){
                    return ResultUtil.error("该优惠券无法使用");
                }
                price = new BigDecimal(price).subtract(new BigDecimal(num2)).setScale(2, RoundingMode.HALF_EVEN).doubleValue();
            }
            if(coupon.getType() == 2){//代金券
                JSONObject jsonObject = JSON.parseObject(coupon.getContent());
                Double num1 = jsonObject.getDouble("num1");
                if(price.compareTo(num1) <= 0){
                    return ResultUtil.error("该优惠券无法使用");
                }
                price = new BigDecimal(price).subtract(new BigDecimal(num1)).setScale(2, RoundingMode.HALF_EVEN).doubleValue();
            }
        }
        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 playPaiCoinPaymentCourse(appUser, price, paymentCourseVo);
        }
        return ResultUtil.success();
    }
    /**
     * 课程微信支付
     * @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.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();
    }
}