|
@@ -1,38 +1,48 @@
|
|
|
package cn.iocoder.yudao.module.trade.service.brokerage;
|
|
|
|
|
|
import cn.hutool.core.collection.CollUtil;
|
|
|
-import cn.hutool.core.date.LocalDateTimeUtil;
|
|
|
-import cn.hutool.core.map.MapUtil;
|
|
|
import cn.hutool.core.util.ObjectUtil;
|
|
|
+import cn.iocoder.yudao.framework.common.enums.UserTypeEnum;
|
|
|
import cn.iocoder.yudao.framework.common.pojo.PageResult;
|
|
|
import cn.iocoder.yudao.framework.common.util.number.MoneyUtils;
|
|
|
+import cn.iocoder.yudao.module.pay.api.transfer.PayTransferApi;
|
|
|
+import cn.iocoder.yudao.module.pay.api.transfer.dto.PayTransferCreateReqDTO;
|
|
|
+import cn.iocoder.yudao.module.pay.api.transfer.dto.PayTransferRespDTO;
|
|
|
+import cn.iocoder.yudao.module.pay.api.wallet.PayWalletApi;
|
|
|
+import cn.iocoder.yudao.module.pay.api.wallet.dto.PayWalletAddBalanceReqDTO;
|
|
|
+import cn.iocoder.yudao.module.pay.enums.transfer.PayTransferStatusEnum;
|
|
|
+import cn.iocoder.yudao.module.pay.enums.transfer.PayTransferTypeEnum;
|
|
|
+import cn.iocoder.yudao.module.pay.enums.wallet.PayWalletBizTypeEnum;
|
|
|
import cn.iocoder.yudao.module.system.api.notify.NotifyMessageSendApi;
|
|
|
-import cn.iocoder.yudao.module.system.api.notify.dto.NotifySendSingleToUserReqDTO;
|
|
|
+import cn.iocoder.yudao.module.system.api.social.SocialUserApi;
|
|
|
+import cn.iocoder.yudao.module.system.api.social.dto.SocialUserRespDTO;
|
|
|
+import cn.iocoder.yudao.module.system.enums.social.SocialTypeEnum;
|
|
|
import cn.iocoder.yudao.module.trade.controller.admin.brokerage.vo.withdraw.BrokerageWithdrawPageReqVO;
|
|
|
import cn.iocoder.yudao.module.trade.controller.app.brokerage.vo.withdraw.AppBrokerageWithdrawCreateReqVO;
|
|
|
import cn.iocoder.yudao.module.trade.convert.brokerage.BrokerageWithdrawConvert;
|
|
|
import cn.iocoder.yudao.module.trade.dal.dataobject.brokerage.BrokerageWithdrawDO;
|
|
|
import cn.iocoder.yudao.module.trade.dal.dataobject.config.TradeConfigDO;
|
|
|
import cn.iocoder.yudao.module.trade.dal.mysql.brokerage.BrokerageWithdrawMapper;
|
|
|
-import cn.iocoder.yudao.module.trade.enums.MessageTemplateConstants;
|
|
|
import cn.iocoder.yudao.module.trade.enums.brokerage.BrokerageRecordBizTypeEnum;
|
|
|
import cn.iocoder.yudao.module.trade.enums.brokerage.BrokerageWithdrawStatusEnum;
|
|
|
import cn.iocoder.yudao.module.trade.enums.brokerage.BrokerageWithdrawTypeEnum;
|
|
|
+import cn.iocoder.yudao.module.trade.framework.order.config.TradeOrderProperties;
|
|
|
import cn.iocoder.yudao.module.trade.service.brokerage.bo.BrokerageWithdrawSummaryRespBO;
|
|
|
import cn.iocoder.yudao.module.trade.service.config.TradeConfigService;
|
|
|
+import jakarta.annotation.Resource;
|
|
|
+import jakarta.validation.Validator;
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
import org.springframework.transaction.annotation.Transactional;
|
|
|
import org.springframework.validation.annotation.Validated;
|
|
|
|
|
|
-import javax.annotation.Resource;
|
|
|
-import javax.validation.Validator;
|
|
|
import java.time.LocalDateTime;
|
|
|
import java.util.Collection;
|
|
|
import java.util.Collections;
|
|
|
import java.util.List;
|
|
|
-import java.util.Map;
|
|
|
|
|
|
import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
|
|
|
+import static cn.iocoder.yudao.framework.common.util.servlet.ServletUtils.getClientIP;
|
|
|
import static cn.iocoder.yudao.module.trade.enums.ErrorCodeConstants.*;
|
|
|
|
|
|
/**
|
|
@@ -42,6 +52,7 @@ import static cn.iocoder.yudao.module.trade.enums.ErrorCodeConstants.*;
|
|
|
*/
|
|
|
@Service
|
|
|
@Validated
|
|
|
+@Slf4j
|
|
|
public class BrokerageWithdrawServiceImpl implements BrokerageWithdrawService {
|
|
|
|
|
|
@Resource
|
|
@@ -54,13 +65,22 @@ public class BrokerageWithdrawServiceImpl implements BrokerageWithdrawService {
|
|
|
|
|
|
@Resource
|
|
|
private NotifyMessageSendApi notifyMessageSendApi;
|
|
|
+ @Resource
|
|
|
+ private PayTransferApi payTransferApi;
|
|
|
+ @Resource
|
|
|
+ private SocialUserApi socialUserApi;
|
|
|
+ @Resource
|
|
|
+ private PayWalletApi payWalletApi;
|
|
|
|
|
|
@Resource
|
|
|
private Validator validator;
|
|
|
|
|
|
+ @Resource
|
|
|
+ private TradeOrderProperties tradeOrderProperties;
|
|
|
+
|
|
|
@Override
|
|
|
@Transactional(rollbackFor = Exception.class)
|
|
|
- public void auditBrokerageWithdraw(Integer id, BrokerageWithdrawStatusEnum status, String auditReason) {
|
|
|
+ public void auditBrokerageWithdraw(Long id, BrokerageWithdrawStatusEnum status, String auditReason, String userIp) {
|
|
|
// 1.1 校验存在
|
|
|
BrokerageWithdrawDO withdraw = validateBrokerageWithdrawExists(id);
|
|
|
// 1.2 校验状态为审核中
|
|
@@ -68,41 +88,67 @@ public class BrokerageWithdrawServiceImpl implements BrokerageWithdrawService {
|
|
|
throw exception(BROKERAGE_WITHDRAW_STATUS_NOT_AUDITING);
|
|
|
}
|
|
|
|
|
|
- // 2. 更新
|
|
|
+ // 2. 更新状态
|
|
|
int rows = brokerageWithdrawMapper.updateByIdAndStatus(id, BrokerageWithdrawStatusEnum.AUDITING.getStatus(),
|
|
|
new BrokerageWithdrawDO().setStatus(status.getStatus()).setAuditReason(auditReason).setAuditTime(LocalDateTime.now()));
|
|
|
if (rows == 0) {
|
|
|
throw exception(BROKERAGE_WITHDRAW_STATUS_NOT_AUDITING);
|
|
|
}
|
|
|
|
|
|
- String templateCode;
|
|
|
+ // 3.1 审批通过的后续处理
|
|
|
if (BrokerageWithdrawStatusEnum.AUDIT_SUCCESS.equals(status)) {
|
|
|
- templateCode = MessageTemplateConstants.SMS_BROKERAGE_WITHDRAW_AUDIT_APPROVE;
|
|
|
- // 3.1 通过时佣金转余额
|
|
|
- if (BrokerageWithdrawTypeEnum.WALLET.getType().equals(withdraw.getType())) {
|
|
|
- // todo 疯狂:
|
|
|
- }
|
|
|
- // TODO 疯狂:调用转账接口
|
|
|
+ auditBrokerageWithdrawSuccess(withdraw);
|
|
|
+ // 3.2 审批不通过的后续处理
|
|
|
} else if (BrokerageWithdrawStatusEnum.AUDIT_FAIL.equals(status)) {
|
|
|
- templateCode = MessageTemplateConstants.SMS_BROKERAGE_WITHDRAW_AUDIT_REJECT;
|
|
|
- // 3.2 驳回时需要退还用户佣金
|
|
|
brokerageRecordService.addBrokerage(withdraw.getUserId(), BrokerageRecordBizTypeEnum.WITHDRAW_REJECT,
|
|
|
String.valueOf(withdraw.getId()), withdraw.getPrice(), BrokerageRecordBizTypeEnum.WITHDRAW_REJECT.getTitle());
|
|
|
} else {
|
|
|
throw new IllegalArgumentException("不支持的提现状态:" + status);
|
|
|
}
|
|
|
+ }
|
|
|
|
|
|
- // 4. 通知用户
|
|
|
- Map<String, Object> templateParams = MapUtil.<String, Object>builder()
|
|
|
- .put("createTime", LocalDateTimeUtil.formatNormal(withdraw.getCreateTime()))
|
|
|
- .put("price", MoneyUtils.fenToYuanStr(withdraw.getPrice()))
|
|
|
- .put("reason", auditReason)
|
|
|
- .build();
|
|
|
- notifyMessageSendApi.sendSingleMessageToMember(new NotifySendSingleToUserReqDTO()
|
|
|
- .setUserId(withdraw.getUserId()).setTemplateCode(templateCode).setTemplateParams(templateParams));
|
|
|
+ private void auditBrokerageWithdrawSuccess(BrokerageWithdrawDO withdraw) {
|
|
|
+ // 1.1 钱包
|
|
|
+ if (BrokerageWithdrawTypeEnum.WALLET.getType().equals(withdraw.getType())) {
|
|
|
+ payWalletApi.addWalletBalance(new PayWalletAddBalanceReqDTO()
|
|
|
+ .setUserId(withdraw.getUserId()).setUserType(UserTypeEnum.MEMBER.getValue())
|
|
|
+ .setBizType(PayWalletBizTypeEnum.BROKERAGE_WITHDRAW.getType()).setBizId(withdraw.getId().toString())
|
|
|
+ .setPrice(withdraw.getPrice()));
|
|
|
+ // 1.2 微信 API
|
|
|
+ } else if (BrokerageWithdrawTypeEnum.WECHAT_API.getType().equals(withdraw.getType())) {
|
|
|
+ // TODO @luchi:这里,要加个转账单号的记录;另外,调用 API 转账,是立马成功,还是有延迟的哈?
|
|
|
+ Long payTransferId = createPayTransfer(withdraw);
|
|
|
+ // 1.3 剩余类型,都是手动打款,所以不处理
|
|
|
+ } else {
|
|
|
+ // TODO 可优化:未来可以考虑,接入支付宝、银联等 API 转账,实现自动打款
|
|
|
+ log.info("[auditBrokerageWithdrawSuccess][withdraw({}) 类型({}) 手动打款,无需处理]", withdraw.getId(), withdraw.getType());
|
|
|
+ }
|
|
|
+
|
|
|
+ // 2. 非支付 API,则直接体现成功
|
|
|
+ if (!BrokerageWithdrawTypeEnum.isApi(withdraw.getType())) {
|
|
|
+ brokerageWithdrawMapper.updateByIdAndStatus(withdraw.getId(), BrokerageWithdrawStatusEnum.AUDIT_SUCCESS.getStatus(),
|
|
|
+ new BrokerageWithdrawDO().setStatus(BrokerageWithdrawStatusEnum.WITHDRAW_SUCCESS.getStatus()));
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
- private BrokerageWithdrawDO validateBrokerageWithdrawExists(Integer id) {
|
|
|
+ private Long createPayTransfer(BrokerageWithdrawDO withdraw) {
|
|
|
+ // 1.1 获取微信 openid
|
|
|
+ SocialUserRespDTO socialUser = socialUserApi.getSocialUserByUserId(
|
|
|
+ UserTypeEnum.MEMBER.getValue(), withdraw.getUserId(), SocialTypeEnum.WECHAT_MINI_APP.getType());
|
|
|
+ // TODO @luchi:这里,需要校验非空。如果空的话,要有业务异常哈;
|
|
|
+ // 1.2 构建请求
|
|
|
+ PayTransferCreateReqDTO payTransferCreateReqDTO = new PayTransferCreateReqDTO()
|
|
|
+ .setAppKey(tradeOrderProperties.getPayAppKey())
|
|
|
+ .setChannelCode("wx_lite").setType(PayTransferTypeEnum.WX_BALANCE.getType())
|
|
|
+ .setMerchantTransferId(withdraw.getId().toString())
|
|
|
+ .setPrice(withdraw.getPrice())
|
|
|
+ .setSubject("佣金提现")
|
|
|
+ .setOpenid(socialUser.getOpenid()).setUserIp(getClientIP());
|
|
|
+ // 2. 发起请求
|
|
|
+ return payTransferApi.createTransfer(payTransferCreateReqDTO);
|
|
|
+ }
|
|
|
+
|
|
|
+ private BrokerageWithdrawDO validateBrokerageWithdrawExists(Long id) {
|
|
|
BrokerageWithdrawDO withdraw = brokerageWithdrawMapper.selectById(id);
|
|
|
if (withdraw == null) {
|
|
|
throw exception(BROKERAGE_WITHDRAW_NOT_EXISTS);
|
|
@@ -111,7 +157,7 @@ public class BrokerageWithdrawServiceImpl implements BrokerageWithdrawService {
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
- public BrokerageWithdrawDO getBrokerageWithdraw(Integer id) {
|
|
|
+ public BrokerageWithdrawDO getBrokerageWithdraw(Long id) {
|
|
|
return brokerageWithdrawMapper.selectById(id);
|
|
|
}
|
|
|
|
|
@@ -141,15 +187,6 @@ public class BrokerageWithdrawServiceImpl implements BrokerageWithdrawService {
|
|
|
return withdraw.getId();
|
|
|
}
|
|
|
|
|
|
- @Override
|
|
|
- public List<BrokerageWithdrawSummaryRespBO> getWithdrawSummaryListByUserId(Collection<Long> userIds,
|
|
|
- BrokerageWithdrawStatusEnum status) {
|
|
|
- if (CollUtil.isEmpty(userIds)) {
|
|
|
- return Collections.emptyList();
|
|
|
- }
|
|
|
- return brokerageWithdrawMapper.selectCountAndSumPriceByUserIdAndStatus(userIds, status.getStatus());
|
|
|
- }
|
|
|
-
|
|
|
/**
|
|
|
* 计算提现手续费
|
|
|
*
|
|
@@ -157,7 +194,7 @@ public class BrokerageWithdrawServiceImpl implements BrokerageWithdrawService {
|
|
|
* @param percent 手续费百分比
|
|
|
* @return 提现手续费
|
|
|
*/
|
|
|
- Integer calculateFeePrice(Integer withdrawPrice, Integer percent) {
|
|
|
+ private Integer calculateFeePrice(Integer withdrawPrice, Integer percent) {
|
|
|
Integer feePrice = 0;
|
|
|
if (percent != null && percent > 0) {
|
|
|
feePrice = MoneyUtils.calculateRatePrice(withdrawPrice, Double.valueOf(percent));
|
|
@@ -171,11 +208,42 @@ public class BrokerageWithdrawServiceImpl implements BrokerageWithdrawService {
|
|
|
* @param withdrawPrice 提现金额
|
|
|
* @return 分销配置
|
|
|
*/
|
|
|
- TradeConfigDO validateWithdrawPrice(Integer withdrawPrice) {
|
|
|
+ private TradeConfigDO validateWithdrawPrice(Integer withdrawPrice) {
|
|
|
TradeConfigDO tradeConfig = tradeConfigService.getTradeConfig();
|
|
|
if (tradeConfig.getBrokerageWithdrawMinPrice() != null && withdrawPrice < tradeConfig.getBrokerageWithdrawMinPrice()) {
|
|
|
throw exception(BROKERAGE_WITHDRAW_MIN_PRICE, MoneyUtils.fenToYuanStr(tradeConfig.getBrokerageWithdrawMinPrice()));
|
|
|
}
|
|
|
return tradeConfig;
|
|
|
}
|
|
|
+
|
|
|
+ @Override
|
|
|
+ @Transactional(rollbackFor = Exception.class)
|
|
|
+ public void updateBrokerageWithdrawTransferred(Long id, Long payTransferId) {
|
|
|
+ BrokerageWithdrawDO withdraw = validateBrokerageWithdrawExists(id);
|
|
|
+ PayTransferRespDTO transfer = payTransferApi.getTransfer(payTransferId);
|
|
|
+ // TODO @luchi:建议参考支付那,即使成功的情况下,也要各种校验;金额是否匹配、转账单号是否匹配、是否重复调用;
|
|
|
+ if (PayTransferStatusEnum.isSuccess(transfer.getStatus())) {
|
|
|
+ withdraw.setStatus(BrokerageWithdrawStatusEnum.WITHDRAW_SUCCESS.getStatus());
|
|
|
+ // TODO @luchi:发送站内信
|
|
|
+ } else if (PayTransferStatusEnum.isPendingStatus(transfer.getStatus())) {
|
|
|
+ // TODO @luchi:这里,是不是不用更新哈?
|
|
|
+ withdraw.setStatus(BrokerageWithdrawStatusEnum.AUDIT_SUCCESS.getStatus());
|
|
|
+ } else {
|
|
|
+ withdraw.setStatus(BrokerageWithdrawStatusEnum.WITHDRAW_FAIL.getStatus());
|
|
|
+ // 3.2 驳回时需要退还用户佣金
|
|
|
+ brokerageRecordService.addBrokerage(withdraw.getUserId(), BrokerageRecordBizTypeEnum.WITHDRAW_REJECT,
|
|
|
+ String.valueOf(withdraw.getId()), withdraw.getPrice(), BrokerageRecordBizTypeEnum.WITHDRAW_REJECT.getTitle());
|
|
|
+ }
|
|
|
+ brokerageWithdrawMapper.updateById(withdraw);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public List<BrokerageWithdrawSummaryRespBO> getWithdrawSummaryListByUserId(Collection<Long> userIds,
|
|
|
+ BrokerageWithdrawStatusEnum status) {
|
|
|
+ if (CollUtil.isEmpty(userIds)) {
|
|
|
+ return Collections.emptyList();
|
|
|
+ }
|
|
|
+ return brokerageWithdrawMapper.selectCountAndSumPriceByUserIdAndStatus(userIds, status.getStatus());
|
|
|
+ }
|
|
|
+
|
|
|
}
|