| 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: Video |
| 7 | */ |
| 8 | import { Injectable } from '@nestjs/common'; |
| 9 | import { InjectModel } from '@nestjs/mongoose'; |
| 10 | import { Model, RootFilterQuery } from 'mongoose'; |
| 11 | import { AccountType } from 'src/db/schema/account.schema'; |
| 12 | import { Video } from 'src/db/schema/video.schema'; |
| 13 | import { TableDto } from 'src/global/dto/table.dto'; |
| 14 | import { VideoPulListDto } from '../dto/video.dto'; |
| 15 | |
| 16 | @Injectable() |
| 17 | export class VideoService { |
| 18 | constructor( |
| 19 | @InjectModel(Video.name) |
| 20 | private readonly videoModel: Model<Video>, |
| 21 | ) {} |
| 22 | |
| 23 | /** |
| 24 | * 创建视频发布数据 |
| 25 | * @param userId |
| 26 | * @param video |
| 27 | * @returns |
| 28 | */ |
| 29 | async newVideoPul(userId: string, video: Partial<Video>) { |
| 30 | video.userId = userId; |
| 31 | return await this.videoModel.create(video); |
| 32 | } |
| 33 | |
| 34 | /** |
| 35 | * 更新视频数据 |
| 36 | * @param id |
| 37 | * @param video |
| 38 | * @returns |
| 39 | */ |
| 40 | async updateVideoPul(id: number, video: Partial<Video>): Promise<boolean> { |
| 41 | const res = await this.videoModel.updateOne({ id }, video); |
| 42 | return res.modifiedCount > 0; |
| 43 | } |
| 44 | |
| 45 | /** |
| 46 | * 获取视频发布信息 |
| 47 | * @param id |
| 48 | * @returns |
| 49 | */ |
| 50 | async getVideoPulInfo(id: number): Promise<Video> { |
| 51 | return await this.videoModel.findOne({ id }); |
| 52 | } |
| 53 | |
| 54 | /** |
| 55 | * 获取发布记录相关的视频发布列表 |
| 56 | * @param pubRecordId |
| 57 | * @returns |
| 58 | */ |
| 59 | async getVideoPulListByPubRecordId(pubRecordId: number): Promise<Video[]> { |
| 60 | const list = await this.videoModel.find({ pubRecordId }); |
| 61 | return list; |
| 62 | } |
| 63 | |
| 64 | // 获取发布记录相关的视频发布列表 |
| 65 | async getVideoPulListByPubRecordIdToShow( |
| 66 | pubRecordId: number, |
| 67 | page: { |
| 68 | page_size: number; |
| 69 | page_no: number; |
| 70 | }, |
| 71 | ) { |
| 72 | const list = await this.videoModel |
| 73 | .find({ pubRecordId }) |
| 74 | .skip((page.page_no - 1) * page.page_size) |
| 75 | .limit(page.page_size) |
| 76 | .sort({ createdAt: -1 }); |
| 77 | |
| 78 | const totalCount = await this.videoModel.countDocuments({ pubRecordId }); |
| 79 | |
| 80 | return { |
| 81 | list, |
| 82 | totalCount, |
| 83 | }; |
| 84 | } |
| 85 | |
| 86 | // 视频发布列表 |
| 87 | async getVideoPulList( |
| 88 | userId: string, |
| 89 | page: TableDto, |
| 90 | query: VideoPulListDto, |
| 91 | ) { |
| 92 | const filter: RootFilterQuery<Video> = { |
| 93 | userId, |
| 94 | ...(query.pubRecordId && { pubRecordId: query.pubRecordId }), |
| 95 | ...(query.title && { title: query.title }), |
| 96 | ...(query.time !== undefined && |
| 97 | query.time.length === 2 && { |
| 98 | createdAt: { $gte: query.time[0], $lte: query.time[1] }, |
| 99 | }), |
| 100 | }; |
| 101 | const list = await this.videoModel |
| 102 | .find(filter) |
| 103 | .skip((page.pageNo - 1) * page.pageSize) |
| 104 | .limit(page.pageSize) |
| 105 | .sort({ createdAt: -1 }); |
| 106 | |
| 107 | return { |
| 108 | list, |
| 109 | totalCount: await this.videoModel.countDocuments(filter), |
| 110 | }; |
| 111 | } |
| 112 | |
| 113 | // 删除视频发布 |
| 114 | async deleteVideoPul(id: number): Promise<boolean> { |
| 115 | const res = await this.videoModel.deleteOne({ id }); |
| 116 | return res.deletedCount > 0; |
| 117 | } |
| 118 | |
| 119 | // 获取不同类型的视频发布的总数 |
| 120 | async getVideoPulTypeCount( |
| 121 | userId: string, |
| 122 | type?: AccountType, |
| 123 | ): Promise<number> { |
| 124 | return await this.videoModel.countDocuments({ userId, type }); |
| 125 | } |
| 126 | |
| 127 | // 删除 |
| 128 | async deleteVideoPulByPubRecordId(pubRecordId: number): Promise<boolean> { |
| 129 | const res = await this.videoModel.deleteMany({ pubRecordId }); |
| 130 | return res.deletedCount > 0; |
| 131 | } |
| 132 | |
| 133 | // 删除 |
| 134 | async deleteByPubRecordAndAccount( |
| 135 | pubRecordId: number, |
| 136 | accountId: number, |
| 137 | ): Promise<boolean> { |
| 138 | const res = await this.videoModel.deleteMany({ |
| 139 | pubRecordId, |
| 140 | accountId, |
| 141 | }); |
| 142 | return res.deletedCount > 0; |
| 143 | } |
| 144 | } |
| 145 |