201 lines
5.7 KiB
TypeScript
201 lines
5.7 KiB
TypeScript
/** 加班套件:工作日/休息日窗口重叠小时数(与 backend oa_overtime_hours 对齐) */
|
|
|
|
export type OvertimeWindows = {
|
|
workdayOvertimeStart: string
|
|
workdayOvertimeEnd: string
|
|
restBreakStart: string
|
|
restBreakEnd: string
|
|
}
|
|
|
|
export const DEFAULT_OVERTIME_WINDOWS: OvertimeWindows = {
|
|
workdayOvertimeStart: '17:30',
|
|
workdayOvertimeEnd: '08:30',
|
|
restBreakStart: '11:30',
|
|
restBreakEnd: '13:30'
|
|
}
|
|
|
|
export type RestDayResolver = (ymd: string) => boolean
|
|
|
|
const DT_RE = /^(\d{4})-(\d{2})-(\d{2})[ T](\d{2}):(\d{2})$/
|
|
|
|
type Interval = [number, number]
|
|
|
|
function parseHm(hm: string): number | null {
|
|
const m = String(hm || '')
|
|
.trim()
|
|
.match(/^(\d{1,2}):(\d{2})$/)
|
|
if (!m) return null
|
|
const h = Number(m[1])
|
|
const min = Number(m[2])
|
|
if (h < 0 || h > 23 || min < 0 || min > 59) return null
|
|
return h * 60 + min
|
|
}
|
|
|
|
function parseDt(s: string): Date | null {
|
|
const m = String(s || '')
|
|
.trim()
|
|
.match(DT_RE)
|
|
if (!m) return null
|
|
const d = new Date(
|
|
Number(m[1]),
|
|
Number(m[2]) - 1,
|
|
Number(m[3]),
|
|
Number(m[4]),
|
|
Number(m[5]),
|
|
0,
|
|
0
|
|
)
|
|
return Number.isNaN(d.getTime()) ? null : d
|
|
}
|
|
|
|
function formatYmd(d: Date): string {
|
|
const y = d.getFullYear()
|
|
const mo = String(d.getMonth() + 1).padStart(2, '0')
|
|
const da = String(d.getDate()).padStart(2, '0')
|
|
return `${y}-${mo}-${da}`
|
|
}
|
|
|
|
function dayStartMs(d: Date): number {
|
|
return new Date(d.getFullYear(), d.getMonth(), d.getDate()).getTime()
|
|
}
|
|
|
|
function dayAtMinutes(d: Date, minutes: number): number {
|
|
return dayStartMs(d) + minutes * 60000
|
|
}
|
|
|
|
function mergeIntervals(intervals: Interval[]): Interval[] {
|
|
if (intervals.length === 0) return []
|
|
const sorted = [...intervals].sort((a, b) => a[0] - b[0])
|
|
const merged: Interval[] = [[sorted[0][0], sorted[0][1]]]
|
|
for (let i = 1; i < sorted.length; i++) {
|
|
const last = merged[merged.length - 1]
|
|
const [s, e] = sorted[i]
|
|
if (s <= last[1]) {
|
|
last[1] = Math.max(last[1], e)
|
|
} else {
|
|
merged.push([s, e])
|
|
}
|
|
}
|
|
return merged
|
|
}
|
|
|
|
function intersectInterval(a: Interval, b: Interval): Interval | null {
|
|
const lo = Math.max(a[0], b[0])
|
|
const hi = Math.min(a[1], b[1])
|
|
return hi > lo ? [lo, hi] : null
|
|
}
|
|
|
|
function subtractInterval(base: Interval, sub: Interval): Interval[] {
|
|
const [bs, be] = base
|
|
const [ss, se] = sub
|
|
if (se <= bs || ss >= be) return [base]
|
|
const out: Interval[] = []
|
|
if (ss > bs) out.push([bs, ss])
|
|
if (se < be) out.push([se, be])
|
|
return out
|
|
}
|
|
|
|
function subtractIntervals(base: Interval, subs: Interval[]): Interval[] {
|
|
let result: Interval[] = [base]
|
|
for (const sub of subs) {
|
|
const next: Interval[] = []
|
|
for (const r of result) {
|
|
next.push(...subtractInterval(r, sub))
|
|
}
|
|
result = next
|
|
}
|
|
return result
|
|
}
|
|
|
|
function intervalMinutes(intervals: Interval[]): number {
|
|
return intervals.reduce((sum, [a, b]) => sum + (b - a) / 60000, 0)
|
|
}
|
|
|
|
function buildWorkdayWindows(
|
|
anchorDay: Date,
|
|
windows: OvertimeWindows
|
|
): Interval | null {
|
|
const startMin = parseHm(windows.workdayOvertimeStart)
|
|
const endMin = parseHm(windows.workdayOvertimeEnd)
|
|
if (startMin == null || endMin == null) return null
|
|
|
|
const anchorStart = dayAtMinutes(anchorDay, startMin)
|
|
const nextDay = new Date(anchorDay)
|
|
nextDay.setDate(nextDay.getDate() + 1)
|
|
const anchorEnd = dayAtMinutes(nextDay, endMin)
|
|
if (anchorEnd <= anchorStart) return null
|
|
return [anchorStart, anchorEnd]
|
|
}
|
|
|
|
function buildRestDaySegments(day: Date, windows: OvertimeWindows): Interval[] {
|
|
const breakStart = parseHm(windows.restBreakStart)
|
|
const breakEnd = parseHm(windows.restBreakEnd)
|
|
if (breakStart == null || breakEnd == null) return []
|
|
|
|
const day0 = dayStartMs(day)
|
|
const nextDay = new Date(day)
|
|
nextDay.setDate(nextDay.getDate() + 1)
|
|
const dayEnd = dayStartMs(nextDay)
|
|
|
|
const segments: Interval[] = []
|
|
if (breakStart > 0) segments.push([day0, dayAtMinutes(day, breakStart)])
|
|
if (breakEnd < 24 * 60) segments.push([dayAtMinutes(day, breakEnd), dayEnd])
|
|
return segments.filter(([a, b]) => b > a)
|
|
}
|
|
|
|
/**
|
|
* 计算加班时长(小时,1 位小数)。
|
|
* end <= start 或非法输入返回 null。
|
|
*/
|
|
export function calcOvertimeHours(
|
|
startStr: string,
|
|
endStr: string,
|
|
windows: OvertimeWindows = DEFAULT_OVERTIME_WINDOWS,
|
|
isRestDay: RestDayResolver
|
|
): number | null {
|
|
const start = parseDt(startStr)
|
|
const end = parseDt(endStr)
|
|
if (!start || !end || end.getTime() <= start.getTime()) return null
|
|
|
|
const query: Interval = [start.getTime(), end.getTime()]
|
|
const startDay = new Date(start.getFullYear(), start.getMonth(), start.getDate())
|
|
const endDay = new Date(end.getFullYear(), end.getMonth(), end.getDate())
|
|
|
|
const workdayIntervals: Interval[] = []
|
|
const anchor = new Date(startDay)
|
|
anchor.setDate(anchor.getDate() - 1)
|
|
const lastAnchor = new Date(endDay)
|
|
while (anchor.getTime() <= lastAnchor.getTime()) {
|
|
const ymd = formatYmd(anchor)
|
|
if (!isRestDay(ymd)) {
|
|
const win = buildWorkdayWindows(anchor, windows)
|
|
if (win) {
|
|
const hit = intersectInterval(win, query)
|
|
if (hit) workdayIntervals.push(hit)
|
|
}
|
|
}
|
|
anchor.setDate(anchor.getDate() + 1)
|
|
}
|
|
|
|
const workdayMerged = mergeIntervals(workdayIntervals)
|
|
|
|
const restIntervals: Interval[] = []
|
|
const day = new Date(startDay)
|
|
while (day.getTime() <= endDay.getTime()) {
|
|
const ymd = formatYmd(day)
|
|
if (isRestDay(ymd)) {
|
|
for (const seg of buildRestDaySegments(day, windows)) {
|
|
const hit = intersectInterval(seg, query)
|
|
if (hit) {
|
|
const remaining = subtractIntervals(hit, workdayMerged)
|
|
restIntervals.push(...remaining)
|
|
}
|
|
}
|
|
}
|
|
day.setDate(day.getDate() + 1)
|
|
}
|
|
|
|
const totalMinutes = intervalMinutes(workdayMerged) + intervalMinutes(restIntervals)
|
|
return Math.round((totalMinutes / 60) * 10) / 10
|
|
}
|