Kaynağa Gözat

feat: 添加租户统计

gatsby 3 hafta önce
ebeveyn
işleme
7c4345d67e

+ 11 - 2
backend/app/plugin/module_payment/account/controller.py

@@ -48,10 +48,11 @@ AccountRouter = APIRouter(
     response_model=ResponseSchema[Dict],
 )
 async def stat_transfer_amount_controller(
-    enterprise_id: Annotated[str, Query(description="企业ID")],
     # start_date: Annotated[Optional[datetime], Query(description="统计开始日期")],
     # end_date: Annotated[Optional[datetime], Query(description="统计结束日期")],
     auth: Annotated[AuthSchema, Depends(AuthPermission(["module_payment:account:transfer"]))],
+    tenant_id: Annotated[Optional[int], Query(description="租户ID")] = None,
+    enterprise_id: Annotated[Optional[str], Query(description="企业ID")] = None,
 ) -> JSONResponse:
     """统计转账金额"""
     from datetime import datetime, timedelta, date
@@ -67,6 +68,7 @@ async def stat_transfer_amount_controller(
 
     amount_of_today = await AccountService.stat_transfer_amount_service(
         auth=auth, 
+        tenant_id=tenant_id,
         enterprise_id=enterprise_id, 
         start_date=start_of_today, 
         end_date=end_of_today
@@ -79,12 +81,19 @@ async def stat_transfer_amount_controller(
 
     amount_of_7days = await AccountService.stat_transfer_amount_service(
         auth=auth, 
+        tenant_id=tenant_id,
         enterprise_id=enterprise_id, 
         start_date=start_of_7days_ago, 
         end_date=now  # 截止到当前精确时间
     )
     # 统计全部转账金额
-    amount_of_all = await AccountService.stat_transfer_amount_service(auth=auth, enterprise_id=enterprise_id, start_date=None, end_date=None)
+    amount_of_all = await AccountService.stat_transfer_amount_service(
+        auth=auth, 
+        tenant_id=tenant_id, 
+        enterprise_id=enterprise_id, 
+        start_date=None, 
+        end_date=None
+    )
 
     return SuccessResponse(data={
         "amount_of_today": amount_of_today,

+ 7 - 3
backend/app/plugin/module_payment/account/crud.py

@@ -100,15 +100,19 @@ class TransferCRUD(CRUDBase[TransferModel, AccountTransferSchema, AccountTransfe
     # 统计企业在指定时间范围内的转账总金额以及每天的转账金额。
     async def get_transfer_amount(
         self, 
-        enterprise_id: str,
+        enterprise_id: Optional[str] = None,
         start_date: Optional[datetime] = None,
         end_date: Optional[datetime] = None,
+        tenant_id: Optional[int] = None,
     ) -> Decimal:
         conditions = [
-            TransferModel.enterprise_id == enterprise_id,
             TransferModel.status == "SUCCESS",
         ]
-
+        
+        if tenant_id:
+            conditions.append(TransferModel.tenant_id == tenant_id)
+        if enterprise_id:
+            conditions.append(TransferModel.enterprise_id == enterprise_id)
         if start_date:
             conditions.append(TransferModel.created_time >= start_date)
         if end_date:

+ 3 - 1
backend/app/plugin/module_payment/account/service.py

@@ -43,7 +43,8 @@ class AccountService:
     async def stat_transfer_amount_service(
         cls,
         auth: AuthSchema,
-        enterprise_id: str,
+        tenant_id: Optional[int] = None,
+        enterprise_id: Optional[str] = None,
         start_date: Optional[datetime] = None,
         end_date: Optional[datetime] = None,
     ) -> Decimal:
@@ -55,6 +56,7 @@ class AccountService:
         crud = TransferCRUD(auth)
         
         return await crud.get_transfer_amount(
+            tenant_id=tenant_id,
             enterprise_id=enterprise_id,
             start_date=start_date,
             end_date=end_date,

+ 2 - 2
frontend/src/api/module_payment/account.ts

@@ -3,13 +3,13 @@ import request from "@/utils/request";
 const API_PATH = "/payment/account";
 
 export const AccountAPI = {
-  statAmount(enterpriseId: string) {
+  statAmount(tenantId?: number, enterpriseId?: string) {
     return request<
       ApiResponse<{ amount_of_today: string; amount_of_7days: string; amount_of_all: string }>
     >({
       url: `${API_PATH}/stat/transfer/amount`,
       method: "get",
-      params: { enterprise_id: enterpriseId },
+      params: { tenant_id: tenantId, enterprise_id: enterpriseId },
     });
   },
 

+ 4 - 2
frontend/src/api/module_system/tenant.ts

@@ -54,6 +54,7 @@ const TenantAPI = {
 export default TenantAPI;
 
 export interface TenantPageQuery extends PageQuery {
+  id?: number;
   name?: string;
   code?: string;
   status?: string;
@@ -66,8 +67,9 @@ export interface TenantInfo {
 }
 
 export interface TenantTable extends BaseType {
-  name: string;
-  code: string;
+  id?: number;
+  name?: string;
+  code?: string;
   start_time?: string;
   end_time?: string;
 }

+ 2 - 0
frontend/src/router/index.ts

@@ -1,6 +1,8 @@
 import type { App } from "vue";
 import { createRouter, createWebHashHistory, type RouteRecordRaw } from "vue-router";
 export const Layout = () => import("@/layouts/index.vue");
+
+
 /**
  * 静态路由
  */

+ 88 - 18
frontend/src/views/dashboard/tenant.vue

@@ -26,29 +26,36 @@
     <!-- 核心数据 -->
     <div class="core-data-section">
       <div class="section-header">
-        <h2>核心数据</h2>
-        <a href="#" class="more-link">更多数据</a>
+        <h2>转账数据统计</h2>
+        <!-- <a href="#" class="more-link">更多数据</a> -->
       </div>
+
+      <div v-if="is_platform_user" style="margin-bottom: 16px; width: 300px;">
+        <el-select v-model="currentTenantId" placeholder="选择商户" @change="handleTenantChange">
+          <el-option
+            v-for="tenant in allTenantData"
+            :key="tenant.id"
+            :label="tenant.name"
+            :value="tenant.id"
+          />
+        </el-select>
+      </div>
+
       <div class="data-cards">
         <div class="data-card">
-          <div class="card-title">近30日因公消费员工数(人)</div>
-          <div class="card-value">--</div>
-          <div class="card-link">日因公消费员工数 --</div>
+          <div class="card-title">今日转账金额(元)</div>
+          <div class="card-value">{{ statAmount.amount_of_today || "--" }}</div>
+          <!-- <div class="card-link"></div> -->
         </div>
         <div class="data-card">
-          <div class="card-title">近30日消费金额(元)</div>
-          <div class="card-value">--</div>
-          <div class="card-link">日消费金额 --</div>
+          <div class="card-title">近7日转账金额(元)</div>
+          <div class="card-value">{{ statAmount.amount_of_7days || "--" }}</div>
+          <!-- <div class="card-link"></div> -->
         </div>
         <div class="data-card">
-          <div class="card-title">近30日消费笔数(笔)</div>
-          <div class="card-value">--</div>
-          <div class="card-link">日消费笔数 --</div>
-        </div>
-        <div class="data-card">
-          <div class="card-title">总激活员工数(人)</div>
-          <div class="card-value">--</div>
-          <div class="card-link">日激活员工数 --</div>
+          <div class="card-title">总转账金额(元)</div>
+          <div class="card-value">{{ statAmount.amount_of_all || "--" }}</div>
+          <!-- <div class="card-link"></div> -->
         </div>
       </div>
     </div>
@@ -58,9 +65,72 @@
 <script setup lang="ts">
 import { ref } from 'vue';
 import { ElButton, ElTabs, ElTabPane } from 'element-plus';
+import { useEnterpriseStore, useUserStore } from '@/store';
+import AccountAPI from '@/api/module_payment/account';
+import TenantAPI, { TenantTable } from '@/api/module_system/tenant';
+
+const userStore = useUserStore();
+const enterpriseStore = useEnterpriseStore();
+
+const is_platform_user = computed(() => {
+  return userStore.is_platform_user;
+});
+
+const currentTenantId = ref<number | undefined>(undefined);
+
+const currentEnterpriseId = computed(() => enterpriseStore.getCurrentEnterprise?.enterprise_id);
+
+const currentEnterprise = computed(() => enterpriseStore.getCurrentEnterprise);
+
+
+const statAmount = ref<{
+  amount_of_today?: string;
+  amount_of_7days?: string;
+  amount_of_all?: string;
+}>({});
+
+function handleTenantChange(value: number | undefined) {
+  currentTenantId.value = value;
+}
+
+const allTenantData = ref<TenantTable[]>([]);
+
+async function fetchStatAmount() {
+  // 如果不是平台用户,把 currentTenantId 设为 undefined
+  if (!is_platform_user.value) {
+    currentTenantId.value = undefined;
+  }
+  try {
+    const res = await AccountAPI.statAmount(currentTenantId.value, currentEnterpriseId.value);
+    statAmount.value = res.data.data || {};
+  } catch (error) {
+    console.error("获取统计金额失败:", error);
+  }
+}
+
+async function fetchAllTenantData() {
+  try {
+    const res = await TenantAPI.listTenant({
+      page_no: 1,
+      page_size: 100,
+    });
+    // 增加全部选项然后合并请求的数据
+    allTenantData.value = [{ id: undefined, name: "全部" }, ...(res.data.data?.items || [])];
+    console.log("所有租户数据:", allTenantData.value);
+  } catch (error) {
+    console.error("获取所有租户数据失败:", error);
+  }
+}
+
+onMounted(() => {
+  if (is_platform_user.value) {
+    fetchAllTenantData();
+  }
+  fetchStatAmount();
+});
 
-// 激活的引导标签
-const activeGuideTab = ref('account');
+// watch currentTenantId 变化,重新获取统计金额数据
+watch(currentTenantId, fetchStatAmount);
 
 // 租户仪表盘组件
 </script>