2025-03-31 20:44:33 +08:00
|
|
|
|
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';
|
2025-03-11 16:15:05 +08:00
|
|
|
|
|
|
|
|
|
|
@Injectable()
|
|
|
|
|
|
export class TrainSituationService extends BaseService<Prisma.TrainSituationDelegate> {
|
2025-03-31 20:44:33 +08:00
|
|
|
|
constructor(
|
|
|
|
|
|
private readonly staffService: StaffService,
|
|
|
|
|
|
private readonly sportStandardService: SportStandardService,
|
|
|
|
|
|
) {
|
|
|
|
|
|
super(db, ObjectType.TRAIN_SITUATION, false);
|
|
|
|
|
|
}
|
|
|
|
|
|
// 创建培训情况
|
|
|
|
|
|
async create(args: Prisma.TrainSituationCreateArgs) {
|
|
|
|
|
|
console.log(args);
|
2025-04-06 19:44:22 +08:00
|
|
|
|
const result = await db.trainSituation.create(args);
|
2025-03-31 20:44:33 +08:00
|
|
|
|
this.emitDataChanged(CrudOperation.CREATED, result);
|
|
|
|
|
|
return result;
|
|
|
|
|
|
}
|
|
|
|
|
|
// 更新培训情况
|
|
|
|
|
|
async update(args: Prisma.TrainSituationUpdateArgs) {
|
2025-04-06 19:44:22 +08:00
|
|
|
|
const result = await db.trainSituation.update(args);
|
2025-03-31 20:44:33 +08:00
|
|
|
|
this.emitDataChanged(CrudOperation.UPDATED, result);
|
|
|
|
|
|
return result;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 查找培训情况
|
|
|
|
|
|
async findMany(args: Prisma.TrainSituationFindManyArgs) {
|
|
|
|
|
|
const result = await super.findMany(args);
|
|
|
|
|
|
return result;
|
|
|
|
|
|
}
|
|
|
|
|
|
// 查找某一单位所有人员的培训情况
|
2025-04-06 19:44:22 +08:00
|
|
|
|
async findManyByDeptId(params: {
|
2025-03-31 20:44:33 +08:00
|
|
|
|
deptId?: string;
|
|
|
|
|
|
domainId?: string;
|
|
|
|
|
|
trainContentId?: string;
|
2025-04-06 19:44:22 +08:00
|
|
|
|
date?: string;
|
2025-03-31 20:44:33 +08:00
|
|
|
|
}): Promise<
|
2025-03-11 22:36:54 +08:00
|
|
|
|
{
|
2025-03-31 20:44:33 +08:00
|
|
|
|
id: string;
|
|
|
|
|
|
staffId: string;
|
|
|
|
|
|
trainContentId: string;
|
|
|
|
|
|
mustTrainTime: number;
|
|
|
|
|
|
alreadyTrainTime: number;
|
|
|
|
|
|
score: number;
|
|
|
|
|
|
}[]
|
|
|
|
|
|
> {
|
2025-04-06 19:44:22 +08:00
|
|
|
|
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()}`,
|
|
|
|
|
|
);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 查询结果并包含关联数据
|
2025-03-31 20:44:33 +08:00
|
|
|
|
const result = await super.findMany({
|
2025-04-06 19:44:22 +08:00
|
|
|
|
where,
|
|
|
|
|
|
include: {
|
|
|
|
|
|
staff: {
|
|
|
|
|
|
include: {
|
|
|
|
|
|
department: true,
|
|
|
|
|
|
position: true,
|
|
|
|
|
|
},
|
2025-03-31 20:44:33 +08:00
|
|
|
|
},
|
2025-04-06 19:44:22 +08:00
|
|
|
|
trainContent: true,
|
|
|
|
|
|
},
|
|
|
|
|
|
orderBy: {
|
|
|
|
|
|
createdAt: 'desc', // 按创建时间倒序排列,最新的记录在前
|
2025-03-31 20:44:33 +08:00
|
|
|
|
},
|
|
|
|
|
|
});
|
|
|
|
|
|
return result;
|
|
|
|
|
|
}
|
|
|
|
|
|
//async createDailyTrainTime()
|
|
|
|
|
|
async createTrainSituation(
|
|
|
|
|
|
args: {
|
|
|
|
|
|
staffId?: string;
|
|
|
|
|
|
trainContentId?: string;
|
|
|
|
|
|
mustTrainTime?: number;
|
|
|
|
|
|
alreadyTrainTime?: number;
|
|
|
|
|
|
value?: string;
|
2025-03-27 08:50:22 +08:00
|
|
|
|
|
2025-03-31 20:44:33 +08:00
|
|
|
|
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;
|
2025-03-27 08:50:22 +08:00
|
|
|
|
|
2025-03-31 20:44:33 +08:00
|
|
|
|
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;
|
|
|
|
|
|
}
|
2025-03-27 08:50:22 +08:00
|
|
|
|
|
2025-03-31 20:44:33 +08:00
|
|
|
|
// 发送数据变化事件
|
|
|
|
|
|
private emitDataChanged(operation: CrudOperation, data: any) {
|
|
|
|
|
|
EventBus.emit('dataChanged', {
|
|
|
|
|
|
type: ObjectType.TRAIN_SITUATION,
|
|
|
|
|
|
operation,
|
|
|
|
|
|
data,
|
|
|
|
|
|
});
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|