235 lines
7.7 KiB
TypeScript
235 lines
7.7 KiB
TypeScript
import { prisma } from '@/lib/prisma'
|
|
|
|
export type PerformanceSurveyQuestion = {
|
|
id: string
|
|
title: string
|
|
description?: string
|
|
}
|
|
|
|
export type PerformanceSurveyScoreBand = {
|
|
min: number
|
|
max: number
|
|
score: number
|
|
}
|
|
|
|
export type PerformanceSurveyTemplateShape = {
|
|
code: string
|
|
name: string
|
|
description: string
|
|
surveyType: string
|
|
weight: number
|
|
questions: PerformanceSurveyQuestion[]
|
|
scoreBands: PerformanceSurveyScoreBand[]
|
|
}
|
|
|
|
export const DEFAULT_PERFORMANCE_SURVEY_TEMPLATES: PerformanceSurveyTemplateShape[] = [
|
|
{
|
|
code: 'course_share',
|
|
name: '课程/分享调研',
|
|
description: '适用于课程培训、经验分享、内部活动后的满意度与效果反馈。',
|
|
surveyType: 'item_course',
|
|
weight: 1,
|
|
questions: [
|
|
{ id: 'content_value', title: '内容价值', description: '内容对工作是否有帮助' },
|
|
{ id: 'clarity', title: '表达清晰度', description: '讲解逻辑、重点和案例是否清楚' },
|
|
{ id: 'practicality', title: '可落地程度', description: '是否能转化为实际行动' },
|
|
{ id: 'interaction', title: '互动与答疑', description: '现场互动和问题回应情况' },
|
|
],
|
|
scoreBands: [
|
|
{ min: 0, max: 12, score: 60 },
|
|
{ min: 12, max: 15, score: 75 },
|
|
{ min: 15, max: 18, score: 90 },
|
|
{ min: 18, max: 20, score: 100 },
|
|
],
|
|
},
|
|
{
|
|
code: 'cross_department',
|
|
name: '跨部门协作考核',
|
|
description: '适用于跨部门项目、流程配合、协同响应等人员周期考核。',
|
|
surveyType: 'person_period',
|
|
weight: 1,
|
|
questions: [
|
|
{ id: 'response_speed', title: '响应速度', description: '需求响应是否及时' },
|
|
{ id: 'delivery_quality', title: '交付质量', description: '交付结果是否稳定可靠' },
|
|
{ id: 'communication', title: '沟通协同', description: '沟通是否主动、清晰、闭环' },
|
|
{ id: 'ownership', title: '责任意识', description: '是否能主动推进问题解决' },
|
|
],
|
|
scoreBands: [
|
|
{ min: 0, max: 12, score: 60 },
|
|
{ min: 12, max: 15, score: 75 },
|
|
{ min: 15, max: 18, score: 90 },
|
|
{ min: 18, max: 20, score: 100 },
|
|
],
|
|
},
|
|
{
|
|
code: 'work_cooperation',
|
|
name: '日常工作配合考核',
|
|
description: '适用于常规工作协作、服务支持、团队内互评。',
|
|
surveyType: 'person_period',
|
|
weight: 1,
|
|
questions: [
|
|
{ id: 'professionalism', title: '专业能力', description: '专业判断和处理问题能力' },
|
|
{ id: 'reliability', title: '可靠程度', description: '承诺事项是否稳定完成' },
|
|
{ id: 'service_attitude', title: '服务态度', description: '协作态度和服务意识' },
|
|
{ id: 'improvement', title: '改进意识', description: '是否持续优化工作方式' },
|
|
],
|
|
scoreBands: [
|
|
{ min: 0, max: 12, score: 60 },
|
|
{ min: 12, max: 15, score: 75 },
|
|
{ min: 15, max: 18, score: 90 },
|
|
{ min: 18, max: 20, score: 100 },
|
|
],
|
|
},
|
|
]
|
|
|
|
export async function ensurePerformanceSurveyTemplates() {
|
|
for (const template of DEFAULT_PERFORMANCE_SURVEY_TEMPLATES) {
|
|
await prisma.performanceSurveyTemplate.upsert({
|
|
where: { code: template.code },
|
|
update: {
|
|
name: template.name,
|
|
description: template.description,
|
|
surveyType: template.surveyType,
|
|
weight: template.weight,
|
|
questions: template.questions,
|
|
scoreBands: template.scoreBands,
|
|
isActive: true,
|
|
},
|
|
create: {
|
|
code: template.code,
|
|
name: template.name,
|
|
description: template.description,
|
|
surveyType: template.surveyType,
|
|
weight: template.weight,
|
|
questions: template.questions,
|
|
scoreBands: template.scoreBands,
|
|
isActive: true,
|
|
},
|
|
})
|
|
}
|
|
}
|
|
|
|
export async function listPerformanceSurveyTemplates() {
|
|
await ensurePerformanceSurveyTemplates()
|
|
|
|
const templates = await prisma.performanceSurveyTemplate.findMany({
|
|
where: { isActive: true },
|
|
orderBy: [{ surveyType: 'asc' }, { createdAt: 'asc' }],
|
|
})
|
|
|
|
return templates.map((template) => ({
|
|
code: template.code,
|
|
name: template.name,
|
|
description: template.description,
|
|
surveyType: template.surveyType,
|
|
weight: template.weight,
|
|
questions: template.questions,
|
|
scoreBands: template.scoreBands,
|
|
}))
|
|
}
|
|
|
|
export async function getPerformanceSurveyTemplate(code: string) {
|
|
await ensurePerformanceSurveyTemplates()
|
|
|
|
const template = await prisma.performanceSurveyTemplate.findUnique({
|
|
where: { code },
|
|
})
|
|
|
|
if (!template || !template.isActive) {
|
|
throw new Error('考核调研模板不存在')
|
|
}
|
|
|
|
return {
|
|
code: template.code,
|
|
name: template.name,
|
|
description: template.description || '',
|
|
surveyType: template.surveyType,
|
|
weight: template.weight,
|
|
questions: template.questions as PerformanceSurveyQuestion[],
|
|
scoreBands: template.scoreBands as PerformanceSurveyScoreBand[],
|
|
}
|
|
}
|
|
|
|
export function calculateResponseScores(
|
|
template: Pick<PerformanceSurveyTemplateShape, 'questions' | 'scoreBands' | 'weight'>,
|
|
scores: Record<string, unknown>,
|
|
) {
|
|
const questionScores = template.questions.map((question) => {
|
|
const value = Number(scores[question.id])
|
|
if (!Number.isFinite(value) || value < 0 || value > 20) {
|
|
throw new Error(`评分项「${question.title}」必须是 0-20 分`)
|
|
}
|
|
return value
|
|
})
|
|
|
|
const totalScore =
|
|
questionScores.length > 0
|
|
? questionScores.reduce((sum, score) => sum + score, 0) / questionScores.length
|
|
: 0
|
|
|
|
const kpiScore = mapAchievementToKpiScore(totalScore, template.scoreBands)
|
|
|
|
return {
|
|
totalScore: roundScore(totalScore),
|
|
achievementScore: roundScore(totalScore),
|
|
kpiScore: roundScore(kpiScore),
|
|
weightedScore: roundScore(kpiScore * Number(template.weight || 1)),
|
|
}
|
|
}
|
|
|
|
export function calculateAggregateScore(
|
|
responses: Array<{ totalScore: number; createdAt: Date | string }>,
|
|
scoreBands: PerformanceSurveyScoreBand[],
|
|
weight: number,
|
|
) {
|
|
const sorted = [...responses].sort((left, right) => {
|
|
const scoreDiff = Number(left.totalScore || 0) - Number(right.totalScore || 0)
|
|
if (scoreDiff !== 0) return scoreDiff
|
|
return new Date(String(left.createdAt)).getTime() - new Date(String(right.createdAt)).getTime()
|
|
})
|
|
|
|
const effective =
|
|
sorted.length >= 3
|
|
? sorted.slice(1, -1)
|
|
: sorted
|
|
|
|
const achievementScore =
|
|
effective.length > 0
|
|
? effective.reduce((sum, response) => sum + Number(response.totalScore || 0), 0) / effective.length
|
|
: 0
|
|
const kpiScore = mapAchievementToKpiScore(achievementScore, scoreBands)
|
|
|
|
return {
|
|
responseCount: responses.length,
|
|
effectiveCount: effective.length,
|
|
droppedHighestLowest: sorted.length >= 3,
|
|
achievementScore: roundScore(achievementScore),
|
|
kpiScore: roundScore(kpiScore),
|
|
weightedScore: roundScore(kpiScore * Number(weight || 1)),
|
|
}
|
|
}
|
|
|
|
function mapAchievementToKpiScore(value: number, bands: PerformanceSurveyScoreBand[]) {
|
|
const normalizedBands = [...bands].sort((left, right) => left.min - right.min)
|
|
const matched = normalizedBands.find((band) => value >= band.min && value <= band.max)
|
|
if (!matched) {
|
|
if (value < normalizedBands[0]?.min) return normalizedBands[0]?.score || 0
|
|
return normalizedBands[normalizedBands.length - 1]?.score || 0
|
|
}
|
|
|
|
const previous = normalizedBands
|
|
.filter((band) => band.max <= matched.min)
|
|
.sort((left, right) => right.max - left.max)[0]
|
|
|
|
if (!previous || matched.max === matched.min) {
|
|
return matched.score
|
|
}
|
|
|
|
const ratio = (value - matched.min) / (matched.max - matched.min)
|
|
return previous.score + ratio * (matched.score - previous.score)
|
|
}
|
|
|
|
function roundScore(value: number) {
|
|
return Math.round(Number(value || 0) * 100) / 100
|
|
}
|