up_group.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. package http
  2. import (
  3. "go-common/app/service/main/up/model"
  4. "go-common/app/service/main/up/service"
  5. "go-common/library/ecode"
  6. "go-common/library/log"
  7. "go-common/library/net/http/blademaster"
  8. "github.com/go-sql-driver/mysql"
  9. )
  10. func addGroup(c *blademaster.Context) {
  11. var res interface{}
  12. var err error
  13. var errMsg string
  14. var r = new(model.AddGroupArg)
  15. switch {
  16. default:
  17. if err = c.Bind(r); err != nil {
  18. errMsg = err.Error()
  19. err = ecode.RequestErr
  20. log.Error("request error, err=%v, req=%v", err, r)
  21. break
  22. }
  23. dbresult, err2 := Svc.AddGroup(c, r)
  24. if err2 != nil {
  25. log.Error("add group error, err=%v, req=%v", err2, r)
  26. if _, ok := err2.(*mysql.MySQLError); ok {
  27. errMsg = "db error"
  28. } else {
  29. errMsg = err2.Error()
  30. }
  31. err = ecode.ServerErr
  32. break
  33. }
  34. var lastID, _ = dbresult.LastInsertId()
  35. log.Info("add group ok, req=%+v, last id=%d", r, lastID)
  36. }
  37. if err != nil {
  38. service.BmHTTPErrorWithMsg(c, err, errMsg)
  39. } else {
  40. c.JSON(res, err)
  41. }
  42. }
  43. func updateGroup(c *blademaster.Context) {
  44. var res interface{}
  45. var err error
  46. var errMsg string
  47. var r = new(model.EditGroupArg)
  48. switch {
  49. default:
  50. r.AddArg = new(model.AddGroupArg)
  51. if err = c.Bind(r.AddArg); err != nil {
  52. errMsg = err.Error()
  53. err = ecode.RequestErr
  54. log.Error("request error, err=%v, req=%v", err, r)
  55. break
  56. }
  57. if err = c.Bind(r); err != nil {
  58. errMsg = err.Error()
  59. err = ecode.RequestErr
  60. log.Error("request error, err=%v, req=%v", err, r)
  61. break
  62. }
  63. dbresult, err2 := Svc.UpdateGroup(c, r)
  64. if err2 != nil {
  65. log.Error("update group error, err=%v, req=%v", err, r)
  66. if me, ok := err2.(*mysql.MySQLError); ok {
  67. switch me.Number {
  68. case 1062:
  69. errMsg = "重复数据已存在"
  70. default:
  71. errMsg = "db error"
  72. }
  73. } else {
  74. errMsg = err2.Error()
  75. }
  76. err = ecode.ServerErr
  77. break
  78. }
  79. var affected, _ = dbresult.RowsAffected()
  80. log.Info("update group ok, req=%+v, affected count=%d", r, affected)
  81. }
  82. if err != nil {
  83. service.BmHTTPErrorWithMsg(c, err, errMsg)
  84. } else {
  85. c.JSON(res, err)
  86. }
  87. }
  88. func removeGroup(c *blademaster.Context) {
  89. var res interface{}
  90. var err error
  91. var errMsg string
  92. var r = new(model.RemoveGroupArg)
  93. switch {
  94. default:
  95. if err = c.Bind(r); err != nil {
  96. errMsg = err.Error()
  97. err = ecode.RequestErr
  98. log.Error("request error, err=%v, req=%v", err, r)
  99. break
  100. }
  101. dbresult, err2 := Svc.RemoveGroup(c, r)
  102. if err2 != nil {
  103. log.Error("remove group error, err=%v, req=%v", err, r)
  104. errMsg = err2.Error()
  105. err = ecode.ServerErr
  106. break
  107. }
  108. var affected, _ = dbresult.RowsAffected()
  109. log.Info("remove group ok, req=%+v, affected count=%d", r, affected)
  110. }
  111. if err != nil {
  112. service.BmHTTPErrorWithMsg(c, err, errMsg)
  113. } else {
  114. c.JSON(res, err)
  115. }
  116. }
  117. func getGroup(c *blademaster.Context) {
  118. var res interface{}
  119. var err error
  120. var errMsg string
  121. var r = &model.GetGroupArg{State: 1}
  122. switch {
  123. default:
  124. if err = c.Bind(r); err != nil {
  125. errMsg = err.Error()
  126. err = ecode.RequestErr
  127. log.Error("request error, err=%v, req=%v", err, r)
  128. break
  129. }
  130. groups, e := Svc.GetGroup(c, r)
  131. if e != nil {
  132. log.Error("get group error, err=%v", e)
  133. errMsg = e.Error()
  134. err = ecode.ServerErr
  135. break
  136. }
  137. res = groups
  138. log.Info("get group ok, count=%d", len(groups))
  139. }
  140. if err != nil {
  141. service.BmHTTPErrorWithMsg(c, err, errMsg)
  142. } else {
  143. c.JSON(res, err)
  144. }
  145. }