44323
2023-11-14 ddbb38c54db9c3670e5ff53f4bf713525de1099d
cloud-server-competition/src/main/java/com/dsh/competition/service/impl/PaymentCompetitionServiceImpl.java
@@ -1,10 +1,40 @@
package com.dsh.competition.service.impl;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dsh.competition.entity.Competition;
import com.dsh.competition.entity.Participant;
import com.dsh.competition.entity.PaymentCompetition;
import com.dsh.competition.entity.UserCompetition;
import com.dsh.competition.feignclient.account.AppUserClient;
import com.dsh.competition.feignclient.account.StudentClient;
import com.dsh.competition.feignclient.account.model.AppUser;
import com.dsh.competition.feignclient.account.model.Student;
import com.dsh.competition.feignclient.course.CoursePackagePaymentClient;
import com.dsh.competition.feignclient.course.model.PaymentDeductionClassHour;
import com.dsh.competition.feignclient.model.BillingRequest;
import com.dsh.competition.feignclient.other.StoreClient;
import com.dsh.competition.feignclient.other.model.Store;
import com.dsh.competition.mapper.PaymentCompetitionMapper;
import com.dsh.competition.model.*;
import com.dsh.competition.service.CompetitionService;
import com.dsh.competition.service.IParticipantService;
import com.dsh.competition.service.IPaymentCompetitionService;
import com.dsh.competition.service.UserCompetitionService;
import com.dsh.competition.util.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import javax.annotation.Resource;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
/**
 * @author zhibing.pu
@@ -12,4 +42,230 @@
 */
