|
|
@@ -1,20 +1,17 @@
|
|
|
package com.kym.service.awoara.event.handle;
|
|
|
|
|
|
+import com.kym.entity.*;
|
|
|
import com.kym.entity.awoara.MessageBody;
|
|
|
import com.kym.entity.awoara.OrderInfoObject;
|
|
|
-import com.kym.entity.Account;
|
|
|
-import com.kym.entity.WalletDetail;
|
|
|
-import com.kym.entity.WashOrder;
|
|
|
-import com.kym.service.AccountService;
|
|
|
-import com.kym.service.WalletDetailService;
|
|
|
-import com.kym.service.WashOrderService;
|
|
|
+import com.kym.service.*;
|
|
|
+import com.kym.service.cache.KymCache;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
import org.springframework.stereotype.Component;
|
|
|
import org.springframework.transaction.annotation.Transactional;
|
|
|
|
|
|
import java.time.Duration;
|
|
|
import java.time.LocalDateTime;
|
|
|
-import java.time.LocalTime;
|
|
|
+import java.util.List;
|
|
|
|
|
|
/**
|
|
|
* 关闭订单事件(订单结算)
|
|
|
@@ -27,11 +24,16 @@ public class OrderCloseEventHandler implements AwoaraEventHandler<OrderInfoObjec
|
|
|
private final WashOrderService washOrderService;
|
|
|
private final WalletDetailService walletDetailService;
|
|
|
private final AccountService accountService;
|
|
|
+ private final InvestorAccountService investorAccountService;
|
|
|
+ private final SplitRecordService splitRecordService;
|
|
|
|
|
|
- public OrderCloseEventHandler(WashOrderService washOrderService, WalletDetailService walletDetailService, AccountService accountService) {
|
|
|
+ public OrderCloseEventHandler(WashOrderService washOrderService, WalletDetailService walletDetailService,
|
|
|
+ AccountService accountService, InvestorAccountService investorAccountService, SplitRecordService splitRecordService) {
|
|
|
this.washOrderService = washOrderService;
|
|
|
this.walletDetailService = walletDetailService;
|
|
|
this.accountService = accountService;
|
|
|
+ this.investorAccountService = investorAccountService;
|
|
|
+ this.splitRecordService = splitRecordService;
|
|
|
}
|
|
|
|
|
|
|
|
|
@@ -66,6 +68,17 @@ public class OrderCloseEventHandler implements AwoaraEventHandler<OrderInfoObjec
|
|
|
.eq(Account::getUserId, washOrder.getUserId()).update();
|
|
|
}
|
|
|
|
|
|
+
|
|
|
+ // todo t_account减(上面已完成),t_investor_account冻结户减,t_investor_account商户加,t_split_record记录
|
|
|
+ // todo 需要判断是否跨网点,分开处理;跨网点结算比例是消费站点分订单额的30%,充值站点分70%
|
|
|
+ if (washOrder.getIsCross()) {
|
|
|
+ // 跨网点订单结算比例是消费站点分订单额的30%,充值站点分70%
|
|
|
+ doCrossSplit(washOrder, KymCache.INSTANCE.getInvestorAdminUserIdByUserId(washOrder.getUserId()));
|
|
|
+ } else {
|
|
|
+ // 不跨网点订单结算比例是消费站点分订单额的100%,消费金额*商家消费分润比例30%-消费金额*平台分润10%=消费金额*(商家消费分润比例-平台分润)
|
|
|
+ doLocalSplit(washOrder);
|
|
|
+ }
|
|
|
+
|
|
|
washOrder
|
|
|
.setCloseType(orderInfo.getClose_type())
|
|
|
.setAmount(orderInfo.getAmount())
|
|
|
@@ -91,9 +104,108 @@ public class OrderCloseEventHandler implements AwoaraEventHandler<OrderInfoObjec
|
|
|
walletDetail.setTransactionTime(LocalDateTime.now());
|
|
|
walletDetail.setStatus(WalletDetail.STATUS_已确认);
|
|
|
walletDetailService.save(walletDetail);
|
|
|
+
|
|
|
+
|
|
|
} else {
|
|
|
log.error("订单不存在,订单号:{}", orderInfo.getOrder_id());
|
|
|
}
|
|
|
|
|
|
}
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 执行(本店)分账操作
|
|
|
+ *
|
|
|
+ * @param washOrder
|
|
|
+ */
|
|
|
+ private void doLocalSplit(WashOrder washOrder) {
|
|
|
+ var investorAccount = investorAccountService.getInvestorAccount(washOrder.getStationId());
|
|
|
+ var amount = (int) (washOrder.getAmount() * (0.3 - 0.1));
|
|
|
+ investorAccountService.lambdaUpdate()
|
|
|
+ .setSql("frozen_amount = frozen_amount - {0}", amount)
|
|
|
+ .eq(InvestorAccount::getId, investorAccount.getId())
|
|
|
+ .update();
|
|
|
+ // 冻结户解冻
|
|
|
+ var splitRecord1 = new SplitRecord()
|
|
|
+ .setFromAccount(investorAccount.getAdminUserId())
|
|
|
+ .setToAccount(investorAccount.getAdminUserId())
|
|
|
+ .setTradeNo(washOrder.getOrderId())
|
|
|
+ .setAmount(amount)
|
|
|
+ .setType(SplitRecord.TYPE_UNFREEZE);
|
|
|
+
|
|
|
+ // 基本户入账
|
|
|
+ var splitRecord2 = new SplitRecord()
|
|
|
+ .setFromAccount(investorAccount.getAdminUserId())
|
|
|
+ .setToAccount(investorAccount.getAdminUserId())
|
|
|
+ .setTradeNo(washOrder.getOrderId())
|
|
|
+ .setAmount(amount)
|
|
|
+ .setType(SplitRecord.TYPE_CONSUME);
|
|
|
+
|
|
|
+ splitRecordService.saveBatch(List.of(splitRecord1, splitRecord2));
|
|
|
+
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 执行跨店分账操作
|
|
|
+ *
|
|
|
+ * @param washOrder
|
|
|
+ * @param InvestorAdminUserId 用户归属的站点的商户投资者adminUserId
|
|
|
+ */
|
|
|
+ private void doCrossSplit(WashOrder washOrder, Long InvestorAdminUserId) {
|
|
|
+ var investorAccount = investorAccountService.getInvestorAccount(washOrder.getStationId());
|
|
|
+ var splitAmount = (int) (washOrder.getAmount() * (0.3 - 0.1));
|
|
|
+ var localAmount = (int) (splitAmount * 0.7);
|
|
|
+ var crossAmount = splitAmount - localAmount;
|
|
|
+
|
|
|
+ // 当前消费站点收入(跨店消费,原充值站点要分订单的70%)
|
|
|
+ investorAccountService.lambdaUpdate()
|
|
|
+ .setSql("balance = balance + {0}", crossAmount)
|
|
|
+ .eq(InvestorAccount::getId, investorAccount.getId())
|
|
|
+ .update();
|
|
|
+
|
|
|
+// // 充值站点收入
|
|
|
+// investorAccountService.lambdaUpdate()
|
|
|
+// .setSql("balance = balance + {0}, frozen_amount = frozen_amount - {1}", localAmount, splitAmount)
|
|
|
+// .eq(InvestorAccount::getId, investorAccount.getId())
|
|
|
+// .update();
|
|
|
+//
|
|
|
+// // 充值站点支出
|
|
|
+// investorAccountService.lambdaUpdate()
|
|
|
+// .setSql("balance = balance - {0}", crossAmount)
|
|
|
+// .eq(InvestorAccount::getId, investorAccount.getId())
|
|
|
+// .update();
|
|
|
+
|
|
|
+ // 以上充值站点的两次操作进行合并
|
|
|
+ // 充值站点收入
|
|
|
+ investorAccountService.lambdaUpdate()
|
|
|
+ .setSql("balance = balance + {0}, frozen_amount = frozen_amount - {1}", localAmount - crossAmount, splitAmount)
|
|
|
+ .eq(InvestorAccount::getId, investorAccount.getId())
|
|
|
+ .update();
|
|
|
+
|
|
|
+
|
|
|
+ // t_split_record 1.消费站点收入 2.归属站点收入 3.充值站点支出
|
|
|
+ var splitRecord1 = new SplitRecord()
|
|
|
+ .setFromAccount(InvestorAdminUserId)
|
|
|
+ .setToAccount(investorAccount.getAdminUserId())
|
|
|
+ .setTradeNo(washOrder.getOrderId())
|
|
|
+ .setAmount(crossAmount)
|
|
|
+ .setType(SplitRecord.TYPE_CONSUME);
|
|
|
+
|
|
|
+ var splitRecord2 = new SplitRecord()
|
|
|
+ .setFromAccount(InvestorAdminUserId)
|
|
|
+ .setToAccount(InvestorAdminUserId)
|
|
|
+ .setTradeNo(washOrder.getOrderId())
|
|
|
+ .setAmount(localAmount)
|
|
|
+ .setType(SplitRecord.TYPE_CONSUME);
|
|
|
+
|
|
|
+ var splitRecord3 = new SplitRecord()
|
|
|
+ .setFromAccount(investorAccount.getAdminUserId())
|
|
|
+ .setToAccount(investorAccount.getAdminUserId())
|
|
|
+ .setTradeNo(washOrder.getOrderId())
|
|
|
+ .setAmount(splitAmount)
|
|
|
+ .setType(SplitRecord.TYPE_UNFREEZE);
|
|
|
+
|
|
|
+ splitRecordService.saveBatch(List.of(splitRecord1, splitRecord2, splitRecord3));
|
|
|
+
|
|
|
+ }
|
|
|
}
|