Skip to content

Commit 19a47a1

Browse files
committed
feat: 查询商户数量
1 parent fb8817f commit 19a47a1

File tree

3 files changed

+30
-4
lines changed

3 files changed

+30
-4
lines changed

src/api/tenant/tenant.module.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { Module } from '@nestjs/common';
22
import { RouterModule } from '@nestjs/core';
33
import { TypeOrmModule } from '@nestjs/typeorm';
44
import { ADMIN_PREFIX } from '@src/constants';
5+
import { AccountEntity } from '../account/entities/account.entity';
56
import { AreaEntity } from '../area/entities/area.entity';
67
import { TenantEntity } from './entities/tenant.entity';
78
import { TenantController } from './tenant.controller';
@@ -15,7 +16,7 @@ import { TenantService } from './tenant.service';
1516
module: TenantModule,
1617
},
1718
]),
18-
TypeOrmModule.forFeature([TenantEntity, AreaEntity]),
19+
TypeOrmModule.forFeature([TenantEntity, AreaEntity, AccountEntity]),
1920
],
2021
providers: [TenantService],
2122
controllers: [TenantController],

src/api/tenant/tenant.service.ts

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,22 @@ import { HttpException, HttpStatus, Injectable } from '@nestjs/common';
22
import { TenantEntity } from './entities/tenant.entity';
33

44
import { InjectRepository } from '@nestjs/typeorm';
5-
import { Equal, FindOperator, ILike, Repository, SelectQueryBuilder } from 'typeorm';
5+
import { Equal, FindOperator, ILike, In, Repository, SelectQueryBuilder } from 'typeorm';
66
import { TenantDto } from './dto/tenant.dto';
77
import { QueryTenantDto } from './dto/tenant.query';
88
import { PageEnum, StatusEnum } from '@src/enums';
99
import { TenantPageVo, TenantVo } from './vo/tenant.vo';
1010
import { AreaEntity } from '../area/entities/area.entity';
1111
import { mapToObj } from '@src/utils';
12+
import { AccountEntity } from '../account/entities/account.entity';
1213

1314
@Injectable()
1415
export 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,

src/api/tenant/vo/tenant.vo.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ export class TenantVo extends QueryVo {
1616
readonly address!: string; // 具体地址
1717
readonly sort!: number; // 排序
1818
readonly description!: string; // 描述
19+
readonly accountTotal?: number; // 账号数量
1920
}
2021

2122
export class TenantPageVo extends QueryListVo {

0 commit comments

Comments
 (0)