statistics.go 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. package dao
  2. import (
  3. "context"
  4. "time"
  5. "go-common/app/interface/main/push-archive/model"
  6. "go-common/library/log"
  7. )
  8. const (
  9. _inStatisticsSQL = "INSERT INTO `push_statistics` (`aid`, `group`, `type`, `mids`, `mids_counter`, `ctime`, `mtime`) VALUES(?,?,?,?,?,?,?);"
  10. _statisticsIDRangeSQL = "SELECT coalesce(min(id), 0), coalesce(max(id) , 0) FROM `push_statistics` WHERE `ctime` < ?"
  11. _delStatisticsByIDSQL = "DELETE FROM `push_statistics` WHERE `id` >=? AND `id`<=?;"
  12. )
  13. //SetStatistics 插入一条记录
  14. func (d *Dao) SetStatistics(ctx context.Context, st *model.PushStatistic) (rows int64, err error) {
  15. res, err := d.setStatisticsStmt.Exec(ctx, st.Aid, st.Group, st.Type, st.Mids, st.MidsCounter, st.CTime, time.Now())
  16. if err != nil {
  17. log.Error("SetStatistics() d.setStatisticsStmt.Exec error(%v), pushstatistic(%v)", err, st)
  18. PromError("db:保存统计数据")
  19. return
  20. }
  21. rows, err = res.RowsAffected()
  22. return
  23. }
  24. //GetStatisticsIDRange get id range
  25. func (d *Dao) GetStatisticsIDRange(ctx context.Context, deadline time.Time) (min int64, max int64, err error) {
  26. if err = d.db.QueryRow(ctx, _statisticsIDRangeSQL, deadline).Scan(&min, &max); err != nil {
  27. log.Error("GetStatisticsIDRange() error(%v), deadline(%v)", err, deadline)
  28. PromError("db:查询统计数据")
  29. }
  30. return
  31. }
  32. //DelStatisticsByID delete by id range
  33. func (d *Dao) DelStatisticsByID(ctx context.Context, min, max int64) (rows int64, err error) {
  34. res, err := d.db.Exec(ctx, _delStatisticsByIDSQL, min, max)
  35. if err != nil {
  36. log.Error("DelStatistics() error(%v), min(%d) max(%d)", err, min, max)
  37. PromError("db:删除统计数据")
  38. return
  39. }
  40. rows, err = res.RowsAffected()
  41. return
  42. }