music.go 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. package http
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "go-common/app/admin/main/creative/model/logcli"
  6. "go-common/app/admin/main/creative/model/music"
  7. "go-common/library/ecode"
  8. "go-common/library/log"
  9. bm "go-common/library/net/http/blademaster"
  10. "go-common/library/net/http/blademaster/binding"
  11. "go-common/library/xstr"
  12. "github.com/jinzhu/gorm"
  13. "github.com/pkg/errors"
  14. )
  15. func searchMusic(c *bm.Context) {
  16. var (
  17. req = c.Request.Form
  18. err error
  19. items []*music.Music
  20. count int64
  21. mid = atoi(req.Get("mid"))
  22. sid = atoi(req.Get("sid"))
  23. tid = req.Get("tid")
  24. musicans = req.Get("musicans")
  25. name = req.Get("name")
  26. page = atoi(req.Get("page"))
  27. size = 20
  28. )
  29. if page == 0 {
  30. page = 1
  31. }
  32. db := svc.DBArchive.Model(&music.Music{}).Where("music.state!=?", music.MusicDelete).
  33. Joins("left join music_with_material on music.sid=music_with_material.sid and music_with_material.state !=? ", music.MusicDelete).
  34. Joins("left join music_material on music_with_material.tid=music_material.id").
  35. Select("music.*,music_material.name as material_name,music_material.pid as pid,music_material.id as tid,music_with_material.id as rid")
  36. if mid != 0 {
  37. db = db.Where("music.mid=?", mid)
  38. }
  39. if sid != 0 {
  40. db = db.Where("music.sid=?", sid)
  41. }
  42. //未添加分类的BGM filter tid=0 filter
  43. if tid != "" {
  44. if atoi(tid) == 0 {
  45. db = db.Where("music_with_material.tid is null")
  46. } else {
  47. db = db.Where("music_with_material.tid=?", atoi(tid))
  48. }
  49. }
  50. if name != "" {
  51. db = db.Where("music.name=?", name)
  52. }
  53. if musicans != "" {
  54. db = db.Where("music.musicians=?", musicans)
  55. }
  56. db.Count(&count)
  57. //投稿时间ctime 倒序排列
  58. if err = db.Order("music.ctime DESC").Offset((page - 1) * size).Limit(size).Find(&items).Error; err != nil {
  59. log.Error("%v\n", err)
  60. c.JSON(nil, err)
  61. return
  62. }
  63. pager := &music.ResultPager{
  64. Items: items,
  65. Pager: &music.Pager{Num: page, Size: size, Total: count},
  66. }
  67. c.JSON(pager, nil)
  68. }
  69. func editMusicTimeline(c *bm.Context) {
  70. var (
  71. req = c.Request.PostForm
  72. sid = parseInt(req.Get("sid"))
  73. timeline = req.Get("timeline")
  74. err error
  75. )
  76. if len(timeline) > 0 {
  77. type timelineItem struct {
  78. //转换成毫秒存储
  79. Point int64 `json:"point"`
  80. Comment string `json:"comment"`
  81. Recommend int8 `json:"recommend"`
  82. }
  83. var timelineExp []*timelineItem
  84. if err = json.Unmarshal([]byte(timeline), &timelineExp); err != nil {
  85. httpCode(c, "timeline json is wrong", ecode.RequestErr)
  86. return
  87. }
  88. }
  89. exist := music.Music{}
  90. if err = svc.DBArchive.Where("sid=?", sid).Where("state!=?", music.MusicDelete).First(&exist).Error; err != nil {
  91. httpCode(c, "sid不存在", ecode.RequestErr)
  92. return
  93. }
  94. if err := svc.DBArchive.Model(&music.Music{}).Where("sid=?", sid).Update(map[string]string{"timeline": timeline}).Error; err != nil {
  95. log.Error("vdaSvc.editMusicTimeline error(%v)", err)
  96. c.JSON(nil, err)
  97. return
  98. }
  99. c.JSON(map[string]int{
  100. "code": 0,
  101. }, nil)
  102. }
  103. //开放来修改一些不复杂的数据
  104. func editMusic(c *bm.Context) {
  105. var (
  106. req = c.Request.PostForm
  107. sid = parseInt(req.Get("sid"))
  108. cooperate = req.Get("cooperate")
  109. err error
  110. )
  111. exist := music.Music{}
  112. if err = svc.DBArchive.Where("sid=?", sid).Where("state!=?", music.MusicDelete).First(&exist).Error; err != nil {
  113. httpCode(c, "sid不存在", ecode.RequestErr)
  114. return
  115. }
  116. if err := svc.DBArchive.Model(&music.Music{}).Where("sid=?", sid).Update(map[string]int{"cooperate": atoi(cooperate)}).Error; err != nil {
  117. log.Error("vdaSvc.editMusic error(%v)", err)
  118. c.JSON(nil, err)
  119. return
  120. }
  121. c.JSON(map[string]int{
  122. "code": 0,
  123. }, nil)
  124. }
  125. func editMusicTags(c *bm.Context) {
  126. var (
  127. req = c.Request.PostForm
  128. sid = parseInt(req.Get("sid"))
  129. tags = req.Get("tags")
  130. err error
  131. )
  132. exist := music.Music{}
  133. if err = svc.DBArchive.Where("sid=?", sid).Where("state!=?", music.MusicDelete).First(&exist).Error; err != nil {
  134. httpCode(c, "sid不存在", ecode.RequestErr)
  135. return
  136. }
  137. if err := svc.DBArchive.Model(&music.Music{}).Where("sid=?", sid).Update(map[string]string{"tags": tags}).Error; err != nil {
  138. log.Error("vdaSvc.editMusicTags error(%v)", err)
  139. c.JSON(nil, err)
  140. return
  141. }
  142. c.JSON(map[string]int{
  143. "code": 0,
  144. }, nil)
  145. }
  146. func batchEditMusicTags(c *bm.Context) {
  147. var (
  148. req = c.Request.PostForm
  149. sids = req.Get("sids")
  150. tags = req.Get("tags")
  151. arrids []int64
  152. err error
  153. )
  154. if arrids, err = xstr.SplitInts(sids); err != nil {
  155. log.Error("svc.batchEditMusicTags SplitInts error(%v)", err)
  156. c.JSON(nil, err)
  157. return
  158. }
  159. if len(arrids) < 2 {
  160. c.JSON(nil, fmt.Errorf("sids参数错误"))
  161. }
  162. if err = svc.DBArchive.Model(&music.Music{}).Where("sid IN (?)", arrids).Update(map[string]string{"tags": tags}).Error; err != nil {
  163. log.Error("vdaSvc.batchEditMusicTags error(%v)", err)
  164. c.JSON(nil, err)
  165. return
  166. }
  167. c.JSON(map[string]int{
  168. "code": 0,
  169. }, nil)
  170. }
  171. //用户侧BGM名称 front_name
  172. func editMusicFrontName(c *bm.Context) {
  173. var (
  174. req = c.Request.PostForm
  175. sid = parseInt(req.Get("sid"))
  176. frontname = req.Get("frontname")
  177. err error
  178. )
  179. exist := music.Music{}
  180. if err = svc.DBArchive.Where("sid=?", sid).Where("state!=?", music.MusicDelete).First(&exist).Error; err != nil {
  181. httpCode(c, "sid不存在", ecode.RequestErr)
  182. return
  183. }
  184. if err := svc.DBArchive.Model(&music.Music{}).Where("sid=?", sid).Update(map[string]string{"frontname": frontname}).Error; err != nil {
  185. log.Error("vdaSvc.editMusicName error(%v)", err)
  186. c.JSON(nil, err)
  187. return
  188. }
  189. c.JSON(map[string]int{
  190. "code": 0,
  191. }, nil)
  192. }
  193. func syncMusic(c *bm.Context) {
  194. var (
  195. err error
  196. )
  197. mp := &music.Param{}
  198. if err = c.BindWith(mp, binding.Form); err != nil {
  199. log.Error("svc.syncMusic bind error(%+v)", err)
  200. log.Error("svc.syncMusic bind error trace(%+v)", errors.Wrap(err, "sync bind error"))
  201. return
  202. }
  203. m := &music.Music{Sid: mp.Sid, Name: mp.Name, Cover: mp.Cover, Stat: mp.Stat, Mid: mp.Mid, Musicians: mp.Musicians, Categorys: mp.Categorys, Playurl: mp.Playurl, PubTime: mp.PubTime, Duration: mp.Duration, Filesize: mp.Filesize, State: mp.State}
  204. if m.State != music.MusicDelete {
  205. m.State = music.MusicOpen
  206. }
  207. exist := music.Music{}
  208. if err = svc.DBArchive.Where("sid=?", mp.Sid).First(&exist).Error; err != nil && err != gorm.ErrRecordNotFound {
  209. log.Error("svc.syncMusic find error(%+v)", err)
  210. c.JSON(nil, errors.Wrap(err, "sync find sid error"))
  211. return
  212. }
  213. uid, uname := getUIDName(c)
  214. if exist.ID > 0 {
  215. m.ID = exist.ID
  216. if err = svc.DBArchive.Model(&music.Music{}).Where("sid=?", mp.Sid).Update(m).Update(map[string]int8{"state": m.State}).Error; err != nil {
  217. log.Error("svc.syncMusic update error(%+v)", err)
  218. c.JSON(nil, errors.Wrap(err, "sync update error"))
  219. return
  220. }
  221. if m.State == music.MusicDelete {
  222. svc.SendMusicLog(c, logcli.LogClientArchiveMusicTypeMusic, &music.LogParam{ID: m.ID, UID: uid, UName: uname, Action: "del", Name: m.Name})
  223. } else {
  224. svc.SendMusicLog(c, logcli.LogClientArchiveMusicTypeMusic, &music.LogParam{ID: m.ID, UID: uid, UName: uname, Action: "update", Name: m.Name})
  225. }
  226. } else {
  227. if err = svc.DBArchive.Create(m).Error; err != nil {
  228. log.Error("svc.syncMusic Create error(%+v)", err)
  229. c.JSON(nil, errors.Wrap(err, "sync add error"))
  230. return
  231. }
  232. svc.SendMusicLog(c, logcli.LogClientArchiveMusicTypeMusic, &music.LogParam{ID: m.ID, UID: uid, UName: uname, Action: "add", Name: m.Name})
  233. }
  234. c.JSON(map[string]int64{
  235. "id": m.ID,
  236. }, nil)
  237. }