report.go 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. package archive
  2. import (
  3. "context"
  4. "time"
  5. "go-common/app/job/main/videoup-report/model/archive"
  6. "go-common/library/database/sql"
  7. "go-common/library/log"
  8. )
  9. const (
  10. _reportAddByTypeIDSQL = "INSERT INTO archive_report_sum(content,ctime,mtime,type) VALUE(?,?,?,?)"
  11. _reportGetByTypeIDSQL = "SELECT id,content,ctime,mtime,type FROM archive_report_sum WHERE type=? AND mtime>=? AND mtime<=?"
  12. _reportLastByTypeIDSQL = "SELECT id,content,ctime,mtime,type FROM archive_report_sum WHERE type=? ORDER BY id DESC LIMIT 1"
  13. )
  14. // ReportLast get last inserted report
  15. func (d *Dao) ReportLast(c context.Context, typeid int8) (report *archive.Report, err error) {
  16. row := d.db.QueryRow(c, _reportLastByTypeIDSQL, typeid)
  17. report = &archive.Report{}
  18. if err = row.Scan(&report.ID, &report.Content, &report.CTime, &report.MTime, &report.TypeID); err != nil {
  19. if err == sql.ErrNoRows {
  20. report = nil
  21. err = nil
  22. } else {
  23. log.Error("row.Scan error(%v)", err)
  24. }
  25. return
  26. }
  27. return
  28. }
  29. // ReportAdd report add of typeid
  30. func (d *Dao) ReportAdd(c context.Context, typeid int8, content string, ctime, mtime time.Time) (lastID int64, err error) {
  31. res, err := d.db.Exec(c, _reportAddByTypeIDSQL, content, ctime, mtime, typeid)
  32. if err != nil {
  33. log.Error("d.TaskTookAddStmt.Exec error(%v)", err)
  34. return
  35. }
  36. lastID, err = res.LastInsertId()
  37. return
  38. }
  39. // Reports report get of typeid
  40. func (d *Dao) Reports(c context.Context, typeid int8, stime, etime time.Time) (reports []*archive.Report, err error) {
  41. rows, err := d.db.Query(c, _reportGetByTypeIDSQL, typeid, stime, etime)
  42. if err != nil {
  43. log.Error("d.Reports.Query error(%v)", err)
  44. return
  45. }
  46. defer rows.Close()
  47. for rows.Next() {
  48. report := &archive.Report{}
  49. if err = rows.Scan(&report.ID, &report.Content, &report.CTime, &report.MTime, &report.TypeID); err != nil {
  50. log.Error("rows.Scan error(%v)", err)
  51. return
  52. }
  53. reports = append(reports, report)
  54. }
  55. return
  56. }