db_project.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. package dao
  2. import (
  3. "go-common/app/admin/ep/saga/conf"
  4. "go-common/app/admin/ep/saga/model"
  5. "github.com/jinzhu/gorm"
  6. pkgerr "github.com/pkg/errors"
  7. )
  8. // ProjectExist check the project exist or not
  9. func (d *Dao) ProjectExist(projID int) (exist bool, err error) {
  10. var count int
  11. if err = pkgerr.WithStack(d.db.Model(&model.ProjectInfo{}).Where(&model.ProjectInfo{ProjectID: projID}).Count(&count).Error); err != nil {
  12. return
  13. }
  14. if count > 0 {
  15. exist = true
  16. }
  17. return
  18. }
  19. // FavoriteProjects get user's favorite projects
  20. func (d *Dao) FavoriteProjects(userName string) (favorites []*model.ProjectFavorite, err error) {
  21. //err = pkgerr.WithStack(d.db.Where(&model.ProjectFavorite{UserName: userName}).Find(&favorites).Error)
  22. err = pkgerr.WithStack(d.db.Where("user_name = ?", userName).Find(&favorites).Error)
  23. return
  24. }
  25. // AddFavorite add favorite project for user
  26. func (d *Dao) AddFavorite(userName string, projID int) (err error) {
  27. return pkgerr.WithStack(d.db.Create(&model.ProjectFavorite{UserName: userName, ProjID: projID}).Error)
  28. }
  29. // DelFavorite delete favorite project for user
  30. func (d *Dao) DelFavorite(userName string, projID int) (err error) {
  31. return pkgerr.WithStack(d.db.Delete(model.ProjectFavorite{UserName: userName, ProjID: projID}).Error)
  32. }
  33. // AddProjectInfo add ProjectInfo
  34. func (d *Dao) AddProjectInfo(projectInfo *model.ProjectInfo) (err error) {
  35. return pkgerr.WithStack(d.db.Create(projectInfo).Error)
  36. }
  37. // ProjectsInfo all the projects in saga
  38. func (d *Dao) ProjectsInfo() (projects []*model.ProjectInfo, err error) {
  39. err = pkgerr.WithStack(d.db.Find(&projects).Error)
  40. return
  41. }
  42. // ProjectInfoByID query ProjectInfo by ID
  43. func (d *Dao) ProjectInfoByID(projectID int) (projectInfo *model.ProjectInfo, err error) {
  44. projectInfo = &model.ProjectInfo{}
  45. err = pkgerr.WithStack(d.db.Where("project_id = ?", projectID).First(projectInfo).Error)
  46. return
  47. }
  48. // UpdateProjectInfo update
  49. func (d *Dao) UpdateProjectInfo(projectID int, projectInfo *model.ProjectInfo) (err error) {
  50. return pkgerr.WithStack(d.db.Model(&model.ProjectInfo{}).Where("project_id = ?", projectID).Update(projectInfo).Error)
  51. }
  52. // HasProjectInfo is exist project info in database.
  53. func (d *Dao) HasProjectInfo(projectID int) (b bool, err error) {
  54. var size int64
  55. if err = pkgerr.WithStack(d.db.Model(&model.ProjectInfo{}).Where("project_id = ?", projectID).Count(&size).Error); err != nil {
  56. return
  57. }
  58. b = size > 0
  59. return
  60. }
  61. // QueryProjectInfo query Project Info.
  62. func (d *Dao) QueryProjectInfo(ifPage bool, req *model.ProjectInfoRequest) (total int, projectInfo []*model.ProjectInfo, err error) {
  63. var (
  64. projectIDs = conf.Conf.Property.DefaultProject.ProjectIDs
  65. db *gorm.DB
  66. )
  67. gDB := d.db.Model(&model.ProjectInfo{})
  68. if req.Name != "" {
  69. gDB = gDB.Where("name = ?", req.Name)
  70. }
  71. if req.Department != "" {
  72. gDB = gDB.Where("department = ?", req.Department)
  73. }
  74. if req.Business != "" {
  75. gDB = gDB.Where("business = ?", req.Business)
  76. }
  77. if err = pkgerr.WithStack(gDB.Count(&total).Error); err != nil {
  78. return
  79. }
  80. if ifPage {
  81. if req.PageNum == 1 {
  82. db = gDB.Order("name DESC").Where("project_id in (?)", projectIDs).Find(&projectInfo)
  83. } else {
  84. db = gDB.Order("name DESC").Offset((req.PageNum - 2) * req.PageSize).Limit(req.PageSize).Find(&projectInfo)
  85. }
  86. } else {
  87. db = gDB.Find(&projectInfo)
  88. }
  89. if db.Error != nil {
  90. if db.RecordNotFound() {
  91. err = nil
  92. } else {
  93. err = pkgerr.WithStack(db.Error)
  94. }
  95. }
  96. return
  97. }
  98. // QueryConfigInfo query saga and runner Info.
  99. func (d *Dao) QueryConfigInfo(name, department, business, queryObject string) (total int, err error) {
  100. gDB := d.db.Model(&model.ProjectInfo{})
  101. if name != "" {
  102. gDB = gDB.Where("name = ?", name)
  103. }
  104. if department != "" {
  105. gDB = gDB.Where("department = ?", department)
  106. }
  107. if business != "" {
  108. gDB = gDB.Where("business = ?", business)
  109. }
  110. if queryObject == model.ObjectSaga {
  111. gDB = gDB.Where("Saga = ?", true)
  112. } else if queryObject == model.ObjectRunner {
  113. gDB = gDB.Where("Runner = ?", true)
  114. } else {
  115. return
  116. }
  117. err = pkgerr.WithStack(gDB.Count(&total).Error)
  118. return
  119. }