首次提交
This commit is contained in:
122
src/service/program.service.ts
Normal file
122
src/service/program.service.ts
Normal file
@@ -0,0 +1,122 @@
|
||||
import {Injectable} from "@nestjs/common";
|
||||
import {ProgramEntity, ProgramGroupEntity, UsersEntity} from "../entity";
|
||||
import {Repository} from "typeorm";
|
||||
import {InjectRepository} from '@nestjs/typeorm';
|
||||
import {ResultData} from "../const/result";
|
||||
|
||||
@Injectable()
|
||||
export class ProgramService {
|
||||
|
||||
constructor(
|
||||
@InjectRepository(ProgramEntity) private readonly programRepository: Repository<ProgramEntity>,
|
||||
@InjectRepository(ProgramGroupEntity) private readonly groupRepository: Repository<ProgramGroupEntity>,
|
||||
) {
|
||||
}
|
||||
|
||||
async getList(program: ProgramEntity) {
|
||||
try {
|
||||
program.page = program.page || 1;
|
||||
program.size = program.size || 10;
|
||||
const offset = (program.page - 1) * program.size;
|
||||
|
||||
const queryBuilder = this.programRepository.createQueryBuilder('program');
|
||||
// 关联用户
|
||||
queryBuilder.leftJoinAndMapOne(
|
||||
'program.user',
|
||||
UsersEntity,
|
||||
'users',
|
||||
'users.id = program.linkman_id'
|
||||
);
|
||||
|
||||
if (program.keyword) {
|
||||
queryBuilder.orWhere('program.name like :name', {name: `%${program.keyword}%`});
|
||||
}
|
||||
|
||||
if (program.linkmanId) {
|
||||
queryBuilder.orWhere('program.linkmanId like :linkmanId', {linkmanId: `%${program.linkmanId}%`});
|
||||
}
|
||||
|
||||
if (program.keyword) {
|
||||
queryBuilder.orWhere('program.company like :company', {company: `%${program.keyword}%`});
|
||||
}
|
||||
|
||||
queryBuilder
|
||||
.orderBy('program.createDate', 'DESC')
|
||||
.take(program.size || 10)
|
||||
.skip(offset || 0);
|
||||
|
||||
const [items, total] = await queryBuilder.getManyAndCount();
|
||||
|
||||
let data = {
|
||||
list: items,
|
||||
total,
|
||||
page: program.page || 1,
|
||||
size: program.size || 10,
|
||||
pages: Math.ceil(total / (program.size || 10))
|
||||
}
|
||||
return ResultData.success(data);
|
||||
} catch (err) {
|
||||
return ResultData.fail(500, err.message);
|
||||
}
|
||||
}
|
||||
|
||||
async add(program: ProgramEntity) {
|
||||
try {
|
||||
let result = await this.programRepository.save(program)
|
||||
return result != null ? ResultData.success() : ResultData.fail();
|
||||
} catch (err) {
|
||||
return ResultData.fail(500, err);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
async getById(id: number) {
|
||||
try {
|
||||
let result = await this.programRepository.createQueryBuilder('program')
|
||||
.leftJoinAndMapOne(
|
||||
'program.user',
|
||||
UsersEntity,
|
||||
'users',
|
||||
'users.id = program.linkman_id'
|
||||
).where('program.id = :id', {id: id}).getOne()
|
||||
|
||||
|
||||
return result != null ? ResultData.success(result) : ResultData.fail();
|
||||
} catch (err) {
|
||||
return ResultData.fail(500, err);
|
||||
}
|
||||
}
|
||||
|
||||
async update(program: ProgramEntity) {
|
||||
try {
|
||||
let data = await this.programRepository.update(program.id, program)
|
||||
return (data.affected != null && data.affected > 0) ? ResultData.success() : ResultData.fail(500, "修改失败");
|
||||
} catch (err) {
|
||||
return ResultData.fail(500, err);
|
||||
}
|
||||
}
|
||||
|
||||
async delete(id: number) {
|
||||
try {
|
||||
let data = await this.programRepository.delete(id)
|
||||
return (data.affected != null && data.affected > 0) ? ResultData.success() : ResultData.fail(500, "删除失败");
|
||||
} catch (err) {
|
||||
return ResultData.fail(500, err);
|
||||
}
|
||||
}
|
||||
|
||||
// 是否进行聊天
|
||||
async isChat(group: ProgramGroupEntity) {
|
||||
try {
|
||||
let result = await this.groupRepository.findOne({
|
||||
where: {
|
||||
programId: group.programId,
|
||||
userId: group.userId
|
||||
}
|
||||
})
|
||||
return ResultData.success({isChat: result != null}, "success")
|
||||
} catch (err) {
|
||||
return ResultData.fail(500, err.message);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user