|
@@ -0,0 +1,223 @@
|
|
|
|
|
+package com.zhongshu.card.server.core.service.pay;
|
|
|
|
|
+
|
|
|
|
|
+import com.github.microservice.components.data.base.page.PageableModel;
|
|
|
|
|
+import com.github.microservice.pay.client.model.ledger.GeneralLedgerTreeModel;
|
|
|
|
|
+import com.github.microservice.pay.client.model.ledger.transaction.*;
|
|
|
|
|
+import com.github.microservice.pay.client.ret.ResultContent;
|
|
|
|
|
+import com.github.microservice.pay.client.ret.ResultState;
|
|
|
|
|
+import com.github.microservice.pay.client.service.ledger.GeneralLedgerService;
|
|
|
|
|
+import com.github.microservice.pay.client.service.ledger.TransactionLogService;
|
|
|
|
|
+import com.github.microservice.pay.client.type.ledger.LedgerEntry;
|
|
|
|
|
+import com.github.microservice.types.payment.PaymentChannelType;
|
|
|
|
|
+import com.zhongshu.card.client.model.pay.BalanceOverviewModel;
|
|
|
|
|
+import com.zhongshu.card.client.model.pay.CollectBillQueryModel;
|
|
|
|
|
+import com.zhongshu.card.client.model.pay.RealTimeOverviewModel;
|
|
|
|
|
+import com.zhongshu.card.client.utils.DateUtils;
|
|
|
|
|
+import com.zhongshu.card.server.core.domain.pay.PayAccount;
|
|
|
|
|
+import com.zhongshu.card.server.core.service.base.SuperService;
|
|
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
|
|
+import org.apache.commons.lang3.StringUtils;
|
|
|
|
|
+import org.apache.commons.math3.stat.descriptive.summary.Sum;
|
|
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
|
|
+import org.springframework.data.domain.Pageable;
|
|
|
|
|
+import org.springframework.stereotype.Service;
|
|
|
|
|
+
|
|
|
|
|
+import java.util.ArrayList;
|
|
|
|
|
+import java.util.HashMap;
|
|
|
|
|
+import java.util.List;
|
|
|
|
|
+import java.util.Map;
|
|
|
|
|
+import java.util.stream.Collectors;
|
|
|
|
|
+
|
|
|
|
|
+@Service
|
|
|
|
|
+@Slf4j
|
|
|
|
|
+public class OrgOverviewService extends SuperService {
|
|
|
|
|
+
|
|
|
|
|
+ @Autowired
|
|
|
|
|
+ TransactionLogService transactionLogService;
|
|
|
|
|
+
|
|
|
|
|
+ @Autowired
|
|
|
|
|
+ GeneralLedgerService generalLedgerService;
|
|
|
|
|
+
|
|
|
|
|
+ @Autowired
|
|
|
|
|
+ PayAccountService payAccountService;
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 查询用户流水
|
|
|
|
|
+ */
|
|
|
|
|
+ public Object queryAccountTreeFlow(Pageable pageable, PayAccount payAccount, Long startTime, Long endTime){
|
|
|
|
|
+ ResultContent<GeneralLedgerTreeModel> descendantRet = generalLedgerService.descendant(payAccount.getLedgerId());
|
|
|
|
|
+ if (!descendantRet.getState().equals(ResultState.Success)){
|
|
|
|
|
+ return com.github.microservice.net.ResultContent.buildFail("获取用户账户及下级账户失败");
|
|
|
|
|
+ }
|
|
|
|
|
+// GeneralLedgerTreeModel generalLedgerTreeModel = descendantRet.getContent();
|
|
|
|
|
+// List<GeneralLedgerTreeModel> children = generalLedgerTreeModel.getChildren();
|
|
|
|
|
+// List<String> childrenIdList = children.stream().map(GeneralLedgerModel::getId).toList();
|
|
|
|
|
+
|
|
|
|
|
+ DescendantQueryTransactionLogModel descendantQueryTransactionLogModel = new DescendantQueryTransactionLogModel();
|
|
|
|
|
+ PageableModel pageableModel = new PageableModel();
|
|
|
|
|
+ pageableModel.setPage(pageable.getPageNumber());
|
|
|
|
|
+ pageableModel.setSize(pageable.getPageSize());
|
|
|
|
|
+ pageableModel.setSort("createTime");
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+ descendantQueryTransactionLogModel.setPage(pageableModel);
|
|
|
|
|
+ descendantQueryTransactionLogModel.setParentGeneralLedgerId(payAccount.getLedgerId());
|
|
|
|
|
+ descendantQueryTransactionLogModel.setStartTime(startTime);
|
|
|
|
|
+ descendantQueryTransactionLogModel.setEndTime(endTime);
|
|
|
|
|
+ return transactionLogService.aggregate(descendantQueryTransactionLogModel);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 统计机构账户余额、待结算金额、提现中金额
|
|
|
|
|
+ */
|
|
|
|
|
+ public BalanceOverviewModel balanceOverview(String projectOid, String oid){
|
|
|
|
|
+ if (StringUtils.isEmpty(projectOid)){
|
|
|
|
|
+ projectOid = getCurrentProjectOid();
|
|
|
|
|
+ }
|
|
|
|
|
+ if (StringUtils.isEmpty(oid)){
|
|
|
|
|
+ oid = getCurrentOid();
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ BalanceOverviewModel balanceOverviewModel = new BalanceOverviewModel();
|
|
|
|
|
+ PayAccount orgParent = payAccountService.getOrgParent(projectOid, oid);
|
|
|
|
|
+ ResultContent<GeneralLedgerTreeModel> descendantRet = generalLedgerService.descendant(orgParent.getLedgerId());
|
|
|
|
|
+ List<GeneralLedgerTreeModel> children = descendantRet.getContent().getChildren();
|
|
|
|
|
+ if (children!=null && !children.isEmpty()){
|
|
|
|
|
+ children.forEach(it->{
|
|
|
|
|
+ PaymentChannelType paymentChannelType = PaymentChannelType.valueOf(PaymentChannelType.class, (String) it.getMeta().get("paymentChannelType"));
|
|
|
|
|
+ if (paymentChannelType == PaymentChannelType.Settle){
|
|
|
|
|
+ balanceOverviewModel.setSettle(it.getBalance());
|
|
|
|
|
+ }else if (paymentChannelType == PaymentChannelType.WaitSettle){
|
|
|
|
|
+ balanceOverviewModel.setWaitSettle(it.getBalance());
|
|
|
|
|
+ }else if (paymentChannelType == PaymentChannelType.WithdrawFrozen){
|
|
|
|
|
+ balanceOverviewModel.setWithdrawFrozen(it.getBalance());
|
|
|
|
|
+ }
|
|
|
|
|
+ });
|
|
|
|
|
+ }
|
|
|
|
|
+ return balanceOverviewModel;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ public Object realTimeOverview(String projectOid, String oid){
|
|
|
|
|
+ if (StringUtils.isEmpty(projectOid)){
|
|
|
|
|
+ projectOid = getCurrentProjectOid();
|
|
|
|
|
+ }
|
|
|
|
|
+ if (StringUtils.isEmpty(oid)){
|
|
|
|
|
+ oid = getCurrentOid();
|
|
|
|
|
+ }
|
|
|
|
|
+ PayAccount waitSettleAccount = payAccountService.getOrgChildren(projectOid, oid, PaymentChannelType.WaitSettle);
|
|
|
|
|
+
|
|
|
|
|
+ //构建实时概况收入数据
|
|
|
|
|
+ List<Long> yesterdayList = new ArrayList<>();
|
|
|
|
|
+ List<Long> todayList = new ArrayList<>();
|
|
|
|
|
+
|
|
|
|
|
+ List<TransactionLogRealTimeAggregateRetModel> yesterday = realTimeAggregate(waitSettleAccount.getLedgerId(), DateUtils.getCurrentDayStartTime().getTime() - 24 * 60 * 60 * 1000,
|
|
|
|
|
+ DateUtils.getCurrentDayEndTime().getTime() - 24 * 60 * 60 * 1000,
|
|
|
|
|
+ Map.of("ledgerEntry", LedgerEntry.Credit));
|
|
|
|
|
+
|
|
|
|
|
+ List<TransactionLogRealTimeAggregateRetModel> today = realTimeAggregate(waitSettleAccount.getLedgerId(), DateUtils.getCurrentDayStartTime().getTime(),
|
|
|
|
|
+ DateUtils.getCurrentDayEndTime().getTime(),
|
|
|
|
|
+ Map.of("ledgerEntry", LedgerEntry.Credit));
|
|
|
|
|
+ List<String> currentIntegralPoint = DateUtils.getCurrentIntegralPoint("yyyy-MM-dd H");
|
|
|
|
|
+
|
|
|
|
|
+ buildRealTimeAmountList(yesterdayList, yesterday, currentIntegralPoint);
|
|
|
|
|
+ buildRealTimeAmountList(todayList, today, currentIntegralPoint);
|
|
|
|
|
+ Long todayAmount = todayList.stream().mapToLong(it -> {
|
|
|
|
|
+ return it;
|
|
|
|
|
+ }).sum();
|
|
|
|
|
+ Long yesterdayAmount = yesterdayList.stream().mapToLong(it -> {
|
|
|
|
|
+ return it;
|
|
|
|
|
+ }).sum();
|
|
|
|
|
+ //构建实时概况订单数据
|
|
|
|
|
+ List<Long> yesterdayOrderList = new ArrayList<>();
|
|
|
|
|
+ List<Long> todayOrderList = new ArrayList<>();
|
|
|
|
|
+
|
|
|
|
|
+ List<TransactionLogRealTimeAggregateRetModel> yesterdayGroupByOrderNumber = realTimeByOrderNumberAggregate(waitSettleAccount.getLedgerId(), DateUtils.getCurrentDayStartTime().getTime() - 24 * 60 * 60 * 1000,
|
|
|
|
|
+ DateUtils.getCurrentDayEndTime().getTime() - 24 * 60 * 60 * 1000,
|
|
|
|
|
+ Map.of("ledgerEntry", LedgerEntry.Credit));
|
|
|
|
|
+
|
|
|
|
|
+ List<TransactionLogRealTimeAggregateRetModel> todayGroupByOrderNumber = realTimeByOrderNumberAggregate(waitSettleAccount.getLedgerId(), DateUtils.getCurrentDayStartTime().getTime(),
|
|
|
|
|
+ DateUtils.getCurrentDayEndTime().getTime(),
|
|
|
|
|
+ Map.of("ledgerEntry", LedgerEntry.Credit));
|
|
|
|
|
+
|
|
|
|
|
+ buildRealTimeAmountList(yesterdayOrderList, yesterdayGroupByOrderNumber, currentIntegralPoint);
|
|
|
|
|
+ buildRealTimeAmountList(todayOrderList, todayGroupByOrderNumber, currentIntegralPoint);
|
|
|
|
|
+ Long todayOrder = todayOrderList.stream().mapToLong(it -> {
|
|
|
|
|
+ return it;
|
|
|
|
|
+ }).sum();
|
|
|
|
|
+ Long yesterdayOrder = yesterdayOrderList.stream().mapToLong(it -> {
|
|
|
|
|
+ return it;
|
|
|
|
|
+ }).sum();
|
|
|
|
|
+
|
|
|
|
|
+ List<String> retIntegralPoint = DateUtils.getCurrentIntegralPoint("HH:mm");
|
|
|
|
|
+ RealTimeOverviewModel realTimeOverviewModel = new RealTimeOverviewModel();
|
|
|
|
|
+ //收入数据
|
|
|
|
|
+ realTimeOverviewModel.setPoint(retIntegralPoint);
|
|
|
|
|
+ realTimeOverviewModel.setTodayAmount(todayAmount);
|
|
|
|
|
+ realTimeOverviewModel.setYesterdayAmount(yesterdayAmount);
|
|
|
|
|
+ realTimeOverviewModel.setTodayList(todayList);
|
|
|
|
|
+ realTimeOverviewModel.setYesterdayList(yesterdayList);
|
|
|
|
|
+ realTimeOverviewModel.setAmountChangeRate(changeRate(todayAmount, yesterdayAmount));
|
|
|
|
|
+ //订单数据
|
|
|
|
|
+ realTimeOverviewModel.setYesterdayOrderList(yesterdayOrderList);
|
|
|
|
|
+ realTimeOverviewModel.setTodayOrderList(todayOrderList);
|
|
|
|
|
+ realTimeOverviewModel.setYesterdayOrder(yesterdayOrder);
|
|
|
|
|
+ realTimeOverviewModel.setTodayOrder(todayOrder);
|
|
|
|
|
+ realTimeOverviewModel.setOrderChangeRate(changeRate(todayOrder, yesterdayOrder));
|
|
|
|
|
+
|
|
|
|
|
+ BalanceOverviewModel balanceOverviewModel = balanceOverview(projectOid, oid);
|
|
|
|
|
+ realTimeOverviewModel.setBalanceOverview(balanceOverviewModel);
|
|
|
|
|
+ //构建24小时大小的
|
|
|
|
|
+ return ResultContent.buildContent(realTimeOverviewModel);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+ private static Long changeRate(Long todayAmount, Long yesterdayAmount) {
|
|
|
|
|
+ Long amountChangeRate = null;
|
|
|
|
|
+ if (yesterdayAmount != 0L){
|
|
|
|
|
+ amountChangeRate = (todayAmount - yesterdayAmount) / yesterdayAmount;
|
|
|
|
|
+ }else {
|
|
|
|
|
+ amountChangeRate = todayAmount;
|
|
|
|
|
+ }
|
|
|
|
|
+ return amountChangeRate;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ private static void buildRealTimeAmountList(List<Long> amountList, List<TransactionLogRealTimeAggregateRetModel> modelList, List<String> currentIntegralPoint) {
|
|
|
|
|
+ Map<String, Long> yesterdayMap;
|
|
|
|
|
+ if (modelList !=null){
|
|
|
|
|
+ yesterdayMap = modelList.stream().collect(Collectors.toMap(TransactionLogRealTimeAggregateRetModel::getDateTime, TransactionLogRealTimeAggregateRetModel::getAmount));
|
|
|
|
|
+ for (String point : currentIntegralPoint) {
|
|
|
|
|
+ amountList.add(yesterdayMap.getOrDefault(point, 0L));
|
|
|
|
|
+ }
|
|
|
|
|
+ }else {
|
|
|
|
|
+ for (int i = 0; i < currentIntegralPoint.size(); i++) {
|
|
|
|
|
+ amountList.add(0L);
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ private List<TransactionLogRealTimeAggregateRetModel> realTimeAggregate(String ledgerId, Long startTime, Long endTime, Map<String, Object> filter) {
|
|
|
|
|
+ GeneralLedgerQueryTransactionLogModel queryModel = new GeneralLedgerQueryTransactionLogModel();
|
|
|
|
|
+ queryModel.setGeneralLedgerId(new String[]{ledgerId});
|
|
|
|
|
+ queryModel.setStartTime(startTime);
|
|
|
|
|
+ queryModel.setEndTime(endTime);
|
|
|
|
|
+ queryModel.setFilter(filter);
|
|
|
|
|
+ ResultContent<List<TransactionLogRealTimeAggregateRetModel>> realTimeAggregateRet = transactionLogService.realTimeAggregate(queryModel);
|
|
|
|
|
+ if (realTimeAggregateRet.getState().equals(ResultState.Success)){
|
|
|
|
|
+ return realTimeAggregateRet.getContent();
|
|
|
|
|
+ }
|
|
|
|
|
+ return null;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ private List<TransactionLogRealTimeAggregateRetModel> realTimeByOrderNumberAggregate(String ledgerId, Long startTime, Long endTime, Map<String, Object> filter) {
|
|
|
|
|
+ GeneralLedgerQueryTransactionLogModel queryModel = new GeneralLedgerQueryTransactionLogModel();
|
|
|
|
|
+ queryModel.setGeneralLedgerId(new String[]{ledgerId});
|
|
|
|
|
+ queryModel.setStartTime(startTime);
|
|
|
|
|
+ queryModel.setEndTime(endTime);
|
|
|
|
|
+ queryModel.setFilter(filter);
|
|
|
|
|
+ ResultContent<List<TransactionLogRealTimeAggregateRetModel>> realTimeAggregateRet = transactionLogService.realTimeByOrderNumberAggregate(queryModel);
|
|
|
|
|
+ if (realTimeAggregateRet.getState().equals(ResultState.Success)){
|
|
|
|
|
+ return realTimeAggregateRet.getContent();
|
|
|
|
|
+ }
|
|
|
|
|
+ return null;
|
|
|
|
|
+ }
|
|
|
|
|
+}
|