Просмотр исходного кода

优化团队服务,新增默认团队数据支持,调整收入统计查询逻辑以包含默认的 agentId 0,并确保默认团队在排序时始终位于最后。

wuyi 2 месяцев назад
Родитель
Сommit
b4b57c8090
1 измененных файлов с 34 добавлено и 11 удалено
  1. 34 11
      src/services/team.service.ts

+ 34 - 11
src/services/team.service.ts

@@ -140,25 +140,26 @@ export class TeamService {
     const todayEnd = new Date()
     todayEnd.setHours(23, 59, 59, 999)
 
-    // 获取所有团队的 userId 列表
+    // 获取所有团队的 userId 列表,并添加默认的 agentId 0
     const teamUserIds = teams.map(team => team.userId)
+    const allUserIds = [...teamUserIds, 0] // 添加默认的 agentId 0
 
-    // 查询所有团队的总收入统计(通过 userId 关联)
+    // 查询所有团队的总收入统计(通过 userId 关联,包括默认的 agentId 0
     const totalRevenueStats =
-      teamUserIds.length > 0
+      allUserIds.length > 0
         ? await this.incomeRecordsRepository
             .createQueryBuilder('record')
             .select(['record.agentId as userId', 'SUM(record.incomeAmount) as totalRevenue'])
             .where('record.delFlag = :delFlag', { delFlag: false })
             .andWhere('record.status = :status', { status: true })
-            .andWhere('record.agentId IN (:...teamUserIds)', { teamUserIds })
+            .andWhere('record.agentId IN (:...allUserIds)', { allUserIds })
             .groupBy('record.agentId')
             .getRawMany()
         : []
 
-    // 查询所有团队的今日收入统计(通过 userId 关联)
+    // 查询所有团队的今日收入统计(通过 userId 关联,包括默认的 agentId 0
     const todayRevenueStats =
-      teamUserIds.length > 0
+      allUserIds.length > 0
         ? await this.incomeRecordsRepository
             .createQueryBuilder('record')
             .select(['record.agentId as userId', 'SUM(record.incomeAmount) as todayRevenue'])
@@ -166,7 +167,7 @@ export class TeamService {
             .andWhere('record.status = :status', { status: true })
             .andWhere('record.createdAt >= :today', { today })
             .andWhere('record.createdAt <= :todayEnd', { todayEnd })
-            .andWhere('record.agentId IN (:...teamUserIds)', { teamUserIds })
+            .andWhere('record.agentId IN (:...allUserIds)', { allUserIds })
             .groupBy('record.agentId')
             .getRawMany()
         : []
@@ -211,13 +212,35 @@ export class TeamService {
       })
     })
 
+    // 添加默认团队数据(agentId 为 0)
+    const defaultTotalRevenue = totalRevenueMap.get(0) || 0
+    const defaultTodayRevenue = todayRevenueMap.get(0) || 0
+
+    statistics.totalRevenue += defaultTotalRevenue
+    statistics.todayRevenue += defaultTodayRevenue
+
+    // 将默认团队数据添加到列表最后
+    statistics.allTeams.push({
+      id: 0, // 使用 0 作为默认团队的 ID
+      name: '默认',
+      totalRevenue: Number(defaultTotalRevenue.toFixed(5)),
+      todayRevenue: Number(defaultTodayRevenue.toFixed(5))
+    })
+
     statistics.averageCommissionRate = teams.length > 0 ? Number((totalCommissionRate / teams.length).toFixed(2)) : 0
 
-    // 按总收入排序
-    statistics.allTeams.sort((a, b) => b.totalRevenue - a.totalRevenue)
+    // 按总收入排序,但确保默认团队始终在最后
+    statistics.allTeams.sort((a, b) => {
+      // 如果其中一个是默认团队(id 为 0),则默认团队排在后面
+      if (a.id === 0) return 1
+      if (b.id === 0) return -1
+      // 其他团队按总收入降序排列
+      return b.totalRevenue - a.totalRevenue
+    })
 
-    // 异步更新团队数据
-    this.updateTeamDataAsync(statistics.allTeams).catch(error => {
+    // 异步更新团队数据(排除默认团队,因为默认团队不存在于数据库中)
+    const realTeams = statistics.allTeams.filter(team => team.id !== 0)
+    this.updateTeamDataAsync(realTeams).catch(error => {
       console.error('更新团队数据失败:', error)
     })