Skip to content

Commit f8e443d

Browse files
committed
feat: 充值接口
1 parent 9d60fbd commit f8e443d

File tree

3 files changed

+142
-80
lines changed

3 files changed

+142
-80
lines changed

src/api/tenant/dto/tenant.dto.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,3 +50,14 @@ export class TenantDto {
5050
@IsOptional({ message: '描述' })
5151
description!: string;
5252
}
53+
54+
export class RechargeDto {
55+
@Min(1, { message: '排序最小值为1' })
56+
@IsInt({ message: '排序必须是整数' })
57+
@Type(() => Number)
58+
@IsNotEmpty({ message: '商户id不能为空' })
59+
tenantId!: number;
60+
61+
@IsNotEmpty({ message: '充值金额不能为空' })
62+
amount!: number;
63+
}

src/api/tenant/tenant.controller.ts

Lines changed: 22 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import {
1212
} from '@nestjs/common';
1313
import { CurrentUser, ICurrentUserType } from '@src/decorators';
1414
import { AuthGuard } from '@src/guard/auth.guard';
15-
import { TenantDto } from './dto/tenant.dto';
15+
import { RechargeDto, TenantDto } from './dto/tenant.dto';
1616
import { QueryTenantDto } from './dto/tenant.query';
1717
import { TenantService } from './tenant.service';
1818
import { TenantPageVo, TenantVo } from './vo/tenant.vo';
@@ -26,14 +26,6 @@ export class TenantController {
2626
return await this.tenantService.createTenantApi(req);
2727
}
2828

29-
@Post('delete')
30-
async batchDeleteTenantByIdListApi(
31-
@Body() idList: number[],
32-
@CurrentUser('userInfo') currentUser: ICurrentUserType
33-
): Promise<string> {
34-
return await this.tenantService.batchDeleteTenantByIdListApi(idList, currentUser);
35-
}
36-
3729
@Delete(':id')
3830
async deleteTenantByIdApi(
3931
@Param('id', new ParseIntPipe()) id: number,
@@ -42,14 +34,6 @@ export class TenantController {
4234
return await this.tenantService.deleteTenantByIdApi(id, currentUser);
4335
}
4436

45-
@Post('/batchStatus')
46-
async batchModifyTenantStatusByIdApi(
47-
@Body() idList: number[],
48-
@CurrentUser('userInfo') currentUser: ICurrentUserType
49-
): Promise<string> {
50-
return await this.tenantService.batchModifyTenantStatusByIdApi(idList, currentUser);
51-
}
52-
5337
@Put('/status/:id')
5438
async modifyTenantStatusByIdApi(
5539
@Param('id', new ParseIntPipe()) id: number,
@@ -77,4 +61,25 @@ export class TenantController {
7761
): Promise<TenantVo | undefined> {
7862
return await this.tenantService.getTenantByIdApi(id);
7963
}
64+
65+
@Post('delete')
66+
async batchDeleteTenantByIdListApi(
67+
@Body() idList: number[],
68+
@CurrentUser('userInfo') currentUser: ICurrentUserType
69+
): Promise<string> {
70+
return await this.tenantService.batchDeleteTenantByIdListApi(idList, currentUser);
71+
}
72+
73+
@Post('/batchStatus')
74+
async batchModifyTenantStatusByIdApi(
75+
@Body() idList: number[],
76+
@CurrentUser('userInfo') currentUser: ICurrentUserType
77+
): Promise<string> {
78+
return await this.tenantService.batchModifyTenantStatusByIdApi(idList, currentUser);
79+
}
80+
81+
@Post('recharge')
82+
async rechargeTenantApi(@Body() req: RechargeDto): Promise<string> {
83+
return await this.tenantService.rechargeTenantApi(req);
84+
}
8085
}

src/api/tenant/tenant.service.ts

Lines changed: 109 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,16 @@ 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, In, Repository, SelectQueryBuilder } from 'typeorm';
6-
import { TenantDto } from './dto/tenant.dto';
5+
import {
6+
DataSource,
7+
Equal,
8+
FindOperator,
9+
ILike,
10+
In,
11+
Repository,
12+
SelectQueryBuilder,
13+
} from 'typeorm';
14+
import { RechargeDto, TenantDto } from './dto/tenant.dto';
715
import { QueryTenantDto } from './dto/tenant.query';
816
import { PageEnum, StatusEnum } from '@src/enums';
917
import { TenantPageVo, TenantVo } from './vo/tenant.vo';
@@ -18,7 +26,8 @@ export class TenantService {
1826
@InjectRepository(TenantEntity)
1927
private readonly tenantRepository: Repository<TenantEntity>,
2028
@InjectRepository(AccountEntity)
21-
private readonly accountRepository: Repository<AccountEntity>
29+
private readonly accountRepository: Repository<AccountEntity>,
30+
private readonly dataSource: DataSource
2231
) {}
2332

