| 1 | /* |
| 2 | * @Author: nevin |
| 3 | * @Date: 2024-06-17 19:19:15 |
| 4 | * @LastEditTime: 2024-09-05 15:19:25 |
| 5 | * @LastEditors: nevin |
| 6 | * @Description: MaterialGroup materialGroup |
| 7 | */ |
| 8 | import { Injectable, Logger } from '@nestjs/common' |
| 9 | import { InjectModel } from '@nestjs/mongoose' |
| 10 | import { UserType } from '@yikart/common' |
| 11 | import { Model, RootFilterQuery } from 'mongoose' |
| 12 | import { MaterialGroup, MaterialType } from '../schemas' |
| 13 | import { BaseRepository } from './base.repository' |
| 14 | |
| 15 | @Injectable() |
| 16 | export class MaterialGroupRepository extends BaseRepository<MaterialGroup> { |
| 17 | logger = new Logger(MaterialGroupRepository.name) |
| 18 | constructor( |
| 19 | @InjectModel(MaterialGroup.name) |
| 20 | private readonly materialGroupModel: Model<MaterialGroup>, |
| 21 | ) { |
| 22 | super(materialGroupModel) |
| 23 | } |
| 24 | |
| 25 | override async create(newData: Partial<MaterialGroup>) { |
| 26 | return await this.materialGroupModel.create(newData) |
| 27 | } |
| 28 | |
| 29 | private async createDefaultGroupIfNotExists(userId: string): Promise<boolean> { |
| 30 | try { |
| 31 | // 检查是否已存在默认组 |
| 32 | const existingGroup = await this.materialGroupModel.findOne({ |
| 33 | userId, |
| 34 | userType: UserType.User, |
| 35 | isDefault: true, |
| 36 | }).lean({ virtuals: true }) |
| 37 | |
| 38 | // 如果已存在默认组,直接返回 true |
| 39 | if (existingGroup) { |
| 40 | return true |
| 41 | } |
| 42 | |
| 43 | // 创建默认组 |
| 44 | const newGroup = await this.materialGroupModel.create({ |
| 45 | userId, |
| 46 | name: 'Default', |
| 47 | userType: UserType.User, |
| 48 | isDefault: true, |
| 49 | }) |
| 50 | |
| 51 | return !!newGroup |
| 52 | } |
| 53 | catch (error) { |
| 54 | this.logger.error(`Error creating default group:`, error) |
| 55 | return false |
| 56 | } |
| 57 | } |
| 58 | |
| 59 | async createDefault(userId: string): Promise<boolean> { |
| 60 | try { |
| 61 | const res = await this.createDefaultGroupIfNotExists(userId) |
| 62 | return res |
| 63 | } |
| 64 | catch (error) { |
| 65 | this.logger.error('Error creating default material groups:', error) |
| 66 | return false |
| 67 | } |
| 68 | } |
| 69 | |
| 70 | // 删除 |
| 71 | async delete(id: string): Promise<boolean> { |
| 72 | const res = await this.materialGroupModel.deleteOne({ _id: id }) |
| 73 | return res.deletedCount > 0 |
| 74 | } |
| 75 | |
| 76 | // 修改 |
| 77 | async update(id: string, newData: Partial<MaterialGroup>) { |
| 78 | const res = await this.materialGroupModel.updateOne({ _id: id }, newData) |
| 79 | return res.modifiedCount > 0 |
| 80 | } |
| 81 | |
| 82 | async getInfo(id: string) { |
| 83 | const info = await this.materialGroupModel.findOne({ _id: id }).lean({ virtuals: true }) |
| 84 | return info |
| 85 | } |
| 86 | |
| 87 | async getInfoByName(userId: string, name: string) { |
| 88 | return await this.materialGroupModel.findOne({ userId, name: { $regex: name, $options: 'i' } }).lean({ virtuals: true }) |
| 89 | } |
| 90 | |
| 91 | async getDefaultGroup(userId: string) { |
| 92 | return await this.materialGroupModel.findOne({ userId, isDefault: true }).sort({ createdAt: -1 }).lean({ virtuals: true }) |
| 93 | } |
| 94 | |
| 95 | async listByIds(ids: string[]): Promise<MaterialGroup[]> { |
| 96 | return this.materialGroupModel.find({ _id: { $in: ids } }).lean({ virtuals: true }) |
| 97 | } |
| 98 | |
| 99 | // 获取列表 |
| 100 | async getList(inFilter: { |
| 101 | userId?: string |
| 102 | userType?: UserType |
| 103 | title?: string |
| 104 | type?: MaterialType |
| 105 | }, pageInfo: { |
| 106 | pageNo: number |
| 107 | pageSize: number |
| 108 | }) { |
| 109 | const { pageNo, pageSize } = pageInfo |
| 110 | const filter: RootFilterQuery<MaterialGroup> = { |
| 111 | ...(inFilter.userId && { userId: inFilter.userId }), |
| 112 | ...(inFilter.userType && { userType: inFilter.userType }), |
| 113 | ...(inFilter.type && { type: inFilter.type }), |
| 114 | ...(inFilter.title && { title: { $regex: inFilter.title, $options: 'i' } }), |
| 115 | } |
| 116 | |
| 117 | const [total, list] = await Promise.all([ |
| 118 | this.materialGroupModel.countDocuments(filter), |
| 119 | this.materialGroupModel |
| 120 | .find(filter) |
| 121 | .sort({ createdAt: -1 }) |
| 122 | .skip((pageNo! - 1) * pageSize) |
| 123 | .limit(pageSize) |
| 124 | .lean({ virtuals: true }), |
| 125 | ]) |
| 126 | |
| 127 | return { |
| 128 | total, |
| 129 | list, |
| 130 | } |
| 131 | } |
| 132 | } |
| 133 |