人事请假问题优化

This commit is contained in:
runny committed 2026-05-26 11:39:27 +08:00
1 parent c14046f9f3
commit b25c4c6797
1 file changed
+151 -34
+151 -34
View File
@@ -925,45 +925,162 @@ const calculateDurationHours = (startTime: string, endTime: string) => {
const formatDurationLabel = (hours: number) => {
if (!hours) return `0 ${t('chat.hourUnit')}`;
if (hours >= 8 && Number.isInteger((hours / 8) * 10) && (hours / 8) >= 1) {
const days = Number((hours / 8).toFixed(2));
return `${days} ${t('chat.dayUnit')} / ${hours} ${t('chat.hourUnit')}`;
}
return `${hours} ${t('chat.hourUnit')}`;
};
const LEAVE_TYPE_MAP: Record<string, string> = {
'事假': '005',
'病假': '006',
'丧假': '007',
'年假': '010',
'因公出差': '021',
'出差': '021',
'调休': '025',
'产假': '401',
'陪产假': '402',
'婚假': '403',
'住院': '405',
'工伤': '406',
'育儿假': '408',
'返乡': 'ZZ01',
};
const extractLeaveTypeFromIntent = (content: string) => {
if (!content) return '';
const map: Record<string, string> = {
'事假': '005',
'病假': '006',
'丧假': '007',
'年假': '010',
'因公出差': '021',
'出差': '021',
'调休': '025',
'产假': '401',
'陪产假': '402',
'婚假': '403',
'住院': '405',
'工伤': '406',
'育儿假': '408',
'返乡': 'ZZ01',
};
for (const [key, val] of Object.entries(map)) {
if (content.includes(key)) {
return val;
}
for (const [key, val] of Object.entries(LEAVE_TYPE_MAP)) {
if (content.includes(key)) return val;
}
return '';
};
const createLeaveFormData = (content?: string): LeaveRequestFormData => {
interface ExtractedLeaveInfo {
leaveType: string;
startTime: string;
endTime: string;
durationHours: number;
}
const extractLeaveInfoFromIntent = (content: string): ExtractedLeaveInfo | null => {
const normalized = content.replace(/\s+/g, ' ').trim();
let leaveType = extractLeaveTypeFromIntent(normalized);
let startTime = '';
let endTime = '';
let durationHours = 0;
const today = new Date();
const thisYear = today.getFullYear();
const parseDateStr = (m: RegExpMatchArray | null, groupOffset: number): Date | null => {
if (!m) return null;
const mo = parseInt(m[groupOffset], 10);
const da = parseInt(m[groupOffset + 1], 10);
if (Number.isNaN(mo) || Number.isNaN(da) || mo < 1 || mo > 12 || da < 1 || da > 31) return null;
return new Date(thisYear, mo - 1, da, 9, 0);
};
const fmtDt = (d: Date, hour: number, minute: number) => {
const y = d.getFullYear();
const mo = String(d.getMonth() + 1).padStart(2, '0');
const da = String(d.getDate()).padStart(2, '0');
const h = String(hour).padStart(2, '0');
const mi = String(minute).padStart(2, '0');
return `${y}-${mo}-${da}T${h}:${mi}`;
};
let startDay: Date | null = null;
let endDay: Date | null = null;
let startHM = { h: 9, m: 0 };
let endHM = { h: 18, m: 0 };
const rangeM = normalized.match(/从?(\d{1,2})[月./-](\d{1,2})[日号]?[到至—\-–](\d{1,2})[月./-]?(\d{1,2})[日号]?/);
if (rangeM) {
const s = parseDateStr(rangeM, 1);
if (rangeM[3] && rangeM[3].length <= 2 && rangeM[4] && rangeM[4].length <= 2) {
const eMo = parseInt(rangeM[3], 10);
const eDa = parseInt(rangeM[4], 10);
if (!Number.isNaN(eMo) && eMo >= 1 && eMo <= 12 && !Number.isNaN(eDa) && eDa >= 1 && eDa <= 31) {
endDay = new Date(thisYear, eMo - 1, eDa, 18, 0);
} else if (!Number.isNaN(eDa) && eDa >= 1 && eDa <= 31 && s) {
endDay = new Date(thisYear, s.getMonth(), eDa, 18, 0);
}
}
if (s) startDay = s;
}
if (!startDay) {
const singleM = normalized.match(/(\d{1,2})[月./-](\d{1,2})[日号]?/);
if (singleM) {
const s = parseDateStr(singleM, 1);
if (s) {
startDay = s;
const dayUnitM = normalized.match(/(\d+\.?\d*)\s*[天日]/);
if (dayUnitM) {
const days = parseFloat(dayUnitM[1]);
if (days > 1) endDay = new Date(s.getFullYear(), s.getMonth(), s.getDate() + Math.ceil(days) - 1, 18, 0);
else endDay = new Date(s.getFullYear(), s.getMonth(), s.getDate(), 18, 0);
} else {
endDay = new Date(s.getFullYear(), s.getMonth(), s.getDate(), 18, 0);
}
}
}
}
const hourM = normalized.match(/(\d+\.?\d*)\s*小时/);
if (hourM) {
durationHours = parseFloat(hourM[1]);
if (startDay && !endDay) {
endDay = new Date(startDay.getTime() + durationHours * 3600000);
}
}
const dayM = normalized.match(/(\d+\.?\d*)\s*[天日]/);
if (dayM && !hourM) {
const days = parseFloat(dayM[1]);
durationHours = days * 8;
if (startDay && !endDay) {
endDay = new Date(startDay.getFullYear(), startDay.getMonth(), startDay.getDate() + Math.ceil(days) - 1, 18, 0);
}
}
if (startDay) {
if (durationHours > 0 && durationHours <= 8 && !endDay) {
endDay = new Date(startDay.getTime() + durationHours * 3600000);
}
startTime = fmtDt(startDay, startHM.h, startHM.m);
}
if (endDay) {
if (hourM && durationHours <= 8) {
endHM = { h: 9 + Math.floor(durationHours), m: (durationHours % 1) * 60 };
} else if (!hourM && durationHours > 0 && durationHours <= 8) {
endHM = { h: 9 + Math.floor(durationHours), m: 0 };
}
endTime = fmtDt(endDay, endHM.h, endHM.m);
}
if (startTime && endTime) {
durationHours = durationHours || calculateDurationHours(startTime, endTime);
}
if (!leaveType && !startTime) return null;
return {
leaveType,
startTime,
endTime,
durationHours,
};
};
const createLeaveFormData = (content?: string, extractedInfo?: ExtractedLeaveInfo | null): LeaveRequestFormData => {
const now = new Date();
const oneHourLater = new Date(now.getTime() + 60 * 60 * 1000);
const startTime = formatDateTime(now);
const endTime = formatDateTime(oneHourLater);
const initialLeaveType = extractLeaveTypeFromIntent(content || '');
const defaultStart = formatDateTime(now);
const defaultEnd = formatDateTime(oneHourLater);
const initialLeaveType = extractedInfo?.leaveType || extractLeaveTypeFromIntent(content || '');
const startTime = extractedInfo?.startTime || defaultStart;
const endTime = extractedInfo?.endTime || defaultEnd;
const durationHours = extractedInfo?.durationHours || calculateDurationHours(startTime, endTime);
return {
applicantName: currentUserDisplayName.value,
employeeId: currentUserEmployeeId.value,
@@ -976,7 +1093,7 @@ const createLeaveFormData = (content?: string): LeaveRequestFormData => {
leaveType: initialLeaveType,
startTime,
endTime,
durationHours: calculateDurationHours(startTime, endTime),
durationHours,
isOutOfOffice: 1,
isWorkInjury: 1,
};
@@ -995,11 +1112,10 @@ const getLeaveIntentNormalized = (content: string) => (content || '').replace(/\
const isStrongLeaveIntent = (content: string) => {
const normalized = getLeaveIntentNormalized(content);
// 弱意图(咨询流程/步骤)应走正常问答,不触发表单分流。
const weakConsultationIntent = /(请假流程|请假怎么走|请假步骤|请假如何申请|请假如何走流程|请假怎么办|请假需要什么|请假规定)/;
if (weakConsultationIntent.test(normalized)) return false;
if (/^(请假|休假|年假|事假|病假|调休)$/.test(normalized)) return true;
return /(我要请|申请请|发起请|请假申请|我要休|申请休|想请|帮我请|代我请|提交请)/.test(normalized);
if (/^(请假|休假|年假|事假|病假|调休|休年假|请事假|请病假|请年假)$/.test(normalized)) return true;
return /(我要请|申请请|发起请|请假申请|我要休|申请休|想请|帮我请|代我请|提交请|帮我申请|我要申请|想申请|申请.*假|请.*假|休.*假)/.test(normalized);
};
const saveLocalMessageToBackend = async (msg: any, userQuery: string = '') => {
@@ -1063,7 +1179,8 @@ const checkLeaveBalance = async (leaveTypeCode: string): Promise<{
};
const startLocalLeaveFlow = async (content: string) => {
const initialLeaveType = extractLeaveTypeFromIntent(content);
const extractedInfo = extractLeaveInfoFromIntent(content);
const initialLeaveType = extractedInfo?.leaveType || extractLeaveTypeFromIntent(content);
if (initialLeaveType && NEED_BALANCE_CHECK_TYPES.has(initialLeaveType)) {
messages.value.push({ role: 'user', content, type: 'text' });
@@ -1111,7 +1228,7 @@ const startLocalLeaveFlow = async (content: string) => {
const formId = newLocalMessageId('leave-form');
const payload: LeaveFormPayload = {
formId,
form: createLeaveFormData(content),
form: createLeaveFormData(content, extractedInfo),
submitting: false,
confirmationGenerated: false,
};