music_category.go 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. package http
  2. import (
  3. "go-common/app/admin/main/creative/model/logcli"
  4. "go-common/app/admin/main/creative/model/music"
  5. "go-common/library/ecode"
  6. "go-common/library/log"
  7. bm "go-common/library/net/http/blademaster"
  8. "go-common/library/net/http/blademaster/binding"
  9. "github.com/jinzhu/gorm"
  10. )
  11. func searchCategory(c *bm.Context) {
  12. var (
  13. req = c.Request.Form
  14. err error
  15. items []*music.Category
  16. count int64
  17. pid = atoi(req.Get("pid"))
  18. name = req.Get("name")
  19. page = atoi(req.Get("page"))
  20. sort = atoi(req.Get("sort"))
  21. size = 20
  22. order string
  23. )
  24. if page == 0 {
  25. page = 1
  26. }
  27. db := svc.DBArchive.Where("state!=?", music.MusicDelete)
  28. if pid != 0 {
  29. db = db.Where("pid=?", pid)
  30. }
  31. //pid 目前不做分级 pid=0
  32. if name != "" {
  33. db = db.Where("name=?", name)
  34. }
  35. db.Model(&music.Category{}).Count(&count)
  36. if sort == 1 {
  37. order = "camera_index"
  38. } else {
  39. order = "index"
  40. }
  41. if err = db.Model(&music.Category{}).Order(order).Offset((page - 1) * size).Limit(size).Find(&items).Error; err != nil {
  42. log.Error("%v\n", err)
  43. c.JSON(nil, err)
  44. return
  45. }
  46. pager := &music.CategoryPager{
  47. Items: items,
  48. Pager: &music.Pager{Num: page, Size: size, Total: count},
  49. }
  50. c.JSON(pager, nil)
  51. }
  52. func categoryInfo(c *bm.Context) {
  53. var (
  54. req = c.Request.Form
  55. id = parseInt(req.Get("id"))
  56. err error
  57. )
  58. m := &music.CategoryParam{}
  59. if err = svc.DBArchive.Where("id=?", id).First(&m).Error; err != nil {
  60. c.JSON(nil, ecode.NothingFound)
  61. return
  62. }
  63. c.JSON(map[string]*music.CategoryParam{
  64. "data": m,
  65. }, nil)
  66. }
  67. func editCategory(c *bm.Context) {
  68. var (
  69. req = c.Request.PostForm
  70. id = parseInt(req.Get("id"))
  71. err error
  72. )
  73. uid, uname := getUIDName(c)
  74. m := &music.CategoryParam{}
  75. if err = c.BindWith(m, binding.Form); err != nil {
  76. return
  77. }
  78. exist := music.Category{}
  79. if err = svc.DBArchive.Where("id=?", id).Where("state!=?", music.MusicDelete).First(&exist).Error; err != nil {
  80. c.JSON(nil, ecode.NothingFound)
  81. return
  82. }
  83. m.UID = uid
  84. if err := svc.DBArchive.Model(&music.Category{}).Where("id=?", id).Update(m).Error; err != nil {
  85. log.Error("svc.editCategory error(%v)", err)
  86. c.JSON(nil, err)
  87. return
  88. }
  89. svc.SendMusicLog(c, logcli.LogClientArchiveMusicTypeCategory, &music.LogParam{ID: m.ID, UID: uid, UName: uname, Action: "index", Name: exist.Name})
  90. c.JSON(map[string]int{
  91. "code": 0,
  92. }, nil)
  93. }
  94. func indexCategory(c *bm.Context) {
  95. var (
  96. req = c.Request.PostForm
  97. err error
  98. cate = atoi(req.Get("type"))
  99. column string
  100. )
  101. uid, uname := getUIDName(c)
  102. m := &music.IndexParam{}
  103. if err = c.BindWith(m, binding.Form); err != nil {
  104. return
  105. }
  106. exist := music.Category{}
  107. if err = svc.DBArchive.Where("id=?", m.ID).Where("state!=?", music.MusicDelete).First(&exist).Error; err != nil {
  108. log.Error("svc.indexCategory error(%v)", err)
  109. c.JSON(nil, err)
  110. return
  111. }
  112. exist1 := music.Category{}
  113. if err = svc.DBArchive.Where("id=?", m.SwitchID).Where("state!=?", music.MusicDelete).First(&exist1).Error; err != nil {
  114. log.Error("svc.indexCategory error(%v)", err)
  115. c.JSON(nil, err)
  116. return
  117. }
  118. if cate == 1 {
  119. column = "camera_index"
  120. } else {
  121. column = "index"
  122. }
  123. m.UID = uid
  124. if err := svc.DBArchive.Model(&music.Category{}).Where("id=?", m.ID).Update(map[string]int64{column: m.SwitchIndex}).Update(map[string]int64{"uid": uid}).Error; err != nil {
  125. log.Error("svc.indexCategory error(%v)", err)
  126. c.JSON(nil, err)
  127. return
  128. }
  129. if err := svc.DBArchive.Model(&music.Category{}).Where("id=?", m.SwitchID).Update(map[string]int64{column: m.Index}).Update(map[string]int64{"uid": uid}).Error; err != nil {
  130. log.Error("svc.indexCategory error(%v)", err)
  131. c.JSON(nil, err)
  132. return
  133. }
  134. svc.SendMusicLog(c, logcli.LogClientArchiveMusicTypeCategory, &music.LogParam{ID: m.ID, UID: uid, UName: uname, Action: "index", Name: exist.Name})
  135. c.JSON(map[string]int{
  136. "code": 0,
  137. }, nil)
  138. }
  139. func addCategory(c *bm.Context) {
  140. var (
  141. err error
  142. )
  143. uid, uname := getUIDName(c)
  144. m := &music.CategoryParam{}
  145. if err = c.BindWith(m, binding.Form); err != nil {
  146. log.Error("svc.addCategory bind error(%v)", err)
  147. return
  148. }
  149. m.UID = uid
  150. exist := &music.Category{}
  151. if err = svc.DBArchive.Where("state!=?", music.MusicDelete).Where("name=?", m.Name).First(&exist).Error; err != nil && err != gorm.ErrRecordNotFound {
  152. c.JSON(nil, err)
  153. return
  154. }
  155. if exist.ID > 0 {
  156. c.JSON(map[string]int64{
  157. "id": exist.ID,
  158. }, nil)
  159. return
  160. }
  161. if err = svc.DBArchive.Create(m).Error; err != nil {
  162. log.Error("svc.addCategory Create error(%v)", err)
  163. c.JSON(nil, err)
  164. return
  165. }
  166. svc.SendMusicLog(c, logcli.LogClientArchiveMusicTypeCategory, &music.LogParam{ID: m.ID, UID: uid, UName: uname, Action: "add", Name: m.Name})
  167. c.JSON(map[string]int64{
  168. "id": m.ID,
  169. }, nil)
  170. }
  171. func delCategory(c *bm.Context) {
  172. var (
  173. req = c.Request.PostForm
  174. id = parseInt(req.Get("id"))
  175. err error
  176. )
  177. exist := music.Category{}
  178. if err = svc.DBArchive.Where("id=?", id).Where("state!=?", music.MusicDelete).First(&exist).Error; err != nil {
  179. c.JSON(nil, err)
  180. return
  181. }
  182. uid, uname := getUIDName(c)
  183. if err := svc.DBArchive.Model(music.Category{}).Where("id=?", id).Update(map[string]int{"state": music.MusicDelete}).Update(map[string]int64{"uid": uid}).Error; err != nil {
  184. log.Error("svc.delCategory error(%v)", err)
  185. c.JSON(nil, err)
  186. return
  187. }
  188. svc.SendMusicLog(c, logcli.LogClientArchiveMusicTypeCategory, &music.LogParam{ID: id, UID: uid, UName: uname, Action: "del", Name: exist.Name})
  189. c.JSON(map[string]int{
  190. "code": 0,
  191. }, nil)
  192. }