|
@@ -2,16 +2,21 @@ package com.chelvc.cloud.vehicle.server.service.impl;
|
|
|
|
|
|
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
|
|
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
|
|
|
+import com.chelvc.cloud.vehicle.api.service.UserInviteService;
|
|
|
import com.chelvc.cloud.vehicle.server.dao.OmsOrderMapper;
|
|
|
import com.chelvc.cloud.vehicle.server.entity.OmsOrder;
|
|
|
-import com.chelvc.cloud.vehicle.server.service.PayService;
|
|
|
+import com.chelvc.cloud.vehicle.server.service.*;
|
|
|
+import com.chelvc.framework.base.exception.ResourceUnavailableException;
|
|
|
import lombok.RequiredArgsConstructor;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
+import org.springframework.transaction.annotation.Transactional;
|
|
|
|
|
|
import java.io.BufferedInputStream;
|
|
|
import java.io.FileInputStream;
|
|
|
import java.io.FileNotFoundException;
|
|
|
+import java.math.BigDecimal;
|
|
|
+import java.math.RoundingMode;
|
|
|
import java.nio.charset.StandardCharsets;
|
|
|
import java.security.KeyStore;
|
|
|
import java.security.PrivateKey;
|
|
@@ -19,14 +24,18 @@ import java.security.PublicKey;
|
|
|
import java.security.Signature;
|
|
|
import java.security.cert.CertificateFactory;
|
|
|
import java.security.cert.X509Certificate;
|
|
|
+import java.util.Date;
|
|
|
import java.util.Objects;
|
|
|
|
|
|
@Service
|
|
|
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
|
|
|
-public class PayServiceImpl implements PayService {
|
|
|
-
|
|
|
+public class OderHandleServiceImpl implements OrderHandleService {
|
|
|
private final OmsOrderMapper omsOrderMapper;
|
|
|
-
|
|
|
+ private final UserInviteService userInviteService;
|
|
|
+ private final UserProfitRatioService userProfitRatioService;
|
|
|
+ private final PlatformProfitRatioService platformProfitRatioService;
|
|
|
+ private final AssetService assetService;
|
|
|
+ private final BalanceDetailService balanceDetailService;
|
|
|
@Override
|
|
|
public void orderPay(Long orderId) {
|
|
|
LambdaQueryWrapper<OmsOrder> wrapper = Wrappers.lambdaQuery();
|
|
@@ -56,6 +65,79 @@ public class PayServiceImpl implements PayService {
|
|
|
sb.append("payResult=").append("10");
|
|
|
}
|
|
|
|
|
|
+ @Override
|
|
|
+ @Transactional(rollbackFor = Exception.class)
|
|
|
+ public void handleOrder(OmsOrder omsOrder) {
|
|
|
+ omsOrder.setStatus(4);
|
|
|
+ omsOrder.setConfirmStatus(1);
|
|
|
+ omsOrder.setReceiveTime(new Date());
|
|
|
+ omsOrder.setUpdateTime(new Date());
|
|
|
+ int i = omsOrderMapper.updateById(omsOrder);
|
|
|
+ if(i != 1){
|
|
|
+ throw new ResourceUnavailableException("处理失败");
|
|
|
+ }
|
|
|
+ //分佣逻辑=====================================
|
|
|
+ //实际支付金额
|
|
|
+ BigDecimal payAmount = omsOrder.getPayAmount();
|
|
|
+ Long userId = omsOrder.getUserId();
|
|
|
+ Long merchantId = omsOrder.getMerchantId();
|
|
|
+ BigDecimal userAmount = BigDecimal.ZERO;
|
|
|
+ BigDecimal merchantAmount = BigDecimal.ZERO;
|
|
|
+ //平台抽佣
|
|
|
+ int platformRatio = platformProfitRatioService.queryRatio(merchantId);
|
|
|
+ //平台抽佣金额----向上取整保留2位小数
|
|
|
+ BigDecimal platformAmount = new BigDecimal(platformRatio).divide(new BigDecimal("100"), 2, RoundingMode.UP)
|
|
|
+ .multiply(payAmount).setScale(2, RoundingMode.UP);
|
|
|
+ //用户抽佣----查看消费用户上级
|
|
|
+ Long inviteUserId = userInviteService.queryUserByTarget(userId, 1);
|
|
|
+ if(inviteUserId != null){
|
|
|
+ //用户分佣
|
|
|
+ int userRatio = userProfitRatioService.queryRatio(inviteUserId, 1);
|
|
|
+ if(userRatio > 0){
|
|
|
+ userAmount = new BigDecimal(userRatio).divide(new BigDecimal("100"), 2, RoundingMode.UP)
|
|
|
+ .multiply(platformAmount).setScale(2, RoundingMode.UP);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ //商户抽佣----查看消费用户上级
|
|
|
+ Long inviteMerchantId = userInviteService.queryUserByTarget(merchantId, 2);
|
|
|
+ if(inviteMerchantId != null){
|
|
|
+ //商户分佣
|
|
|
+ int merchantRatio = userProfitRatioService.queryRatio(inviteMerchantId, 2);
|
|
|
+ if(merchantRatio > 0){
|
|
|
+ merchantAmount = new BigDecimal(merchantRatio).divide(new BigDecimal("100"), 2, RoundingMode.UP)
|
|
|
+ .multiply(platformAmount).setScale(2, RoundingMode.UP);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ //商家应得金额
|
|
|
+ BigDecimal operateAmount = payAmount.subtract(platformAmount);
|
|
|
+ BigDecimal surplusAmount = assetService.updateAsset(0, operateAmount, merchantId, 2);
|
|
|
+ balanceDetailService.recordFlow(omsOrder.getId(), 2, merchantId, operateAmount, surplusAmount, 0, 1, userId);
|
|
|
+ BigDecimal platformSurplusAmount = assetService.updateAsset(1, operateAmount, 1L, 0);
|
|
|
+ balanceDetailService.recordFlow(omsOrder.getId(), 0, 1L, operateAmount, platformSurplusAmount, 1, 1, merchantId);
|
|
|
+ //邀请用户---分得佣金
|
|
|
+ if(userAmount.compareTo(BigDecimal.ZERO) > 0){
|
|
|
+ BigDecimal userSurplusAmount = assetService.updateAsset(0, userAmount, inviteUserId, 1);
|
|
|
+ balanceDetailService.recordFlow(omsOrder.getId(), 1, inviteUserId, userAmount, userSurplusAmount, 0, 1, userId);
|
|
|
+ BigDecimal platformSurplusAmount1 = assetService.updateAsset(1, userAmount, 1L, 0);
|
|
|
+ balanceDetailService.recordFlow(omsOrder.getId(), 0, 1L, userAmount, platformSurplusAmount1, 1, 1, inviteUserId);
|
|
|
+ }
|
|
|
+ //邀请商家---分得佣金
|
|
|
+ if(merchantAmount.compareTo(BigDecimal.ZERO) > 0){
|
|
|
+ BigDecimal merchantSurplusAmount2 = assetService.updateAsset(0, merchantAmount, inviteMerchantId, 1);
|
|
|
+ balanceDetailService.recordFlow(omsOrder.getId(), 1, inviteMerchantId, merchantAmount, merchantSurplusAmount2, 0, 1, merchantId);
|
|
|
+ BigDecimal platformSurplusAmount2 = assetService.updateAsset(1, merchantAmount, 1L, 0);
|
|
|
+ balanceDetailService.recordFlow(omsOrder.getId(), 0, 1L, merchantAmount, platformSurplusAmount2, 1, 1, inviteMerchantId);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ @Transactional(rollbackFor = Exception.class)
|
|
|
+ public void paySuccess(OmsOrder order) {
|
|
|
+ omsOrderMapper.updateById(order);
|
|
|
+ BigDecimal surplusAmount = assetService.updateAsset(0, order.getPayAmount(), 1L, 0);
|
|
|
+ balanceDetailService.recordFlow(order.getId(), 0, 1L, order.getPayAmount(), surplusAmount, 0, 1, order.getUserId());
|
|
|
+ }
|
|
|
+
|
|
|
public static void main(String[] args) {
|
|
|
|
|
|
StringBuilder sb = new StringBuilder();
|
|
@@ -183,7 +265,7 @@ public class PayServiceImpl implements PayService {
|
|
|
KeyStore ks = KeyStore.getInstance("PKCS12");
|
|
|
|
|
|
//读取密钥仓库(相对路径)
|
|
|
- String file = Objects.requireNonNull(PayServiceImpl.class.getClassLoader().getResource("payUtils/10012951683.pfx")).getPath().replaceAll("%20", " ");
|
|
|
+ String file = Objects.requireNonNull(OderHandleServiceImpl.class.getClassLoader().getResource("payUtils/10012951683.pfx")).getPath().replaceAll("%20", " ");
|
|
|
System.out.println(file);
|
|
|
|
|
|
FileInputStream ksfis = new FileInputStream(file);
|
|
@@ -213,7 +295,7 @@ public class PayServiceImpl implements PayService {
|
|
|
try {
|
|
|
//快钱公钥
|
|
|
//String file = Pkipair.class.getResource("99bill.RSA.cer").toURI().getPath();
|
|
|
- String file = Objects.requireNonNull(PayServiceImpl.class.getClassLoader().getResource("payUtils/CFCA_sandbox.cer")).toURI().getPath();
|
|
|
+ String file = Objects.requireNonNull(OderHandleServiceImpl.class.getClassLoader().getResource("payUtils/CFCA_sandbox.cer")).toURI().getPath();
|
|
|
//System.out.println(file);
|
|
|
FileInputStream inStream = new FileInputStream(file);
|
|
|
|