|
|
@@ -127,7 +127,7 @@ export class TeamService {
|
|
|
}> {
|
|
|
// 根据 userId 参数决定查询范围
|
|
|
const whereCondition = userId ? { userId } : {}
|
|
|
-
|
|
|
+
|
|
|
// 获取团队信息(如果指定了 userId,只查询该用户的团队)
|
|
|
const teams = await this.teamRepository.find({
|
|
|
where: whereCondition,
|
|
|
@@ -144,32 +144,32 @@ export class TeamService {
|
|
|
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() : []
|
|
|
+ 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() : []
|
|
|
+ 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>()
|
|
|
@@ -199,7 +199,6 @@ export class TeamService {
|
|
|
const teamTotalRevenue = totalRevenueMap.get(team.userId) || 0
|
|
|
const teamTodayRevenue = todayRevenueMap.get(team.userId) || 0
|
|
|
|
|
|
-
|
|
|
statistics.totalRevenue += teamTotalRevenue
|
|
|
statistics.todayRevenue += teamTodayRevenue
|
|
|
totalCommissionRate += Number(team.commissionRate)
|
|
|
@@ -217,9 +216,58 @@ export class TeamService {
|
|
|
// 按总收入排序
|
|
|
statistics.allTeams.sort((a, b) => b.totalRevenue - a.totalRevenue)
|
|
|
|
|
|
+ // 异步更新团队数据
|
|
|
+ this.updateTeamDataAsync(statistics.allTeams).catch(error => {
|
|
|
+ console.error('更新团队数据失败:', error)
|
|
|
+ })
|
|
|
+
|
|
|
return statistics
|
|
|
}
|
|
|
|
|
|
+ async updateTeamData(
|
|
|
+ updates: Array<{
|
|
|
+ userId: number
|
|
|
+ totalRevenue?: number
|
|
|
+ todayRevenue?: number
|
|
|
+ }>
|
|
|
+ ): Promise<Team[]> {
|
|
|
+ const results: Team[] = []
|
|
|
+
|
|
|
+ for (const update of updates) {
|
|
|
+ const team = await this.findByUserId(update.userId)
|
|
|
+
|
|
|
+ // 更新团队数据
|
|
|
+ if (update.totalRevenue !== undefined) {
|
|
|
+ team.totalRevenue = update.totalRevenue
|
|
|
+ }
|
|
|
+ if (update.todayRevenue !== undefined) {
|
|
|
+ team.todayRevenue = update.todayRevenue
|
|
|
+ }
|
|
|
+
|
|
|
+ const savedTeam = await this.teamRepository.save(team)
|
|
|
+ results.push(savedTeam)
|
|
|
+ }
|
|
|
+
|
|
|
+ return results
|
|
|
+ }
|
|
|
+
|
|
|
+ private async updateTeamDataAsync(
|
|
|
+ teams: Array<{ id: number; totalRevenue: number; todayRevenue: number }>
|
|
|
+ ): Promise<void> {
|
|
|
+ const updates = teams.map(team => ({
|
|
|
+ id: team.id,
|
|
|
+ totalRevenue: team.totalRevenue,
|
|
|
+ todayRevenue: team.todayRevenue
|
|
|
+ }))
|
|
|
+
|
|
|
+ for (const update of updates) {
|
|
|
+ await this.teamRepository.update(update.id, {
|
|
|
+ totalRevenue: update.totalRevenue,
|
|
|
+ todayRevenue: update.todayRevenue
|
|
|
+ })
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
async getTeamStatistics(userId: number): Promise<Team> {
|
|
|
return this.teamRepository.findOneOrFail({ where: { userId } })
|
|
|
}
|