business.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. package http
  2. import (
  3. "strconv"
  4. "time"
  5. "go-common/app/admin/main/push/model"
  6. "go-common/library/ecode"
  7. "go-common/library/log"
  8. bm "go-common/library/net/http/blademaster"
  9. )
  10. func businessList(c *bm.Context) {
  11. var (
  12. items []*model.Business
  13. err error
  14. count int
  15. apps []*model.App
  16. appsMap = make(map[int64]*model.App)
  17. )
  18. pager := new(model.Pager)
  19. if err = c.Bind(pager); err != nil {
  20. return
  21. }
  22. if err = pushSrv.DB.Offset((pager.Pn - 1) * pager.Ps).Limit(pager.Ps).Find(&items).Error; err != nil {
  23. log.Error("businessList(%d,%d) error(%v)", pager.Pn, pager.Ps, err)
  24. c.JSON(nil, err)
  25. return
  26. }
  27. if err = pushSrv.DB.Find(&apps).Error; err != nil {
  28. log.Error("businessList(%d,%d) error(%v)", pager.Pn, pager.Ps, err)
  29. c.JSON(nil, err)
  30. return
  31. }
  32. for _, app := range apps {
  33. appsMap[app.ID] = app
  34. }
  35. for _, item := range items {
  36. if appsMap[item.AppID] != nil {
  37. item.AppName = appsMap[item.AppID].Name
  38. }
  39. }
  40. if err = pushSrv.DB.Model(&model.Business{}).Count(&count).Error; err != nil {
  41. log.Error("businessList count error(%v)", err)
  42. c.JSON(nil, err)
  43. return
  44. }
  45. data := map[string]interface{}{
  46. "data": items,
  47. "pager": &model.Pager{
  48. Pn: pager.Pn,
  49. Ps: pager.Ps,
  50. Total: count,
  51. },
  52. }
  53. c.JSONMap(data, nil)
  54. }
  55. func addBusiness(c *bm.Context) {
  56. biz := new(model.Business)
  57. if err := c.Bind(biz); err != nil {
  58. return
  59. }
  60. biz.Token = model.RandomString(32)
  61. if !pushSrv.DB.Where("app_id=? and name=?", biz.AppID, biz.Name).First(&model.Business{}).RecordNotFound() {
  62. log.Warn("addBusiness(%+v) repeat", biz)
  63. c.JSON(nil, ecode.PushRecordRepeatErr)
  64. return
  65. }
  66. if err := pushSrv.DB.Create(biz).Error; err != nil {
  67. log.Error("addBusiness(%+v) error(%v)", biz, err)
  68. c.JSON(nil, err)
  69. return
  70. }
  71. c.JSON(nil, nil)
  72. }
  73. func businessInfo(c *bm.Context) {
  74. var (
  75. req = c.Request.Form
  76. info = &model.Business{}
  77. )
  78. id, _ := strconv.ParseInt(req.Get("id"), 10, 64)
  79. if id == 0 {
  80. c.JSON(nil, ecode.RequestErr)
  81. return
  82. }
  83. if err := pushSrv.DB.First(info, id).Error; err != nil {
  84. log.Error("businessInfo(%d) error(%v)", id, err)
  85. c.JSON(nil, err)
  86. return
  87. }
  88. c.JSON(info, nil)
  89. }
  90. func saveBusiness(c *bm.Context) {
  91. biz := new(model.Business)
  92. if err := c.Bind(biz); err != nil {
  93. c.JSON(nil, ecode.RequestErr)
  94. return
  95. }
  96. if err := pushSrv.DB.Omit("token", "mtime", "ctime", "dtime").Save(biz).Error; err != nil {
  97. log.Error("saveBusiness(%+v) error(%v)", biz, err)
  98. c.JSON(nil, err)
  99. return
  100. }
  101. c.JSON(nil, nil)
  102. }
  103. func delBusiness(c *bm.Context) {
  104. id, _ := strconv.ParseInt(c.Request.Form.Get("id"), 10, 64)
  105. if id == 0 {
  106. c.JSON(nil, ecode.RequestErr)
  107. return
  108. }
  109. if err := pushSrv.DB.Model(&model.Business{ID: id}).Update("dtime", time.Now().Unix()).Error; err != nil {
  110. log.Error("delBusiness(%d) error(%v)", id, err)
  111. c.JSON(nil, err)
  112. return
  113. }
  114. c.JSON(nil, nil)
  115. }