music.go 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. package http
  2. import (
  3. "github.com/jinzhu/gorm"
  4. "github.com/pkg/errors"
  5. "go-common/app/admin/main/videoup/model/archive"
  6. "go-common/app/admin/main/videoup/model/music"
  7. "go-common/library/log"
  8. bm "go-common/library/net/http/blademaster"
  9. "go-common/library/net/http/blademaster/binding"
  10. )
  11. const ()
  12. func getUIDName(c *bm.Context) (uid int64, uname string) {
  13. unamei, ok := c.Get("username")
  14. if ok {
  15. uname = unamei.(string)
  16. }
  17. uidi, ok := c.Get("uid")
  18. if ok {
  19. uid = uidi.(int64)
  20. }
  21. return
  22. }
  23. func syncMusic(c *bm.Context) {
  24. var (
  25. err error
  26. )
  27. mp := &music.Param{}
  28. if err = c.BindWith(mp, binding.Form); err != nil {
  29. log.Error("vdaSvc.syncMusic bind error(%+v)", err)
  30. log.Error("vdaSvc.syncMusic bind error trace(%+v)", errors.Wrap(err, "sync bind error"))
  31. return
  32. }
  33. 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}
  34. if m.State != music.MusicDelete {
  35. m.State = music.MusicOpen
  36. }
  37. exist := music.Music{}
  38. if err = vdaSvc.DB.Where("sid=?", mp.Sid).First(&exist).Error; err != nil && err != gorm.ErrRecordNotFound {
  39. log.Error("vdaSvc.syncMusic find error(%+v)", err)
  40. c.JSON(nil, errors.Wrap(err, "sync find sid error"))
  41. return
  42. }
  43. uid, uname := getUIDName(c)
  44. if exist.ID > 0 {
  45. m.ID = exist.ID
  46. if err = vdaSvc.DB.Model(&music.Music{}).Where("sid=?", mp.Sid).Update(m).Update(map[string]int8{"state": m.State}).Error; err != nil {
  47. log.Error("vdaSvc.syncMusic update error(%+v)", err)
  48. c.JSON(nil, errors.Wrap(err, "sync update error"))
  49. return
  50. }
  51. if m.State == music.MusicDelete {
  52. vdaSvc.SendMusicLog(c, archive.LogClientArchiveMusicTypeMusic, &music.LogParam{ID: m.ID, UID: uid, UName: uname, Action: "del", Name: m.Name})
  53. } else {
  54. vdaSvc.SendMusicLog(c, archive.LogClientArchiveMusicTypeMusic, &music.LogParam{ID: m.ID, UID: uid, UName: uname, Action: "update", Name: m.Name})
  55. }
  56. } else {
  57. if err = vdaSvc.DB.Create(m).Error; err != nil {
  58. log.Error("vdaSvc.syncMusic Create error(%+v)", err)
  59. c.JSON(nil, errors.Wrap(err, "sync add error"))
  60. return
  61. }
  62. vdaSvc.SendMusicLog(c, archive.LogClientArchiveMusicTypeMusic, &music.LogParam{ID: m.ID, UID: uid, UName: uname, Action: "add", Name: m.Name})
  63. }
  64. c.JSON(map[string]int64{
  65. "id": m.ID,
  66. }, nil)
  67. }