@@ -2,19 +2,22 @@ import { HttpException, HttpStatus, Injectable } from '@nestjs/common';
22import { TenantEntity } from './entities/tenant.entity' ;
33
44import { InjectRepository } from '@nestjs/typeorm' ;
5- import { Equal , FindOperator , ILike , Repository , SelectQueryBuilder } from 'typeorm' ;
5+ import { Equal , FindOperator , ILike , In , Repository , SelectQueryBuilder } from 'typeorm' ;
66import { TenantDto } from './dto/tenant.dto' ;
77import { QueryTenantDto } from './dto/tenant.query' ;
88import { PageEnum , StatusEnum } from '@src/enums' ;
99import { TenantPageVo , TenantVo } from './vo/tenant.vo' ;
1010import { AreaEntity } from '../area/entities/area.entity' ;
1111import { mapToObj } from '@src/utils' ;
12+ import { AccountEntity } from '../account/entities/account.entity' ;
1213
1314@Injectable ( )
1415export class TenantService {
1516 constructor (
1617 @InjectRepository ( TenantEntity )
17- private readonly tenantRepository : Repository < TenantEntity >
18+ private readonly tenantRepository : Repository < TenantEntity > ,
19+ @InjectRepository ( AccountEntity )
20+ private readonly accountRepository : Repository < AccountEntity >
1821 ) { }
1922
2023 /**
@@ -149,8 +152,29 @@ export class TenantService {
149152 . createQueryBuilder ( 'tenant' )
150153 . where ( mapToObj ( query ) )
151154 . getCount ( ) ;
155+ // 根据商户id去查询account表数据
156+ const tenantIdList = data . map ( ( item ) => item . id ) ;
157+ const accountTotalList = await this . accountRepository
158+ . createQueryBuilder ( 'account' )
159+ . select ( 'account.tenantId' , 'tenantId' )
160+ . addSelect ( 'count(*)' , 'total' )
161+ . where ( { tenantId : In ( tenantIdList ) } )
162+ . groupBy ( 'account.tenantId' )
163+ . getRawMany ( ) ;
164+ // 将accountTotalList -> map[tenantId] = total
165+ const accountTotalMap = new Map ( ) ;
166+ for ( const item of accountTotalList ) {
167+ accountTotalMap . set ( item . tenantId , item . total ) ;
168+ }
169+ const resultData : TenantVo [ ] = [ ] ;
170+ for ( const item of data ) {
171+ resultData . push ( {
172+ ...item ,
173+ accountTotal : + accountTotalMap . get ( item . id ) ,
174+ } ) ;
175+ }
152176 return {
153- data,
177+ data : resultData ,
154178 total,
155179 pageNumber,
156180 pageSize,
0 commit comments