193 lines
5.3 KiB
TypeScript
193 lines
5.3 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 db.trainSituation.create(args);
|
||
this.emitDataChanged(CrudOperation.CREATED, result);
|
||
return result;
|
||
}
|
||
// 更新培训情况
|
||
async update(args: Prisma.TrainSituationUpdateArgs) {
|
||
const result = await db.trainSituation.update(args);
|
||
this.emitDataChanged(CrudOperation.UPDATED, result);
|
||
return result;
|
||
}
|
||
|
||
// 查找培训情况
|
||
async findMany(args: Prisma.TrainSituationFindManyArgs) {
|
||
const result = await super.findMany(args);
|
||
return result;
|
||
}
|
||
// 查找某一单位所有人员的培训情况
|
||
async findManyByDeptId(params: {
|
||
deptId?: string;
|
||
domainId?: string;
|
||
trainContentId?: string;
|
||
date?: string;
|
||
}): Promise<
|
||
{
|
||
id: string;
|
||
staffId: string;
|
||
trainContentId: string;
|
||
mustTrainTime: number;
|
||
alreadyTrainTime: number;
|
||
score: number;
|
||
}[]
|
||
> {
|
||
const { deptId, domainId, trainContentId, date } = params;
|
||
|
||
// 构建查询条件
|
||
const where: Prisma.TrainSituationWhereInput = {};
|
||
|
||
if (deptId) {
|
||
const staffs = await this.staffService.findManyByDeptId(deptId);
|
||
where.staffId = { in: staffs.map((s) => s.id) };
|
||
}
|
||
|
||
if (trainContentId) {
|
||
where.trainContentId = trainContentId;
|
||
}
|
||
|
||
// 添加日期过滤条件
|
||
if (date) {
|
||
// 创建日期的开始时间(当天的00:00:00)
|
||
const startDate = new Date(date);
|
||
startDate.setHours(0, 0, 0, 0);
|
||
|
||
// 创建日期的结束时间(当天的23:59:59.999)
|
||
const endDate = new Date(date);
|
||
endDate.setHours(23, 59, 59, 999);
|
||
|
||
// 设置createdAt字段在指定的日期范围内
|
||
where.createdAt = {
|
||
gte: startDate,
|
||
lte: endDate,
|
||
};
|
||
|
||
// 日志输出,方便调试
|
||
console.log(
|
||
`Filtering train situations between ${startDate.toISOString()} and ${endDate.toISOString()}`,
|
||
);
|
||
}
|
||
|
||
// 查询结果并包含关联数据
|
||
const result = await super.findMany({
|
||
where,
|
||
include: {
|
||
staff: {
|
||
include: {
|
||
department: true,
|
||
position: true,
|
||
},
|
||
},
|
||
trainContent: true,
|
||
},
|
||
orderBy: {
|
||
createdAt: 'desc', // 按创建时间倒序排列,最新的记录在前
|
||
},
|
||
});
|
||
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,
|
||
});
|
||
}
|
||
}
|