| 1 | import { forwardRef, Inject, Injectable } from '@nestjs/common'; |
| 2 | import { InjectModel } from '@nestjs/mongoose'; |
| 3 | import { Model } from 'mongoose'; |
| 4 | import { |
| 5 | AccountGroup, |
| 6 | AccountGroupDefaultType, |
| 7 | } from '../../../db/schema/accountGroup.schema'; |
| 8 | import { AccountService } from '../account.service'; |
| 9 | import { IdService } from '../../../db/id.service'; |
| 10 | |
| 11 | @Injectable() |
| 12 | export class AccountGroupService { |
| 13 | constructor( |
| 14 | @InjectModel(AccountGroup.name) |
| 15 | private readonly accountGroupModel: Model<AccountGroup>, |
| 16 | @Inject(forwardRef(() => AccountService)) |
| 17 | private readonly accountService: AccountService, |
| 18 | private readonly idService: IdService, |
| 19 | ) {} |
| 20 | |
| 21 | private async getId() { |
| 22 | return this.idService.createId('accountGroupId', 100000000, 1); |
| 23 | } |
| 24 | |
| 25 | /** |
| 26 | * 添加或者更新组 |
| 27 | * @param accountGroup |
| 28 | */ |
| 29 | async addOrUpdateAccountGroup( |
| 30 | accountGroup: Partial<AccountGroup>, |
| 31 | ): Promise<AccountGroup> { |
| 32 | // 更新数据 |
| 33 | if (accountGroup.id) { |
| 34 | return this.accountGroupModel.findOneAndUpdate( |
| 35 | { id: accountGroup.id }, |
| 36 | accountGroup, |
| 37 | { |
| 38 | new: true, |
| 39 | }, |
| 40 | ); |
| 41 | } |
| 42 | |
| 43 | accountGroup.id = await this.getId(); |
| 44 | // 添加数据 |
| 45 | return this.accountGroupModel.create(accountGroup); |
| 46 | } |
| 47 | |
| 48 | /** |
| 49 | * 删除多个组 |
| 50 | * @param ids |
| 51 | * @param userId |
| 52 | */ |
| 53 | async deleteAccountGroup(ids: number[], userId: string): Promise<boolean> { |
| 54 | const accountGorupList = await this.accountGroupModel |
| 55 | .find({ userId, id: { $in: ids } }) |
| 56 | .exec(); |
| 57 | // 默认用户组 |
| 58 | const defaultGroup = await this.getDefaultGroup(userId); |
| 59 | |
| 60 | // 将删除的组下面的账户切换为默认组 |
| 61 | for (const gorup of accountGorupList) { |
| 62 | await this.accountService.switchToDefaultGroup( |
| 63 | userId, |
| 64 | gorup.id, |
| 65 | defaultGroup.id, |
| 66 | ); |
| 67 | } |
| 68 | |
| 69 | // 删除 |
| 70 | const res = await this.accountGroupModel.deleteMany({ |
| 71 | id: { $in: ids }, |
| 72 | userId, |
| 73 | }); |
| 74 | return res.deletedCount > 0; |
| 75 | } |
| 76 | |
| 77 | // 获取默认用户组 |
| 78 | async getDefaultGroup(userId: string) { |
| 79 | return this.accountGroupModel |
| 80 | .findOne({ |
| 81 | userId, |
| 82 | isDefault: AccountGroupDefaultType.Default, |
| 83 | }) |
| 84 | .exec(); |
| 85 | } |
| 86 | |
| 87 | // 创建默认用户组 |
| 88 | async createDefaultGroup(userId: string): Promise<AccountGroup> { |
| 89 | return this.addOrUpdateAccountGroup({ |
| 90 | name: '默认列表', |
| 91 | rank: 0, |
| 92 | createTime: new Date(), |
| 93 | updateTime: new Date(), |
| 94 | userId, |
| 95 | isDefault: AccountGroupDefaultType.Default, |
| 96 | }); |
| 97 | } |
| 98 | |
| 99 | // 获取所有组 |
| 100 | async getAccountGroup(userId: string): Promise<AccountGroup[]> { |
| 101 | const accountGroupList = await this.accountGroupModel |
| 102 | .find({ userId }) |
| 103 | .exec(); |
| 104 | |
| 105 | // 创建默认用户组 |
| 106 | if (accountGroupList.length === 0) { |
| 107 | const accountGroup = await this.createDefaultGroup(userId); |
| 108 | return [accountGroup]; |
| 109 | } |
| 110 | |
| 111 | return accountGroupList; |
| 112 | } |
| 113 | } |
| 114 |