|
|
@@ -31,6 +31,7 @@ import com.cyksj.mapper.vip.VipOrderDonDeductRelationMapper;
|
|
|
import com.cyksj.mapper.vip.VipOrderDonMapper;
|
|
|
import com.cyksj.mapper.vip.VipUserMapper;
|
|
|
import com.cyksj.model.dto.AliPayParams;
|
|
|
+import com.cyksj.model.dto.GalaxyCoinPayConfigDto;
|
|
|
import com.cyksj.model.dto.VipDeductDto;
|
|
|
import com.cyksj.model.entity.*;
|
|
|
import com.cyksj.model.manage.views.GroupsRelationView;
|
|
|
@@ -43,6 +44,7 @@ import com.cyksj.service.claude.ClaudeService;
|
|
|
import com.cyksj.service.order.VipOrderDonService;
|
|
|
import com.cyksj.service.pay.AliPayCommonService;
|
|
|
import com.cyksj.service.relation.GroupRelationFrontService;
|
|
|
+import com.cyksj.service.sys.SysConfigService;
|
|
|
import com.cyksj.service.user.UserBenefitsService;
|
|
|
import com.cyksj.service.user.UserBindRelationService;
|
|
|
import com.cyksj.service.vip.VipFrontService;
|
|
|
@@ -133,6 +135,12 @@ public class VipOrderDonServiceImpl extends ServiceImpl<VipOrderDonMapper, VipOr
|
|
|
|
|
|
private final BusinessDistributeDefaultRateConfigMapper businessDistributeDefaultRateConfigMapper;
|
|
|
|
|
|
+ private final SysConfigService sysConfigService;
|
|
|
+
|
|
|
+ private final UserGalaxyCoinRecordMapper userGalaxyCoinRecordMapper;
|
|
|
+
|
|
|
+ private final OrderDonGalaxyCoinRecordMapper orderDonGalaxyCoinRecordMapper;
|
|
|
+
|
|
|
@Override
|
|
|
public VipOrderDon submit(VipOrderDonPayReq payRequest) {
|
|
|
Long userId = payRequest.getUserId();
|
|
|
@@ -147,6 +155,7 @@ public class VipOrderDonServiceImpl extends ServiceImpl<VipOrderDonMapper, VipOr
|
|
|
//获取会员价格
|
|
|
VipDeductDto vipMoneyIfDeduct = getVipMoneyIfDeduct(user, payRequest.getIsDeduct());
|
|
|
BigDecimal vipMoney = getVipMoney();
|
|
|
+ BigDecimal actuallyPayMoney = vipMoney;
|
|
|
payRequest.setDonMoney(vipMoney);
|
|
|
if (vipMoneyIfDeduct != null) {
|
|
|
payRequest.setDonMoney(vipMoney.subtract(vipMoneyIfDeduct.getDeductMoney()));
|
|
|
@@ -161,7 +170,7 @@ public class VipOrderDonServiceImpl extends ServiceImpl<VipOrderDonMapper, VipOr
|
|
|
orderDon.setType(VipOrderDon.Type.getType(payRequest.getType()));
|
|
|
orderDon.setIsWebPay(payRequest.getIsWebPay());
|
|
|
//扣除使用优惠券、余额的订单金额
|
|
|
- deductOrderMoney(orderDon, user, payRequest);
|
|
|
+ deductOrderMoney(orderDon, user, payRequest, actuallyPayMoney);
|
|
|
//设置订单支付商品信息
|
|
|
orderDon.setPayTitle(Constant.VIP_TITLE);
|
|
|
orderDon.setGoodsId(Constant.VIP_RELATE_GOODS_ID);
|
|
|
@@ -270,7 +279,7 @@ public class VipOrderDonServiceImpl extends ServiceImpl<VipOrderDonMapper, VipOr
|
|
|
orderDon.setType(VipOrderDon.Type.getType(payRequest.getType()));
|
|
|
orderDon.setIsWebPay(payRequest.getIsWebPay());
|
|
|
//扣除使用优惠券、余额的订单金额
|
|
|
- deductOrderMoney(orderDon, user, payRequest);
|
|
|
+ deductOrderMoney(orderDon, user, payRequest, orderDon.getMoney());
|
|
|
//设置订单支付商品信息
|
|
|
orderDon.setPayTitle(Constant.VIP_TITLE);
|
|
|
orderDon.setGoodsId(Constant.VIP_RELATE_GOODS_ID);
|
|
|
@@ -430,12 +439,97 @@ public class VipOrderDonServiceImpl extends ServiceImpl<VipOrderDonMapper, VipOr
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- private void deductOrderMoney(VipOrderDon orderDon, User user, VipOrderDonPayReq payRequest) {
|
|
|
+ private void deductOrderMoney(VipOrderDon orderDon, User user, VipOrderDonPayReq payRequest, BigDecimal actuallyPayMoney) {
|
|
|
BigDecimal balance = payRequest.getBalance();
|
|
|
//存在余额
|
|
|
deductBalancePayOrderMoney(orderDon, balance, user);
|
|
|
+ //存在银河币
|
|
|
+ deductGalaxyCoinPayOrderMoney(orderDon, payRequest.getGalaxyCoin(), user, actuallyPayMoney);
|
|
|
}
|
|
|
|
|
|
+ private void deductGalaxyCoinPayOrderMoney(VipOrderDon orderDon, BigDecimal toUseGalaxyCoin, User user, BigDecimal actuallyPayMoney) {
|
|
|
+ //存在银河币
|
|
|
+ if (toUseGalaxyCoin != null && toUseGalaxyCoin.compareTo(BigDecimal.ZERO) > 0) {
|
|
|
+ BigDecimal userGalaxyCoin = user.getGalaxyCoin();
|
|
|
+ if (userGalaxyCoin.compareTo(toUseGalaxyCoin) < 0) {
|
|
|
+ throw BusinessRuntimeException.getInstance("您的银河币不足!");
|
|
|
+ }
|
|
|
+ BigDecimal orderMoney = orderDon.getMoney();
|
|
|
+ //订单金额不大于0
|
|
|
+ if (orderMoney.compareTo(BigDecimal.ZERO) == 0) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ //去掉余额 多出的部分
|
|
|
+ BigDecimal subGalxyCoin = toUseGalaxyCoin;
|
|
|
+ SysConfig galaxyCoinPayConfig = sysConfigService.getOne(Wrappers.lambdaQuery(SysConfig.class)
|
|
|
+ .eq(SysConfig::getSysKey, Constant.GALAXY_COIN_PAY_DEDUCT_KEY).last("limit 1"));
|
|
|
+ GalaxyCoinPayConfigDto galaxyCoinPayConfigDto = null;
|
|
|
+ if (galaxyCoinPayConfig != null && StrUtil.isNotEmpty(galaxyCoinPayConfig.getSysValue())) {
|
|
|
+ try {
|
|
|
+ galaxyCoinPayConfigDto = Jsons.parseObject(galaxyCoinPayConfig.getSysValue(), GalaxyCoinPayConfigDto.class);
|
|
|
+ } catch (Exception e) {
|
|
|
+ }
|
|
|
+ if (galaxyCoinPayConfigDto == null) {
|
|
|
+ throw BusinessRuntimeException.getInstance("银河币抵扣系统错误,请联系客服");
|
|
|
+ }
|
|
|
+ //每个银河币可抵扣金额
|
|
|
+ BigDecimal deductMoneyEachCoin = galaxyCoinPayConfigDto.getGalaxyCoin().divide(galaxyCoinPayConfigDto.getMoney());
|
|
|
+ //此次使用抵扣金额
|
|
|
+ BigDecimal thisDeductMoney = toUseGalaxyCoin.multiply(deductMoneyEachCoin);
|
|
|
+ //该笔平台最高可抵扣金额
|
|
|
+ BigDecimal maxDeductMoney = actuallyPayMoney.multiply(galaxyCoinPayConfigDto.getMaxRate());
|
|
|
+ if (thisDeductMoney.compareTo(maxDeductMoney) > 0) {
|
|
|
+ thisDeductMoney = maxDeductMoney;
|
|
|
+ //此时需要扣除的银河币
|
|
|
+ subGalxyCoin = thisDeductMoney.divide(deductMoneyEachCoin);
|
|
|
+ }
|
|
|
+ if (orderDon.getMoney().compareTo(BigDecimal.ZERO) == 0) {
|
|
|
+ orderDon.setType(VipOrderDon.Type.GALAXY_COIN);
|
|
|
+ }
|
|
|
+ //银河币抵扣
|
|
|
+ List<UserGalaxyCoinRecord> availableGalaxyCoinList = userGalaxyCoinRecordMapper.selectList(Wrappers.lambdaQuery(UserGalaxyCoinRecord.class)
|
|
|
+ .eq(UserGalaxyCoinRecord::getUserId, orderDon.getUserId())
|
|
|
+ .eq(UserGalaxyCoinRecord::getIsUsed, Boolean.FALSE)
|
|
|
+ .gt(UserGalaxyCoinRecord::getExpiryTime, DateTime.now())
|
|
|
+ .gt(UserGalaxyCoinRecord::getRemainGalaxyCoin, 0));
|
|
|
+ //需要使用的银河币
|
|
|
+ BigDecimal userFinalUseGalaxyCoin = subGalxyCoin;
|
|
|
+ for (UserGalaxyCoinRecord galaxyCoinRecord : availableGalaxyCoinList) {
|
|
|
+ BigDecimal remainGalaxyCoin = galaxyCoinRecord.getRemainGalaxyCoin();
|
|
|
+ Long galaxyCoinRecordId = galaxyCoinRecord.getId();
|
|
|
+ //扣完
|
|
|
+ if (remainGalaxyCoin.compareTo(userFinalUseGalaxyCoin) == 0) {
|
|
|
+ //完全抵扣
|
|
|
+ deductUserGalaxyCoin(galaxyCoinRecordId, remainGalaxyCoin, remainGalaxyCoin, true);
|
|
|
+ userFinalUseGalaxyCoin = BigDecimal.ZERO;
|
|
|
+ } else if (remainGalaxyCoin.compareTo(userFinalUseGalaxyCoin) < 0) {
|
|
|
+ //不足以抵扣
|
|
|
+ deductUserGalaxyCoin(galaxyCoinRecordId, remainGalaxyCoin, remainGalaxyCoin, true);
|
|
|
+ userFinalUseGalaxyCoin = userFinalUseGalaxyCoin.subtract(remainGalaxyCoin);
|
|
|
+ } else if (remainGalaxyCoin.compareTo(userFinalUseGalaxyCoin) > 0) {
|
|
|
+ //足以抵扣
|
|
|
+ deductUserGalaxyCoin(galaxyCoinRecordId, userFinalUseGalaxyCoin, remainGalaxyCoin, false);
|
|
|
+ userFinalUseGalaxyCoin = BigDecimal.ZERO;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ //最终消耗的银河币需要为0 大于0则说明抵扣失败
|
|
|
+ if (userFinalUseGalaxyCoin.compareTo(BigDecimal.ZERO) > 0) {
|
|
|
+ throw BusinessRuntimeException.getInstance("您的银河币不足!");
|
|
|
+ }
|
|
|
+ //扣除银河币 是否成功
|
|
|
+ Integer success = userMapper.subUserGalaxyCoin(user.getId(), subGalxyCoin, userGalaxyCoin);
|
|
|
+ if (success != 1) throw BusinessRuntimeException.getInstance("您的银河币不足!");
|
|
|
+ //记录使用详情
|
|
|
+ OrderDonGalaxyCoinRecord orderDonGalaxyCoinRecord = new OrderDonGalaxyCoinRecord();
|
|
|
+ orderDonGalaxyCoinRecord.setUserId(user.getId());
|
|
|
+ orderDonGalaxyCoinRecord.setOrderNo(orderDon.getOrderNo());
|
|
|
+ orderDonGalaxyCoinRecord.setGalaxyCoin(subGalxyCoin);
|
|
|
+ orderDonGalaxyCoinRecord.setDeductMoney(thisDeductMoney);
|
|
|
+ orderDonGalaxyCoinRecord.setIsVip(Boolean.TRUE);
|
|
|
+ orderDonGalaxyCoinRecordMapper.insert(orderDonGalaxyCoinRecord);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
private VipDeductDto getVipMoneyIfDeduct(User user, Boolean isDeduct) {
|
|
|
//若用户抵扣
|
|
|
if (isDeduct) {
|
|
|
@@ -709,4 +803,16 @@ public class VipOrderDonServiceImpl extends ServiceImpl<VipOrderDonMapper, VipOr
|
|
|
userPlatDistributeRate.setSecondRate(secondRate);
|
|
|
return userPlatDistributeRate;
|
|
|
}
|
|
|
+
|
|
|
+ private void deductUserGalaxyCoin(Long galaxyCoinRecordId, BigDecimal deductGalaxyCoin, BigDecimal remainGalaxyCoin, boolean isUsed) {
|
|
|
+ Integer update = userGalaxyCoinRecordMapper.update(null, Wrappers.lambdaUpdate(UserGalaxyCoinRecord.class)
|
|
|
+ .set(UserGalaxyCoinRecord::getRemainGalaxyCoin, remainGalaxyCoin.subtract(deductGalaxyCoin))
|
|
|
+ .set(isUsed, UserGalaxyCoinRecord::getIsUsed, isUsed)
|
|
|
+ .eq(UserGalaxyCoinRecord::getId, galaxyCoinRecordId)
|
|
|
+ .eq(UserGalaxyCoinRecord::getIsUsed, Boolean.FALSE)
|
|
|
+ .eq(UserGalaxyCoinRecord::getRemainGalaxyCoin, remainGalaxyCoin));
|
|
|
+ if (update == 0) {
|
|
|
+ throw BusinessRuntimeException.getInstance("银河币抵扣异常,请刷新页面重试");
|
|
|
+ }
|
|
|
+ }
|
|
|
}
|