|
|
@@ -1,22 +1,42 @@
|
|
|
package com.zhongshu.card.server.core.service.pay;
|
|
|
|
|
|
import com.github.microservice.pay.client.model.ledger.GeneralLedgerQueryModel;
|
|
|
+import com.github.microservice.pay.client.model.ledger.TransactionLogModel;
|
|
|
+import com.github.microservice.pay.client.model.ledger.TransferTransactionsModel;
|
|
|
import com.github.microservice.pay.client.model.ledger.transaction.GeneralLedgerQueryTransactionLogModel;
|
|
|
import com.github.microservice.pay.client.model.ledger.transaction.TransactionLogAggregateRetModel;
|
|
|
import com.github.microservice.pay.client.ret.ResultContent;
|
|
|
import com.github.microservice.pay.client.ret.ResultState;
|
|
|
import com.github.microservice.pay.client.service.ledger.GeneralLedgerService;
|
|
|
import com.github.microservice.pay.client.service.ledger.TransactionLogService;
|
|
|
+import com.github.microservice.pay.client.type.ledger.TransactionStatus;
|
|
|
+import com.github.microservice.pay.client.type.ledger.TransactionType;
|
|
|
import com.github.microservice.types.payment.PaymentChannelType;
|
|
|
import com.github.microservice.types.payment.PaymentType;
|
|
|
+import com.zhongshu.card.client.model.pay.ProcessWithdrawParam;
|
|
|
+import com.zhongshu.card.client.model.pay.WithdrawParam;
|
|
|
+import com.zhongshu.card.client.model.paySetting.paySetting.OrgPayConfigModel;
|
|
|
+import com.zhongshu.card.client.service.org.UserAccountService;
|
|
|
+import com.zhongshu.card.client.type.WithdrawStatus;
|
|
|
+import com.zhongshu.card.client.type.paySetting.WithdrawMethodType;
|
|
|
+import com.zhongshu.card.server.core.dao.pay.WithdrawOrderDao;
|
|
|
+import com.zhongshu.card.server.core.domain.org.UserAccount;
|
|
|
import com.zhongshu.card.server.core.domain.pay.PayAccount;
|
|
|
+import com.zhongshu.card.server.core.domain.pay.WithdrawOrder;
|
|
|
+import com.zhongshu.card.server.core.domain.paySetting.ProjectMainPaySetting;
|
|
|
import com.zhongshu.card.server.core.service.base.SuperService;
|
|
|
+import com.zhongshu.card.server.core.service.paySetting.ProjectMainPaySettingService;
|
|
|
+import com.zhongshu.card.server.core.service.paySetting.ProjectPaySettingServiceImpl;
|
|
|
+import com.zhongshu.card.server.core.service.user.UserAccountServiceImpl;
|
|
|
+import com.zhongshu.card.server.core.util.CommonUtil;
|
|
|
import org.apache.commons.lang3.StringUtils;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
|
|
|
import java.awt.print.Pageable;
|
|
|
import java.math.BigDecimal;
|
|
|
+import java.math.RoundingMode;
|
|
|
+import java.util.List;
|
|
|
import java.util.Map;
|
|
|
|
|
|
@Service
|
|
|
@@ -31,6 +51,18 @@ public class WithdrawService extends SuperService {
|
|
|
@Autowired
|
|
|
GeneralLedgerService generalLedgerService;
|
|
|
|
|
|
+ @Autowired
|
|
|
+ ProjectMainPaySettingService projectMainPaySettingService;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ ProjectPaySettingServiceImpl projectPaySettingService;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ UserAccountServiceImpl userAccountService;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ WithdrawOrderDao withdrawOrderDao;
|
|
|
+
|
|
|
/**
|
|
|
* 查询可提现余额
|
|
|
*/
|
|
|
@@ -54,12 +86,10 @@ public class WithdrawService extends SuperService {
|
|
|
* 查询当前支付渠道可提现余额
|
|
|
*/
|
|
|
public com.github.microservice.net.ResultContent queryWithdrawAmount(String projectOid, String oid, PaymentType paymentType){
|
|
|
- //todo 统计当前支付渠道的收入与支出
|
|
|
Long amount = queryWithdrawAmountByPaymentType(projectOid, oid, paymentType);
|
|
|
if (null == amount){
|
|
|
return com.github.microservice.net.ResultContent.buildFail("查询余额失败");
|
|
|
}
|
|
|
- //todo 收入-支出 等于当前可提现余额
|
|
|
return com.github.microservice.net.ResultContent.buildContent(amount);
|
|
|
}
|
|
|
|
|
|
@@ -72,48 +102,183 @@ public class WithdrawService extends SuperService {
|
|
|
if (!aggregateRetModelResultContent.getState().equals(ResultState.Success)){
|
|
|
return null;
|
|
|
}
|
|
|
- long amount = aggregateRetModelResultContent.getContent().getStatistics().getCreditCount() - aggregateRetModelResultContent.getContent().getStatistics().getDebitCount();
|
|
|
- return amount;
|
|
|
+ return aggregateRetModelResultContent.getContent().getStatistics().getCreditCount() - aggregateRetModelResultContent.getContent().getStatistics().getDebitCount();
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 发起提现
|
|
|
*/
|
|
|
- public Object withdrawApply(String projectOid, String oid, PaymentType paymentType, BigDecimal amount){
|
|
|
- //TODO 校验提现金额是否大约可以提现金额
|
|
|
- Long waitAmount = queryWithdrawAmountByPaymentType(projectOid, oid, paymentType);
|
|
|
- //TODO 获取支付配置 为线上或者线下
|
|
|
+ public Object withdrawApply(WithdrawParam param){
|
|
|
+ String projectOid = param.getProjectOid();
|
|
|
+ String oid = param.getOid();
|
|
|
+ String userId = param.getUserId();
|
|
|
+ PaymentType paymentType = param.getPaymentType();
|
|
|
+ BigDecimal amount = param.getAmount();
|
|
|
+
|
|
|
+ if (StringUtils.isBlank(projectOid)){
|
|
|
+ projectOid = getCurrentProjectOid();
|
|
|
+ }
|
|
|
|
|
|
- //TODO 创建流程
|
|
|
+ if (StringUtils.isBlank(oid)){
|
|
|
+ oid = getCurrentOid();
|
|
|
+ }
|
|
|
|
|
|
- //TODO 支付中心划账:机构-已结算子账户- 机构-已冻结金额+
|
|
|
- return null;
|
|
|
+ if (StringUtils.isBlank(userId)){
|
|
|
+ userId = getCurrentUserId();
|
|
|
+ }
|
|
|
+
|
|
|
+ List<UserAccount> userAccounts = userAccountService.getUserAccounts(List.of(userId));
|
|
|
+ if (userAccounts==null || userAccounts.isEmpty()){
|
|
|
+ return com.github.microservice.net.ResultContent.buildFail("用户信息不存在");
|
|
|
+ }
|
|
|
+
|
|
|
+ //校验提现金额是否大约可以提现金额
|
|
|
+ Long waitAmount = queryWithdrawAmountByPaymentType(projectOid, oid, paymentType);
|
|
|
+ if (null == waitAmount){
|
|
|
+ return com.github.microservice.net.ResultContent.buildFail("当前可提现余额为0");
|
|
|
+ }
|
|
|
+ if (amount.compareTo(new BigDecimal(waitAmount))>0){
|
|
|
+ return com.github.microservice.net.ResultContent.buildFail("大于可提现金额");
|
|
|
+ }
|
|
|
+ //获取支付配置 为线上或者线下
|
|
|
+ ProjectMainPaySetting projectMainPaySetting = projectMainPaySettingService.getProjectMainPaySetting(projectOid, paymentType);
|
|
|
+ OrgPayConfigModel orgPayConfig = projectPaySettingService.getOrgPayConfig(projectOid, oid, paymentType);
|
|
|
+ if (null == orgPayConfig || null == projectMainPaySetting){
|
|
|
+ return com.github.microservice.net.ResultContent.buildFail("支付产品未配置完成");
|
|
|
+ }
|
|
|
+ //支付中心划账:机构-已结算子账户- 机构-已冻结金额+
|
|
|
+ PayAccount settleAccount = payAccountService.getOrgChildren(projectOid, oid, PaymentChannelType.Settle);
|
|
|
+ PayAccount frozenAccount = payAccountService.getOrgChildren(projectOid, oid, PaymentChannelType.WithdrawFrozen);
|
|
|
+ //构建转账参数
|
|
|
+ TransferTransactionsModel transferModel = new TransferTransactionsModel();
|
|
|
+ String withdrawOrderNo = CommonUtil.UUID();
|
|
|
+ //构建出账账户
|
|
|
+ TransferTransactionsModel.GeneralLedgerTransaction sourceTransaction = new TransferTransactionsModel.GeneralLedgerTransaction();
|
|
|
+ sourceTransaction.setGeneralLedgerId(settleAccount.getLedgerId());
|
|
|
+ sourceTransaction.setOrderNumber(withdrawOrderNo);
|
|
|
+ sourceTransaction.setTransactionType(TransactionType.WithdrawFrozen);
|
|
|
+ sourceTransaction.setTransactionStatus(TransactionStatus.Success);
|
|
|
+ sourceTransaction.setAmount(amount.negate().longValue());
|
|
|
+ sourceTransaction.setMeta(Map.of("paymentType", paymentType, "paymentChannelType", paymentType.getChannelType(), "description", "提现冻结"));
|
|
|
+ transferModel.setSource(new TransferTransactionsModel.GeneralLedgerTransaction[]{sourceTransaction});
|
|
|
+ //构建入账账户
|
|
|
+ TransferTransactionsModel.GeneralLedgerTransaction destinationTransaction = new TransferTransactionsModel.GeneralLedgerTransaction();
|
|
|
+ destinationTransaction.setGeneralLedgerId(frozenAccount.getLedgerId());
|
|
|
+ destinationTransaction.setOrderNumber(withdrawOrderNo);
|
|
|
+ destinationTransaction.setTransactionType(TransactionType.WithdrawFrozen);
|
|
|
+ destinationTransaction.setTransactionStatus(TransactionStatus.Success);
|
|
|
+ destinationTransaction.setAmount(amount.longValue());
|
|
|
+ destinationTransaction.setMeta(Map.of("paymentType", paymentType, "paymentChannelType", paymentType.getChannelType(), "description", "提现冻结"));
|
|
|
+ transferModel.setDestinations(new TransferTransactionsModel.GeneralLedgerTransaction[]{destinationTransaction});
|
|
|
+ ResultContent<List<TransactionLogModel>> transfer = transactionLogService.transfer(transferModel);
|
|
|
+ if (!transfer.getState().equals(ResultState.Success)){
|
|
|
+ return com.github.microservice.net.ResultContent.buildFail(transfer.getMsg());
|
|
|
+ }
|
|
|
+ //创建流程
|
|
|
+ WithdrawOrder withdrawOrder = new WithdrawOrder();
|
|
|
+ withdrawOrder.setWithdrawType(projectMainPaySetting.getWithdrawType());
|
|
|
+ withdrawOrder.setWithdrawMethodType(projectMainPaySetting.getWithdrawMethodType());
|
|
|
+ withdrawOrder.setBankAccountNumber(orgPayConfig.getBankSubName() + orgPayConfig.getBankName() + orgPayConfig.getBankAccountNumber());
|
|
|
+ withdrawOrder.setOrderNo(withdrawOrderNo);
|
|
|
+ withdrawOrder.setAmount(amount);
|
|
|
+ withdrawOrder.setWithdrawStatus(WithdrawStatus.Processing);
|
|
|
+ withdrawOrder.setWithdrawUser(userAccounts.get(0));
|
|
|
+ withdrawOrder.setFrozenTransactionLogs(transfer.getContent());
|
|
|
+ withdrawOrder.setProjectOid(projectOid);
|
|
|
+ withdrawOrder.setOid(oid);
|
|
|
+ withdrawOrder.setPaymentType(paymentType);
|
|
|
+ withdrawOrderDao.save(withdrawOrder);
|
|
|
+ return com.github.microservice.net.ResultContent.buildSuccess();
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 审核
|
|
|
*/
|
|
|
- public Object withdraw(String id){
|
|
|
+ public com.github.microservice.net.ResultContent withdraw(ProcessWithdrawParam param){
|
|
|
+ String userId = param.getUserId();
|
|
|
+ if (StringUtils.isBlank(userId)){
|
|
|
+ userId = getCurrentUserId();
|
|
|
+ }
|
|
|
+ List<UserAccount> userAccounts = userAccountService.getUserAccounts(List.of(userId));
|
|
|
+ if (userAccounts==null || userAccounts.isEmpty()){
|
|
|
+ return com.github.microservice.net.ResultContent.buildFail("用户信息不存在");
|
|
|
+ }
|
|
|
//TODO 判断权限?
|
|
|
|
|
|
- //TODO 获取提现订单数据
|
|
|
+ //获取提现订单数据
|
|
|
+ WithdrawOrder withdrawOrder = withdrawOrderDao.findTop1ById(param.getId());
|
|
|
+ if (withdrawOrder == null){
|
|
|
+ return com.github.microservice.net.ResultContent.buildFail("数据不存在");
|
|
|
+ }
|
|
|
+ //判断状态是否为待审核
|
|
|
+ if (!withdrawOrder.getWithdrawStatus().equals(WithdrawStatus.Processing)){
|
|
|
+ return com.github.microservice.net.ResultContent.buildFail("只能操作处理中的订单");
|
|
|
+ }
|
|
|
|
|
|
- //TODO 判断状态是否为待审核
|
|
|
+ PayAccount withdrawFrozenAccount = payAccountService.getOrgChildren(withdrawOrder.getProjectOid(), withdrawOrder.getOid(), PaymentChannelType.WithdrawFrozen);
|
|
|
+ PayAccount projectAccount = payAccountService.getProjectChildren(withdrawOrder.getProjectOid(), PaymentChannelType.SecretFreePayment);
|
|
|
+ PayAccount settleAccount = payAccountService.getOrgChildren(withdrawOrder.getProjectOid(), withdrawOrder.getOid(), PaymentChannelType.Settle);
|
|
|
|
|
|
//TODO 审批通过, 判断当前可提现余额是否大于提现订单金额
|
|
|
+ if (param.getStatus().equals(WithdrawStatus.Success)){//审批通过
|
|
|
|
|
|
- //TODO 判断是否线上提现,调用支付渠道对应的提现接口
|
|
|
-
|
|
|
- //TODO 划账,机构已冻结金额- 项目无感支付+
|
|
|
-
|
|
|
- //TODO 修改提现订单状态:提现处理中
|
|
|
+ //划账,机构已冻结金额- 项目无感支付+
|
|
|
+ TransferTransactionsModel transferTransactionsModel = buildTransferModel(projectAccount, withdrawFrozenAccount, withdrawOrder.getOrderNo(),
|
|
|
+ withdrawOrder.getAmount(), TransactionType.Withdrawal,
|
|
|
+ Map.of("paymentType", withdrawOrder.getPaymentType(), "paymentChannelType", withdrawOrder.getPaymentType().getChannelType(), "description", "提现"));
|
|
|
+ ResultContent<List<TransactionLogModel>> transferResult = transactionLogService.transfer(transferTransactionsModel);
|
|
|
+ if (!transferResult.getState().equals(ResultState.Success)){
|
|
|
+ return com.github.microservice.net.ResultContent.buildFail(transferResult.getMsg());
|
|
|
+ }
|
|
|
+ withdrawOrder.setWithdrawTransactionLogs(transferResult.getContent());
|
|
|
+ //TODO 判断是否线上提现,调用支付渠道对应的提现接口
|
|
|
+ if (withdrawOrder.getWithdrawMethodType().equals(WithdrawMethodType.OnLine)) {
|
|
|
|
|
|
- //TODO 审批拒绝
|
|
|
+ }
|
|
|
|
|
|
- //TODO 划账:机构已冻结金额- 机构已结算金额+
|
|
|
+ withdrawOrder.setWithdrawStatus(WithdrawStatus.Success);
|
|
|
+ withdrawOrder.setReceiveAmount(withdrawOrder.getAmount().movePointLeft(2));
|
|
|
+ }else if (param.getStatus().equals(WithdrawStatus.Fail)){ //审批拒绝
|
|
|
+ withdrawOrder.setWithdrawStatus(WithdrawStatus.Fail);
|
|
|
+ //划账退回金额: 机构已冻结金额- 机构已结算 +
|
|
|
+ TransferTransactionsModel transferTransactionsModel = buildTransferModel(settleAccount, withdrawFrozenAccount, withdrawOrder.getOrderNo(),
|
|
|
+ withdrawOrder.getAmount(), TransactionType.WithdrawRefund,
|
|
|
+ Map.of("paymentType", withdrawOrder.getPaymentType(), "paymentChannelType", withdrawOrder.getPaymentType().getChannelType(), "description", "提现退回"));
|
|
|
+ ResultContent<List<TransactionLogModel>> transfer = transactionLogService.transfer(transferTransactionsModel);
|
|
|
+ if (!transfer.getState().equals(ResultState.Success)){
|
|
|
+ return com.github.microservice.net.ResultContent.buildFail("余额退回失败");
|
|
|
+ }
|
|
|
+ withdrawOrder.setWithdrawTransactionLogs(transfer.getContent());
|
|
|
+ }
|
|
|
+ withdrawOrder.setAttachInfo(param.getAttachInfo());
|
|
|
+ withdrawOrder.setAttachFile(param.getAttachFile());
|
|
|
+ withdrawOrder.setProcessUser(userAccounts.get(0));
|
|
|
+ withdrawOrder.setProcessTime(System.currentTimeMillis());
|
|
|
+ withdrawOrderDao.save(withdrawOrder);
|
|
|
+ return com.github.microservice.net.ResultContent.buildSuccess();
|
|
|
+ }
|
|
|
|
|
|
- //TODO 修改提现订单状态:已拒绝
|
|
|
- return null;
|
|
|
+ private TransferTransactionsModel buildTransferModel(PayAccount credit, PayAccount debit, String orderNo, BigDecimal amount, TransactionType type, Map<String, Object> meta){
|
|
|
+ TransferTransactionsModel transferModel = new TransferTransactionsModel();
|
|
|
+ //构建出账账户
|
|
|
+ TransferTransactionsModel.GeneralLedgerTransaction sourceTransaction = new TransferTransactionsModel.GeneralLedgerTransaction();
|
|
|
+ sourceTransaction.setGeneralLedgerId(debit.getLedgerId());
|
|
|
+ sourceTransaction.setOrderNumber(orderNo);
|
|
|
+ sourceTransaction.setTransactionType(type);
|
|
|
+ sourceTransaction.setTransactionStatus(TransactionStatus.Success);
|
|
|
+ sourceTransaction.setAmount(amount.negate().longValue());
|
|
|
+ sourceTransaction.setMeta(meta);
|
|
|
+ transferModel.setSource(new TransferTransactionsModel.GeneralLedgerTransaction[]{sourceTransaction});
|
|
|
+ //构建入账账户
|
|
|
+ TransferTransactionsModel.GeneralLedgerTransaction destinationTransaction = new TransferTransactionsModel.GeneralLedgerTransaction();
|
|
|
+ destinationTransaction.setGeneralLedgerId(credit.getLedgerId());
|
|
|
+ destinationTransaction.setOrderNumber(orderNo);
|
|
|
+ destinationTransaction.setTransactionType(type);
|
|
|
+ destinationTransaction.setTransactionStatus(TransactionStatus.Success);
|
|
|
+ destinationTransaction.setAmount(amount.longValue());
|
|
|
+ destinationTransaction.setMeta(meta);
|
|
|
+ transferModel.setDestinations(new TransferTransactionsModel.GeneralLedgerTransaction[]{destinationTransaction});
|
|
|
+ return transferModel;
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
@@ -123,6 +288,17 @@ public class WithdrawService extends SuperService {
|
|
|
return null;
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * 查询详情
|
|
|
+ */
|
|
|
+ public Object queryDetailById(String id){
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+ public Object queryDetailByOrderNo(String orderNo){
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
/**
|
|
|
* 处理银联无感支付,提现结果通知回调
|
|
|
*/
|