| 1 | /* |
| 2 | * @Author: nevin |
| 3 | * @Date: 2025-01-24 17:10:35 |
| 4 | * @LastEditors: nevin |
| 5 | * @Description: 发布 |
| 6 | */ |
| 7 | import { AppDataSource } from '../../db'; |
| 8 | import { Injectable } from '../core/decorators'; |
| 9 | import { FindManyOptions, FindOptionsWhere, Repository } from 'typeorm'; |
| 10 | import { CorrectQuery, backPageData } from '../../global/table'; |
| 11 | import { PubRecordModel } from '../../db/models/pubRecord'; |
| 12 | import { EtEvent } from '../../global/event'; |
| 13 | import { getUserInfo } from '../user/comment'; |
| 14 | import { PubStatus } from '../../../commont/publish/PublishEnum'; |
| 15 | @Injectable() |
| 16 | export class PublishService { |
| 17 | private pubRecordRepository: Repository<PubRecordModel>; |
| 18 | constructor() { |
| 19 | this.pubRecordRepository = AppDataSource.getRepository(PubRecordModel); |
| 20 | console.log('PublishService constructor'); |
| 21 | } |
| 22 | |
| 23 | // 创建发布记录 |
| 24 | async createPubRecord(pubRecord: PubRecordModel) { |
| 25 | return await this.pubRecordRepository.save(pubRecord); |
| 26 | } |
| 27 | |
| 28 | // 获取发布记录列表 |
| 29 | async getPubRecordList( |
| 30 | userId: string, |
| 31 | page: CorrectQuery, |
| 32 | query?: FindOptionsWhere<PubRecordModel>, |
| 33 | ) { |
| 34 | const file: FindManyOptions<PubRecordModel> = { |
| 35 | where: { userId: userId, ...query }, |
| 36 | order: { publishTime: 'DESC' }, |
| 37 | skip: (page.page_no - 1) * page.page_size, |
| 38 | }; |
| 39 | const [list, totalCount] = |
| 40 | await this.pubRecordRepository.findAndCount(file); |
| 41 | return backPageData(list, totalCount, page); |
| 42 | } |
| 43 | |
| 44 | // 获取发布记录信息 |
| 45 | async getPubRecordInfo(id: number) { |
| 46 | const userInfo = getUserInfo(); |
| 47 | const pubRecordInfo = await this.pubRecordRepository.findOne({ |
| 48 | where: { id }, |
| 49 | }); |
| 50 | if (!pubRecordInfo || pubRecordInfo.userId !== userInfo.id) { |
| 51 | console.error('发布记录不存在'); |
| 52 | } |
| 53 | return pubRecordInfo; |
| 54 | } |
| 55 | |
| 56 | // 更新发布记录的状态 |
| 57 | async updatePubRecordStatus(id: number, status: PubStatus) { |
| 58 | return await this.pubRecordRepository.update(id, { status }); |
| 59 | } |
| 60 | |
| 61 | // 删除发布记录 |
| 62 | async deletePubRecordById(id: number): Promise<boolean> { |
| 63 | const { affected } = await this.pubRecordRepository.delete(id); |
| 64 | const res = affected ? true : false; |
| 65 | if (res) EtEvent.emit('ET_DEL_PUB_RECORD_ITEM', id); |
| 66 | return res; |
| 67 | } |
| 68 | } |
| 69 |