audit.go 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. package audit
  2. import (
  3. "context"
  4. "go-common/app/interface/main/app-channel/conf"
  5. "go-common/library/database/sql"
  6. "go-common/library/log"
  7. )
  8. const (
  9. _getSQL = "SELECT mobi_app,build FROM audit"
  10. )
  11. // Dao is audit dao.
  12. type Dao struct {
  13. db *sql.DB
  14. }
  15. // New new a audit dao.
  16. func New(c *conf.Config) (d *Dao) {
  17. d = &Dao{
  18. db: sql.NewMySQL(c.MySQL.Show),
  19. }
  20. return
  21. }
  22. // Audits get all audit build.
  23. func (d *Dao) Audits(ctx context.Context) (res map[string]map[int]struct{}, err error) {
  24. rows, err := d.db.Query(ctx, _getSQL)
  25. if err != nil {
  26. log.Error("query error(%v)", err)
  27. return
  28. }
  29. defer rows.Close()
  30. var (
  31. mobiApp string
  32. build int
  33. )
  34. res = map[string]map[int]struct{}{}
  35. for rows.Next() {
  36. if err = rows.Scan(&mobiApp, &build); err != nil {
  37. log.Error("rows.Scan error(%v)", err)
  38. res = nil
  39. return
  40. }
  41. if plat, ok := res[mobiApp]; ok {
  42. plat[build] = struct{}{}
  43. } else {
  44. res[mobiApp] = map[int]struct{}{
  45. build: struct{}{},
  46. }
  47. }
  48. }
  49. return
  50. }
  51. // Close close memcache resource.
  52. func (dao *Dao) Close() {
  53. if dao.db != nil {
  54. dao.db.Close()
  55. }
  56. }