|
@@ -1,27 +1,35 @@
|
|
|
package com.chelvc.cloud.vehicle.server.service.impl;
|
|
|
|
|
|
+import com.alibaba.fastjson.JSONObject;
|
|
|
import com.alibaba.nacos.shaded.com.google.gson.Gson;
|
|
|
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
|
|
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
|
|
|
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
|
|
import com.chelvc.cloud.vehicle.client.constant.PlatformConstant;
|
|
|
+import com.chelvc.cloud.vehicle.client.constant.ReserveStatus;
|
|
|
import com.chelvc.cloud.vehicle.client.constant.TradeConstant;
|
|
|
import com.chelvc.cloud.vehicle.client.dto.*;
|
|
|
+import com.chelvc.cloud.vehicle.client.param.WithdrawNotifyParam;
|
|
|
import com.chelvc.cloud.vehicle.client.param.WithdrawParam;
|
|
|
import com.chelvc.cloud.vehicle.server.dao.AssetMapper;
|
|
|
import com.chelvc.cloud.vehicle.server.entity.Asset;
|
|
|
import com.chelvc.cloud.vehicle.server.entity.MerchantRelation;
|
|
|
+import com.chelvc.cloud.vehicle.server.entity.OmsWithdraw;
|
|
|
import com.chelvc.cloud.vehicle.server.handle.TradeHandle;
|
|
|
import com.chelvc.cloud.vehicle.server.service.*;
|
|
|
import com.chelvc.framework.base.context.SessionContextHolder;
|
|
|
import com.chelvc.framework.common.exception.ResourceUnavailableException;
|
|
|
+import com.chelvc.framework.common.function.Executor;
|
|
|
+import com.chelvc.framework.database.context.DatabaseContextHolder;
|
|
|
import lombok.RequiredArgsConstructor;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.context.annotation.Lazy;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
+import org.springframework.transaction.annotation.Transactional;
|
|
|
|
|
|
import java.math.BigDecimal;
|
|
|
import java.time.LocalDateTime;
|
|
|
+import java.util.Date;
|
|
|
import java.util.HashMap;
|
|
|
|
|
|
@Service
|
|
@@ -146,18 +154,65 @@ public class AssetServiceImpl extends ServiceImpl<AssetMapper, Asset> implements
|
|
|
if(merchantRelation == null){
|
|
|
throw new ResourceUnavailableException("请联系管理员开通商户号");
|
|
|
}
|
|
|
- //生成提现订单
|
|
|
- Long withdrawId = withdrawService.createWithdraw(userId, type, amount, 1);
|
|
|
+ Long finalUserId = userId;
|
|
|
+ DatabaseContextHolder.transactional((Executor) () -> {
|
|
|
+ //生成提现订单
|
|
|
+ Long withdrawId = withdrawService.createWithdraw(finalUserId, type, amount, 1);
|
|
|
+ String sxyMerchantId = merchantRelation.getSxyMerchantId();
|
|
|
+ //生成提现参数
|
|
|
+ HashMap<String, Object> map = new HashMap<>();
|
|
|
+ map.put("merchantId", sxyMerchantId);
|
|
|
+ map.put("requestId", withdrawId);
|
|
|
+ map.put("withdrawAmount", amount.multiply(new BigDecimal("100")));
|
|
|
+ map.put("notifyUrl", TradeConstant.WITHDRAW_NOTIFY_URL);
|
|
|
+ map.put("remark", "");
|
|
|
+ Gson gson = new Gson();
|
|
|
+ JSONObject json = TradeHandle.handle(gson.toJson(map),
|
|
|
+ TradeConstant.WITHDRAW_URL, sxyMerchantId, TradeConstant.PARTNER_ID);
|
|
|
+ String status = json.getString("status");
|
|
|
+ if(!"SUCCESS".equals(status)){
|
|
|
+ //提现申请失败
|
|
|
+ withdrawService.updateStatusById(withdrawId, 2);
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ //冻结金额
|
|
|
+ int i = baseMapper.updateFrozenAmount(amount, LocalDateTime.now(), finalUserId, type);
|
|
|
+ if(i != 1){
|
|
|
+ withdrawService.updateStatusById(withdrawId, 2);
|
|
|
+ throw new ResourceUnavailableException("提现申请失败");
|
|
|
+ }
|
|
|
+ });
|
|
|
+ }
|
|
|
|
|
|
- String sxyMerchantId = merchantRelation.getSxyMerchantId();
|
|
|
- //生成提现参数
|
|
|
- HashMap<String, Object> map = new HashMap<>();
|
|
|
- map.put("merchantId", sxyMerchantId);
|
|
|
- map.put("requestId", withdrawId);
|
|
|
- map.put("withdrawAmount", amount.multiply(new BigDecimal("100")));
|
|
|
- map.put("notifyUrl", TradeConstant.WITHDRAW_NOTIFY_URL);
|
|
|
- map.put("remark", "");
|
|
|
- Gson gson = new Gson();
|
|
|
- TradeHandle.handle(gson.toJson(map), TradeConstant.WITHDRAW_URL, sxyMerchantId, TradeConstant.PARTNER_ID);
|
|
|
+ @Override
|
|
|
+ @Transactional(rollbackFor = Exception.class)
|
|
|
+ public void withdrawNotify(WithdrawNotifyParam param) {
|
|
|
+ String requestId = param.getRequestId();
|
|
|
+ Long id = Long.parseLong(requestId);
|
|
|
+ OmsWithdraw withdraw = withdrawService.queryById(id);
|
|
|
+ if(withdraw == null){
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ String withdrawAmount = param.getWithdrawAmount();
|
|
|
+ BigDecimal amount = withdraw.getAmount();
|
|
|
+ if(amount.compareTo(new BigDecimal(withdrawAmount)) != 0){
|
|
|
+ //金额不一致
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ String withdrawStatus = param.getWithdrawStatus();
|
|
|
+ int status = 2;
|
|
|
+ if("SUCCESS".equals(withdrawStatus)){
|
|
|
+ status = 1;
|
|
|
+ }
|
|
|
+ //更新提现订单状态和外部订单号
|
|
|
+ int i = withdrawService.updateByNotify(id, param.getOrderId(), status);
|
|
|
+ if(i == 1){
|
|
|
+ if(status == 2){
|
|
|
+ //失败解冻金额
|
|
|
+ baseMapper.backFrozen(amount, withdraw.getUserId(), withdraw.getType(), LocalDateTime.now());
|
|
|
+ } else {
|
|
|
+ baseMapper.deductFrozen(amount, withdraw.getUserId(),withdraw.getType(), LocalDateTime.now());
|
|
|
+ }
|
|
|
+ }
|
|
|
}
|
|
|
}
|