首次提交
This commit is contained in:
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