material_category.go 5.3 KB

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