puzhibing
2024-02-22 cbca62b053eb3dc3cc7754aa0062aaf1e04e2cb4
cloud-server-communityWorldCup/src/main/java/com/dsh/communityWorldCup/service/impl/WorldCupServiceImpl.java
@@ -5,26 +5,32 @@
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dsh.communityWorldCup.entity.WorldCup;
import com.dsh.communityWorldCup.entity.WorldCupCompetitor;
import com.dsh.communityWorldCup.entity.WorldCupPaymentParticipant;
import com.dsh.communityWorldCup.entity.WorldCupStore;
import com.dsh.communityWorldCup.entity.*;
import com.dsh.communityWorldCup.feignclient.account.AppUserClient;
import com.dsh.communityWorldCup.feignclient.account.StudentClient;
import com.dsh.communityWorldCup.feignclient.account.model.AppUser;
import com.dsh.communityWorldCup.feignclient.account.model.TStudent;
import com.dsh.communityWorldCup.feignclient.competition.ParticipantClient;
import com.dsh.communityWorldCup.feignclient.competition.model.Participant;
import com.dsh.communityWorldCup.feignclient.course.CoursePackageOrderStudentClient;
import com.dsh.communityWorldCup.feignclient.course.model.CoursePackageOrderStudent;
import com.dsh.communityWorldCup.feignclient.other.StoreClient;
import com.dsh.communityWorldCup.feignclient.other.model.Store;
import com.dsh.communityWorldCup.mapper.WorldCupMapper;
import com.dsh.communityWorldCup.model.*;
import com.dsh.communityWorldCup.service.IWorldCupCompetitorService;
import com.dsh.communityWorldCup.service.IWorldCupPaymentParticipantService;
import com.dsh.communityWorldCup.service.IWorldCupService;
import com.dsh.communityWorldCup.service.IWorldCupStoreService;
import com.dsh.communityWorldCup.service.*;
import com.dsh.communityWorldCup.util.GeodesyUtil;
import com.dsh.communityWorldCup.util.PayMoneyUtil;
import com.dsh.communityWorldCup.util.ResultUtil;
import com.dsh.communityWorldCup.util.UUIDUtil;
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.stream.Collectors;
/**
 * @author zhibing.pu
@@ -44,6 +50,24 @@
    @Autowired
    private IWorldCupStoreService worldCupStoreService;
    @Resource
    private StudentClient studentClient;
    @Resource
    private ParticipantClient participantClient;
    @Autowired
    private IWorldCupPaymentService worldCupPaymentService;
    @Autowired
    private PayMoneyUtil payMoneyUtil;
    @Resource
    private AppUserClient appUserClient;
    @Resource
    private CoursePackageOrderStudentClient coursePackageOrderStudentClient;
@@ -160,4 +184,292 @@
        worldCupInfo.setStores(jsonArray.toJSONString());
        return worldCupInfo;
    }
    /**
     * 报名前校验支付方式
     * @param checkPaymentMethod
     * @return
     */
    @Override
    public CheckPaymentMethodVo checkPaymentMethod(CheckPaymentMethod checkPaymentMethod) {
        WorldCup worldCup = this.getById(checkPaymentMethod.getId());
        JSONArray jsonArray = JSON.parseArray(checkPaymentMethod.getIds());
        CheckPaymentMethodVo checkPaymentMethodVo = new CheckPaymentMethodVo();
        AppUser appUser = appUserClient.getAppUser(checkPaymentMethod.getUid());
        BigDecimal multiply = new BigDecimal(jsonArray.size()).multiply(new BigDecimal(worldCup.getPaiCoin()));
        if(appUser.getPlayPaiCoins() < multiply.intValue()){
            checkPaymentMethodVo.setPaiCoin(0);
        }else{
            checkPaymentMethodVo.setPaiCoin(1);
        }
        int classHour = 1;
        for (int i = 0; i < jsonArray.size(); i++) {
            JSONObject jsonObject = jsonArray.getJSONObject(i);
            Integer id = jsonObject.getInteger("id");
            //是否是学员(0=否,1=是)
            Integer isStudent = jsonObject.getInteger("isStudent");
            if(0 == isStudent){
                classHour = 0;
                break;
            }
            List<CoursePackageOrderStudent> coursePackageOrderStudent = coursePackageOrderStudentClient.getCoursePackageOrderStudent(id);
            int sum = coursePackageOrderStudent.stream().mapToInt(CoursePackageOrderStudent::getLaveClassHours).sum();
            if(sum < worldCup.getClassHour()){
                classHour = 0;
                break;
            }
        }
        checkPaymentMethodVo.setClassHour(classHour);
        return checkPaymentMethodVo;
    }
    /**
     * 报名世界杯
     * @param paymentWorldCup
     * @return
     */
    @Override
    public ResultUtil paymentWorldCup(PaymentWorldCup paymentWorldCup) {
        WorldCup worldCup = this.getById(paymentWorldCup.getId());
        if(null == worldCup){
            return ResultUtil.error("报名数据异常");
        }
        if(System.currentTimeMillis() > worldCup.getRegistrationClosingTime().getTime()){
            return ResultUtil.error("报名时间已结束,无法完成报名");
        }
        if(System.currentTimeMillis() > worldCup.getEndTime().getTime()){
            return ResultUtil.error("世界杯赛事已结束,无法完成报名");
        }
        String ids = paymentWorldCup.getIds();
        JSONArray jsonArray = JSON.parseArray(ids);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy");
        for (int i = 0; i < jsonArray.size(); i++) {
            JSONObject jsonObject = jsonArray.getJSONObject(i);
            Integer id = jsonObject.getInteger("id");
            //是否是学员(0=否,1=是)
            Integer isStudent = jsonObject.getInteger("isStudent");
            if(isStudent == 1){
                TStudent tStudent = studentClient.queryById(id);
                if(worldCup.getStartAge() > Integer.valueOf(sdf.format(tStudent.getBirthday()))){
                    return ResultUtil.error("【" + tStudent.getName() + "】年龄不符合要求");
                }
                if(worldCup.getEndAge() < Integer.valueOf(sdf.format(tStudent.getBirthday()))){
                    return ResultUtil.error("【" + tStudent.getName() + "】年龄不符合要求");
                }
            }else{
                Participant participant = participantClient.getParticipant(id);
                if(worldCup.getStartAge() > Integer.valueOf(sdf.format(participant.getBirthday()))){
                    return ResultUtil.error("【" + participant.getName() + "】年龄不符合要求");
                }
                if(worldCup.getEndAge() < Integer.valueOf(sdf.format(participant.getBirthday()))){
                    return ResultUtil.error("【" + participant.getName() + "】年龄不符合要求");
                }
            }
        }
        //开始支付操作
        ResultUtil payment = null;
        try {
            payment = payment(worldCup, paymentWorldCup);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return payment;
    }
    /**
     * 支付逻辑
     * @param worldCup
     * @param paymentWorldCup
     * @return
     * @throws Exception
     */
    private ResultUtil payment(WorldCup worldCup, PaymentWorldCup paymentWorldCup) throws Exception {
        WorldCupPayment worldCupPayment = new WorldCupPayment();
        worldCupPayment.setCode(UUIDUtil.getTimeStr() + UUIDUtil.getNumberRandom(5));
        worldCupPayment.setWorldCupId(paymentWorldCup.getId());
        worldCupPayment.setAppUserId(paymentWorldCup.getUid());
        worldCupPayment.setPayType(paymentWorldCup.getPayType());
        worldCupPayment.setPayStatus(1);
        worldCupPayment.setState(1);
        worldCupPayment.setCreateTime(new Date());
        worldCupPayment.setEntrant(paymentWorldCup.getIds());
        int num = JSON.parseArray(paymentWorldCup.getIds()).size();
        //不免费
        if(worldCup.getPayType() != 0){
            Integer payType = paymentWorldCup.getPayType();
            //微信
            if(payType == 1){
                worldCupPayment.setAmount(new BigDecimal(num).multiply(worldCup.getCash()));
                worldCupPaymentService.save(worldCupPayment);
                return payMoneyUtil.weixinpay("社区世界杯报名", "", worldCupPayment.getCode(),
                        worldCup.getCash().toString(), "/base/worldCup/wxPayWorldCupCallback", "APP", "");
            }
            //支付宝
            if(payType == 2){
                worldCupPayment.setAmount(new BigDecimal(num).multiply(worldCup.getCash()));
                worldCupPaymentService.save(worldCupPayment);
                return payMoneyUtil.alipay("2088330203191220", "社区世界杯报名", "世界杯报名", "", worldCupPayment.getCode(),
                        worldCup.getCash().toString(), "/base/worldCup/aliPayWorldCupCallback");
            }
            //玩湃币
            if(payType == 3){
                AppUser appUser = appUserClient.getAppUser(paymentWorldCup.getUid());
                BigDecimal multiply = new BigDecimal(num).multiply(new BigDecimal(worldCup.getPaiCoin()));
                if(appUser.getPlayPaiCoins() < multiply.intValue()){
                    return ResultUtil.error("报名失败,玩湃币不足,请充值!");
                }
                worldCupPayment.setAmount(multiply);
                worldCupPayment.setPayStatus(2);
                worldCupPayment.setPayTime(new Date());
                worldCupPaymentService.save(worldCupPayment);
                appUser.setPlayPaiCoins(appUser.getPlayPaiCoins() - multiply.intValue());
                appUserClient.updateAppUser(appUser);
                //添加报名明细
                String ids = paymentWorldCup.getIds();
                JSONArray jsonArray = JSON.parseArray(ids);
                for (int i = 0; i < jsonArray.size(); i++) {
                    JSONObject jsonObject = jsonArray.getJSONObject(i);
                    Integer id = jsonObject.getInteger("id");
                    //是否是学员(0=否,1=是)
                    Integer isStudent = jsonObject.getInteger("isStudent");
                    WorldCupPaymentParticipant worldCupPaymentParticipant = new WorldCupPaymentParticipant();
                    worldCupPaymentParticipant.setWorldCupId(paymentWorldCup.getId());
                    worldCupPaymentParticipant.setWorldCupPaymentId(worldCupPayment.getId());
                    worldCupPaymentParticipant.setAppUserId(paymentWorldCup.getUid());
                    worldCupPaymentParticipant.setParticipantType(isStudent == 0 ? 2 : 1);
                    worldCupPaymentParticipant.setParticipantId(id);
                    worldCupPaymentParticipantService.save(worldCupPaymentParticipant);
                }
                return ResultUtil.success();
            }
            //课时
            if(payType == 4){
                BigDecimal multiply = new BigDecimal(num).multiply(new BigDecimal(worldCup.getClassHour()));
                String ids = paymentWorldCup.getIds();
                JSONArray jsonArray = JSON.parseArray(ids);
                List<DeductionClassHour> list = new ArrayList<>();
                for (int i = 0; i < jsonArray.size(); i++) {
                    JSONObject jsonObject = jsonArray.getJSONObject(i);
                    Integer id = jsonObject.getInteger("id");
                    //是否是学员(0=否,1=是)
                    Integer isStudent = jsonObject.getInteger("isStudent");
                    if(0 == isStudent){
                        return ResultUtil.error("非学员无法使用课时报名");
                    }
                    List<CoursePackageOrderStudent> coursePackageOrderStudent = coursePackageOrderStudentClient.getCoursePackageOrderStudent(id);
                    int sum = coursePackageOrderStudent.stream().mapToInt(CoursePackageOrderStudent::getLaveClassHours).sum();
                    if(sum < worldCup.getClassHour()){
                        return ResultUtil.error("报名失败,课时不足,请续课!");
                    }
                    //构建扣减课时数据
                    Integer classHour = worldCup.getClassHour();
                    for (CoursePackageOrderStudent packageOrderStudent : coursePackageOrderStudent) {
                        Integer laveClassHours = packageOrderStudent.getLaveClassHours();
                        DeductionClassHour deductionClassHour = new DeductionClassHour();
                        if(classHour.compareTo(laveClassHours) > 0){
                            //不够扣除,轮询直到扣除完
                            classHour = classHour - laveClassHours;
                            deductionClassHour.setId(packageOrderStudent.getId());
                            deductionClassHour.setClassHour(laveClassHours);
                            deductionClassHour.setStudentId(packageOrderStudent.getStudentId());
                            deductionClassHour.setCoursePackageId(packageOrderStudent.getCoursePackageId());
                            list.add(deductionClassHour);
                        }else{
                            //够扣除直接跳出进行下个学员
                            deductionClassHour.setId(packageOrderStudent.getId());
                            deductionClassHour.setClassHour(classHour);
                            deductionClassHour.setStudentId(packageOrderStudent.getStudentId());
                            deductionClassHour.setCoursePackageId(packageOrderStudent.getCoursePackageId());
                            list.add(deductionClassHour);
                            break;
                        }
                    }
                }
                //扣减课时操作
                DeductionClassHourList deductionClassHourList = new DeductionClassHourList();
                deductionClassHourList.setDeductionClassHourList(list);
                coursePackageOrderStudentClient.deductionClassHour(deductionClassHourList);
                worldCupPayment.setAmount(multiply);
                worldCupPayment.setPayStatus(2);
                worldCupPayment.setPayTime(new Date());
                worldCupPaymentService.save(worldCupPayment);
                return ResultUtil.success();
            }
        }else{
            worldCupPayment.setPayType(0);
            worldCupPayment.setAmount(BigDecimal.ZERO);
            worldCupPayment.setPayStatus(2);
            worldCupPayment.setPayTime(new Date());
            worldCupPaymentService.save(worldCupPayment);
            //添加报名明细
            String ids = paymentWorldCup.getIds();
            JSONArray jsonArray = JSON.parseArray(ids);
            for (int i = 0; i < jsonArray.size(); i++) {
                JSONObject jsonObject = jsonArray.getJSONObject(i);
                Integer id = jsonObject.getInteger("id");
                //是否是学员(0=否,1=是)
                Integer isStudent = jsonObject.getInteger("isStudent");
                WorldCupPaymentParticipant worldCupPaymentParticipant = new WorldCupPaymentParticipant();
                worldCupPaymentParticipant.setWorldCupId(paymentWorldCup.getId());
                worldCupPaymentParticipant.setWorldCupPaymentId(worldCupPayment.getId());
                worldCupPaymentParticipant.setAppUserId(paymentWorldCup.getUid());
                worldCupPaymentParticipant.setParticipantType(isStudent == 0 ? 2 : 1);
                worldCupPaymentParticipant.setParticipantId(id);
                worldCupPaymentParticipantService.save(worldCupPaymentParticipant);
            }
            return ResultUtil.success();
        }
        return ResultUtil.success();
    }
    /**
     * 第三方支付完成后的处理逻辑
     * @param code
     * @param outTradeNo
     * @return
     */
    @Override
    public ResultUtil paymentWorldCupCallback(String code, String outTradeNo) {
        WorldCupPayment worldCupPayment = worldCupPaymentService.getOne(new QueryWrapper<WorldCupPayment>().eq("code", code));
        worldCupPayment.setPayStatus(2);
        worldCupPayment.setPayTime(new Date());
        worldCupPaymentService.updateById(worldCupPayment);
        //添加报名明细
        String ids = worldCupPayment.getEntrant();
        JSONArray jsonArray = JSON.parseArray(ids);
        for (int i = 0; i < jsonArray.size(); i++) {
            JSONObject jsonObject = jsonArray.getJSONObject(i);
            Integer id = jsonObject.getInteger("id");
            //是否是学员(0=否,1=是)
            Integer isStudent = jsonObject.getInteger("isStudent");
            WorldCupPaymentParticipant worldCupPaymentParticipant = new WorldCupPaymentParticipant();
            worldCupPaymentParticipant.setWorldCupId(worldCupPayment.getWorldCupId());
            worldCupPaymentParticipant.setWorldCupPaymentId(worldCupPayment.getId());
            worldCupPaymentParticipant.setAppUserId(worldCupPayment.getAppUserId());
            worldCupPaymentParticipant.setParticipantType(isStudent == 0 ? 2 : 1);
            worldCupPaymentParticipant.setParticipantId(id);
            worldCupPaymentParticipantService.save(worldCupPaymentParticipant);
        }
        return ResultUtil.success();
    }
}