@Service
public class PaymentCompetitionServiceImpl extends ServiceImpl<PaymentCompetitionMapper, PaymentCompetition> implements IPaymentCompetitionService {
    @Autowired
    private CompetitionService competitionService;
    @Resource
    private StoreClient storeClient;
    @Autowired
    private UserCompetitionService userCompetitionService;
    @Autowired
    private IParticipantService participantService;
    @Autowired
    private PayMoneyUtil payMoneyUtil;
    @Resource
    private AppUserClient appUserClient;
    @Resource
    private CoursePackagePaymentClient coursePackagePaymentClient;
    @Resource
    private StudentClient studentClient;
    @Autowired
    private PaymentCompetitionMapper paymentCompetitionMapper;
    /**
     * 获取我的报名赛事记录
     * @param uid
     * @param type
     * @param pageSize
     * @param pageNo
     * @return
     * @throws Exception
     */
    @Override
    public List<CompetitionListVo> queryMyCompetitionList(Integer uid, Integer type, Integer pageSize, Integer pageNo) throws Exception {
        pageNo = (pageNo - 1) * pageSize;
        if(0 == type){
            type = null;
        }
        return this.baseMapper.queryMyCompetitionList(uid, type, pageSize, pageNo);
    }
    /**
     * 获取我报名的赛事详情
     * @param id
     * @return
     * @throws Exception
     */
    @Override
    public CompetitionInfo queryMyCompetitionInfo(Long id) throws Exception {
        PaymentCompetition paymentCompetition = this.getById(id);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        Competition competition = competitionService.getById(paymentCompetition.getCompetitionId());
        CompetitionInfo competitionInfo = new CompetitionInfo();
        competitionInfo.setId(id);
        competitionInfo.setImgs(competition.getImgs());
        competitionInfo.setName(competition.getName());
        competitionInfo.setRegisterCondition(competition.getRegisterCondition());
        Store store = storeClient.queryStoreById(Integer.valueOf(competition.getStoreId().split(",")[0]));
        competitionInfo.setStoreName(store.getName());
        competitionInfo.setStoreAddress(store.getAddress());
        competitionInfo.setStoreLon(store.getLon());
        competitionInfo.setStoreLat(store.getLat());
        competitionInfo.setStoreCoverDrawing(store.getCoverDrawing());
        competitionInfo.setRegisterEndTime(sdf.format(competition.getRegisterEndTime()));
        competitionInfo.setStartTime(sdf.format(competition.getStartTime()));
        competitionInfo.setEndTime(sdf.format(competition.getEndTime()));
        competitionInfo.setAge(competition.getStartAge() + "-" + competition.getEndAge());
        competitionInfo.setProvince(competition.getEntryProvince());
        competitionInfo.setCity(competition.getEntryCity());
        competitionInfo.setAddress(competition.getEntryAddress());
        competitionInfo.setCashPrice(competition.getCashPrice());
        competitionInfo.setPlayPaiCoin(competition.getPlayPaiCoin());
        competitionInfo.setClassPrice(competition.getClassPrice());
        competitionInfo.setIntroduction(competition.getIntroduction());
        competitionInfo.setRegistrationNotes(competition.getRegistrationNotes());
        competitionInfo.setApply(1);
        List<ParticipantVo> participant = new ArrayList<>();
        List<UserCompetition> list = userCompetitionService.list(new QueryWrapper<UserCompetition>().eq("paymentCompetitionId", paymentCompetition.getId()));
        List<Integer> collect = list.stream().map(UserCompetition::getParticipantId).collect(Collectors.toList());
        if(collect.size()==0){
            collect.add(-1);
        }
        List<Participant> participants = participantService.listByIds(collect);
        SimpleDateFormat sdf_year = new SimpleDateFormat("yyyy");
        for (Participant participant1 : participants) {
            ParticipantVo participantVo = new ParticipantVo();
            participantVo.setId(participant1.getId());
            participantVo.setName(participant1.getName());
            participantVo.setIdcard(participant1.getIdcard());
            Integer age = Integer.valueOf(sdf_year.format(new Date())) - Integer.valueOf(sdf_year.format(participant1.getBirthday()));
            participantVo.setAge(age);
            participant.add(participantVo);
        }
        competitionInfo.setParticipant(participant);
        competitionInfo.setStatus(competition.getStatus());
        if(paymentCompetition.getPayStatus() == 3){
            competitionInfo.setStatus(4);
        }
        competitionInfo.setPayMoney(paymentCompetition.getAmount());
        competitionInfo.setPayType(paymentCompetition.getPayType());
        return competitionInfo;
    }
    /**
     * 取消赛事报名
     * @param id
     * @return
     * @throws Exception
     */
    @Override
    public ResultUtil cancelMyCompetition(Long id) throws Exception {
        PaymentCompetition paymentCompetition = this.getById(id);
        if(paymentCompetition.getPayStatus() == 3){
            return ResultUtil.error("不能重复取消");
        }
        String code = paymentCompetition.getCode();
        Double amount = paymentCompetition.getAmount();
        Competition competition = competitionService.getById(paymentCompetition.getCompetitionId());
        if(System.currentTimeMillis() >= competition.getRegisterEndTime().getTime()){
            return ResultUtil.error("赛事已结束报名,无法取消");
        }
        String payOrderNo = paymentCompetition.getPayOrderNo();
        if(paymentCompetition.getPayType() == 1){//微信支付
            Map<String, String> map = payMoneyUtil.wxRefund(payOrderNo, code, amount.toString(), amount.toString(), "/base/competition/weChatCancelPaymentCompetitionCallback");
            String return_code = map.get("return_code");
            if(!"SUCCESS".equals(return_code)){
                return ResultUtil.error(map.get("return_msg"));
            }
            String refund_id = map.get("refund_id");
            paymentCompetition.setRefundOrderNo(refund_id);
            this.updateById(paymentCompetition);
            storeClient.addBackRecord(paymentCompetition.getAmount()+"_"+paymentCompetition.getAppUserId());
        }
        if(paymentCompetition.getPayType() == 2){//支付宝支付
            Map<String, String> map = payMoneyUtil.aliRefund(payOrderNo, amount.toString());
            String return_code = map.get("code");
            if(!"10000".equals(return_code)){
                return ResultUtil.error(map.get("msg"));
            }
            String refund_id = map.get("trade_no");
            paymentCompetition.setRefundOrderNo(refund_id);
            paymentCompetition.setRefundTime(new Date());
            paymentCompetition.setPayStatus(3);
            paymentCompetition.setAppUserId(null);
            this.updateById(paymentCompetition);
            competition.setApplicantsNumber(competition.getApplicantsNumber() - 1);
            competitionService.updateById(competition);
        }
        if(paymentCompetition.getPayType() == 3){//玩湃币支付
            AppUser appUser = appUserClient.queryAppUser(paymentCompetition.getAppUserId());
            appUser.setPlayPaiCoins(appUser.getPlayPaiCoins() + amount.intValue());
            appUserClient.updateAppUser(appUser);
            paymentCompetition.setRefundOrderNo("");
            paymentCompetition.setRefundTime(new Date());
            paymentCompetition.setPayStatus(3);
            paymentCompetition.setAppUserId(null);
            this.updateById(paymentCompetition);
            competition.setApplicantsNumber(competition.getApplicantsNumber() - 1);
            competitionService.updateById(competition);
        }
        if(paymentCompetition.getPayType() == 4){//课程支付
            List<UserCompetition> list = userCompetitionService.list(new QueryWrapper<UserCompetition>().eq("paymentCompetitionId", paymentCompetition.getId()));
            for (UserCompetition userCompetition : list) {
                Participant participant = participantService.getById(userCompetition.getId());
                Student student = studentClient.queryStudentByPhone(participant.getPhone());
                PaymentDeductionClassHour paymentDeductionClassHour = new PaymentDeductionClassHour();
                paymentDeductionClassHour.setId(student.getId());
                paymentDeductionClassHour.setClassHour(competition.getClassPrice());
                paymentDeductionClassHour.setCode(code);
                coursePackagePaymentClient.rollbackPaymentDeductionClassHour(paymentDeductionClassHour);
            }
            paymentCompetition.setRefundOrderNo("");
            paymentCompetition.setRefundTime(new Date());
            paymentCompetition.setPayStatus(3);
            paymentCompetition.setAppUserId(null);
            this.updateById(paymentCompetition);
            competition.setApplicantsNumber(competition.getApplicantsNumber() - 1);
            competitionService.updateById(competition);
        }
        return ResultUtil.success();
    }
    @Override
    public List<BillingRequest> queryDatas(Integer appUserId, String monthStart, String monthEnd) {
        return this.baseMapper.queryDatas(appUserId,monthStart,monthEnd);
    }
    @Override
    public List<BillingRequest> queryCancelDatas(Integer appUserId, String monthStart, String monthEnd) {
        return this.baseMapper.queryCancelDatas(appUserId,monthStart,monthEnd);
    }
    @Override
    public List<PaymentCompetition> listAll(CompetitionQuery query) {
        String STime = null;
        String ETime = null;
        if (StringUtils.hasLength(query.getTime())) {
            STime = query.getTime().split(" - ")[0] + " 00:00:00";
            ETime = query.getTime().split(" - ")[1] + " 23:59:59";
        }
        return paymentCompetitionMapper.listAll(query,STime,ETime,query.getAmount());
    }
    @Override
    public Integer queryByCode(String code) {
        return this.baseMapper.queryBycode(code);
    }
}