version.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. package http
  2. import (
  3. "go-common/app/admin/main/tv/model"
  4. "go-common/library/ecode"
  5. "go-common/library/log"
  6. bm "go-common/library/net/http/blademaster"
  7. "go-common/library/time"
  8. )
  9. const (
  10. _platform = 12
  11. _isDeleted = 1
  12. )
  13. func versionList(c *bm.Context) {
  14. var (
  15. req = c.Request.Form
  16. err error
  17. items []*model.Version
  18. count int64
  19. ver = req.Get("ver")
  20. page = atoi(req.Get("page"))
  21. size = 20
  22. )
  23. if page == 0 {
  24. page = 1
  25. }
  26. db := tvSrv.DBShow.Where("plat=?", _platform).Where("state!=?", _isDeleted)
  27. if ver != "" {
  28. db = db.Where("version=?", ver)
  29. }
  30. db.Model(&model.Version{}).Count(&count)
  31. if err = db.Model(&model.Version{}).Offset((page - 1) * size).Limit(size).Find(&items).Error; err != nil {
  32. log.Error("%v\n", err)
  33. c.JSON(nil, err)
  34. return
  35. }
  36. pager := &model.VersionPager{
  37. TotalCount: count,
  38. Pn: page,
  39. Ps: size,
  40. Items: items,
  41. }
  42. c.JSON(pager, nil)
  43. }
  44. func versionInfo(c *bm.Context) {
  45. var (
  46. req = c.Request.Form
  47. vid = parseInt(req.Get("id"))
  48. err error
  49. )
  50. exist := model.Version{}
  51. if err = tvSrv.DBShow.Where("id=?", vid).Where("state!=?", _isDeleted).First(&exist).Error; err != nil {
  52. c.JSON(nil, err)
  53. return
  54. }
  55. c.JSON(exist, nil)
  56. }
  57. func saveVersion(c *bm.Context) {
  58. var (
  59. req = c.Request.PostForm
  60. vid = parseInt(req.Get("id"))
  61. err error
  62. )
  63. exist := model.Version{}
  64. if err = tvSrv.DBShow.Where("id=?", vid).Where("state!=?", _isDeleted).First(&exist).Error; err != nil {
  65. c.JSON(nil, err)
  66. return
  67. }
  68. alert, simple := validateVerPostData(c)
  69. if alert != "" {
  70. renderErrMsg(c, ecode.RequestErr.Code(), alert)
  71. return
  72. }
  73. if err := tvSrv.DBShow.Model(&model.Version{}).Where("id=?", vid).Update(simple).Error; err != nil {
  74. log.Error("tvSrv.saveVersion error(%v)", err)
  75. c.JSON(nil, err)
  76. return
  77. }
  78. if err := tvSrv.DBShow.Model(&model.Version{}).Where("id=?", vid).Update(map[string]int8{"state": simple.State}).Error; err != nil {
  79. log.Error("tvSrv.saveVersion error(%v)", err)
  80. c.JSON(nil, err)
  81. return
  82. }
  83. c.JSON(nil, nil)
  84. }
  85. func addVersion(c *bm.Context) {
  86. var (
  87. err error
  88. )
  89. alert, simple := validateVerPostData(c)
  90. if alert != "" {
  91. renderErrMsg(c, ecode.RequestErr.Code(), alert)
  92. return
  93. }
  94. if err = tvSrv.DBShow.Create(simple).Error; err != nil {
  95. log.Error("tvSrv.addVersion error(%v)", err)
  96. c.JSON(nil, err)
  97. return
  98. }
  99. c.JSON(nil, nil)
  100. }
  101. func versionDel(c *bm.Context) {
  102. var (
  103. req = c.Request.PostForm
  104. vid = parseInt(req.Get("id"))
  105. err error
  106. )
  107. exist := model.Version{}
  108. if err = tvSrv.DBShow.Where("id=?", vid).Where("state!=?", _isDeleted).First(&exist).Error; err != nil {
  109. c.JSON(nil, err)
  110. return
  111. }
  112. if err := tvSrv.DBShow.Model(&model.Version{}).Where("id=?", vid).Update(map[string]int{"state": _isDeleted}).Error; err != nil {
  113. log.Error("tvSrv.versionDel error(%v)", err)
  114. c.JSON(nil, err)
  115. return
  116. }
  117. c.JSON(nil, nil)
  118. }
  119. func validateVerPostData(c *bm.Context) (alert string, simple *model.Version) {
  120. var (
  121. req = c.Request.PostForm
  122. plat = atoi(req.Get("plat"))
  123. version = req.Get("version")
  124. build = atoi(req.Get("build"))
  125. ptime = time.Time(parseInt(req.Get("ptime")))
  126. state = atoi(req.Get("state"))
  127. desc = req.Get("description")
  128. )
  129. if plat == 0 {
  130. alert = "平台不能为空"
  131. return
  132. }
  133. if version == "" {
  134. alert = "版本号不能为空"
  135. return
  136. }
  137. if build == 0 {
  138. alert = "build号不能为空"
  139. return
  140. }
  141. if int64(ptime) == 0 {
  142. alert = "发布时间不能为空"
  143. return
  144. }
  145. if desc == "" {
  146. alert = "描述不能为空"
  147. return
  148. }
  149. return "", &model.Version{Plat: int8(plat), Description: desc, Version: version, Build: build, State: int8(state), Ptime: ptime}
  150. }