mysql_bugly_batch_run.go 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. package dao
  2. import (
  3. "go-common/app/admin/ep/marthe/model"
  4. "go-common/library/ecode"
  5. pkgerr "github.com/pkg/errors"
  6. )
  7. // InsertBuglyBatchRuns Insert Bugly Batch Runs.
  8. func (d *Dao) InsertBuglyBatchRuns(buglyBatchRuns []*model.BuglyBatchRun) (err error) {
  9. tx := d.db.Begin()
  10. defer func() {
  11. if err != nil {
  12. if err == ecode.NothingFound {
  13. err = nil
  14. } else {
  15. err = pkgerr.WithStack(err)
  16. }
  17. }
  18. }()
  19. if err = tx.Error; err != nil {
  20. return
  21. }
  22. for _, buglyBatchRun := range buglyBatchRuns {
  23. if err = d.db.Create(buglyBatchRun).Error; err != nil {
  24. tx.Rollback()
  25. return
  26. }
  27. }
  28. if err = tx.Commit().Error; err != nil {
  29. tx.Rollback()
  30. }
  31. return
  32. }
  33. // InsertBuglyBatchRun Insert Bugly Batch Run.
  34. func (d *Dao) InsertBuglyBatchRun(buglyBatchRun *model.BuglyBatchRun) error {
  35. return pkgerr.WithStack(d.db.Create(buglyBatchRun).Error)
  36. }
  37. // UpdateBuglyBatchRun Update Bugly Batch Run.
  38. func (d *Dao) UpdateBuglyBatchRun(buglyBatchRun *model.BuglyBatchRun) error {
  39. return pkgerr.WithStack(d.db.Model(&model.BuglyBatchRun{}).Where("id=?", buglyBatchRun.ID).Updates(buglyBatchRun).Error)
  40. }
  41. // FindBuglyBatchRuns Find Bugly Batch Runs.
  42. func (d *Dao) FindBuglyBatchRuns(req *model.QueryBuglyBatchRunsRequest) (total int64, buglyBatchRuns []*model.BuglyBatchRun, err error) {
  43. gDB := d.db.Model(&model.BuglyBatchRun{})
  44. if req.Version != "" {
  45. gDB = gDB.Where("version=?", req.Version)
  46. }
  47. if req.Status != 0 {
  48. gDB = gDB.Where("status=?", req.Status)
  49. }
  50. if req.BatchID != "" {
  51. gDB = gDB.Where("batch_id=?", req.BatchID)
  52. }
  53. if err = pkgerr.WithStack(gDB.Count(&total).Error); err != nil {
  54. return
  55. }
  56. err = pkgerr.WithStack(gDB.Order("ctime desc").Offset((req.PageNum - 1) * req.PageSize).Limit(req.PageSize).Find(&buglyBatchRuns).Error)
  57. return
  58. }
  59. // QueryBuglyBatchRunsByStatus Find Bugly Batch Runs By Status.
  60. func (d *Dao) QueryBuglyBatchRunsByStatus(status int) (buglyBatchRuns []*model.BuglyBatchRun, err error) {
  61. err = pkgerr.WithStack(d.db.Where("status = ?", status).Find(&buglyBatchRuns).Error)
  62. return
  63. }
  64. // QueryLastSuccessBatchRunByVersion Find Last Success Batch Run By Version.
  65. func (d *Dao) QueryLastSuccessBatchRunByVersion(version string) (buglyBatchRun *model.BuglyBatchRun, err error) {
  66. buglyBatchRun = &model.BuglyBatchRun{}
  67. if err = d.db.Where("version = ? and status = ?", version, model.BuglyBatchRunStatusDone).Order("id desc").First(&buglyBatchRun).Error; err != nil {
  68. if err == ecode.NothingFound {
  69. err = nil
  70. } else {
  71. err = pkgerr.WithStack(err)
  72. }
  73. }
  74. return
  75. }