|
@@ -46,23 +46,41 @@ function isWeekend(date: Date): boolean {
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
/**
|
|
|
- * 计算从月初到指定日期的工作日天数(排除周末)
|
|
|
|
|
|
|
+ * 计算工作日天数(排除周末)
|
|
|
|
|
+ * 规则:
|
|
|
|
|
+ * - 当前月份:计算到今天的实际工作日天数
|
|
|
|
|
+ * - 过去月份:显示该月全部工作日天数
|
|
|
|
|
+ * - 未来月份:显示0天
|
|
|
* @param year 年份
|
|
* @param year 年份
|
|
|
* @param month 月份 (1-12)
|
|
* @param month 月份 (1-12)
|
|
|
- * @param day 截止日期,默认为当天
|
|
|
|
|
* @returns 工作日天数
|
|
* @returns 工作日天数
|
|
|
*/
|
|
*/
|
|
|
-function calculateWorkDaysUpTo(year: number, month: number, day: number): number {
|
|
|
|
|
|
|
+function calculateWorkDays(year: number, month: number): number {
|
|
|
const today = new Date()
|
|
const today = new Date()
|
|
|
- const targetDay = day || today.getDate()
|
|
|
|
|
- const targetMonth = month || today.getMonth() + 1
|
|
|
|
|
- const targetYear = year || today.getFullYear()
|
|
|
|
|
-
|
|
|
|
|
- // 如果查询的是当前月份,只计算到今天
|
|
|
|
|
- const endDate = targetYear === today.getFullYear() && targetMonth === today.getMonth() + 1
|
|
|
|
|
- ? Math.min(targetDay, today.getDate())
|
|
|
|
|
- : targetDay
|
|
|
|
|
|
|
+ const targetYear = year
|
|
|
|
|
+ const targetMonth = month
|
|
|
|
|
+
|
|
|
|
|
+ // 判断月份相对于当前月份的位置
|
|
|
|
|
+ const currentYear = today.getFullYear()
|
|
|
|
|
+ const currentMonth = today.getMonth() + 1
|
|
|
|
|
+
|
|
|
|
|
+ // 获取该月的最后一天
|
|
|
|
|
+ const lastDayOfMonth = new Date(targetYear, targetMonth, 0).getDate()
|
|
|
|
|
+
|
|
|
|
|
+ let endDate: number
|
|
|
|
|
+
|
|
|
|
|
+ if (targetYear > currentYear || (targetYear === currentYear && targetMonth > currentMonth)) {
|
|
|
|
|
+ // 未来月份:显示0天
|
|
|
|
|
+ return 0
|
|
|
|
|
+ } else if (targetYear === currentYear && targetMonth === currentMonth) {
|
|
|
|
|
+ // 当前月份:计算到今天
|
|
|
|
|
+ endDate = Math.min(today.getDate(), lastDayOfMonth)
|
|
|
|
|
+ } else {
|
|
|
|
|
+ // 过去月份:显示该月全部天数
|
|
|
|
|
+ endDate = lastDayOfMonth
|
|
|
|
|
+ }
|
|
|
|
|
|
|
|
|
|
+ // 计算工作日
|
|
|
let workDays = 0
|
|
let workDays = 0
|
|
|
for (let d = 1; d <= endDate; d++) {
|
|
for (let d = 1; d <= endDate; d++) {
|
|
|
const date = new Date(targetYear, targetMonth - 1, d)
|
|
const date = new Date(targetYear, targetMonth - 1, d)
|
|
@@ -88,12 +106,34 @@ export function generateMockAttendanceData(year: number, month: number): {
|
|
|
const dates = getDatesInMonth(year, month)
|
|
const dates = getDatesInMonth(year, month)
|
|
|
const records: AttendanceRecord[] = []
|
|
const records: AttendanceRecord[] = []
|
|
|
|
|
|
|
|
- // 计算截止到今天的实际工作日天数
|
|
|
|
|
- const normalDays = calculateWorkDaysUpTo(year, month, new Date().getDate())
|
|
|
|
|
|
|
+ // 计算工作日天数(当前月到今天,过去月全部天数,未来月为0)
|
|
|
|
|
+ const normalDays = calculateWorkDays(year, month)
|
|
|
|
|
+
|
|
|
|
|
+ // 确定记录生成的截止日期
|
|
|
|
|
+ const today = new Date()
|
|
|
|
|
+ const currentYear = today.getFullYear()
|
|
|
|
|
+ const currentMonth = today.getMonth() + 1
|
|
|
|
|
+ let lastRecordDay = dates.length // 默认生成整月记录
|
|
|
|
|
+
|
|
|
|
|
+ if (year === currentYear && month === currentMonth) {
|
|
|
|
|
+ // 当前月份:只生成到今天的记录
|
|
|
|
|
+ lastRecordDay = today.getDate()
|
|
|
|
|
+ } else if (year > currentYear || (year === currentYear && month > currentMonth)) {
|
|
|
|
|
+ // 未来月份:不生成任何记录
|
|
|
|
|
+ lastRecordDay = 0
|
|
|
|
|
+ }
|
|
|
|
|
+ // 过去月份:生成整月记录
|
|
|
|
|
|
|
|
- // 从后向前生成记录(倒序)
|
|
|
|
|
|
|
+ // 从后向前生成记录(倒序),但只到截止日期
|
|
|
for (let i = dates.length - 1; i >= 0; i--) {
|
|
for (let i = dates.length - 1; i >= 0; i--) {
|
|
|
const date = dates[i]
|
|
const date = dates[i]
|
|
|
|
|
+ const dayOfMonth = date.getDate()
|
|
|
|
|
+
|
|
|
|
|
+ // 只生成到截止日期
|
|
|
|
|
+ if (dayOfMonth > lastRecordDay) {
|
|
|
|
|
+ continue
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
const weekday = getWeekday(date)
|
|
const weekday = getWeekday(date)
|
|
|
const weekend = isWeekend(date)
|
|
const weekend = isWeekend(date)
|
|
|
|
|
|