puzhibing
2024-03-08 2a3d0885c11a73d41fb03c985f0032086cd8fa07
cloud-server-communityWorldCup/src/main/java/com/dsh/communityWorldCup/service/impl/WorldCupPaymentServiceImpl.java
@@ -1,10 +1,32 @@
package com.dsh.communityWorldCup.service.impl;
import com.alibaba.fastjson.JSON;
import com.alipay.api.AlipayApiException;
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.WorldCupPayment;
import com.dsh.communityWorldCup.entity.WorldCupPaymentParticipant;
import com.dsh.communityWorldCup.feignclient.account.AppUserClient;
import com.dsh.communityWorldCup.feignclient.account.model.AppUser;
import com.dsh.communityWorldCup.feignclient.course.CoursePackageOrderStudentClient;
import com.dsh.communityWorldCup.feignclient.course.model.CoursePackageOrderStudent;
import com.dsh.communityWorldCup.mapper.WorldCupPaymentMapper;
import com.dsh.communityWorldCup.model.DeductionClassHourList;
import com.dsh.communityWorldCup.service.IWorldCupPaymentParticipantService;
import com.dsh.communityWorldCup.service.IWorldCupPaymentService;
import com.dsh.communityWorldCup.service.IWorldCupService;
import com.dsh.communityWorldCup.util.PayMoneyUtil;
import com.dsh.communityWorldCup.util.ResultUtil;
import net.bytebuddy.asm.Advice;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.Map;
/**
 * @author zhibing.pu
@@ -12,4 +34,100 @@
 */
@Service
public class WorldCupPaymentServiceImpl extends ServiceImpl<WorldCupPaymentMapper, WorldCupPayment> implements IWorldCupPaymentService {
    @Autowired
    private IWorldCupPaymentParticipantService worldCupPaymentParticipantService;
    @Autowired
    private IWorldCupService worldCupService;
    @Autowired
    private PayMoneyUtil payMoneyUtil;
    @Resource
    private AppUserClient appUserClient;
    @Resource
    private CoursePackageOrderStudentClient coursePackageOrderStudentClient;
    /**
     * 取消已报名的世界杯
     * @param id
     * @return
     */
    @Override
    public ResultUtil cancelMyWorldCup(String id) {
        WorldCupPaymentParticipant worldCupPaymentParticipant = worldCupPaymentParticipantService.getById(id);
        WorldCupPayment worldCupPayment = this.getById(worldCupPaymentParticipant.getWorldCupPaymentId());
        WorldCup worldCup = worldCupService.getById(worldCupPayment.getWorldCupId());
        //开始前一天不能取消
        if(worldCup.getStartTime().getTime() < System.currentTimeMillis() + 86400000L){
            return ResultUtil.error("世界杯快开始了,不能取消");
        }
        //开始处理退款
        //免费
        if(worldCupPayment.getPayType() == 0){
            worldCupPayment.setRefundOrderNo("");
            worldCupPayment.setRefundTime(new Date());
            worldCupPayment.setPayStatus(3);
            this.updateById(worldCupPayment);
            return ResultUtil.success();
        }
        List<WorldCupPaymentParticipant> list1 = worldCupPaymentParticipantService.list(new QueryWrapper<WorldCupPaymentParticipant>()
                .eq("worldCupPaymentId", worldCupPayment.getId()).eq("alreadyEntered", 0));
        BigDecimal multiply = worldCupPayment.getUnitPrice().multiply(new BigDecimal(list1.size()));
        //微信支付
        if(worldCupPayment.getPayType() == 1){
            Map<String, String> map = payMoneyUtil.wxRefund(worldCupPayment.getPayOrderNo(), worldCupPayment.getCode(),
                    worldCupPayment.getAmount().toString(), multiply.toString(), "/base/worldCup/wxRefundWorldCupCallback");
            if(!"SUCCESS".equals(map.get("return_code"))){
                System.err.println("-------------微信退款失败---------");
                System.err.println(map.get("return_msg"));
                return ResultUtil.error("微信退款失败");
            }
        }
        //支付宝支付
        if(worldCupPayment.getPayType() == 2){
            Map<String, String> map = null;
            try {
                map = payMoneyUtil.aliRefund(worldCupPayment.getPayOrderNo(), multiply.toString());
            } catch (AlipayApiException e) {
                throw new RuntimeException(e);
            }
            if("10000".equals(map.get("code"))){
                String trade_no = map.get("trade_no");
                worldCupPayment.setRefundTime(new Date());
                worldCupPayment.setRefundOrderNo(trade_no);
                worldCupPayment.setPayStatus(3);
                this.updateById(worldCupPayment);
            }
        }
        //玩湃币支付
        if(worldCupPayment.getPayType() == 3){
            Integer appUserId = worldCupPayment.getAppUserId();
            AppUser appUser = appUserClient.getAppUser(appUserId);
            appUser.setPlayPaiCoins(appUser.getPlayPaiCoins() + multiply.intValue());
            appUserClient.updateAppUser(appUser);
            worldCupPayment.setRefundTime(new Date());
            worldCupPayment.setRefundOrderNo("");
            worldCupPayment.setPayStatus(3);
            this.updateById(worldCupPayment);
        }
        //课时支付
        if(worldCupPayment.getPayType() == 4){
            for (WorldCupPaymentParticipant worldCupPaymentParticipant1 : list1) {
                String content = worldCupPaymentParticipant1.getContent();
                DeductionClassHourList deductionClassHourList = JSON.parseObject(content, DeductionClassHourList.class);
                coursePackageOrderStudentClient.backspaceClassHour(deductionClassHourList);
            }
            worldCupPayment.setRefundTime(new Date());
            worldCupPayment.setRefundOrderNo("");
            worldCupPayment.setPayStatus(3);
            this.updateById(worldCupPayment);
        }
        return ResultUtil.success();
    }
}