package com.dsh.account.service.impl;
|
|
import com.alibaba.fastjson.JSON;
|
import com.alipay.api.response.AlipayTradeQueryResponse;
|
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
import com.dsh.account.entity.TAppUser;
|
import com.dsh.account.entity.VipPayment;
|
import com.dsh.account.feignclient.activity.UserConponClient;
|
import com.dsh.account.mapper.VipPaymentMapper;
|
import com.dsh.account.model.IncomeQuery;
|
import com.dsh.account.service.IVipPaymentService;
|
import com.dsh.account.service.TAppUserService;
|
import com.dsh.account.util.PayMoneyUtil;
|
import com.dsh.account.util.RedisUtil;
|
import com.dsh.account.util.ResultUtil;
|
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.util.Calendar;
|
import java.util.Date;
|
import java.util.List;
|
import java.util.Map;
|
|
/**
|
* @author zhibing.pu
|
* @date 2023/6/24 10:03
|
*/
|
@Service
|
public class VipPaymentServiceImpl extends ServiceImpl<VipPaymentMapper, VipPayment> implements IVipPaymentService {
|
|
@Autowired
|
private TAppUserService appUserService;
|
|
@Autowired
|
private PayMoneyUtil payMoneyUtil;
|
|
@Resource
|
private UserConponClient userConponClient;
|
|
@Autowired
|
private RedisUtil redisUtil;
|
|
|
/**
|
* 购买年度会员
|
*
|
* @param uid
|
* @return
|
* @throws Exception
|
*/
|
@Override
|
public ResultUtil addVipPayment(Integer uid, Integer payType) throws Exception {
|
VipPayment vipPayment = new VipPayment();
|
vipPayment.setCode(VipPayment.CODE());
|
vipPayment.setAmount(0.01D);
|
vipPayment.setAppUserId(uid);
|
vipPayment.setInsertTime(new Date());
|
vipPayment.setPayStatus(1);
|
vipPayment.setState(1);
|
this.baseMapper.insert(vipPayment);
|
if (payType == 1) {//微信
|
return weixinpay(vipPayment);
|
}
|
if (payType == 2) {//支付宝
|
return alipay(vipPayment);
|
}
|
return ResultUtil.success();
|
}
|
|
|
/**
|
* 微信
|
*
|
* @param vipPayment
|
* @return
|
* @throws Exception
|
*/
|
public ResultUtil weixinpay(VipPayment vipPayment) throws Exception {
|
String code = vipPayment.getCode();
|
ResultUtil weixinpay = payMoneyUtil.weixinpay("购买年度会员", "", code, vipPayment.getAmount().toString(),
|
"/base/appUser/addVipPaymentWeChatCallback", "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);
|
VipPayment vipPayment1 = VipPaymentServiceImpl.this.getOne(new QueryWrapper<VipPayment>().eq("code", code));
|
if (vipPayment1.getPayStatus() == 2) {
|
return;
|
}
|
/**
|
* SUCCESS--支付成功
|
* REFUND--转入退款
|
* NOTPAY--未支付
|
* CLOSED--已关闭
|
* REVOKED--已撤销(刷卡支付)
|
* USERPAYING--用户支付中
|
* PAYERROR--支付失败(其他原因,如银行返回失败)
|
* ACCEPT--已接收,等待扣款
|
*/
|
ResultUtil<Map<String, String>> resultUtil = payMoneyUtil.queryWXOrder(code, "");
|
if (resultUtil.getCode() == 200) {
|
Map<String, String> map = resultUtil.getData();
|
String trade_type = map.get("trade_type");
|
String trade_state = map.get("trade_state");
|
String transaction_id = map.get("transaction_id");
|
if ("REFUND".equals(trade_state) || "NOTPAY".equals(trade_state) || "CLOSED".equals(trade_state) || "REVOKED".equals(trade_state) || "PAYERROR".equals(trade_state)) {
|
vipPayment1.setState(3);
|
VipPaymentServiceImpl.this.updateById(vipPayment1);
|
return;
|
}
|
if ("SUCCESS".equals(trade_state)) {
|
vipPayment1.setPayStatus(2);
|
vipPayment1.setPayTime(new Date());
|
vipPayment1.setOrderNumber(transaction_id);
|
VipPaymentServiceImpl.this.updateById(vipPayment1);
|
TAppUser appUser = appUserService.getById(vipPayment1.getAppUserId());
|
Date vipEndTime = appUser.getVipEndTime();
|
if (null == vipEndTime) {
|
vipEndTime = new Date();
|
}
|
Calendar calendar = Calendar.getInstance();
|
calendar.setTime(vipEndTime);
|
calendar.set(Calendar.YEAR, calendar.get(Calendar.YEAR) + 1);
|
appUser.setIsVip(1);
|
appUser.setVipEndTime(calendar.getTime());
|
appUserService.updateById(appUser);
|
// 注册会员送券 先判断是否有注册送券类型的优惠券 判断优惠券状态 审核是否通过 是否删除 是否在有效期内 是否领取数量达上限
|
List<Long> longs = userConponClient.queryCouponByUser(appUser.getId());
|
redisUtil.setStrValue("VIP_P_" + vipPayment1.getAppUserId(), JSON.toJSONString(longs), 3600);
|
return;
|
}
|
if ("USERPAYING".equals(trade_state) || "ACCEPT".equals(trade_state)) {
|
Thread.sleep(wait);
|
num++;
|
}
|
} else {
|
Thread.sleep(wait);
|
num++;
|
}
|
if (10 == num) {
|
vipPayment1.setState(3);
|
VipPaymentServiceImpl.this.updateById(vipPayment1);
|
}
|
}
|
} catch (Exception e) {
|
e.printStackTrace();
|
}
|
}
|
}).start();
|
}
|
return weixinpay;
|
}
|
|
|
/**
|
* 支付宝
|
*
|
* @param vipPayment
|
* @return
|
* @throws Exception
|
*/
|
public ResultUtil alipay(VipPayment vipPayment) throws Exception {
|
String code = vipPayment.getCode();
|
ResultUtil alipay = payMoneyUtil.alipay("购买年度会员", "购买年度会员", vipPayment.getAppUserId() + "", code, vipPayment.getAmount().toString(),
|
"/base/appUser/addVipPaymentAliCallback");
|
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);
|
VipPayment vipPayment1 = VipPaymentServiceImpl.this.getOne(new QueryWrapper<VipPayment>().eq("code", code));
|
if (vipPayment1.getPayStatus() != 1) {
|
return;
|
}
|
|
/**
|
* 交易状态:
|
* WAIT_BUYER_PAY(交易创建,等待买家付款)、
|
* TRADE_CLOSED(未付款交易超时关闭,或支付完成后全额退款)、
|
* TRADE_SUCCESS(交易支付成功)、
|
* TRADE_FINISHED(交易结束,不可退款)
|
*/
|
AlipayTradeQueryResponse resultUtil = payMoneyUtil.queryALIOrder(code);
|
|
if (resultUtil.getCode().equals("10000")) {
|
String tradeNo = resultUtil.getTradeNo();
|
String tradeStatus = resultUtil.getTradeStatus();
|
System.out.println("ssssss" + tradeStatus);
|
if ("TRADE_CLOSED".equals(tradeStatus) || "TRADE_FINISHED".equals(tradeStatus)) {
|
vipPayment1.setState(3);
|
VipPaymentServiceImpl.this.updateById(vipPayment1);
|
return;
|
}
|
if ("TRADE_SUCCESS".equals(tradeStatus)) {
|
vipPayment1.setPayStatus(2);
|
vipPayment1.setPayTime(new Date());
|
vipPayment1.setOrderNumber(tradeNo);
|
VipPaymentServiceImpl.this.updateById(vipPayment1);
|
|
TAppUser appUser = appUserService.getById(vipPayment1.getAppUserId());
|
Date vipEndTime = appUser.getVipEndTime();
|
appUser.setIsVip(1);
|
if (null == vipEndTime) {
|
vipEndTime = new Date();
|
}
|
Calendar calendar = Calendar.getInstance();
|
calendar.setTime(vipEndTime);
|
calendar.set(Calendar.YEAR, calendar.get(Calendar.YEAR) + 1);
|
appUser.setVipEndTime(calendar.getTime());
|
appUserService.updateById(appUser);
|
// 注册会员送券 先判断是否有注册送券类型的优惠券 判断优惠券状态 审核是否通过 是否删除 是否在有效期内 是否领取数量达上限
|
List<Long> longs = userConponClient.queryCouponByUser(appUser.getId());
|
redisUtil.setStrValue("VIP_P_" + vipPayment1.getAppUserId(), JSON.toJSONString(longs), 3600);
|
return;
|
}
|
if ("WAIT_BUYER_PAY".equals(tradeStatus)) {
|
Thread.sleep(wait);
|
num++;
|
}
|
} else {
|
Thread.sleep(wait);
|
num++;
|
}
|
if (10 == num) {
|
vipPayment1.setState(3);
|
VipPaymentServiceImpl.this.updateById(vipPayment1);
|
}
|
}
|
} catch (Exception e) {
|
e.printStackTrace();
|
}
|
}
|
}).start();
|
}
|
return alipay;
|
}
|
|
|
/**
|
* 购买年度会员支付回调处理
|
*
|
* @param code
|
* @param orderNumber
|
* @throws Exception
|
*/
|
@Override
|
public synchronized ResultUtil addVipPaymentCallback(String code, String orderNumber) throws Exception {
|
VipPayment vipPayment1 = VipPaymentServiceImpl.this.getOne(new QueryWrapper<VipPayment>().eq("code", code));
|
if (vipPayment1.getPayStatus() == 2) {
|
return ResultUtil.success();
|
}
|
vipPayment1.setPayStatus(2);
|
vipPayment1.setPayTime(new Date());
|
vipPayment1.setOrderNumber(orderNumber);
|
VipPaymentServiceImpl.this.updateById(vipPayment1);
|
|
TAppUser appUser = appUserService.getById(vipPayment1.getAppUserId());
|
Date vipEndTime = appUser.getVipEndTime();
|
if (null == vipEndTime) {
|
vipEndTime = new Date();
|
}
|
Calendar calendar = Calendar.getInstance();
|
calendar.setTime(vipEndTime);
|
calendar.set(Calendar.YEAR, calendar.get(Calendar.YEAR) + 1);
|
appUser.setIsVip(1);
|
appUser.setVipEndTime(calendar.getTime());
|
appUserService.updateById(appUser);
|
// 注册会员送券 先判断是否有注册送券类型的优惠券 判断优惠券状态 审核是否通过 是否删除 是否在有效期内 是否领取数量达上限
|
List<Long> longs = userConponClient.queryCouponByUser(appUser.getId());
|
redisUtil.setStrValue("VIP_P_" + vipPayment1.getAppUserId(), JSON.toJSONString(longs), 3600);
|
return ResultUtil.success();
|
}
|
|
|
}
|