Compare commits
4 Commits
541d7842ce
...
master
| Author | SHA1 | Date | |
|---|---|---|---|
| 52d0ec9c08 | |||
| 8cbead59d9 | |||
| d9bbd0d922 | |||
| 86e5596aed |
@@ -15,6 +15,7 @@ import * as entity from './entity';
|
||||
database: 'test',
|
||||
synchronize: false,
|
||||
entities: Object.values(entity),
|
||||
timezone: "Z",
|
||||
}),
|
||||
TypeOrmModule.forFeature(Object.values(entity))
|
||||
],
|
||||
|
||||
@@ -20,4 +20,10 @@ export class MessageController {
|
||||
return await this.messageService.read(readDto)
|
||||
}
|
||||
|
||||
// 获取消息记录
|
||||
@Get("list")
|
||||
async getList(@Query() message: ProgramGroupMessageEntity) {
|
||||
return await this.messageService.getList(message)
|
||||
}
|
||||
|
||||
}
|
||||
@@ -21,9 +21,15 @@ export class ProgramGroupController {
|
||||
return await this.programGroupService.getList(programGroup);
|
||||
}
|
||||
|
||||
// 继续聊
|
||||
/** 根据项目和用户获取群 */
|
||||
@Get("one")
|
||||
async getOne(@Param() programGroup: ProgramGroupEntity) {
|
||||
return await this.programGroupService.getOne(programGroup);
|
||||
}
|
||||
|
||||
@Get(":groupId")
|
||||
getGroup(@Param("groupId") groupId: number){
|
||||
return this.programGroupService.getGroup(groupId)
|
||||
}
|
||||
}
|
||||
@@ -1,5 +1,6 @@
|
||||
import {Column, Entity, PrimaryGeneratedColumn} from "typeorm";
|
||||
import {ProgramEntity} from "./program.entity";
|
||||
import {format} from "date-fns";
|
||||
|
||||
@Entity("program_group", {schema: "test"})
|
||||
export class ProgramGroupEntity {
|
||||
@@ -22,15 +23,22 @@ export class ProgramGroupEntity {
|
||||
name: "create_time",
|
||||
comment: "创建时间",
|
||||
default: () => "CURRENT_TIMESTAMP",
|
||||
transformer: {
|
||||
from: (value: Date) => value ? format(value, "yyyy-MM-dd HH:mm:ss") : null,
|
||||
to: (value: number) => value,
|
||||
}
|
||||
})
|
||||
createTime: Date;
|
||||
|
||||
// 虚拟字段
|
||||
program: ProgramEntity;
|
||||
program: ProgramEntity | null;
|
||||
|
||||
page: number = 1;
|
||||
size: number = 10;
|
||||
|
||||
userUnread: any
|
||||
chargeUnread: any
|
||||
customerUnread: any
|
||||
|
||||
keyword: string | null;
|
||||
}
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
import {Column, Entity, PrimaryGeneratedColumn} from "typeorm";
|
||||
import {UsersEntity} from "./users.entity";
|
||||
import {format} from "date-fns";
|
||||
|
||||
@Entity("program_group_message", {schema: "test"})
|
||||
export class ProgramGroupMessageEntity {
|
||||
@@ -31,6 +32,10 @@ export class ProgramGroupMessageEntity {
|
||||
name: "create_time",
|
||||
comment: "发送时间",
|
||||
default: () => "CURRENT_TIMESTAMP",
|
||||
transformer: {
|
||||
to: (value: Date) => value,
|
||||
from: (value: string) => value ? format(value, "yyyy-MM-dd HH:mm:ss") : null
|
||||
}
|
||||
})
|
||||
createTime: Date;
|
||||
|
||||
@@ -38,4 +43,11 @@ export class ProgramGroupMessageEntity {
|
||||
height?: number;
|
||||
|
||||
userInfo: UsersEntity | null;
|
||||
|
||||
userId: number;
|
||||
chargeId: number;
|
||||
|
||||
sort: "create_desc" | "create_asc";
|
||||
page: number;
|
||||
size: number;
|
||||
}
|
||||
|
||||
@@ -1,11 +1,16 @@
|
||||
import {Injectable} from "@nestjs/common";
|
||||
import {ReadDto} from "../entity";
|
||||
import {ProgramGroupMessageEntity, ReadDto, UsersEntity} from "../entity";
|
||||
import {RedisService} from "./redis.service";
|
||||
import {ResultData} from "../const/result";
|
||||
import {InjectRepository} from "@nestjs/typeorm";
|
||||
import {Repository} from "typeorm";
|
||||
|
||||
@Injectable()
|
||||
export class MessageService {
|
||||
constructor(private readonly redisService: RedisService) {
|
||||
constructor(
|
||||
private readonly redisService: RedisService,
|
||||
@InjectRepository(ProgramGroupMessageEntity) private readonly messageRepository: Repository<ProgramGroupMessageEntity>,
|
||||
) {
|
||||
}
|
||||
|
||||
// 获取未读消息
|
||||
@@ -20,4 +25,41 @@ export class MessageService {
|
||||
this.redisService.delValue(`${readDto.groupId}_${readDto.userId}`)
|
||||
return ResultData.success()
|
||||
}
|
||||
|
||||
async getList(message: ProgramGroupMessageEntity) {
|
||||
try {
|
||||
message.page = message.page || 1;
|
||||
message.size = message.size || 10;
|
||||
const offset = (message.page - 1) * message.size;
|
||||
|
||||
let queryBuilder = this.messageRepository.createQueryBuilder("message")
|
||||
|
||||
queryBuilder.leftJoinAndMapOne(
|
||||
"message.userInfo",
|
||||
UsersEntity,
|
||||
"user",
|
||||
"message.create_id = user.id"
|
||||
).where("message.group_id = :groupId", {groupId: message.groupId})
|
||||
|
||||
queryBuilder
|
||||
.orderBy('message.createTime', 'DESC')
|
||||
.take(message.size || 10)
|
||||
.skip(offset || 0);
|
||||
|
||||
|
||||
let [items, total] = await queryBuilder.getManyAndCount()
|
||||
|
||||
let data = {
|
||||
list: items,
|
||||
total,
|
||||
page: message.page || 1,
|
||||
size: message.size || 10,
|
||||
pages: Math.ceil(total / (message.size || 10))
|
||||
}
|
||||
return ResultData.success(data);
|
||||
} catch (err) {
|
||||
return ResultData.fail(500, err.message)
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
@@ -100,6 +100,14 @@ export class ProgramGroupService {
|
||||
"program",
|
||||
"group.program_id = program.id"
|
||||
)
|
||||
|
||||
queryBuilder.leftJoinAndMapOne(
|
||||
"group.chargeUser",
|
||||
UsersEntity,
|
||||
"chargeUser",
|
||||
"group.charge_id = chargeUser.id"
|
||||
)
|
||||
|
||||
queryBuilder.leftJoinAndMapOne(
|
||||
"group.user",
|
||||
UsersEntity,
|
||||
@@ -107,20 +115,6 @@ export class ProgramGroupService {
|
||||
"group.user_id = user.id"
|
||||
)
|
||||
|
||||
// 查询最新消息
|
||||
// queryBuilder.leftJoinAndMapOne(
|
||||
// "group.latest_message",
|
||||
// ProgramGroupMessageEntity,
|
||||
// "message",
|
||||
// `
|
||||
// message.group_id = group.group_id and
|
||||
// group.group_id = (
|
||||
// select group_id from program_group_message
|
||||
// where program_group_message.group_id = group.group_id
|
||||
// order by create_time desc
|
||||
// limit 1
|
||||
// )
|
||||
// `)
|
||||
queryBuilder.addSelect(subQuery => {
|
||||
return subQuery
|
||||
.select("max(message.create_time)")
|
||||
@@ -137,11 +131,23 @@ export class ProgramGroupService {
|
||||
queryBuilder.orWhere("group.charge_id = :charge_id", {charge_id: programGroup.userId})
|
||||
}
|
||||
|
||||
if (programGroup.keyword != null){
|
||||
queryBuilder.andWhere(
|
||||
"(program.name LIKE :nameKeyword OR program.company LIKE :companyKeyword)",
|
||||
{
|
||||
nameKeyword: '%' + programGroup.keyword + '%',
|
||||
companyKeyword: '%' + programGroup.keyword + '%'
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
queryBuilder
|
||||
.orderBy("latest_message_time", 'DESC')
|
||||
.take(programGroup.size || 10)
|
||||
.skip(offset || 0);
|
||||
|
||||
// console.log(queryBuilder.getSql())
|
||||
|
||||
let [items, total] = await queryBuilder.getManyAndCount();
|
||||
|
||||
// 将未读数据(redis)塞入数据(list)中
|
||||
@@ -181,9 +187,38 @@ export class ProgramGroupService {
|
||||
let item = list[i];
|
||||
let userUnreadValue = await this.redisService.getValue(`${item.groupId}_${item.userId}`)
|
||||
let chargeUnreadValue = await this.redisService.getValue(`${item.groupId}_${item.chargeId}`)
|
||||
let customerUnreadValue = await this.redisService.getValue(`${item.groupId}_${item.customerId}`)
|
||||
item.userUnread = userUnreadValue == null ? [] : JSON.parse(userUnreadValue)
|
||||
item.chargeUnread = chargeUnreadValue == null ? [] : JSON.parse(chargeUnreadValue)
|
||||
item.customerUnread = customerUnreadValue == null ? [] : JSON.parse(customerUnreadValue)
|
||||
}
|
||||
return list
|
||||
}
|
||||
|
||||
|
||||
async getGroup(groupId: number) {
|
||||
try {
|
||||
let result = await this.programGroupRepository.findOne({
|
||||
where: {
|
||||
groupId
|
||||
}
|
||||
})
|
||||
if (result != null){
|
||||
result.program = await this.programRepository.createQueryBuilder("program")
|
||||
.leftJoinAndMapOne(
|
||||
"program.linkman",
|
||||
UsersEntity,
|
||||
"linkman",
|
||||
"program.linkman_id = linkman.id"
|
||||
).where("program.id = :id", {id: result.programId})
|
||||
.getOne()
|
||||
return ResultData.success(result)
|
||||
}else {
|
||||
throw "未找到群组信息"
|
||||
}
|
||||
} catch (err) {
|
||||
return ResultData.fail(500, err);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user