123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197 |
- package http
- import (
- "encoding/json"
- "math/rand"
- "strconv"
- "strings"
- "time"
- "go-common/app/service/openplatform/abtest/model"
- "go-common/app/service/openplatform/abtest/model/validator"
- "go-common/library/ecode"
- "go-common/library/log"
- bm "go-common/library/net/http/blademaster"
- )
- func versionID(c *bm.Context) {
- params := new(validator.VerionParams)
- if err := c.Bind(params); err != nil {
- return
- }
- c.JSON(abSvr.VersionID(c, params.Group))
- }
- func version(c *bm.Context) {
- var (
- key = c.Request.Form.Get("key")
- verStr = c.Request.Form.Get("version")
- ver = &model.Version{}
- groupStr = c.Request.Form.Get("group")
- appKey = c.Request.Form.Get("appkey")
- group int
- err error
- )
- if verStr != "" {
- if err = json.Unmarshal([]byte(verStr), ver); err != nil {
- log.Warn("[http.stra|version] json.Unmarshal(%s) err: %v", verStr, err)
- }
- }
- if groupStr != "" {
- if group, err = strconv.Atoi(groupStr); err != nil {
- log.Warn("[http.stra|version] strconv.Atoi(group:%s) err: %v", groupStr, err)
- c.JSON(nil, ecode.RequestErr)
- return
- }
- }
- c.JSON(abSvr.Version(c, group, key, ver, appKey))
- }
- func listAb(c *bm.Context) {
- var (
- params = new(validator.ListParams)
- data []*model.AB
- total int
- err error
- )
- if err = c.Bind(params); err != nil {
- return
- }
- if err = checkStatus(params.Mstatus); err != nil {
- c.JSON(nil, ecode.RequestErr)
- return
- }
- if data, total, err = abSvr.ListAb(c, params.Pn, params.Ps, params.Mstatus, params.Group); err != nil {
- log.Error("[http.stra|version] abSvr.ListAb(%d,%d) err: %v", params.Pn, params.Ps, err)
- c.JSON(nil, err)
- return
- }
- c.JSON(map[string]interface{}{
- "result": data,
- "total": total,
- }, nil)
- }
- func addAb(c *bm.Context) {
- var (
- params = new(validator.AddAbParams)
- ab = &model.AB{}
- err error
- )
- if err = c.Bind(params); err != nil {
- return
- }
- // params
- if err = json.Unmarshal([]byte(params.Data), ab); err != nil {
- c.JSON(nil, ecode.RequestErr)
- return
- }
- if ab.Group == 0 {
- ab.Group = params.Group
- }
- if ab.Group == 0 {
- c.JSON(nil, ecode.RequestErr)
- return
- }
- if params.Group != 0 && ab.Group != params.Group {
- c.JSON(nil, ecode.RequestErr)
- return
- }
- if !ab.Stra.Check() {
- c.JSON(nil, ecode.RequestErr)
- return
- }
- rand.Seed(time.Now().Unix())
- c.JSON(abSvr.AddAb(c, ab))
- }
- func updateAb(c *bm.Context) {
- var (
- err error
- ab *model.AB
- srcAb model.AB
- )
- params := new(validator.UpdateAbParams)
- if err = c.Bind(params); err != nil {
- return
- }
- if ab, err = abSvr.Ab(c, params.ID, params.Group); err != nil {
- c.JSON(nil, err)
- return
- }
- if params.Group != 0 && ab.Group != params.Group {
- c.JSON(nil, ecode.RequestErr)
- return
- }
- srcAb = *ab
- if err = json.Unmarshal([]byte(params.Data), ab); err != nil {
- c.JSON(nil, ecode.RequestErr)
- return
- }
- if !ab.Stra.Check() {
- c.JSON(nil, ecode.RequestErr)
- return
- }
- ab.Version = srcAb.Version
- ab.Status = srcAb.Status
- ab.Group = srcAb.Group
- c.JSON(abSvr.UpdateAb(c, params.ID, ab))
- }
- func updateStatus(c *bm.Context) {
- var (
- ab *model.AB
- err error
- )
- params := new(validator.UpdateStatusAbParams)
- if err = c.Bind(params); err != nil {
- return
- }
- if params.Status > 3 || params.Status < 0 {
- c.JSON(nil, ecode.RequestErr)
- return
- }
- if ab, err = abSvr.Ab(c, params.ID, params.Group); err != nil {
- c.JSON(nil, err)
- return
- }
- if params.Group != 0 && ab.Group != params.Group {
- c.JSON(nil, ecode.RequestErr)
- return
- }
- c.JSON(abSvr.UpdateStatus(c, params.ID, params.Status, params.Modifier, ab.Group))
- }
- func deleteAb(c *bm.Context) {
- var (
- ab *model.AB
- err error
- )
- params := new(validator.DelAbParams)
- if err = c.Bind(params); err != nil {
- return
- }
- if ab, err = abSvr.Ab(c, params.ID, params.Group); err != nil {
- c.JSON(nil, err)
- return
- }
- if params.Group != 0 && ab.Group != params.Group {
- c.JSON(nil, ecode.RequestErr)
- return
- }
- c.JSON(abSvr.DeleteAb(c, params.ID))
- }
- func checkStatus(s string) (err error) {
- strs := strings.Split(s, ",")
- for _, a := range strs {
- if _, err = strconv.Atoi(a); err != nil {
- return
- }
- }
- return
- }
- func total(c *bm.Context) {
- c.JSON(abSvr.Total(c))
- }
|