package controllers import ( "encoding/json" "strings" "time" "server/models" "server/services/gueststore" "github.com/beego/beego/v2/core/logs" ) const ( guestModuleNotebook = "notebook" guestModuleSchedule = "schedule" guestModulePasswordStore = "passwordStore" ) type guestNoteItem struct { ID uint64 `json:"id"` Title string `json:"title"` Content string `json:"content"` Pinned bool `json:"pinned"` CreatedAt string `json:"created_at"` UpdatedAt string `json:"updated_at"` } type guestScheduleItem struct { ID uint64 `json:"id"` Content string `json:"content"` ScheduleTime string `json:"schedule_time"` RemindChannels []string `json:"remind_channels"` AdvanceMinutes int `json:"advance_minutes"` RepeatIntervalMinutes int `json:"repeat_interval_minutes"` MaxSendCount int `json:"max_send_count"` IsFinished bool `json:"is_finished"` CreatedAt string `json:"created_at"` UpdatedAt string `json:"updated_at"` } func mergeGuestToUser(guest *models.GuestUser, uid uint64, tid int, username string) (map[string]int, error) { counts := map[string]int{} if rows, err := gueststore.List(guest.ID, guestModuleNotebook); err != nil { logs.Error("[guest-merge] list notebook failed guest=%d: %v", guest.ID, err) } else { for _, r := range rows { var item guestNoteItem if json.Unmarshal(r.Payload, &item) != nil { continue } userID, name := uid, username pinned := int8(0) if item.Pinned { pinned = 1 } if _, err := models.Orm.Insert(&models.BackendNotebook{ Tid: tid, Title: item.Title, Content: item.Content, Pinned: pinned, UserID: &userID, UserName: &name, }); err != nil { logs.Error("[guest-merge] insert notebook row=%d: %v", r.ID, err) continue } counts[guestModuleNotebook]++ } } if rows, err := gueststore.List(guest.ID, guestModuleSchedule); err != nil { logs.Error("[guest-merge] list schedule failed guest=%d: %v", guest.ID, err) } else { for _, r := range rows { var item guestScheduleItem if json.Unmarshal(r.Payload, &item) != nil { continue } schedTime, terr := time.ParseInLocation("2006-01-02 15:04:05", item.ScheduleTime, time.Local) if terr != nil { logs.Warn("[guest-merge] schedule row=%d 时间非法,跳过", r.ID) continue } title := firstLineOf(item.Content, "日程提醒") schedule := models.BackendSchedule{ Tid: tid, Title: title, Content: item.Content, ScheduleTime: schedTime, UserID: uid, } schedID, ierr := models.Orm.Insert(&schedule) if ierr != nil { logs.Error("[guest-merge] insert schedule row=%d: %v", r.ID, ierr) continue } status := int8(0) if item.IsFinished { status = 2 } for _, ch := range normalizeRemindChannels(item.RemindChannels) { reminder := models.BackendScheduleReminder{ Tid: tid, ScheduleID: uint64(schedID), RemindChannel: ch, AdvanceMinutes: item.AdvanceMinutes, NextRemindTime: schedTime.Add(-time.Duration(item.AdvanceMinutes) * time.Minute), ReceiverUserID: uid, RemindStatus: status, RepeatIntervalMinutes: item.RepeatIntervalMinutes, MaxSendCount: item.MaxSendCount, CreateTime: time.Now(), UpdateTime: time.Now(), } if reminder.MaxSendCount <= 0 { reminder.MaxSendCount = 1 } if _, rerr := models.Orm.Insert(&reminder); rerr != nil { logs.Error("[guest-merge] insert reminder row=%d ch=%s: %v", r.ID, ch, rerr) } } counts[guestModuleSchedule]++ } } if rows, err := gueststore.List(guest.ID, guestModulePasswordStore); err != nil { logs.Error("[guest-merge] list passwordStore failed guest=%d: %v", guest.ID, err) } else { for _, r := range rows { var item PasswordStoreItemDTO if json.Unmarshal(r.Payload, &item) != nil { continue } if item.Platform == "" { continue } if _, err := models.Orm.Insert(&models.BackendPasswordStore{ Tid: tid, UserID: uid, Platform: item.Platform, URL: item.URL, Accounts: accountsToJSON(item.Accounts), Remark: item.Remark, }); err != nil { logs.Error("[guest-merge] insert passwordStore row=%d: %v", r.ID, err) continue } counts[guestModulePasswordStore]++ } } if err := gueststore.MarkMerged(guest.ID); err != nil { return counts, err } return counts, nil } func firstLineOf(content, fallback string) string { title := strings.SplitN(content, "\n", 2)[0] if len(title) > 60 { title = title[:60] } if title == "" { title = fallback } return title } func normalizeRemindChannels(list []string) []string { out := make([]string, 0, len(list)) for _, ch := range list { ch = strings.ToUpper(strings.TrimSpace(ch)) if ch == "APP" { ch = "SITE_MSG" } if ch != "SMS" && ch != "EMAIL" && ch != "BARK" && ch != "SITE_MSG" { continue } out = append(out, ch) } return out }