app.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. package service
  2. import (
  3. "go-common/library/log"
  4. bm "go-common/library/net/http/blademaster"
  5. "go-common/app/admin/main/apm/model/app"
  6. "github.com/jinzhu/gorm"
  7. )
  8. // AppAdd Appadd
  9. func (s *Service) AppAdd(c *bm.Context, username string, AppTreeID int64, AppID string, Limit int64) (err error) {
  10. a := &app.App{}
  11. b := &app.Auth{}
  12. tx := s.DB.Begin()
  13. var sqlLogs []*map[string]interface{}
  14. if err = s.DB.Where("app_tree_id = ?", AppTreeID).First(a).Error; err == gorm.ErrRecordNotFound {
  15. //新加
  16. a = &app.App{
  17. AppTreeID: AppTreeID,
  18. AppID: AppID,
  19. Limit: Limit,
  20. }
  21. if err = tx.Create(a).Error; err != nil {
  22. log.Error("s.appAdd create error(%v)", err)
  23. tx.Rollback()
  24. return
  25. }
  26. sqlLog := &map[string]interface{}{
  27. "SQLType": "add",
  28. "Content": a,
  29. }
  30. sqlLogs = append(sqlLogs, sqlLog)
  31. aa := &app.App{}
  32. if a.AppID != "main.common-arch.msm-service" {
  33. if err = tx.Where("app_id=?", "main.common-arch.msm-service").First(aa).Error; err != nil {
  34. log.Error("s.appAdd not find main.common-arch.msm-service error(%v)", err)
  35. tx.Rollback()
  36. return
  37. }
  38. //查询授权
  39. if err = tx.Where("service_tree_id=? and app_tree_id=?", aa.AppTreeID, a.AppTreeID).First(b).Error; err != nil {
  40. //创建msm授权
  41. b = &app.Auth{
  42. ServiceTreeID: aa.AppTreeID,
  43. ServiceID: aa.AppID,
  44. AppTreeID: a.AppTreeID,
  45. AppID: a.AppID,
  46. RPCMethod: "ALL",
  47. HTTPMethod: "ALL",
  48. Quota: 10000000,
  49. }
  50. if err = tx.Create(b).Error; err != nil {
  51. log.Error("s.appAdd main.common-arch.msm-service create error(%v)", err)
  52. tx.Rollback()
  53. return
  54. }
  55. sqlLog := &map[string]interface{}{
  56. "SQLType": "add",
  57. "Content": b,
  58. }
  59. sqlLogs = append(sqlLogs, sqlLog)
  60. }
  61. }
  62. } else if err != nil {
  63. log.Error("s.appAdd app_tree_id first error(%v)", err)
  64. tx.Rollback()
  65. return
  66. } else {
  67. //更新
  68. ups := map[string]interface{}{
  69. "app_id": AppID,
  70. }
  71. if err = tx.Model(a).Where("app_tree_id = ?", AppTreeID).Updates(ups).Error; err != nil {
  72. log.Error("s.appEdit updates error(%v)", err)
  73. tx.Rollback()
  74. return
  75. }
  76. sqlLog := &map[string]interface{}{
  77. "SQLType": "update",
  78. "Where": "app_tree_id = ?",
  79. "Value1": AppTreeID,
  80. "Update": ups,
  81. "Old": "",
  82. }
  83. sqlLogs = append(sqlLogs, sqlLog)
  84. var (
  85. services []*app.Auth
  86. )
  87. if err = tx.Where("app_tree_id = ?", AppTreeID).Find(&services).Error; err == nil {
  88. for _, v := range services {
  89. ups = map[string]interface{}{
  90. "app_id": AppID,
  91. }
  92. if err = tx.Model(&app.Auth{}).Where("id = ?", v.ID).Updates(ups).Error; err != nil {
  93. log.Error("s.appEdit auth2 app_tree_id updates error(%v)", err)
  94. tx.Rollback()
  95. return
  96. }
  97. sqlLog := &map[string]interface{}{
  98. "SQLType": "update",
  99. "Where": "id = ?",
  100. "Value1": v.ID,
  101. "Update": ups,
  102. "Old": v,
  103. }
  104. sqlLogs = append(sqlLogs, sqlLog)
  105. }
  106. }
  107. if err = tx.Where("service_tree_id=?", AppTreeID).Find(&services).Error; err == nil {
  108. for _, v := range services {
  109. ups = map[string]interface{}{
  110. "service_id": AppID,
  111. }
  112. if err = tx.Model(&app.Auth{}).Where("id=?", v.ID).Updates(ups).Error; err != nil {
  113. log.Error("s.appEdit auth2 service_tree_id updates error(%v)", err)
  114. tx.Rollback()
  115. return
  116. }
  117. sqlLog := &map[string]interface{}{
  118. "SQLType": "update",
  119. "Where": "id = ?",
  120. "Value1": v.ID,
  121. "Update": ups,
  122. "Old": v,
  123. }
  124. sqlLogs = append(sqlLogs, sqlLog)
  125. }
  126. }
  127. }
  128. tx.Commit()
  129. s.SendLog(*c, username, 0, 5, 0, "apmSvc.appAdd", sqlLogs)
  130. return
  131. }