|
|
@@ -1,8 +1,10 @@
|
|
|
package com.zhongshu.card.server.core.service.pay;
|
|
|
|
|
|
import com.github.microservice.components.data.base.page.PageableModel;
|
|
|
+import com.github.microservice.pay.client.model.UpdateMetaFieldModel;
|
|
|
import com.github.microservice.pay.client.model.ledger.*;
|
|
|
import com.github.microservice.pay.client.model.ledger.transaction.GeneralLedgerQueryTransactionLogModel;
|
|
|
+import com.github.microservice.pay.client.model.ledger.transaction.TransactionLogAggregateNotPageRetModel;
|
|
|
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;
|
|
|
@@ -17,6 +19,7 @@ import com.zhongshu.card.server.core.util.CommonUtil;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
import org.bson.Document;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.data.domain.Pageable;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
import org.springframework.transaction.annotation.Transactional;
|
|
|
|
|
|
@@ -42,41 +45,47 @@ public class SettleService {
|
|
|
PayAccount waitSettleAccount = payAccountService.getOrgChildren(projectOid, oid, PaymentChannelType.WaitSettle);
|
|
|
//查询时间内所有待结算账单
|
|
|
GeneralLedgerQueryTransactionLogModel queryModel = new GeneralLedgerQueryTransactionLogModel();
|
|
|
- PageableModel pageableModel = new PageableModel();
|
|
|
- pageableModel.setPage(0);
|
|
|
- pageableModel.setSize(1000000);
|
|
|
- queryModel.setPage(pageableModel);
|
|
|
// queryModel.setStartTime(startTime);
|
|
|
queryModel.setEndTime(endTime);
|
|
|
queryModel.setGeneralLedgerId(new String[]{waitSettleAccount.getLedgerId()});
|
|
|
//过滤出对应支付渠道的账单
|
|
|
- queryModel.setFilter(Map.of("meta.paymentType", paymentType, "meta.settleStatus", SettleStatus.WaitSettle));
|
|
|
-// queryModel.setFilter(Map.of("meta.paymentType", paymentType, "$or", Arrays.asList(
|
|
|
-// new Document()
|
|
|
-// .append("meta.settleStatus", new Document()
|
|
|
-// .append("$exists", false)
|
|
|
-// ),
|
|
|
-// new Document()
|
|
|
-// .append("meta.settleStatus", "WaitSettle")
|
|
|
-// )));
|
|
|
- ResultContent<TransactionLogAggregateRetModel> aggregateResult = transactionLogService.aggregate(queryModel);
|
|
|
+// queryModel.setFilter(Map.of("meta.paymentType", paymentType));
|
|
|
+ queryModel.setFilter(Map.of("meta.paymentType", paymentType, "$or", Arrays.asList(
|
|
|
+ new Document()
|
|
|
+ .append("meta.settleStatus", new Document()
|
|
|
+ .append("$exists", false)
|
|
|
+ ),
|
|
|
+ new Document()
|
|
|
+ .append("meta.settleStatus", "WaitSettle")
|
|
|
+ )));
|
|
|
+ ResultContent<TransactionLogAggregateNotPageRetModel> aggregateResult = transactionLogService.aggregateNotPage(queryModel);
|
|
|
+
|
|
|
+
|
|
|
if (!aggregateResult.getState().equals(ResultState.Success)){
|
|
|
log.info("资金结算:查询待结算账单失败");
|
|
|
- return aggregateResult;
|
|
|
+ throw new RuntimeException(aggregateResult.getMsg());
|
|
|
}
|
|
|
- if (aggregateResult.getContent().getPages().getContent().isEmpty()){
|
|
|
+ if (aggregateResult.getContent().getList().isEmpty()){
|
|
|
return ResultContent.buildContent(new HashSet<>());
|
|
|
}
|
|
|
- List<TransactionLogModel> transactionLogModelList = aggregateResult.getContent().getPages().getContent();
|
|
|
+ List<TransactionLogModel> transactionLogModelList = aggregateResult.getContent().getList();
|
|
|
Set<String> orderNoSet = new HashSet<>();
|
|
|
List<String> settleList = transactionLogModelList.stream().map(it -> {
|
|
|
orderNoSet.add(it.getOrderNumber());
|
|
|
return it.getId();
|
|
|
}).toList();
|
|
|
|
|
|
+ log.info("结算账单集合:{}", transactionLogModelList);
|
|
|
+ log.info("结算订单集合:{}", orderNoSet);
|
|
|
+
|
|
|
|
|
|
//收入-支出=可提现金额
|
|
|
- long settleAmount = aggregateResult.getContent().getStatistics().getCreditCount() - aggregateResult.getContent().getStatistics().getDebitCount();
|
|
|
+ long settleAmount = aggregateResult.getContent().getStatistics().getCreditCount() + aggregateResult.getContent().getStatistics().getDebitCount();
|
|
|
+
|
|
|
+ if (settleAmount<=0L){
|
|
|
+ log.info("该账户可结算金额小于或等于0, 账户:{},可提现金额:{}, 时间{}", waitSettleAccount.getLedgerId(), settleAmount, endTime);
|
|
|
+ throw new RuntimeException("该账户可结算金额小于或等于0");
|
|
|
+ }
|
|
|
//转账 机构待结算子账户- 机构待提现子账户+
|
|
|
PayAccount settleAccount = payAccountService.getOrgChildren(projectOid, oid, PaymentChannelType.Settle);
|
|
|
|
|
|
@@ -105,21 +114,18 @@ public class SettleService {
|
|
|
transferModel.setDestinations(new TransferTransactionsModel.GeneralLedgerTransaction[]{destinationTransaction});
|
|
|
ResultContent<List<TransactionLogModel>> transfer = transactionLogService.transfer(transferModel);
|
|
|
|
|
|
- List<TransactionLogModel> content = aggregateResult.getContent().getPages().getContent();
|
|
|
-
|
|
|
- if (transfer.getState().equals(ResultState.Success)){
|
|
|
- content.forEach(it -> {
|
|
|
- UpdateTransactionLogModel updateTransactionLogModel = new UpdateTransactionLogModel();
|
|
|
- Map<String, Object> meta = it.getMeta();
|
|
|
- meta.put("settleStatus", SettleStatus.Settle);
|
|
|
- updateTransactionLogModel.setMeta(meta);
|
|
|
- updateTransactionLogModel.setId(it.getId());
|
|
|
- transactionLogService.update(updateTransactionLogModel);
|
|
|
-// orderNoSet.add(it.getOrderNumber());
|
|
|
- });
|
|
|
- return ResultContent.buildContent(orderNoSet);
|
|
|
+ if (!transfer.getState().equals(ResultState.Success)){
|
|
|
+ log.error("结算划账失败, projectOid: {}, paymentType: {}", projectOid, paymentType);
|
|
|
+ throw new RuntimeException(transfer.getMsg());
|
|
|
+ }
|
|
|
+ UpdateMetaFieldModel updateMetaFieldModel = new UpdateMetaFieldModel();
|
|
|
+ updateMetaFieldModel.setIds(settleList);
|
|
|
+ updateMetaFieldModel.setMetaField(Map.of("settleStatus", SettleStatus.Settle));
|
|
|
+ ResultContent<Void> updateManyMetaField = transactionLogService.updateManyMetaField(updateMetaFieldModel);
|
|
|
+ if (!updateManyMetaField.getState().equals(ResultState.Success)){
|
|
|
+ log.error("修改账单meta字段失败, projectOid: {}, paymentType: {}", projectOid, paymentType);
|
|
|
+ throw new RuntimeException(updateManyMetaField.getMsg());
|
|
|
}
|
|
|
- log.error("结算划账失败, projectOid: {}, paymentType: {}", projectOid, paymentType);
|
|
|
- return transfer;
|
|
|
+ return ResultContent.buildContent(orderNoSet);
|
|
|
}
|
|
|
}
|