增加時間限定可以減少重複簽到的問題以及減少記錄的資料數量
我需要給人員資料庫增加一個欄位 紀錄最後一次簽到的時間 增加限制上次簽到時間與當前時間大於12小時才呢以簽到
這次遇到少數需要修正的bug並且是在語法上的錯誤,如果是從未學過程式的人因該會無從入手我只能人工修正因為大小寫混用倒置的出錯. 且AI會使用階段進行的方式執行,如果AI做到你不想出現的東西時隨時可以暫停
//model.swift
@objc dynamic var lastCheckInTime: Date? = nil
// 執行簽到並寫入資料庫
private func performCheckIn(for user: User, note: String?) {
let realm = try! Realm()
// 檢查上次簽到時間,判斷是否允許再次簽到
if let lastCheckInTime = user.lastCheckInTime {
// 計算上次簽到時間與當前時間的時間差(小時)
let timeInterval = Date().timeIntervalSince(lastCheckInTime) / 3600
// 如果時間差小於 12 小時,則不允許簽到
if timeInterval < 12 {
// 計算還需等待的時間
let hoursLeft = 12 - timeInterval
let minutesLeft = Int((hoursLeft - Double(Int(hoursLeft))) * 60)
let message = String(format: "距離上次簽到時間不足 12 小時,還需等待 %d 小時 %d 分鐘", Int(hoursLeft), minutesLeft)
showErrorAlert(message: message)
return
}
}
// 建立新的簽到記錄
let checkInRecord = CheckInRecord(userId: user.userId, note: note)
do {
try realm.write {
// 添加簽到記錄
realm.add(checkInRecord)
// 更新使用者的最後簽到時間
let userToUpdate = realm.objects(User.self).filter("userId == %@", user.userId).first
userToUpdate?.lastCheckInTime = checkInRecord.checkInTime
}
// 顯示簽到成功訊息
showSuccessAlert(message: "\(user.Name) 簽到成功!時間:\(formatDate(checkInRecord.checkInTime))")
} catch {
showErrorAlert(message: "簽到失敗:\(error.localizedDescription)")
}
}