152 lines
4.2 KiB
TypeScript
152 lines
4.2 KiB
TypeScript
import { Injectable } from '@nestjs/common';
|
|
import { BaseService } from '../base/base.service';
|
|
import { db, ObjectType, Prisma, UserProfile } from '@nice/common';
|
|
import EventBus, { CrudOperation } from '@server/utils/event-bus';
|
|
import { DefaultArgs } from '@prisma/client/runtime/library';
|
|
import { StaffService } from '../staff/staff.service';
|
|
import { SportStandardService } from '../sport-standard/sportStandard.service';
|
|
import { uuidv4 } from 'lib0/random';
|
|
|
|
@Injectable()
|
|
export class TrainSituationService extends BaseService<Prisma.TrainSituationDelegate> {
|
|
constructor(
|
|
private readonly staffService: StaffService,
|
|
private readonly sportStandardService: SportStandardService,
|
|
) {
|
|
super(db, ObjectType.TRAIN_SITUATION, false);
|
|
}
|
|
// 创建培训情况
|
|
async create(args: Prisma.TrainSituationCreateArgs) {
|
|
console.log(args);
|
|
const result = await super.create(args);
|
|
this.emitDataChanged(CrudOperation.CREATED, result);
|
|
return result;
|
|
}
|
|
// 更新培训情况
|
|
async update(args: Prisma.TrainSituationUpdateArgs) {
|
|
const result = await super.update(args);
|
|
this.emitDataChanged(CrudOperation.UPDATED, result);
|
|
return result;
|
|
}
|
|
|
|
// 查找培训情况
|
|
async findMany(args: Prisma.TrainSituationFindManyArgs) {
|
|
const result = await super.findMany(args);
|
|
return result;
|
|
}
|
|
// 查找某一单位所有人员的培训情况
|
|
async findManyByDeptId(args: {
|
|
deptId?: string;
|
|
domainId?: string;
|
|
trainContentId?: string;
|
|
}): Promise<
|
|
{
|
|
id: string;
|
|
staffId: string;
|
|
trainContentId: string;
|
|
mustTrainTime: number;
|
|
alreadyTrainTime: number;
|
|
score: number;
|
|
}[]
|
|
> {
|
|
const staffs = await this.staffService.findByDept({
|
|
deptId: args.deptId,
|
|
domainId: args.domainId,
|
|
});
|
|
const result = await super.findMany({
|
|
where: {
|
|
staffId: {
|
|
in: staffs.map((staff) => staff.id),
|
|
},
|
|
...(args.trainContentId ? { trainContentId: args.trainContentId } : {}),
|
|
},
|
|
});
|
|
return result;
|
|
}
|
|
//async createDailyTrainTime()
|
|
async createTrainSituation(
|
|
args: {
|
|
staffId?: string;
|
|
trainContentId?: string;
|
|
mustTrainTime?: number;
|
|
alreadyTrainTime?: number;
|
|
value?: string;
|
|
|
|
projectUnit?: string;
|
|
personType?: string;
|
|
projectId?: string;
|
|
gender?: boolean;
|
|
age?: number;
|
|
performance?: number | string;
|
|
},
|
|
groupId?: string,
|
|
) {
|
|
console.log('传入的参数', args);
|
|
const score = await this.sportStandardService.getScore({
|
|
projectId: args.projectId,
|
|
gender: args.gender,
|
|
age: args.age,
|
|
performance: args.performance,
|
|
personType: args.personType,
|
|
projectUnit: args.projectUnit,
|
|
});
|
|
console.log('计算出的分数', score);
|
|
const data: Prisma.TrainSituationCreateArgs = {
|
|
data: {
|
|
staffId: args.staffId,
|
|
trainContentId: args.trainContentId,
|
|
mustTrainTime: args.mustTrainTime,
|
|
alreadyTrainTime: args.alreadyTrainTime,
|
|
value: args.value,
|
|
score: score,
|
|
groupId: groupId,
|
|
},
|
|
};
|
|
console.log('创建的数据', data);
|
|
const result = await super.create(data);
|
|
this.emitDataChanged(CrudOperation.CREATED, result);
|
|
return result;
|
|
}
|
|
async createManyTrainSituation(
|
|
args: {
|
|
staffId?: string;
|
|
trainContentId?: string;
|
|
mustTrainTime?: number;
|
|
alreadyTrainTime?: number;
|
|
value?: string;
|
|
|
|
projectUnit?: string;
|
|
personType?: string;
|
|
projectId?: string;
|
|
gender?: boolean;
|
|
age?: number;
|
|
performance?: number | string;
|
|
}[],
|
|
) {
|
|
console.log('传入的参数', args);
|
|
const groupId = uuidv4();
|
|
args.forEach(async (item) => {
|
|
await this.createTrainSituation(item, groupId);
|
|
});
|
|
}
|
|
async deleteSameGroupTrainSituation(args: { groupId?: string }) {
|
|
const { groupId } = args;
|
|
const result = await super.deleteMany({
|
|
where: {
|
|
groupId,
|
|
},
|
|
});
|
|
this.emitDataChanged(CrudOperation.DELETED, result);
|
|
return result;
|
|
}
|
|
|
|
// 发送数据变化事件
|
|
private emitDataChanged(operation: CrudOperation, data: any) {
|
|
EventBus.emit('dataChanged', {
|
|
type: ObjectType.TRAIN_SITUATION,
|
|
operation,
|
|
data,
|
|
});
|
|
}
|
|
}
|