music.go 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. package archive
  2. import (
  3. "context"
  4. "encoding/json"
  5. "sort"
  6. "strings"
  7. "go-common/app/interface/main/creative/model/music"
  8. "go-common/library/log"
  9. )
  10. const (
  11. _AllMusicsSQL = "SELECT cooperate,id,sid,name,frontname,musicians,mid,cover,playurl,state,duration,filesize,ctime,pubtime,tags,timeline FROM music"
  12. )
  13. // AllMusics fn
  14. func (d *Dao) AllMusics(c context.Context) (res map[int64]*music.Music, err error) {
  15. rows, err := d.db.Query(c, _AllMusicsSQL)
  16. if err != nil {
  17. log.Error("mysqlDB.Query error(%v)", err)
  18. return
  19. }
  20. defer rows.Close()
  21. res = make(map[int64]*music.Music)
  22. for rows.Next() {
  23. v := &music.Music{}
  24. var fName string
  25. if err = rows.Scan(&v.Cooperate, &v.ID, &v.SID, &v.Name, &fName, &v.Musicians, &v.UpMID, &v.Cover, &v.Playurl, &v.State, &v.Duration, &v.FileSize, &v.CTime, &v.Pubtime, &v.TagsStr, &v.Timeline); err != nil {
  26. log.Error("row.Scan error(%v)", err)
  27. return
  28. }
  29. if len(fName) > 0 {
  30. v.Name = fName
  31. }
  32. v.Tl = make([]*music.TimePoint, 0)
  33. if len(v.Timeline) > 0 {
  34. if err = json.Unmarshal([]byte(v.Timeline), &v.Tl); err != nil {
  35. log.Error("json.Unmarshal Timeline failed error(%v)", err)
  36. continue
  37. }
  38. sort.Slice(v.Tl, func(i, j int) bool {
  39. return v.Tl[i].Point < v.Tl[j].Point
  40. })
  41. if len(v.Tl) > 0 {
  42. for _, point := range v.Tl {
  43. if point.Recommend == 1 {
  44. v.RecommendPoint = point.Point
  45. break
  46. }
  47. }
  48. }
  49. }
  50. v.Tags = make([]string, 0)
  51. if len(v.TagsStr) > 0 {
  52. v.Tags = strings.Split(v.TagsStr, ",")
  53. }
  54. res[v.SID] = v
  55. }
  56. return
  57. }