music_material.go 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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. "go-common/library/xstr"
  10. "github.com/jinzhu/gorm"
  11. )
  12. func searchMaterial(c *bm.Context) {
  13. var (
  14. req = c.Request.Form
  15. err error
  16. items []*music.MaterialMixParent
  17. count int64
  18. pid = atoi(req.Get("pid"))
  19. name = req.Get("name")
  20. )
  21. db := svc.DBArchive.Where("music_material.state!=?", music.MusicDelete)
  22. if pid != 0 {
  23. db = db.Where("music_material.pid=?", pid)
  24. }
  25. if name != "" {
  26. db = db.Where("music_material.name=?", name)
  27. }
  28. db.Model(&music.Material{}).Count(&count)
  29. if err = db.Model(&music.Material{}).
  30. Joins("left join music_material p on p.id=music_material.pid").
  31. Select("music_material.*,p.name as p_name").
  32. Order("music_material.index").Find(&items).Error; err != nil {
  33. log.Error("%v\n", err)
  34. c.JSON(nil, err)
  35. return
  36. }
  37. pager := &music.MaterialMixParentPager{
  38. Pager: &music.Pager{Num: 1, Size: int(count), Total: count},
  39. Items: items,
  40. }
  41. c.JSON(pager, nil)
  42. }
  43. func materialInfo(c *bm.Context) {
  44. var (
  45. req = c.Request.Form
  46. id = parseInt(req.Get("id"))
  47. err error
  48. )
  49. m := &music.MaterialParam{}
  50. if err = svc.DBArchive.Where("id=?", id).Where("state!=?", music.MusicDelete).First(&m).Error; err != nil {
  51. c.JSON(nil, ecode.NothingFound)
  52. return
  53. }
  54. c.JSON(map[string]*music.MaterialParam{
  55. "data": m,
  56. }, nil)
  57. }
  58. func editMaterial(c *bm.Context) {
  59. var (
  60. req = c.Request.PostForm
  61. id = parseInt(req.Get("id"))
  62. err error
  63. )
  64. m := &music.MaterialParam{}
  65. if err = c.BindWith(m, binding.Form); err != nil {
  66. return
  67. }
  68. exist := music.Material{}
  69. if err = svc.DBArchive.Where("id=?", id).Where("state!=?", music.MusicDelete).First(&exist).Error; err != nil {
  70. c.JSON(nil, ecode.NothingFound)
  71. return
  72. }
  73. uid, uname := getUIDName(c)
  74. m.UID = uid
  75. if err = svc.DBArchive.Model(&music.Material{}).Where("id=?", id).Update(m).Error; err != nil {
  76. log.Error("svc.editMaterial error(%v)", err)
  77. c.JSON(nil, err)
  78. return
  79. }
  80. svc.SendMusicLog(c, logcli.LogClientArchiveMusicTypeMaterial, &music.LogParam{ID: m.ID, UID: uid, UName: uname, Action: "update", Name: m.Name})
  81. c.JSON(map[string]int{
  82. "code": 0,
  83. }, nil)
  84. }
  85. func addMaterial(c *bm.Context) {
  86. var (
  87. err error
  88. )
  89. m := &music.MaterialParam{}
  90. if err = c.BindWith(m, binding.Form); err != nil {
  91. return
  92. }
  93. uid, uname := getUIDName(c)
  94. m.UID = uid
  95. exist := &music.Material{}
  96. if err = svc.DBArchive.Where("state!=?", music.MusicDelete).Where("name=?", m.Name).First(&exist).Error; err != nil && err != gorm.ErrRecordNotFound {
  97. c.JSON(nil, err)
  98. return
  99. }
  100. if exist.ID > 0 {
  101. c.JSON(map[string]int64{
  102. "id": exist.ID,
  103. }, nil)
  104. return
  105. }
  106. if err = svc.DBArchive.Create(m).Error; err != nil {
  107. log.Error("svc.addMaterial error(%v)", err)
  108. c.JSON(nil, err)
  109. return
  110. }
  111. svc.SendMusicLog(c, logcli.LogClientArchiveMusicTypeMaterial, &music.LogParam{ID: m.ID, UID: uid, UName: uname, Action: "add", Name: m.Name})
  112. c.JSON(map[string]int64{
  113. "id": m.ID,
  114. }, nil)
  115. }
  116. func delMaterial(c *bm.Context) {
  117. var (
  118. req = c.Request.PostForm
  119. id = parseInt(req.Get("id"))
  120. err error
  121. )
  122. exist := music.Material{}
  123. if err = svc.DBArchive.Where("id=?", id).Where("state!=?", music.MusicDelete).First(&exist).Error; err != nil {
  124. c.JSON(nil, ecode.NothingFound)
  125. return
  126. }
  127. uid, uname := getUIDName(c)
  128. if err = svc.DBArchive.Model(music.Material{}).Where("id=?", id).Update(map[string]int{"state": music.MusicDelete}).Update(map[string]int64{"uid": uid}).Error; err != nil {
  129. log.Error("svc.delMaterial error(%v)", err)
  130. c.JSON(nil, err)
  131. return
  132. }
  133. svc.SendMusicLog(c, logcli.LogClientArchiveMusicTypeMaterial, &music.LogParam{ID: id, UID: uid, UName: uname, Action: "del", Name: exist.Name})
  134. c.JSON(map[string]int{
  135. "code": 0,
  136. }, nil)
  137. }
  138. func batchDeleteMaterial(c *bm.Context) {
  139. var (
  140. err error
  141. req = c.Request.PostForm
  142. ids = req.Get("ids")
  143. arrids []int64
  144. )
  145. uid, uname := getUIDName(c)
  146. if arrids, err = xstr.SplitInts(ids); err != nil {
  147. log.Error("svc.batchDeleteCategoryRelation SplitInts error(%v)", err)
  148. c.JSON(nil, err)
  149. return
  150. }
  151. for _, id := range arrids {
  152. if err = svc.DBArchive.Model(music.Material{}).Where("id=?", id).Update(map[string]int{"state": music.MusicDelete}).Error; err != nil {
  153. log.Error("svc.batchDeleteMaterial error(%v)", err)
  154. err = nil
  155. continue
  156. }
  157. //for log
  158. exist := music.Material{}
  159. if err = svc.DBArchive.Where("id=?", id).First(&exist).Error; err != nil {
  160. err = nil
  161. continue
  162. }
  163. svc.SendMusicLog(c, logcli.LogClientArchiveMusicTypeMaterial, &music.LogParam{ID: id, UID: uid, UName: uname, Action: "del", Name: exist.Name})
  164. }
  165. c.JSON(map[string]int64{
  166. "code": 0,
  167. }, nil)
  168. }