2025-09-30 16:41:10 +08:00
|
|
|
import {Injectable} from "@nestjs/common";
|
2025-10-09 09:18:20 +08:00
|
|
|
import {ProgramGroupMessageEntity, ReadDto} from "../entity";
|
2025-09-30 16:41:10 +08:00
|
|
|
import {RedisService} from "./redis.service";
|
|
|
|
|
import {ResultData} from "../const/result";
|
2025-10-09 09:18:20 +08:00
|
|
|
import {InjectRepository} from "@nestjs/typeorm";
|
|
|
|
|
import {Repository} from "typeorm";
|
2025-09-30 16:41:10 +08:00
|
|
|
|
|
|
|
|
@Injectable()
|
|
|
|
|
export class MessageService {
|
2025-10-09 09:18:20 +08:00
|
|
|
constructor(
|
|
|
|
|
private readonly redisService: RedisService,
|
|
|
|
|
@InjectRepository(ProgramGroupMessageEntity) private readonly messageRepository: Repository<ProgramGroupMessageEntity>,
|
|
|
|
|
) {
|
2025-09-30 16:41:10 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 获取未读消息
|
|
|
|
|
async getUnread(readDto: ReadDto) {
|
|
|
|
|
let data = await this.redisService.getValue(`${readDto.groupId}_${readDto.userId}`)
|
|
|
|
|
data = data == null ? [] : JSON.parse(data)
|
|
|
|
|
return ResultData.success(data)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 未读 -> 已读
|
|
|
|
|
async read(readDto: ReadDto) {
|
|
|
|
|
this.redisService.delValue(`${readDto.groupId}_${readDto.userId}`)
|
|
|
|
|
return ResultData.success()
|
|
|
|
|
}
|
2025-10-09 09:18:20 +08:00
|
|
|
|
|
|
|
|
async getList(message: ProgramGroupMessageEntity) {
|
|
|
|
|
let queryBuilder = this.messageRepository.createQueryBuilder("message")
|
|
|
|
|
|
|
|
|
|
queryBuilder.leftJoinAndMapOne("message.userInfo", "users", "user", "message.create_id = user.id")
|
|
|
|
|
|
|
|
|
|
let [item, total] = await queryBuilder.getManyAndCount()
|
|
|
|
|
|
|
|
|
|
return ResultData.success({
|
|
|
|
|
list: item,
|
|
|
|
|
total
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
}
|
2025-09-30 16:41:10 +08:00
|
|
|
}
|