|
@@ -14,14 +14,33 @@ package vip.xiaonuo.biz.modular.rechargerecord.service.impl;
|
|
|
|
|
|
import cn.hutool.core.bean.BeanUtil;
|
|
|
import cn.hutool.core.collection.CollStreamUtil;
|
|
|
+import cn.hutool.core.date.DatePattern;
|
|
|
+import cn.hutool.core.date.DateUtil;
|
|
|
import cn.hutool.core.util.ObjectUtil;
|
|
|
+import cn.hutool.core.util.RandomUtil;
|
|
|
import cn.hutool.core.util.StrUtil;
|
|
|
+import com.alibaba.fastjson.JSONObject;
|
|
|
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
|
|
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
|
|
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
|
|
+import com.wechat.pay.java.core.RSAAutoCertificateConfig;
|
|
|
+import com.wechat.pay.java.core.exception.HttpException;
|
|
|
+import com.wechat.pay.java.core.exception.MalformedMessageException;
|
|
|
+import com.wechat.pay.java.core.exception.ServiceException;
|
|
|
+import com.wechat.pay.java.service.payments.jsapi.JsapiServiceExtension;
|
|
|
+import com.wechat.pay.java.service.payments.jsapi.model.Amount;
|
|
|
+import com.wechat.pay.java.service.payments.jsapi.model.Payer;
|
|
|
+import com.wechat.pay.java.service.payments.jsapi.model.PrepayRequest;
|
|
|
+import com.wechat.pay.java.service.payments.jsapi.model.PrepayWithRequestPaymentResponse;
|
|
|
import jakarta.annotation.Resource;
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
import org.springframework.transaction.annotation.Transactional;
|
|
|
+import vip.xiaonuo.auth.core.util.StpLoginUserUtil;
|
|
|
+import vip.xiaonuo.biz.modular.rechargeplanconfig.entity.BizRechargePlanConfig;
|
|
|
+import vip.xiaonuo.biz.modular.rechargeplanconfig.mapper.BizRechargePlanConfigMapper;
|
|
|
+import vip.xiaonuo.biz.modular.user.entity.BizUser;
|
|
|
+import vip.xiaonuo.biz.modular.user.mapper.BizUserMapper;
|
|
|
import vip.xiaonuo.common.enums.CommonSortOrderEnum;
|
|
|
import vip.xiaonuo.common.exception.CommonException;
|
|
|
import vip.xiaonuo.common.page.CommonPageRequest;
|
|
@@ -35,33 +54,44 @@ import vip.xiaonuo.biz.modular.rechargerecord.service.BizRechargeRecordService;
|
|
|
import vip.xiaonuo.common.util.CommonWxUtil;
|
|
|
import vip.xiaonuo.common.wx.WxPayConfig;
|
|
|
|
|
|
+import java.math.BigDecimal;
|
|
|
+import java.math.RoundingMode;
|
|
|
+import java.util.HashMap;
|
|
|
import java.util.List;
|
|
|
+import java.util.Map;
|
|
|
|
|
|
/**
|
|
|
* 充值记录表Service接口实现类
|
|
|
*
|
|
|
* @author wulei
|
|
|
- * @date 2025/02/06 17:41
|
|
|
+ * @date 2025/02/06 17:41
|
|
|
**/
|
|
|
@Service
|
|
|
+@Slf4j
|
|
|
public class BizRechargeRecordServiceImpl extends ServiceImpl<BizRechargeRecordMapper, BizRechargeRecord> implements BizRechargeRecordService {
|
|
|
|
|
|
@Resource
|
|
|
private CommonWxUtil commonWxUtil;
|
|
|
@Resource
|
|
|
private WxPayConfig wxPayConfig;
|
|
|
+ @Resource
|
|
|
+ private RSAAutoCertificateConfig rsaAutoCertificateConfig;
|
|
|
+ @Resource
|
|
|
+ private BizRechargePlanConfigMapper bizRechargePlanConfigMapper;
|
|
|
+ @Resource
|
|
|
+ private BizUserMapper bizUserMapper;
|
|
|
|
|
|
|
|
|
@Override
|
|
|
public Page<BizRechargeRecord> page(BizRechargeRecordPageParam bizRechargeRecordPageParam) {
|
|
|
QueryWrapper<BizRechargeRecord> queryWrapper = new QueryWrapper<BizRechargeRecord>().checkSqlInjection();
|
|
|
- if(ObjectUtil.isNotEmpty(bizRechargeRecordPageParam.getUserId())) {
|
|
|
+ if (ObjectUtil.isNotEmpty(bizRechargeRecordPageParam.getUserId())) {
|
|
|
queryWrapper.lambda().like(BizRechargeRecord::getUserId, bizRechargeRecordPageParam.getUserId());
|
|
|
}
|
|
|
- if(ObjectUtil.isNotEmpty(bizRechargeRecordPageParam.getStartRechargeTime()) && ObjectUtil.isNotEmpty(bizRechargeRecordPageParam.getEndRechargeTime())) {
|
|
|
+ if (ObjectUtil.isNotEmpty(bizRechargeRecordPageParam.getStartRechargeTime()) && ObjectUtil.isNotEmpty(bizRechargeRecordPageParam.getEndRechargeTime())) {
|
|
|
queryWrapper.lambda().between(BizRechargeRecord::getRechargeTime, bizRechargeRecordPageParam.getStartRechargeTime(), bizRechargeRecordPageParam.getEndRechargeTime());
|
|
|
}
|
|
|
- if(ObjectUtil.isAllNotEmpty(bizRechargeRecordPageParam.getSortField(), bizRechargeRecordPageParam.getSortOrder())) {
|
|
|
+ if (ObjectUtil.isAllNotEmpty(bizRechargeRecordPageParam.getSortField(), bizRechargeRecordPageParam.getSortOrder())) {
|
|
|
CommonSortOrderEnum.validate(bizRechargeRecordPageParam.getSortOrder());
|
|
|
queryWrapper.orderBy(true, bizRechargeRecordPageParam.getSortOrder().equals(CommonSortOrderEnum.ASC.getValue()),
|
|
|
StrUtil.toUnderlineCase(bizRechargeRecordPageParam.getSortField()));
|
|
@@ -73,9 +103,100 @@ public class BizRechargeRecordServiceImpl extends ServiceImpl<BizRechargeRecordM
|
|
|
|
|
|
@Transactional(rollbackFor = Exception.class)
|
|
|
@Override
|
|
|
- public void add(BizRechargeRecordAddParam bizRechargeRecordAddParam) {
|
|
|
- BizRechargeRecord bizRechargeRecord = BeanUtil.toBean(bizRechargeRecordAddParam, BizRechargeRecord.class);
|
|
|
+ public synchronized Map<String, Object> add(BizRechargeRecordAddParam bizRechargeRecordAddParam) {
|
|
|
+ Map<String, Object> resultMap = new HashMap<>(5);
|
|
|
+ BizRechargeRecord bizRechargeRecord = new BizRechargeRecord();
|
|
|
+ //充值金额不能为0
|
|
|
+ if (bizRechargeRecordAddParam.getRechargeAmount().compareTo(BigDecimal.ZERO) == 0) {
|
|
|
+ throw new CommonException("充值金额不能为0");
|
|
|
+ }
|
|
|
+ // 查询用户信息
|
|
|
+ BizUser bizUser = bizUserMapper.selectById(StpLoginUserUtil.getLoginUser().getId());
|
|
|
+ if (ObjectUtil.isNull(bizUser)) {
|
|
|
+ throw new CommonException("未查询到该用户信息");
|
|
|
+ }
|
|
|
+ if (ObjectUtil.isNotEmpty(bizRechargeRecordAddParam.getRechargePlanId())) {
|
|
|
+ // 校验充值方案
|
|
|
+ BizRechargePlanConfig bizRechargePlanConfig = bizRechargePlanConfigMapper.selectById(bizRechargeRecordAddParam.getRechargePlanId());
|
|
|
+ if (ObjectUtil.isNull(bizRechargePlanConfig)) {
|
|
|
+ throw new CommonException("未查询到该充值方案");
|
|
|
+ }
|
|
|
+ if (bizRechargeRecordAddParam.getRechargeAmount().compareTo(bizRechargePlanConfig.getRechargeAmount()) < 0) {
|
|
|
+ throw new CommonException("当前充值金额不符合该优惠方案");
|
|
|
+ }
|
|
|
+ bizRechargeRecord.setRechargePlanId(bizRechargeRecordAddParam.getRechargePlanId());
|
|
|
+ bizRechargeRecord.setPlanAccountBalance(bizRechargePlanConfig.getAccountBalance());
|
|
|
+ bizRechargeRecord.setCouponAmount(bizRechargePlanConfig.getCouponAmount());
|
|
|
+ bizRechargeRecord.setCouponNum(bizRechargePlanConfig.getCouponNum());
|
|
|
+ bizRechargeRecord.setRebateRatio(bizRechargePlanConfig.getRebateRatio());
|
|
|
+
|
|
|
+ //计算返点金额
|
|
|
+ if (ObjectUtil.isNotEmpty(bizUser.getReferralUser())) {
|
|
|
+ BigDecimal rebate = bizRechargePlanConfig.getRebateRatio().divide(new BigDecimal("100"), 2, RoundingMode.HALF_UP);
|
|
|
+ BigDecimal rebateAmount = bizRechargeRecordAddParam.getRechargeAmount().multiply(rebate);
|
|
|
+ bizRechargeRecord.setRebateAmount(rebateAmount);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ String openId = commonWxUtil.getOpenId(bizRechargeRecordAddParam.getCode(), wxPayConfig.getGetOpenIdUrl(), wxPayConfig.getAppId(), wxPayConfig.getAppSecret());
|
|
|
+ if (ObjectUtil.isEmpty(openId)) {
|
|
|
+ throw new CommonException("openId获取异常,请联系管理员");
|
|
|
+ }
|
|
|
+ log.info("================ 生成基础充值记录内容 =============");
|
|
|
+ bizRechargeRecord.setRechargeAmount(bizRechargeRecordAddParam.getRechargeAmount());
|
|
|
+ // 本系统订单号
|
|
|
+ String orderNumber = "BBT" + DateUtil.format(DateUtil.date(), DatePattern.PURE_DATETIME_MS_PATTERN) + RandomUtil.randomNumbers(3);
|
|
|
+ bizRechargeRecord.setOrderNo(orderNumber);
|
|
|
+ bizRechargeRecord.setRechargeTime(DateUtil.date());
|
|
|
+ bizRechargeRecord.setUserId(bizUser.getId());
|
|
|
this.save(bizRechargeRecord);
|
|
|
+ //转换金额为分
|
|
|
+ Integer totalFee = bizRechargeRecordAddParam.getRechargeAmount().multiply(new BigDecimal("100")).intValue();
|
|
|
+
|
|
|
+ //请求微信支付相关配置
|
|
|
+ JsapiServiceExtension service =
|
|
|
+ new JsapiServiceExtension.Builder()
|
|
|
+ .config(rsaAutoCertificateConfig)
|
|
|
+ .signType("RSA") // 不填默认为RSA
|
|
|
+ .build();
|
|
|
+ PrepayWithRequestPaymentResponse response;
|
|
|
+ try {
|
|
|
+ PrepayRequest request = new PrepayRequest();
|
|
|
+ request.setAppid(wxPayConfig.getAppId());
|
|
|
+ request.setMchid(wxPayConfig.getMerchantId());
|
|
|
+ request.setDescription("爸爸糖手工吐司会员账户充值");
|
|
|
+ request.setOutTradeNo(orderNumber);
|
|
|
+ request.setNotifyUrl(wxPayConfig.getPayNotifyUrl());
|
|
|
+ Amount amount = new Amount();
|
|
|
+ amount.setTotal(totalFee);
|
|
|
+ request.setAmount(amount);
|
|
|
+ Payer payer = new Payer();
|
|
|
+ payer.setOpenid(openId);
|
|
|
+ request.setPayer(payer);
|
|
|
+ log.info("请求生成订单,请求参数:{}", JSONObject.toJSONString(request));
|
|
|
+ // 调用预下单接口
|
|
|
+ response = service.prepayWithRequestPayment(request);
|
|
|
+ resultMap.put("package", response.getPackageVal());
|
|
|
+ resultMap.put("nonceStr", response.getNonceStr());
|
|
|
+ resultMap.put("signType", response.getSignType());
|
|
|
+ resultMap.put("timeStamp", response.getTimeStamp());
|
|
|
+ resultMap.put("paySign", response.getPaySign());
|
|
|
+ resultMap.put("dataId", bizRechargeRecord.getId());
|
|
|
+ log.info("订单【{}】生成订单成功,返回信息:{}", orderNumber, response);
|
|
|
+ } catch (HttpException e) {
|
|
|
+ // 发送HTTP请求失败
|
|
|
+ log.error("微信下单发送HTTP请求失败,错误信息:{}", e.getMessage());
|
|
|
+ throw new CommonException("微信下单失败,请联系管理员");
|
|
|
+ } catch (ServiceException e) {
|
|
|
+ // 服务返回状态小于200或大于等于300,例如500
|
|
|
+ log.error("微信下单服务状态错误,错误信息:{}", e.getErrorMessage());
|
|
|
+ throw new CommonException("微信下单失败,请联系管理员");
|
|
|
+ } catch (MalformedMessageException e) {
|
|
|
+ // 服务返回成功,返回体类型不合法,或者解析返回体失败
|
|
|
+ log.error("服务返回成功,返回体类型不合法,或者解析返回体失败,错误信息:{}", e.getMessage());
|
|
|
+ throw new CommonException("微信下单失败,请联系管理员");
|
|
|
+ }
|
|
|
+ //3. 返回微信订单参数,方便小程序唤起支付
|
|
|
+ return resultMap;
|
|
|
}
|
|
|
|
|
|
// @Transactional(rollbackFor = Exception.class)
|
|
@@ -101,7 +222,7 @@ public class BizRechargeRecordServiceImpl extends ServiceImpl<BizRechargeRecordM
|
|
|
@Override
|
|
|
public BizRechargeRecord queryEntity(String id) {
|
|
|
BizRechargeRecord bizRechargeRecord = this.getById(id);
|
|
|
- if(ObjectUtil.isEmpty(bizRechargeRecord)) {
|
|
|
+ if (ObjectUtil.isEmpty(bizRechargeRecord)) {
|
|
|
throw new CommonException("充值记录表不存在,id值为:{}", id);
|
|
|
}
|
|
|
return bizRechargeRecord;
|