stream-change-log.go 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. package dao
  2. import (
  3. "context"
  4. "fmt"
  5. "go-common/app/service/video/stream-mng/model"
  6. "go-common/library/database/sql"
  7. "time"
  8. "github.com/pkg/errors"
  9. )
  10. const (
  11. _insertStreamChangeLog = "INSERT INTO %s (room_id, from_origin,to_origin, source,operate_name,reason) VALUES (?,?,?,?,?,?)"
  12. _selectStreamChangelog = "SELECT room_id, from_origin, to_origin, source, operate_name,reason, ctime FROM %s where room_id = ? ORDER BY mtime DESC LIMIT ?"
  13. )
  14. // InsertChangeLog 插入日志
  15. func (d *Dao) InsertChangeLog(c context.Context, change *model.StreamChangeLog) error {
  16. // 判断当前年和月
  17. now := time.Now().Format("200601")
  18. tableName := fmt.Sprintf("stream_change_log_%s", now)
  19. _, err := d.db.Exec(c, fmt.Sprintf(_insertStreamChangeLog, tableName), change.RoomID, change.FromOrigin, change.ToOrigin, change.Source, change.OperateName, change.Reason)
  20. return err
  21. }
  22. // GetChangeLogByRoomID 查询日志
  23. func (d *Dao) GetChangeLogByRoomID(c context.Context, rid int64, limit int64) (infos []*model.StreamChangeLog, err error) {
  24. now := time.Now().Format("200601")
  25. tableName := fmt.Sprintf("stream_change_log_%s", now)
  26. var rows *sql.Rows
  27. if rows, err = d.db.Query(c, fmt.Sprintf(_selectStreamChangelog, tableName), rid, limit); err != nil {
  28. err = errors.WithStack(err)
  29. return
  30. }
  31. defer rows.Close()
  32. for rows.Next() {
  33. info := new(model.StreamChangeLog)
  34. if err = rows.Scan(&info.RoomID, &info.FromOrigin, &info.ToOrigin, &info.Source, &info.OperateName, &info.Reason, &info.CTime); err != nil {
  35. err = errors.WithStack(err)
  36. infos = nil
  37. return
  38. }
  39. infos = append(infos, info)
  40. }
  41. err = rows.Err()
  42. return
  43. }