ソースを参照

对接账单查询

wujiefeng 1 年間 前
コミット
92bd975de8

+ 17 - 0
FullCardClient/src/main/java/com/zhongshu/card/client/model/pay/BalanceOverviewModel.java

@@ -0,0 +1,17 @@
+package com.zhongshu.card.client.model.pay;
+
+import io.swagger.v3.oas.annotations.media.Schema;
+import lombok.Data;
+
+@Data
+public class BalanceOverviewModel {
+
+    @Schema(description = "账户余额/已结算金额")
+    private long settle;
+
+    @Schema(description = "待结算金额")
+    private long waitSettle;
+
+    @Schema(description = "提现中金额/提现冻结金额")
+    private long withdrawFrozen;
+}

+ 9 - 0
FullCardClient/src/main/java/com/zhongshu/card/client/model/pay/CollectBillQueryModel.java

@@ -0,0 +1,9 @@
+package com.zhongshu.card.client.model.pay;
+
+public class CollectBillQueryModel {
+
+    private Integer year;
+
+    private Integer month;
+
+}

+ 45 - 0
FullCardClient/src/main/java/com/zhongshu/card/client/model/pay/RealTimeOverviewModel.java

@@ -0,0 +1,45 @@
+package com.zhongshu.card.client.model.pay;
+
+import io.swagger.v3.oas.annotations.media.Schema;
+import lombok.Data;
+
+import java.util.List;
+
+@Data
+public class RealTimeOverviewModel {
+
+    @Schema(description = "时间点")
+    private List<String> point;
+
+    @Schema(description = "昨日收入")
+    private List<Long> yesterdayList;
+
+    @Schema(description = "今日收入")
+    private List<Long> todayList;
+
+    @Schema(description = "昨天收入总和")
+    private Long yesterdayAmount = 0L;
+
+    @Schema(description = "今日收入总和")
+    private Long todayAmount = 0L;
+
+    @Schema(description = "收入变化率")
+    private Long amountChangeRate;
+
+    @Schema(description = "昨日订单")
+    private List<Long> yesterdayOrderList;
+
+    @Schema(description = "今日订单")
+    private List<Long> todayOrderList;
+
+    @Schema(description = "昨天订单数量总和")
+    private Long yesterdayOrder = 0L;
+
+    @Schema(description = "今日订单数量总和")
+    private Long todayOrder = 0L;
+
+    @Schema(description = "订单数量变化率")
+    private Long orderChangeRate;
+
+    private BalanceOverviewModel balanceOverview;
+}

+ 23 - 0
FullCardClient/src/main/java/com/zhongshu/card/client/utils/DateUtils.java

@@ -8,6 +8,7 @@ import java.sql.Timestamp;
 import java.text.ParseException;
 import java.text.SimpleDateFormat;
 import java.time.*;
+import java.time.format.DateTimeFormatter;
 import java.time.temporal.IsoFields;
 import java.time.temporal.WeekFields;
 import java.util.*;
@@ -802,6 +803,28 @@ public class DateUtils {
         return calendar;
     }
 