2433
/**
@@ -43,32 +52,6 @@ export class TenantService {
4352
return '创建成功';
4453
}
4554

46-
/**
47-
* @Author: 水痕
48-
* @Date: 2023-10-09 20:58:51
49-
* @LastEditors: 水痕
50-
* @Description: 根据id列表批量删除
51-
* @param {number} idList
52-
* @return {*}
53-
*/
54-
async batchDeleteTenantByIdListApi(
55-
idList: number[],
56-
currentUser: ICurrentUserType
57-
): Promise<string> {
58-
console.log(idList, '获取到的数据', currentUser);
59-
const { tenantId } = currentUser;
60-
console.log(tenantId, idList.includes(tenantId), '???');
61-
if (idList.includes(tenantId)) {
62-
throw new HttpException('自己不能删除自己', HttpStatus.OK);
63-
}
64-
const { affected } = await this.tenantRepository.softDelete(idList);
65-
if (affected) {
66-
return '删除成功';
67-
} else {
68-
return '删除成功';
69-
}
70-
}
71-
7255
/**
7356
* @Author: 水痕
7457
* @Date: 2023-10-07 10:50:10
@@ -90,40 +73,6 @@ export class TenantService {
9073
}
9174
}
9275

93-
/**
94-
* @Author: 水痕
95-
* @Date: 2023-10-09 22:06:30
96-
* @LastEditors: 水痕
97-
* @Description: 批量修改状态
98-
* @return {*}
99-
*/
100-
async batchModifyTenantStatusByIdApi(
101-
idList: number[],
102-
currentUser: ICurrentUserType
103-
): Promise<string> {
104-
const { tenantId } = currentUser;
105-
if (idList.includes(tenantId)) {
106-
throw new HttpException('自己不能修改自己', HttpStatus.OK);
107-
}
108-
const tenantEntityList: Pick<TenantEntity, 'status'>[] = await this.tenantRepository.find({
109-
where: { id: In(idList) },
110-
select: ['status'],
111-
});
112-
if ([...new Set(tenantEntityList.map((item) => item.status))].length > 1) {
113-
throw new HttpException('当前状态不统一,不能批量修改', HttpStatus.OK);
114-
}
115-
const { affected } = await this.tenantRepository.update(idList, {
116-
status:
117-
tenantEntityList[0]?.status == StatusEnum.FORBIDDEN
118-
? StatusEnum.NORMAL
119-
: StatusEnum.FORBIDDEN,
120-
});
121-
if (affected) {
122-
return '修改成功';
123-
} else {
124-
return '修改失败';
125-
}
126-
}
12776
/**
12877
* @Author: 水痕
12978
* @Date: 2023-10-07 20:51:30
@@ -263,6 +212,103 @@ export class TenantService {
263212
return await queryBuilder.where('tenant.id = :id', { id }).getRawOne();
264213
}
265214

215+
/**
216+
* @Author: 水痕
217+
* @Date: 2023-10-09 20:58:51
218+
* @LastEditors: 水痕
219+
* @Description: 根据id列表批量删除
220+
* @param {number} idList
221+
* @return {*}
222+
*/
223+
async batchDeleteTenantByIdListApi(
224+
idList: number[],
225+
currentUser: ICurrentUserType
226+
): Promise<string> {
227+
console.log(idList, '获取到的数据', currentUser);
228+
const { tenantId } = currentUser;
229+
console.log(tenantId, idList.includes(tenantId), '???');
230+
if (idList.includes(tenantId)) {
231+
throw new HttpException('自己不能删除自己', HttpStatus.OK);
232+
}
233+
const { affected } = await this.tenantRepository.softDelete(idList);
234+
if (affected) {
235+
return '删除成功';
236+
} else {
237+
return '删除成功';
238+
}
239+
}
240+
241+
/**
242+
* @Author: 水痕
243+
* @Date: 2023-10-09 22:06:30
244+
* @LastEditors: 水痕
245+
* @Description: 批量修改状态
246+
* @return {*}
247+
*/
248+
async batchModifyTenantStatusByIdApi(
249+
idList: number[],
250+
currentUser: ICurrentUserType
251+
): Promise<string> {
252+
const { tenantId } = currentUser;
253+
if (idList.includes(tenantId)) {
254+
throw new HttpException('自己不能修改自己', HttpStatus.OK);
255+
}
256+
const tenantEntityList: Pick<TenantEntity, 'status'>[] = await this.tenantRepository.find({
257+
where: { id: In(idList) },
258+
select: ['status'],
259+
});
260+
if ([...new Set(tenantEntityList.map((item) => item.status))].length > 1) {
261+
throw new HttpException('当前状态不统一,不能批量修改', HttpStatus.OK);
262+
}
263+
const { affected } = await this.tenantRepository.update(idList, {
264+
status:
265+
tenantEntityList[0]?.status == StatusEnum.FORBIDDEN
266+
? StatusEnum.NORMAL
267+
: StatusEnum.FORBIDDEN,
268+
});
269+
if (affected) {
270+
return '修改成功';
271+
} else {
272+
return '修改失败';
273+
}
274+
}
275+
276+
/**
277+
* @Author: 水痕
278+
* @Date: 2023-10-09 22:36:34
279+
* @LastEditors: 水痕
280+
* @Description: 给商户充值
281+
* @param {RechargeDto} req
282+
* @return {*}
283+
*/
284+
async rechargeTenantApi(req: RechargeDto): Promise<string> {
285+
// 1.当前的要添加
286+
// 2.充值记录中要添加
287+
const queryRunner = this.dataSource.createQueryRunner();
288+
await queryRunner.connect();
289+
await queryRunner.startTransaction(); // 开启事物
290+
try {
291+
const { tenantId, amount } = req;
292+
// 1.插入video表中
293+
const tenant = queryRunner.manager.increment<TenantEntity>(
294+
TenantEntity,
295+
{ id: Equal(tenantId) },
296+
'balance',
297+
amount
298+
);
299+
// TODO 记录表中插入数据
300+
console.log(tenant);
301+
await queryRunner.commitTransaction(); // 提交事务
302+
return '创建成功';
303+
} catch (err) {
304+
console.log(err, '创建失败');
305+
await queryRunner.rollbackTransaction(); // 回滚操作
306+
throw new HttpException('创建失败', HttpStatus.OK);
307+
} finally {
308+
// 最后你需要释放一个手动实例化的queryRunner
309+
await queryRunner.release();
310+
}
311+
}
266312
/**
267313
* @Author: 水痕
268314
* @Date: 2023-10-07 11:05:41

0 commit comments

Comments
 (0)