stra.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. package http
  2. import (
  3. "encoding/json"
  4. "math/rand"
  5. "strconv"
  6. "strings"
  7. "time"
  8. "go-common/app/service/openplatform/abtest/model"
  9. "go-common/app/service/openplatform/abtest/model/validator"
  10. "go-common/library/ecode"
  11. "go-common/library/log"
  12. bm "go-common/library/net/http/blademaster"
  13. )
  14. func versionID(c *bm.Context) {
  15. params := new(validator.VerionParams)
  16. if err := c.Bind(params); err != nil {
  17. return
  18. }
  19. c.JSON(abSvr.VersionID(c, params.Group))
  20. }
  21. func version(c *bm.Context) {
  22. var (
  23. key = c.Request.Form.Get("key")
  24. verStr = c.Request.Form.Get("version")
  25. ver = &model.Version{}
  26. groupStr = c.Request.Form.Get("group")
  27. appKey = c.Request.Form.Get("appkey")
  28. group int
  29. err error
  30. )
  31. if verStr != "" {
  32. if err = json.Unmarshal([]byte(verStr), ver); err != nil {
  33. log.Warn("[http.stra|version] json.Unmarshal(%s) err: %v", verStr, err)
  34. }
  35. }
  36. if groupStr != "" {
  37. if group, err = strconv.Atoi(groupStr); err != nil {
  38. log.Warn("[http.stra|version] strconv.Atoi(group:%s) err: %v", groupStr, err)
  39. c.JSON(nil, ecode.RequestErr)
  40. return
  41. }
  42. }
  43. c.JSON(abSvr.Version(c, group, key, ver, appKey))
  44. }
  45. func listAb(c *bm.Context) {
  46. var (
  47. params = new(validator.ListParams)
  48. data []*model.AB
  49. total int
  50. err error
  51. )
  52. if err = c.Bind(params); err != nil {
  53. return
  54. }
  55. if err = checkStatus(params.Mstatus); err != nil {
  56. c.JSON(nil, ecode.RequestErr)
  57. return
  58. }
  59. if data, total, err = abSvr.ListAb(c, params.Pn, params.Ps, params.Mstatus, params.Group); err != nil {
  60. log.Error("[http.stra|version] abSvr.ListAb(%d,%d) err: %v", params.Pn, params.Ps, err)
  61. c.JSON(nil, err)
  62. return
  63. }
  64. c.JSON(map[string]interface{}{
  65. "result": data,
  66. "total": total,
  67. }, nil)
  68. }
  69. func addAb(c *bm.Context) {
  70. var (
  71. params = new(validator.AddAbParams)
  72. ab = &model.AB{}
  73. err error
  74. )
  75. if err = c.Bind(params); err != nil {
  76. return
  77. }
  78. // params
  79. if err = json.Unmarshal([]byte(params.Data), ab); err != nil {
  80. c.JSON(nil, ecode.RequestErr)
  81. return
  82. }
  83. if ab.Group == 0 {
  84. ab.Group = params.Group
  85. }
  86. if ab.Group == 0 {
  87. c.JSON(nil, ecode.RequestErr)
  88. return
  89. }
  90. if params.Group != 0 && ab.Group != params.Group {
  91. c.JSON(nil, ecode.RequestErr)
  92. return
  93. }
  94. if !ab.Stra.Check() {
  95. c.JSON(nil, ecode.RequestErr)
  96. return
  97. }
  98. rand.Seed(time.Now().Unix())
  99. c.JSON(abSvr.AddAb(c, ab))
  100. }
  101. func updateAb(c *bm.Context) {
  102. var (
  103. err error
  104. ab *model.AB
  105. srcAb model.AB
  106. )
  107. params := new(validator.UpdateAbParams)
  108. if err = c.Bind(params); err != nil {
  109. return
  110. }
  111. if ab, err = abSvr.Ab(c, params.ID, params.Group); err != nil {
  112. c.JSON(nil, err)
  113. return
  114. }
  115. if params.Group != 0 && ab.Group != params.Group {
  116. c.JSON(nil, ecode.RequestErr)
  117. return
  118. }
  119. srcAb = *ab
  120. if err = json.Unmarshal([]byte(params.Data), ab); err != nil {
  121. c.JSON(nil, ecode.RequestErr)
  122. return
  123. }
  124. if !ab.Stra.Check() {
  125. c.JSON(nil, ecode.RequestErr)
  126. return
  127. }
  128. ab.Version = srcAb.Version
  129. ab.Status = srcAb.Status
  130. ab.Group = srcAb.Group
  131. c.JSON(abSvr.UpdateAb(c, params.ID, ab))
  132. }
  133. func updateStatus(c *bm.Context) {
  134. var (
  135. ab *model.AB
  136. err error
  137. )
  138. params := new(validator.UpdateStatusAbParams)
  139. if err = c.Bind(params); err != nil {
  140. return
  141. }
  142. if params.Status > 3 || params.Status < 0 {
  143. c.JSON(nil, ecode.RequestErr)
  144. return
  145. }
  146. if ab, err = abSvr.Ab(c, params.ID, params.Group); err != nil {
  147. c.JSON(nil, err)
  148. return
  149. }
  150. if params.Group != 0 && ab.Group != params.Group {
  151. c.JSON(nil, ecode.RequestErr)
  152. return
  153. }
  154. c.JSON(abSvr.UpdateStatus(c, params.ID, params.Status, params.Modifier, ab.Group))
  155. }
  156. func deleteAb(c *bm.Context) {
  157. var (
  158. ab *model.AB
  159. err error
  160. )
  161. params := new(validator.DelAbParams)
  162. if err = c.Bind(params); err != nil {
  163. return
  164. }
  165. if ab, err = abSvr.Ab(c, params.ID, params.Group); err != nil {
  166. c.JSON(nil, err)
  167. return
  168. }
  169. if params.Group != 0 && ab.Group != params.Group {
  170. c.JSON(nil, ecode.RequestErr)
  171. return
  172. }
  173. c.JSON(abSvr.DeleteAb(c, params.ID))
  174. }
  175. func checkStatus(s string) (err error) {
  176. strs := strings.Split(s, ",")
  177. for _, a := range strs {
  178. if _, err = strconv.Atoi(a); err != nil {
  179. return
  180. }
  181. }
  182. return
  183. }
  184. func total(c *bm.Context) {
  185. c.JSON(abSvr.Total(c))
  186. }