| 1 | /* |
| 2 | * @Author: nevin |
| 3 | * @Date: 2025-01-24 17:10:35 |
| 4 | * @LastEditors: nevin |
| 5 | * @Description: 视频发布服务 |
| 6 | */ |
| 7 | import { VideoModel } from '../../../db/models/video'; |
| 8 | import { AppDataSource } from '../../../db'; |
| 9 | import { Injectable } from '../../core/decorators'; |
| 10 | import { FindManyOptions, FindOptionsWhere, Repository } from 'typeorm'; |
| 11 | import { CorrectQuery, backPageData } from '../../../global/table'; |
| 12 | import { getUserInfo } from '../../user/comment'; |
| 13 | import { PlatType } from '../../../../commont/AccountEnum'; |
| 14 | |
| 15 | @Injectable() |
| 16 | export class VideoPubService { |
| 17 | private videoRepository: Repository<VideoModel>; |
| 18 | constructor() { |
| 19 | this.videoRepository = AppDataSource.getRepository(VideoModel); |
| 20 | console.log('VideoPubService constructor'); |
| 21 | } |
| 22 | |
| 23 | // 创建视频发布数据 |
| 24 | async newVideoPul(video: VideoModel) { |
| 25 | video.userId = getUserInfo().id; |
| 26 | return await this.videoRepository.save(video); |
| 27 | } |
| 28 | |
| 29 | // 更新视频数据 |
| 30 | async updateVideoPul(videoModel: VideoModel) { |
| 31 | return await this.videoRepository.update(videoModel.id!, videoModel); |
| 32 | } |
| 33 | |
| 34 | // 根据dataid更新数据 |
| 35 | async updateVideoPulByDataId(videoModel: Partial<VideoModel>) { |
| 36 | return await this.videoRepository.update( |
| 37 | { dataId: videoModel.dataId }, |
| 38 | videoModel, |
| 39 | ); |
| 40 | } |
| 41 | |
| 42 | // 获取视频发布信息 |
| 43 | async getVideoPulInfo(id: number) { |
| 44 | return await this.videoRepository.findOne({ where: { id } }); |
| 45 | } |
| 46 | |
| 47 | // 获取发布记录相关的视频发布列表 |
| 48 | async getVideoPulListByPubRecordId(pubRecordId: number) { |
| 49 | const file: FindManyOptions<VideoModel> = { |
| 50 | where: { pubRecordId: pubRecordId }, |
| 51 | }; |
| 52 | return await this.videoRepository.find(file); |
| 53 | } |
| 54 | |
| 55 | // 获取发布记录相关的视频发布列表 |
| 56 | async getVideoPulListByPubRecordIdToShow( |
| 57 | pubRecordId: number, |
| 58 | page: CorrectQuery, |
| 59 | ) { |
| 60 | const file: FindManyOptions<VideoModel> = { |
| 61 | where: { pubRecordId: pubRecordId }, |
| 62 | order: { publishTime: 'DESC' }, |
| 63 | skip: (page.page_no - 1) * page.page_size, |
| 64 | take: page.page_size, |
| 65 | }; |
| 66 | const [list, totalCount] = await this.videoRepository.findAndCount(file); |
| 67 | return backPageData(list, totalCount, page); |
| 68 | } |
| 69 | |
| 70 | // 视频发布列表 |
| 71 | async getVideoPulList( |
| 72 | userId: string, |
| 73 | page: CorrectQuery, |
| 74 | query?: FindOptionsWhere<VideoModel>, |
| 75 | ) { |
| 76 | const file: FindManyOptions<VideoModel> = { |
| 77 | where: { userId: userId, ...query }, |
| 78 | order: { publishTime: 'DESC' }, |
| 79 | skip: (page.page_no - 1) * page.page_size, |
| 80 | take: page.page_size, |
| 81 | }; |
| 82 | const [list, totalCount] = await this.videoRepository.findAndCount(file); |
| 83 | return backPageData(list, totalCount, page); |
| 84 | } |
| 85 | |
| 86 | // 删除视频发布 |
| 87 | async deleteVideoPul(id: number) { |
| 88 | return await this.videoRepository.delete(id); |
| 89 | } |
| 90 | |
| 91 | // 获取不同类型的视频发布的总数 |
| 92 | async getVideoPulTypeCount(userId: string, type?: PlatType) { |
| 93 | return await this.videoRepository.count({ |
| 94 | where: { userId: userId, type: type }, |
| 95 | }); |
| 96 | } |
| 97 | |
| 98 | // 删除 |
| 99 | async deleteVideoPulByPubRecordId(pubRecordId: number): Promise<boolean> { |
| 100 | const res = await this.videoRepository.delete({ pubRecordId }); |
| 101 | return res.affected ? true : false; |
| 102 | } |
| 103 | |
| 104 | // 删除 |
| 105 | async deleteByPubRecordAndAccount( |
| 106 | pubRecordId: number, |
| 107 | accountId: number, |
| 108 | ): Promise<boolean> { |
| 109 | const res = await this.videoRepository.delete({ pubRecordId, accountId }); |
| 110 | return res.affected ? true : false; |
| 111 | } |
| 112 | } |
| 113 |