首次提交
This commit is contained in:
70
src/service/chat.gateway.ts
Normal file
70
src/service/chat.gateway.ts
Normal file
@@ -0,0 +1,70 @@
|
||||
import {
|
||||
MessageBody,
|
||||
SubscribeMessage,
|
||||
WebSocketGateway,
|
||||
WebSocketServer,
|
||||
ConnectedSocket, OnGatewayInit
|
||||
} from '@nestjs/websockets';
|
||||
|
||||
import {Server, Socket} from 'socket.io';
|
||||
import {ProgramGroupEntity, ProgramGroupMessageEntity, UsersEntity} from "../entity";
|
||||
import {InjectRepository} from "@nestjs/typeorm";
|
||||
import {Repository} from "typeorm";
|
||||
|
||||
@WebSocketGateway({
|
||||
cors: {
|
||||
origin: '*',
|
||||
}
|
||||
})
|
||||
export class ChatGateway implements OnGatewayInit {
|
||||
|
||||
constructor(
|
||||
@InjectRepository(ProgramGroupMessageEntity) private readonly messageRepository: Repository<ProgramGroupMessageEntity>,
|
||||
@InjectRepository(ProgramGroupEntity) private readonly groupRepository: Repository<ProgramGroupEntity>,
|
||||
@InjectRepository(UsersEntity) private readonly usersRepository: Repository<UsersEntity>,
|
||||
) {
|
||||
}
|
||||
|
||||
@WebSocketServer()
|
||||
server: Server;
|
||||
|
||||
afterInit(server: any) {
|
||||
console.log("ChatGateway === 初始化成功")
|
||||
}
|
||||
|
||||
/**
|
||||
* 加入群组
|
||||
* @param groupEntity
|
||||
*/
|
||||
@SubscribeMessage('addGroup')
|
||||
async createGroup(@ConnectedSocket() client: Socket, @MessageBody() groupEntity: ProgramGroupEntity): Promise<any> {
|
||||
// 查找群组信息
|
||||
let group = await this.groupRepository.findOne({
|
||||
where: {
|
||||
groupId: groupEntity.groupId
|
||||
}
|
||||
})
|
||||
if (group != null) {
|
||||
client.join(group.groupId.toString());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 接收客户端消息
|
||||
* @param data
|
||||
*/
|
||||
@SubscribeMessage('clientMsg')
|
||||
async clientMessage(@MessageBody() data: ProgramGroupMessageEntity): Promise<any> {
|
||||
// 保存记录
|
||||
let saveMessage = await this.messageRepository.save(data)
|
||||
|
||||
saveMessage.userInfo = await this.usersRepository.findOne({
|
||||
where: {
|
||||
id: saveMessage.createId
|
||||
}
|
||||
})
|
||||
|
||||
this.server.to(data.groupId.toString()).emit('serverMsg', saveMessage)
|
||||
}
|
||||
|
||||
}
|
||||
2
src/service/index.ts
Normal file
2
src/service/index.ts
Normal file
@@ -0,0 +1,2 @@
|
||||
export * from './programGroupService.service'
|
||||
export * from './chat.gateway'
|
||||
122
src/service/programGroupService.service.ts
Normal file
122
src/service/programGroupService.service.ts
Normal file
@@ -0,0 +1,122 @@
|
||||
import {Injectable} from '@nestjs/common';
|
||||
import {InjectRepository} from "@nestjs/typeorm";
|
||||
import {ProgramEntity, ProgramGroupEntity, UsersEntity} from "../entity";
|
||||
import {Repository} from "typeorm";
|
||||
import {ResultData} from "../const/result";
|
||||
import {isEmptyString} from "../utils/common";
|
||||
|
||||
@Injectable()
|
||||
export class ProgramGroupService {
|
||||
|
||||
constructor(
|
||||
@InjectRepository(ProgramGroupEntity) private readonly programGroupRepository: Repository<ProgramGroupEntity>,
|
||||
@InjectRepository(ProgramEntity) private readonly programRepository: Repository<ProgramEntity>,
|
||||
@InjectRepository(UsersEntity) private readonly usersRepository: Repository<UsersEntity>,
|
||||
) {
|
||||
}
|
||||
|
||||
|
||||
async add(programGroup: ProgramGroupEntity) {
|
||||
try {
|
||||
// 前置判断
|
||||
// 判断是否为空
|
||||
if (isEmptyString(programGroup.programId) || isEmptyString(programGroup.userId)) {
|
||||
return ResultData.fail(500, "参数不能为空")
|
||||
}
|
||||
// 查找program信息
|
||||
let program = await this.programRepository.findOne({
|
||||
where: {
|
||||
id: programGroup.programId
|
||||
}
|
||||
})
|
||||
if (program == null) {
|
||||
return ResultData.fail(500, "未找到项目信息")
|
||||
}
|
||||
// 用户是否有权限发起群组(本人不行,系统用户不行,创建过同样会话不行)
|
||||
if (programGroup.userId == program.linkmanId) {
|
||||
return ResultData.fail(500, "不能创建自己的项目群组")
|
||||
}
|
||||
let user = await this.usersRepository.findOne({
|
||||
where: {
|
||||
id: programGroup.userId
|
||||
}
|
||||
})
|
||||
if (user?.isSystemAccount) {
|
||||
return ResultData.fail(500, "系统用户不能创建群组")
|
||||
}
|
||||
let groups = await this.programGroupRepository.find({
|
||||
where: {
|
||||
programId: programGroup.programId,
|
||||
userId: programGroup.userId
|
||||
}
|
||||
})
|
||||
if (groups.length > 0) {
|
||||
return ResultData.fail(500, "已经创建过群组")
|
||||
}
|
||||
|
||||
// 匹配客服
|
||||
let customers = await this.usersRepository.findBy({userType: "customer"});
|
||||
if (customers.length == 0) {
|
||||
return ResultData.fail(500, "没有客服人员")
|
||||
}
|
||||
|
||||
let customerList = await this.usersRepository.createQueryBuilder("users")
|
||||
.leftJoinAndMapMany("users.groups", "program_group", "group", "users.id = group.customer_id")
|
||||
.where("users.user_type = :type", {type: "customer"})
|
||||
.getMany()
|
||||
|
||||
// 客服分配任务量
|
||||
var index = 0 // 任务最小值
|
||||
for (let i = 0; i < customerList.length - 1; i++) {
|
||||
if (customerList[i].groups.length > customerList[i + 1].groups.length) {
|
||||
index = i + 1
|
||||
}
|
||||
}
|
||||
programGroup.customerId = customerList[index].id
|
||||
|
||||
// 创建聊天
|
||||
programGroup.chargeId = program.linkmanId
|
||||
|
||||
let result = await this.programGroupRepository.save(programGroup)
|
||||
return result != null ? ResultData.success("创建群组成功") : ResultData.fail(500, "创建群组失败");
|
||||
} catch (err) {
|
||||
return ResultData.fail(500, err);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
async getList(programGroup: ProgramGroupEntity) {
|
||||
try {
|
||||
programGroup.page = programGroup.page || 1;
|
||||
programGroup.size = programGroup.size || 10;
|
||||
const offset = (programGroup.page - 1) * programGroup.size;
|
||||
|
||||
const queryBuilder = this.programGroupRepository.createQueryBuilder("group")
|
||||
queryBuilder.leftJoinAndMapOne(
|
||||
"group.program",
|
||||
ProgramEntity,
|
||||
"program",
|
||||
"group.program_id = program.id"
|
||||
)
|
||||
|
||||
|
||||
queryBuilder
|
||||
.orderBy('program.id', 'DESC')
|
||||
.take(programGroup.size || 10)
|
||||
.skip(offset || 0);
|
||||
|
||||
const [items, total] = await queryBuilder.getManyAndCount();
|
||||
|
||||
let data = {
|
||||
list: items,
|
||||
total,
|
||||
page: programGroup.page || 1,
|
||||
size: programGroup.size || 10,
|
||||
pages: Math.ceil(total / (programGroup.size || 10))
|
||||
}
|
||||
return ResultData.success(data);
|
||||
} catch (err) {
|
||||
return ResultData.fail(500, err);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user