music_with_material.go 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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 musicMaterialRelationInfo(c *bm.Context) {
  13. var (
  14. req = c.Request.Form
  15. id = parseInt(req.Get("id"))
  16. err error
  17. )
  18. m := &music.WithMaterialParam{}
  19. if err = svc.DBArchive.Where("id=?", id).First(&m).Error; err != nil {
  20. c.JSON(nil, ecode.NothingFound)
  21. return
  22. }
  23. c.JSON(map[string]*music.WithMaterialParam{
  24. "data": m,
  25. }, nil)
  26. }
  27. func editMaterialRelation(c *bm.Context) {
  28. var (
  29. err error
  30. )
  31. uid, uname := getUIDName(c)
  32. m := &music.WithMaterialParam{}
  33. if err = c.BindWith(m, binding.Form); err != nil {
  34. return
  35. }
  36. exist := music.WithMaterial{}
  37. if err = svc.DBArchive.Where("sid=?", m.Sid).Where("state!=?", music.MusicDelete).First(&exist).Error; err != nil {
  38. c.JSON(nil, ecode.NothingFound)
  39. return
  40. }
  41. m.ID = exist.ID
  42. m.UID = uid
  43. m.Index = exist.Index
  44. if err = svc.DBArchive.Model(&music.WithMaterial{}).Where("id=?", exist.ID).Update(m).Error; err != nil {
  45. log.Error("svc.editMaterialRelation error(%v)", err)
  46. c.JSON(nil, err)
  47. return
  48. }
  49. svc.SendMusicLog(c, logcli.LogClientArchiveMusicTypeMaterialRelation, &music.LogParam{ID: m.ID, UID: uid, UName: uname, Action: "update", Name: string(m.ID)})
  50. c.JSON(map[string]int{
  51. "code": 0,
  52. }, nil)
  53. }
  54. func addMaterialRelation(c *bm.Context) {
  55. var (
  56. err error
  57. )
  58. m := &music.WithMaterialParam{}
  59. if err = c.BindWith(m, binding.Form); err != nil {
  60. return
  61. }
  62. exist := &music.WithMaterial{}
  63. //一期 sid tid 是 一一对应的关系
  64. //check sid bind
  65. if err = svc.DBArchive.Where("state!=?", music.MusicDelete).Where("sid=?", m.Sid).First(&exist).Error; err != nil && err != gorm.ErrRecordNotFound {
  66. c.JSON(nil, err)
  67. return
  68. }
  69. if exist.ID > 0 {
  70. c.JSON(map[string]int64{
  71. "id": exist.ID,
  72. }, nil)
  73. return
  74. }
  75. if err = svc.DBArchive.Create(m).Error; err != nil {
  76. log.Error("svc.addMaterialRelation error(%v)", err)
  77. c.JSON(nil, err)
  78. return
  79. }
  80. uid, uname := getUIDName(c)
  81. svc.SendMusicLog(c, logcli.LogClientArchiveMusicTypeMaterialRelation, &music.LogParam{ID: m.ID, UID: uid, UName: uname, Action: "add", Name: string(m.ID)})
  82. c.JSON(map[string]int64{
  83. "id": m.ID,
  84. }, nil)
  85. }
  86. func batchAddMaterialRelation(c *bm.Context) {
  87. var (
  88. err error
  89. sids []int64
  90. )
  91. uid, uname := getUIDName(c)
  92. m := &music.BatchMusicWithMaterialParam{}
  93. if err = c.BindWith(m, binding.Form); err != nil {
  94. log.Error("svc.batchAddMaterialRelation bind error(%v)", err)
  95. return
  96. }
  97. //todo tid 必须是二级分类
  98. if sids, err = xstr.SplitInts(m.SidList); err != nil {
  99. log.Error("svc.batchAddMaterialRelation SplitInts error(%v)", err)
  100. c.JSON(nil, err)
  101. return
  102. }
  103. max := music.WithMaterial{}
  104. if err = svc.DBArchive.Model(&music.WithMaterial{}).Where("tid=?", m.Tid).Where("state!=?", music.MusicDelete).Order("music_with_material.index desc").First(&max).Error; err != nil && err != gorm.ErrRecordNotFound {
  105. //sql err
  106. log.Error("svc.batchAddMaterialRelation max index error(%v)", err)
  107. return
  108. }
  109. thisIndex := max.Index
  110. i := int64(1)
  111. for _, sid := range sids {
  112. //check exists
  113. exists := music.WithMaterial{}
  114. if err = svc.DBArchive.Model(&music.WithMaterial{}).Where("sid=?", sid).Where("state!=?", music.MusicDelete).First(&exists).Error; err != nil && err != gorm.ErrRecordNotFound {
  115. log.Error("svc.batchAddMaterialRelation check exist sid tid (%d,%d) error(%v)", sid, m.Tid, err)
  116. c.JSON(nil, err)
  117. return
  118. }
  119. if exists.ID > 0 {
  120. //覆盖
  121. if err = svc.DBArchive.Model(&music.WithMaterial{}).Where("sid=?", sid).Where("state!=?", music.MusicDelete).Update("tid", m.Tid).Error; err != nil && err != gorm.ErrRecordNotFound {
  122. log.Error("svc.batchAddMaterialRelation Update error(%v)", err)
  123. continue
  124. }
  125. svc.SendMusicLog(c, logcli.LogClientArchiveMusicTypeMaterialRelation, &music.LogParam{ID: exists.Sid, UID: uid, UName: uname, Action: "update", Name: string(exists.Sid)})
  126. } else {
  127. mp := &music.WithMaterialParam{Sid: sid, Tid: m.Tid, Index: thisIndex + i}
  128. if err = svc.DBArchive.Create(mp).Error; err != nil {
  129. log.Error("svc.batchAddMaterialRelation Create error(%v)", err)
  130. continue
  131. }
  132. i++
  133. svc.SendMusicLog(c, logcli.LogClientArchiveMusicTypeMaterialRelation, &music.LogParam{ID: mp.Sid, UID: uid, UName: uname, Action: "add", Name: string(mp.Sid)})
  134. }
  135. }
  136. c.JSON(map[string]int64{
  137. "code": 0,
  138. }, nil)
  139. }