mysql_bugly_version.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. package dao
  2. import (
  3. "database/sql"
  4. "fmt"
  5. "go-common/app/admin/ep/marthe/model"
  6. "go-common/library/ecode"
  7. pkgerr "github.com/pkg/errors"
  8. )
  9. const (
  10. _versionInnerJoinProjectSql = "select a.id,a.version,a.bugly_project_id,a.action,a.task_status,a.update_by,a.ctime,a.mtime,b.project_name,b.exception_type from bugly_versions as a inner join bugly_projects as b on a.bugly_project_id = b.id"
  11. _versionInnerJoinProjectSqlCount = "select count(-1) as totalCount from bugly_versions as a inner join bugly_projects as b on a.bugly_project_id = b.id"
  12. _where = "WHERE"
  13. _and = "AND"
  14. )
  15. // InsertBuglyVersion Insert Bugly Version.
  16. func (d *Dao) InsertBuglyVersion(buglyVersion *model.BuglyVersion) error {
  17. return pkgerr.WithStack(d.db.Create(buglyVersion).Error)
  18. }
  19. // UpdateBuglyVersion Update Bugly Version.
  20. func (d *Dao) UpdateBuglyVersion(buglyVersion *model.BuglyVersion) error {
  21. return pkgerr.WithStack(d.db.Model(&model.BuglyVersion{}).Updates(buglyVersion).Error)
  22. }
  23. // QueryBuglyVersionByVersion Query Bugly Version By Version.
  24. func (d *Dao) QueryBuglyVersionByVersion(version string) (buglyVersion *model.BuglyVersion, err error) {
  25. buglyVersion = &model.BuglyVersion{}
  26. if err = d.db.Where("version = ?", version).First(buglyVersion).Error; err != nil {
  27. if err == ecode.NothingFound {
  28. err = nil
  29. } else {
  30. err = pkgerr.WithStack(err)
  31. }
  32. }
  33. return
  34. }
  35. // QueryBuglyVersion Query Bugly Version .
  36. func (d *Dao) QueryBuglyVersion(id int64) (buglyVersion *model.BuglyVersion, err error) {
  37. buglyVersion = &model.BuglyVersion{}
  38. if err = d.db.Where("id = ?", id).First(buglyVersion).Error; err != nil {
  39. if err == ecode.NothingFound {
  40. err = nil
  41. } else {
  42. err = pkgerr.WithStack(err)
  43. }
  44. }
  45. return
  46. }
  47. // QueryBuglyVersionList Query Bugly Version List.
  48. func (d *Dao) QueryBuglyVersionList() (versionList []string, err error) {
  49. var (
  50. rows *sql.Rows
  51. )
  52. sql := "select DISTINCT version from bugly_versions"
  53. if rows, err = d.db.Raw(sql).Rows(); err != nil {
  54. return
  55. }
  56. defer rows.Close()
  57. for rows.Next() {
  58. var ver string
  59. if err = rows.Scan(&ver); err != nil {
  60. return
  61. }
  62. versionList = append(versionList, ver)
  63. }
  64. return
  65. }
  66. // FindBuglyProjectVersions Find Bugly Project Versions.
  67. func (d *Dao) FindBuglyProjectVersions(req *model.QueryBuglyVersionRequest) (total int64, buglyProjectVersions []*model.BuglyProjectVersion, err error) {
  68. var (
  69. qSQL = _versionInnerJoinProjectSql
  70. cSQL = _versionInnerJoinProjectSqlCount
  71. rows *sql.Rows
  72. )
  73. if req.UpdateBy != "" || req.ProjectName != "" || req.Action > 0 || req.Version != "" {
  74. var (
  75. partSql string
  76. logicalWord = _where
  77. )
  78. if req.UpdateBy != "" {
  79. partSql = fmt.Sprintf("%s %s a.update_by = '%s'", partSql, logicalWord, req.UpdateBy)
  80. logicalWord = _and
  81. }
  82. if req.ProjectName != "" {
  83. partSql = fmt.Sprintf("%s %s b.project_name like '%s'", partSql, logicalWord, _wildcards+req.ProjectName+_wildcards)
  84. logicalWord = _and
  85. }
  86. if req.Action > 0 {
  87. partSql = fmt.Sprintf("%s %s a.action = %d", partSql, logicalWord, req.Action)
  88. logicalWord = _and
  89. }
  90. if req.Version != "" {
  91. partSql = fmt.Sprintf("%s %s a.version like '%s'", partSql, logicalWord, _wildcards+req.Version+_wildcards)
  92. logicalWord = _and
  93. }
  94. qSQL = qSQL + partSql
  95. cSQL = cSQL + partSql
  96. }
  97. cDB := d.db.Raw(cSQL)
  98. if err = pkgerr.WithStack(cDB.Count(&total).Error); err != nil {
  99. return
  100. }
  101. gDB := d.db.Raw(qSQL)
  102. if rows, err = gDB.Order("a.ctime DESC").Offset((req.PageNum - 1) * req.PageSize).Limit(req.PageSize).Rows(); err != nil {
  103. return
  104. }
  105. defer rows.Close()
  106. for rows.Next() {
  107. pv := &model.BuglyProjectVersion{}
  108. if err = rows.Scan(&pv.ID, &pv.Version, &pv.BuglyProjectID, &pv.Action, &pv.TaskStatus, &pv.UpdateBy, &pv.CTime, &pv.MTime, &pv.ProjectName, &pv.ExceptionType); err != nil {
  109. return
  110. }
  111. buglyProjectVersions = append(buglyProjectVersions, pv)
  112. }
  113. return
  114. }
  115. // FindEnableAndReadyVersions Find Enable And Ready Versions.
  116. func (d *Dao) FindEnableAndReadyVersions() (buglyVersions []*model.BuglyVersion, err error) {
  117. err = pkgerr.WithStack(d.db.Where("action = ? and task_status = ?", model.BuglyVersionActionEnable, model.BuglyVersionTaskStatusReady).Find(&buglyVersions).Error)
  118. return
  119. }