@@ -2,8 +2,16 @@ import { HttpException, HttpStatus, Injectable } from '@nestjs/common';
22import { TenantEntity } from './entities/tenant.entity' ;
33
44import { 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' ;
715import { QueryTenantDto } from './dto/tenant.query' ;
816import { PageEnum , StatusEnum } from '@src/enums' ;
917import { 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