|
|
@@ -1,7 +1,114 @@
|
|
|
package modules.withdraw;
|
|
|
|
|
|
+import com.alibaba.fastjson.JSONObject;
|
|
|
+import com.jfinal.plugin.activerecord.Db;
|
|
|
+import common.jfinal.AppConfig;
|
|
|
+import common.model.User;
|
|
|
+import common.model.Withdraw;
|
|
|
+import common.utils.http.MyRet;
|
|
|
+import common.utils.hyg.HygSDK;
|
|
|
+import modules.order.OrderService;
|
|
|
+
|
|
|
public class WithdrawService {
|
|
|
+ // 提现手续费,750代表万分之750(7.5%)
|
|
|
+ public static final long WITHDRAW_FEE = 750;
|
|
|
+
|
|
|
public String hello() {
|
|
|
return "Hello Withdraw";
|
|
|
}
|
|
|
+
|
|
|
+ public MyRet create(long amount, User user) {
|
|
|
+ long amountWithoutFee = amount * (10000 - WITHDRAW_FEE) / 10000;
|
|
|
+
|
|
|
+ // 用户余额判断
|
|
|
+ if (user.getBalance() < (float)amount) {
|
|
|
+ return MyRet.fail(String.format("提现失败,余额不足,你仅有%s而提现%s", user.getBalance(), amount));
|
|
|
+ }
|
|
|
+
|
|
|
+ try {
|
|
|
+ String orderSn = "DLTBH_WD_" + OrderService.generateOrderSn();
|
|
|
+
|
|
|
+ Db.tx(() -> {
|
|
|
+ user.setBalance(user.getBalance() - (float)amount);
|
|
|
+
|
|
|
+ if (!user.update()) {
|
|
|
+ throw new RuntimeException("用户余额扣减失败");
|
|
|
+ }
|
|
|
+
|
|
|
+ // 保存提现单据
|
|
|
+ Withdraw withdraw = new Withdraw();
|
|
|
+ withdraw.setWithdrawSn(orderSn);
|
|
|
+ withdraw.setAmount((float)amount);
|
|
|
+ withdraw.setState(10);
|
|
|
+ withdraw.setUserId(user.getId());
|
|
|
+ withdraw.setCreateTime(System.currentTimeMillis());
|
|
|
+ withdraw.setUpdateTime(System.currentTimeMillis());
|
|
|
+ withdraw.setIsDeleted(0);
|
|
|
+
|
|
|
+ if (!withdraw.save()) {
|
|
|
+ throw new RuntimeException("提现单据保存失败");
|
|
|
+ }
|
|
|
+
|
|
|
+ return true;
|
|
|
+ });
|
|
|
+
|
|
|
+ float amountCNY = (float)amountWithoutFee / (float)100;
|
|
|
+ float feePercent = (float)WITHDRAW_FEE / (float)100;
|
|
|
+ return MyRet.ok(String.format("提现%s火花,实际到账%.2f(元),手续费%s%%,申请已提交", amount, amountCNY, feePercent));
|
|
|
+ } catch (Exception e) {
|
|
|
+ return MyRet.fail("提现申请失败: " + e.getMessage());
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ public MyRet pass(Withdraw withdraw, long approverId) {
|
|
|
+ // 记录操作人
|
|
|
+ withdraw.setApproverId(approverId);
|
|
|
+
|
|
|
+ try {
|
|
|
+ User user = User.dao.findById(withdraw.getUserId());
|
|
|
+ long amountWithoutFee = (long) (withdraw.getAmount() * (10000 - WITHDRAW_FEE) / 10000);
|
|
|
+
|
|
|
+ String workerId = user.getHygWorkerId();
|
|
|
+ String amountWithoutFeeStr = amountWithoutFee + "";
|
|
|
+ String hygPositionId = System.getenv("HYG_POSITION_ID");
|
|
|
+
|
|
|
+ AppConfig.LOGGER.info("{}, {}, {}, {}", workerId, amountWithoutFeeStr, withdraw.getWithdrawSn(), hygPositionId);
|
|
|
+ JSONObject withdrawRst = HygSDK.singleDistribute(workerId, amountWithoutFeeStr, withdraw.getWithdrawSn(), hygPositionId);
|
|
|
+
|
|
|
+ // 首先保证提现是成功的
|
|
|
+ if (!withdrawRst.getString("statusCode").equals("000000")) {
|
|
|
+ withdraw.setHygOrigin(withdrawRst.toString());
|
|
|
+ withdraw.setReason("慧用工提现失败");
|
|
|
+ withdraw.setState(40);
|
|
|
+
|
|
|
+ // 原子化操作user和withdraw的状态
|
|
|
+ Db.tx(() -> {
|
|
|
+ // 把款项退给用户
|
|
|
+ user.setBalance(user.getBalance() + withdraw.getAmount());
|
|
|
+ if (!user.update()) {
|
|
|
+ throw new RuntimeException("用户余额归还失败");
|
|
|
+ }
|
|
|
+
|
|
|
+ if (!withdraw.update()) {
|
|
|
+ throw new RuntimeException("单据状态更新失败");
|
|
|
+ }
|
|
|
+
|
|
|
+ return true;
|
|
|
+ });
|
|
|
+
|
|
|
+ return MyRet.fail("慧用工提现失败:" + withdrawRst.getString("statusText"));
|
|
|
+ }
|
|
|
+
|
|
|
+ // 如果是成功提现
|
|
|
+ withdraw.setHygOrigin(withdrawRst.toString());
|
|
|
+ withdraw.setState(20);
|
|
|
+ if (withdraw.update()) {
|
|
|
+ return MyRet.ok("打款请求已发起");
|
|
|
+ } else {
|
|
|
+ return MyRet.ok("单据状态更新失败");
|
|
|
+ }
|
|
|
+ } catch (Exception e) {
|
|
|
+ return MyRet.fail("打款请求已发起失败: " + e.getMessage());
|
|
|
+ }
|
|
|
+ }
|
|
|
}
|