|
@@ -1,6 +1,7 @@
|
|
|
import { Repository, Like } from 'typeorm'
|
|
import { Repository, Like } from 'typeorm'
|
|
|
import { FastifyInstance } from 'fastify'
|
|
import { FastifyInstance } from 'fastify'
|
|
|
import { Team } from '../entities/team.entity'
|
|
import { Team } from '../entities/team.entity'
|
|
|
|
|
+import { IncomeRecords, IncomeType } from '../entities/income-records.entity'
|
|
|
import { PaginationResponse } from '../dto/common.dto'
|
|
import { PaginationResponse } from '../dto/common.dto'
|
|
|
import { CreateTeamBody, UpdateTeamBody, ListTeamQuery } from '../dto/team.dto'
|
|
import { CreateTeamBody, UpdateTeamBody, ListTeamQuery } from '../dto/team.dto'
|
|
|
import { UserService } from './user.service'
|
|
import { UserService } from './user.service'
|
|
@@ -10,11 +11,13 @@ import * as randomstring from 'randomstring'
|
|
|
|
|
|
|
|
export class TeamService {
|
|
export class TeamService {
|
|
|
private teamRepository: Repository<Team>
|
|
private teamRepository: Repository<Team>
|
|
|
|
|
+ private incomeRecordsRepository: Repository<IncomeRecords>
|
|
|
private userService: UserService
|
|
private userService: UserService
|
|
|
private sysConfigService: SysConfigService
|
|
private sysConfigService: SysConfigService
|
|
|
|
|
|
|
|
constructor(app: FastifyInstance) {
|
|
constructor(app: FastifyInstance) {
|
|
|
this.teamRepository = app.dataSource.getRepository(Team)
|
|
this.teamRepository = app.dataSource.getRepository(Team)
|
|
|
|
|
+ this.incomeRecordsRepository = app.dataSource.getRepository(IncomeRecords)
|
|
|
this.userService = new UserService(app)
|
|
this.userService = new UserService(app)
|
|
|
this.sysConfigService = new SysConfigService(app)
|
|
this.sysConfigService = new SysConfigService(app)
|
|
|
}
|
|
}
|
|
@@ -122,10 +125,61 @@ export class TeamService {
|
|
|
averageCommissionRate: number
|
|
averageCommissionRate: number
|
|
|
allTeams: Array<{ id: number; name: string; totalRevenue: number; todayRevenue: number }>
|
|
allTeams: Array<{ id: number; name: string; totalRevenue: number; todayRevenue: number }>
|
|
|
}> {
|
|
}> {
|
|
|
|
|
+ // 获取所有团队信息
|
|
|
const teams = await this.teamRepository.find({
|
|
const teams = await this.teamRepository.find({
|
|
|
- select: ['id', 'name', 'totalRevenue', 'todayRevenue', 'commissionRate']
|
|
|
|
|
|
|
+ select: ['id', 'name', 'userId', 'commissionRate']
|
|
|
})
|
|
})
|
|
|
|
|
|
|
|
|
|
+ // 获取今天的开始时间
|
|
|
|
|
+ const today = new Date()
|
|
|
|
|
+ today.setHours(0, 0, 0, 0)
|
|
|
|
|
+ const todayEnd = new Date()
|
|
|
|
|
+ todayEnd.setHours(23, 59, 59, 999)
|
|
|
|
|
+
|
|
|
|
|
+ // 获取所有团队的 userId 列表
|
|
|
|
|
+ const teamUserIds = teams.map(team => team.userId)
|
|
|
|
|
+
|
|
|
|
|
+ // 查询所有团队的总收入统计(通过 userId 关联)
|
|
|
|
|
+ const totalRevenueStats = teamUserIds.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 })
|
|
|
|
|
+ .groupBy('record.agentId')
|
|
|
|
|
+ .getRawMany() : []
|
|
|
|
|
+
|
|
|
|
|
+ // 查询所有团队的今日收入统计(通过 userId 关联)
|
|
|
|
|
+ const todayRevenueStats = teamUserIds.length > 0 ? await this.incomeRecordsRepository
|
|
|
|
|
+ .createQueryBuilder('record')
|
|
|
|
|
+ .select([
|
|
|
|
|
+ 'record.agentId as userId',
|
|
|
|
|
+ 'SUM(record.incomeAmount) as todayRevenue'
|
|
|
|
|
+ ])
|
|
|
|
|
+ .where('record.delFlag = :delFlag', { delFlag: false })
|
|
|
|
|
+ .andWhere('record.status = :status', { status: true })
|
|
|
|
|
+ .andWhere('record.createdAt >= :today', { today })
|
|
|
|
|
+ .andWhere('record.createdAt <= :todayEnd', { todayEnd })
|
|
|
|
|
+ .andWhere('record.agentId IN (:...teamUserIds)', { teamUserIds })
|
|
|
|
|
+ .groupBy('record.agentId')
|
|
|
|
|
+ .getRawMany() : []
|
|
|
|
|
+
|
|
|
|
|
+ // 构建统计数据映射(使用 userId 作为键)
|
|
|
|
|
+ const totalRevenueMap = new Map<number, number>()
|
|
|
|
|
+ const todayRevenueMap = new Map<number, number>()
|
|
|
|
|
+
|
|
|
|
|
+ totalRevenueStats.forEach(stat => {
|
|
|
|
|
+ totalRevenueMap.set(stat.userId, Number(stat.totalRevenue) || 0)
|
|
|
|
|
+ })
|
|
|
|
|
+
|
|
|
|
|
+ todayRevenueStats.forEach(stat => {
|
|
|
|
|
+ todayRevenueMap.set(stat.userId, Number(stat.todayRevenue) || 0)
|
|
|
|
|
+ })
|
|
|
|
|
+
|
|
|
|
|
+ // 计算统计数据
|
|
|
const statistics = {
|
|
const statistics = {
|
|
|
totalTeams: teams.length,
|
|
totalTeams: teams.length,
|
|
|
totalRevenue: 0,
|
|
totalRevenue: 0,
|
|
@@ -136,22 +190,28 @@ export class TeamService {
|
|
|
|
|
|
|
|
let totalCommissionRate = 0
|
|
let totalCommissionRate = 0
|
|
|
|
|
|
|
|
|
|
+ // 构建团队列表并计算总计(使用 team.userId 进行映射)
|
|
|
teams.forEach(team => {
|
|
teams.forEach(team => {
|
|
|
- statistics.totalRevenue += Number(team.totalRevenue)
|
|
|
|
|
- statistics.todayRevenue += Number(team.todayRevenue)
|
|
|
|
|
- totalCommissionRate += Number(team.commissionRate)
|
|
|
|
|
- })
|
|
|
|
|
|
|
+ const teamTotalRevenue = totalRevenueMap.get(team.userId) || 0
|
|
|
|
|
+ const teamTodayRevenue = todayRevenueMap.get(team.userId) || 0
|
|
|
|
|
|
|
|
- statistics.averageCommissionRate = teams.length > 0 ? totalCommissionRate / teams.length : 0
|
|
|
|
|
|
|
|
|
|
- statistics.allTeams = teams
|
|
|
|
|
- .sort((a, b) => Number(b.totalRevenue) - Number(a.totalRevenue))
|
|
|
|
|
- .map(team => ({
|
|
|
|
|
|
|
+ statistics.totalRevenue += teamTotalRevenue
|
|
|
|
|
+ statistics.todayRevenue += teamTodayRevenue
|
|
|
|
|
+ totalCommissionRate += Number(team.commissionRate)
|
|
|
|
|
+
|
|
|
|
|
+ statistics.allTeams.push({
|
|
|
id: team.id,
|
|
id: team.id,
|
|
|
name: team.name,
|
|
name: team.name,
|
|
|
- totalRevenue: Number(team.totalRevenue),
|
|
|
|
|
- todayRevenue: Number(team.todayRevenue)
|
|
|
|
|
- }))
|
|
|
|
|
|
|
+ totalRevenue: Number(teamTotalRevenue.toFixed(5)),
|
|
|
|
|
+ todayRevenue: Number(teamTodayRevenue.toFixed(5))
|
|
|
|
|
+ })
|
|
|
|
|
+ })
|
|
|
|
|
+
|
|
|
|
|
+ statistics.averageCommissionRate = teams.length > 0 ? Number((totalCommissionRate / teams.length).toFixed(2)) : 0
|
|
|
|
|
+
|
|
|
|
|
+ // 按总收入排序
|
|
|
|
|
+ statistics.allTeams.sort((a, b) => b.totalRevenue - a.totalRevenue)
|
|
|
|
|
|
|
|
return statistics
|
|
return statistics
|
|
|
}
|
|
}
|