+    public static List<String> getCurrentIntegralPoint(String pattern){
+        // 获取当前日期
+        LocalDate today = LocalDate.now();
+
+        // 创建一个列表来存储格式化后的整点时间
+        List<String> formattedWholeHours = new ArrayList<>();
+
+        // 遍历 00:00 到 23:00 的每一个整点
+        for (int hour = 0; hour < 24; hour++) {
+            LocalTime time = LocalTime.of(hour, 0); // 整点时间
+            LocalDateTime dateTime = LocalDateTime.of(today, time);
+
+            // 格式化时间
+            DateTimeFormatter formatter = DateTimeFormatter.ofPattern(pattern);
+            String formattedDateTime = dateTime.format(formatter);
+
+            // 添加到列表
+            formattedWholeHours.add(formattedDateTime);
+        }
+        return formattedWholeHours;
+    }
+
     public static void main(String[] args) {
         log.info("本年开始时间:{},结束时间:{}", paresTime(getYearStartTime(2023), FORMAT_LONG), paresTime(getYearEndTime(2023), FORMAT_LONG));
         log.info("本月开始时间:{},结束时间:{}", paresTime(getMonthStartTime(2023, 9), FORMAT_LONG), paresTime(getMonthEndTime(2023, 9), FORMAT_LONG));

+ 33 - 0
FullCardServer/src/main/java/com/zhongshu/card/server/core/controller/pay/statistics/PayOverviewController.java

@@ -0,0 +1,33 @@
+package com.zhongshu.card.server.core.controller.pay.statistics;
+
+import com.zhongshu.card.server.core.service.pay.OrgOverviewService;
+import io.swagger.v3.oas.annotations.Operation;
+import io.swagger.v3.oas.annotations.tags.Tag;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.web.bind.annotation.RequestMapping;
+import org.springframework.web.bind.annotation.RequestMethod;
+import org.springframework.web.bind.annotation.RequestParam;
+import org.springframework.web.bind.annotation.RestController;
+
+@RestController
+@RequestMapping("balance/recharge")
+@Tag(name = "资金概况", description = "资金概况")
+public class PayOverviewController {
+
+    @Autowired
+    OrgOverviewService orgOverviewService;
+
+//    @Operation(summary = "余额概况", description = "资金概况-顶部三个余额")
+//    @RequestMapping(value = "balanceOverview", method = RequestMethod.GET)
+//    public Object balanceOverview(@RequestParam(name = "projectOid", required = false) String projectOid,
+//                                  @RequestParam(name = "oid" ,required = false) String oid){
+//        return orgOverviewService.balanceOverview(projectOid, oid);
+//    }
+
+    @Operation(summary = "实时概况", description = "顶部数据及资金概况-折线图")
+    @RequestMapping(value = "realTimeOverview", method = RequestMethod.GET)
+    public Object realTimeOverview(@RequestParam(name = "projectOid", required = false) String projectOid,
+                                  @RequestParam(name = "oid" ,required = false) String oid){
+        return orgOverviewService.realTimeOverview(projectOid, oid);
+    }
+}

+ 0 - 60
FullCardServer/src/main/java/com/zhongshu/card/server/core/service/pay/AccountBookService.java

@@ -1,60 +0,0 @@
-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.GeneralLedgerModel;
-import com.github.microservice.pay.client.model.ledger.GeneralLedgerTreeModel;
-import com.github.microservice.pay.client.model.ledger.transaction.BaseQueryTransactionLogModel;
-import com.github.microservice.pay.client.model.ledger.transaction.DescendantQueryTransactionLogModel;
-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;
-import com.github.microservice.pay.client.service.ledger.GeneralLedgerService;
-import com.github.microservice.pay.client.service.ledger.TransactionLogService;
-import com.zhongshu.card.client.utils.DateUtils;
-import com.zhongshu.card.server.core.domain.pay.PayAccount;
-import org.springframework.beans.factory.annotation.Autowired;
-import org.springframework.data.domain.Pageable;
-import org.springframework.stereotype.Service;
-
-import java.util.List;
-import java.util.stream.Collectors;
-
-@Service
-public class AccountBookService {
-
-    @Autowired
-    TransactionLogService transactionLogService;
-
-    @Autowired
-    GeneralLedgerService generalLedgerService;
-
-    @Autowired
-    PayAccountService payAccountService;
-
-    /**
-     * 查询用户流水
-     */
-    public Object queryFlow(Pageable pageable, String userId, String projectOid){
-        PayAccount userParent = payAccountService.getUserParent(projectOid, userId);
-        ResultContent<GeneralLedgerTreeModel> descendantRet = generalLedgerService.descendant(userParent.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(userParent.getLedgerId());
-
-        descendantQueryTransactionLogModel.setStartTime(DateUtils.getMonthStartTime(2024, 10));
-        return transactionLogService.aggregate(descendantQueryTransactionLogModel);
-    }
-}

+ 223 - 0
FullCardServer/src/main/java/com/zhongshu/card/server/core/service/pay/OrgOverviewService.java

@@ -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;
+    }
+}

+ 1 - 1
FullCardServer/src/main/java/com/zhongshu/card/server/core/service/pay/PayAccountService.java

@@ -149,7 +149,7 @@ public class PayAccountService {
         PayAccount payAccount = new PayAccount();
         payAccount.setProjectOid(projectOid);
         payAccount.setOid(oid);
-        payAccount.setLevel(PayAccountLevel.projectParent);
+        payAccount.setLevel(PayAccountLevel.OrgParent);
         String ledgerId = createLedger("机构总账", null, Map.of("projectOid", projectOid, "oid", oid, "level", PayAccountLevel.OrgParent), 999999999L, -999999999L);
         if (StringUtils.isBlank(ledgerId)){
             throw new Exception("支付中心创建机构总账失败");