package approval import ( "encoding/json" "strings" "time" "server/models" "github.com/beego/beego/v2/client/orm" ) func SyncAttendanceFixResult(inst *models.ApprovalInstance, status int8) { if inst == nil || inst.BizID == 0 { return } qs := models.Orm.QueryTable(new(models.OaAttendance)).Filter("id", inst.BizID) if tid := bizTenantID(inst); tid > 0 { qs = qs.Filter("tid", tid) } var row models.OaAttendance if err := qs.One(&row); err != nil { return } now := time.Now() update := orm.Params{"update_time": now, "fix_instance_id": inst.ID} switch status { case models.ApprovalInstanceApproved: update["fix_status"] = models.OaAttendFixApproved fixType, fixTime, fixReason := attendanceFixParams(inst, &row) update["fix_type"] = fixType update["fix_time"] = fixTime update["fix_reason"] = fixReason applyAttendanceFix(&row, update, fixType, fixTime) case models.ApprovalInstanceRejected: update["fix_status"] = models.OaAttendFixRejected case models.ApprovalInstanceWithdrawn, models.ApprovalInstanceCanceled: update["fix_status"] = models.OaAttendFixWithdrawn default: return } _, _ = models.Orm.QueryTable(new(models.OaAttendance)). Filter("id", row.ID).Update(update) } func attendanceFixParams(inst *models.ApprovalInstance, row *models.OaAttendance) (int8, string, string) { fixType, fixTime, fixReason := row.FixType, row.FixTime, row.FixReason if inst == nil || inst.FormData == nil { return fixType, fixTime, fixReason } var m map[string]interface{} if json.Unmarshal([]byte(*inst.FormData), &m) != nil { return fixType, fixTime, fixReason } if v, ok := m["fix_type"].(float64); ok && v > 0 { fixType = int8(v) } if v, ok := m["fix_time"].(string); ok && v != "" { fixTime = v } if v, ok := m["reason"].(string); ok && v != "" { fixReason = v } return fixType, fixTime, fixReason } func applyAttendanceFix(row *models.OaAttendance, update orm.Params, fixType int8, fixTime string) { switch fixType { case models.OaAttendFixTypeIn: note := strings.TrimSpace(row.ClockInNote) if row.ClockInTime == nil { if t, ok := oaFixPunchTime(row.AttendDate, fixTime); ok { update["clock_in_time"] = t note = appendFixNote(note, "补卡通过(补录 "+t.Format("15:04")+")") } else { note = appendFixNote(note, "补卡通过") } } else { note = appendFixNote(note, "补卡通过(原"+oaFixClockText(row.ClockInStatus, false)+")") } update["clock_in_status"] = models.OaAttendClockNormal update["clock_in_note"] = trimFixNote(note) case models.OaAttendFixTypeOut: note := strings.TrimSpace(row.ClockOutNote) if row.ClockOutTime == nil { if t, ok := oaFixPunchTime(row.AttendDate, fixTime); ok { update["clock_out_time"] = t note = appendFixNote(note, "补卡通过(补录 "+t.Format("15:04")+")") } else { note = appendFixNote(note, "补卡通过") } } else { note = appendFixNote(note, "补卡通过(原"+oaFixClockText(row.ClockOutStatus, true)+")") } update["clock_out_status"] = models.OaAttendClockNormal update["clock_out_note"] = trimFixNote(note) } } func oaFixPunchTime(date, hm string) (time.Time, bool) { date = strings.TrimSpace(date) hm = strings.TrimSpace(hm) if len(date) > 10 { date = date[:10] } if date == "" || hm == "" { return time.Time{}, false } t, err := time.ParseInLocation("2006-01-02 15:04", date+" "+hm, time.Local) if err != nil { return time.Time{}, false } return t, true } func oaFixClockText(status int8, isOut bool) string { switch status { case models.OaAttendClockLate: if isOut { return "早退" } return "迟到" case models.OaAttendClockNoShift: return "无排班打卡" case models.OaAttendClockOffTime: return "时段异常" case models.OaAttendClockNone: return "缺卡" } return "异常" } func appendFixNote(note, extra string) string { note = strings.TrimSpace(note) if note == "" { return extra } return note + ";" + extra } func trimFixNote(note string) string { r := []rune(note) if len(r) > 255 { return string(r[:255]) } return note }