mango.go 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. package service
  2. import (
  3. "fmt"
  4. "go-common/app/admin/main/tv/model"
  5. "go-common/library/ecode"
  6. "go-common/library/log"
  7. bm "go-common/library/net/http/blademaster"
  8. "github.com/pkg/errors"
  9. )
  10. // recomExist checks whether the recom exist or not
  11. func (s *Service) recomExist(id int64) (exist bool) {
  12. var recom model.MangoRecom
  13. if err := s.DB.Where("id = ?", id).Where("deleted = 0").Find(&recom).Error; err != nil {
  14. log.Error("[recomExist] ID %d, Err %v", id, err)
  15. return
  16. }
  17. if recom.ID != 0 {
  18. return true
  19. }
  20. return
  21. }
  22. func (s *Service) resExist(rid int64, rtype int) (exist bool) {
  23. var recom model.MangoRecom
  24. if err := s.DB.Where("rid = ?", rid).Where("rtype = ?", rtype).Where("deleted = 0").Find(&recom).Error; err != nil {
  25. log.Error("[resExist] rid %d, Err %v", rid, err)
  26. return
  27. }
  28. if recom.ID != 0 {
  29. return true
  30. }
  31. return
  32. }
  33. // MangoList picks the mango recom list data
  34. func (s *Service) MangoList(c *bm.Context) (data *model.MangoListResp, err error) {
  35. var (
  36. recoms []*model.MangoRecom
  37. msg = s.c.Cfg.MangoErr
  38. invalids []string
  39. mre *model.MRecomMC
  40. )
  41. data = &model.MangoListResp{List: make([]*model.MangoRecom, 0)}
  42. if err = s.DB.Where("deleted = ?", 0).Order("`rorder` ASC").Find(&recoms).Error; err != nil {
  43. log.Error("[MangoList] DB query fail(%v)", err)
  44. return
  45. }
  46. for _, v := range recoms { // check whether the archive or the season is still valid, otherwise we delete it and remind the user
  47. if v.Rtype == _TypePGC {
  48. if ok, _ := s.snValid(v.RID); ok {
  49. data.List = append(data.List, v)
  50. } else {
  51. invalids = append(invalids, fmt.Sprintf("p%d", v.RID))
  52. s.MangoDel(c, v.ID)
  53. }
  54. } else if v.Rtype == _TypeUGC {
  55. if ok, _ := s.arcValid(v.RID); ok {
  56. data.List = append(data.List, v)
  57. } else {
  58. invalids = append(invalids, fmt.Sprintf("u%d", v.RID))
  59. s.MangoDel(c, v.ID)
  60. }
  61. } else {
  62. log.Error("MangoList ID %d, Rid %d, Type %d, TypeError", v.ID, v.RID, v.Rtype)
  63. invalids = append(invalids, fmt.Sprintf("%d", v.RID))
  64. }
  65. }
  66. if len(invalids) > 0 {
  67. data.Message = msg + joinStr(invalids)
  68. }
  69. if mre, err = s.dao.GetMRecom(c); err != nil {
  70. log.Error("MangoList GetMRecom Err %v", err)
  71. err = nil
  72. return
  73. }
  74. data.Pubtime = mre.Pubtime.Time().Format("2006-01-02 15:04:05")
  75. return
  76. }
  77. // joinStr joins strings
  78. func joinStr(src []string) (res string) {
  79. for k, v := range src {
  80. if k == len(src)-1 {
  81. res = res + v
  82. } else {
  83. res = res + v + ","
  84. }
  85. }
  86. return
  87. }
  88. // MangoAdd adds the mango recom data
  89. func (s *Service) MangoAdd(c *bm.Context, rtype int, rids []int64) (data *model.MangoAdd, err error) {
  90. data = &model.MangoAdd{
  91. Succ: make([]int64, 0),
  92. Invalids: make([]int64, 0),
  93. }
  94. var (
  95. succRecoms []*model.MangoRecom
  96. newRids []int64
  97. )
  98. for _, v := range rids { // 检查是否存在
  99. if ok := s.resExist(v, rtype); ok {
  100. data.Invalids = append(data.Invalids, v)
  101. continue
  102. }
  103. newRids = append(newRids, v)
  104. }
  105. if rtype == _TypePGC { // 检查对应的pgc和ugc是否存在并有效
  106. for _, v := range newRids {
  107. if ok, sn := s.snValid(v); !ok {
  108. data.Invalids = append(data.Invalids, v)
  109. } else {
  110. succRecoms = append(succRecoms, sn.ToMango())
  111. }
  112. }
  113. } else if rtype == _TypeUGC {
  114. for _, v := range newRids {
  115. if ok, arc := s.arcValid(v); !ok {
  116. data.Invalids = append(data.Invalids, v)
  117. } else {
  118. var pid int32
  119. if _, pid, err = s.arcPName(arc.TypeID); err != nil || pid == 0 {
  120. log.Warn("MangoAdd Aid %d, TypeID %d, Err %v", v, arc.TypeID, err)
  121. data.Invalids = append(data.Invalids, v)
  122. continue
  123. }
  124. succRecoms = append(succRecoms, arc.ToMango(int(pid)))
  125. }
  126. }
  127. } else {
  128. err = ecode.TvDangbeiWrongType
  129. return
  130. }
  131. if len(succRecoms) > 0 { // 选取最大顺序,在之后递增
  132. tx := s.DB.Begin()
  133. maxOrder := s.dao.MaxOrder(c)
  134. for _, v := range succRecoms {
  135. maxOrder = maxOrder + 1
  136. v.Rorder = maxOrder
  137. if err = tx.Create(v).Error; err != nil {
  138. log.Error("MangoAdd Create Rid %d, Recom %v, Err %v", v.RID, v, err)
  139. tx.Rollback()
  140. return
  141. }
  142. }
  143. tx.Commit() // add succ
  144. for _, v := range succRecoms {
  145. data.Succ = append(data.Succ, v.RID)
  146. }
  147. }
  148. return
  149. }
  150. // MangoDel deletes the mango resource
  151. func (s *Service) MangoDel(c *bm.Context, id int64) (err error) {
  152. if !s.recomExist(id) {
  153. return ecode.NothingFound
  154. }
  155. return s.dao.DelMRecom(c, id)
  156. }
  157. // MangoEdit edits the mango resource
  158. func (s *Service) MangoEdit(c *bm.Context, req *model.ReqMangoEdit) (err error) {
  159. if !s.recomExist(req.ID) {
  160. return ecode.NothingFound
  161. }
  162. if err = s.DB.Model(&model.MangoRecom{}).Where("id = ?", req.ID).Update(map[string]interface{}{
  163. "title": req.Title,
  164. "cover": req.Cover,
  165. "content": req.Content,
  166. "staff": req.Staff,
  167. "jid": req.JID,
  168. "playcount": req.Playcount,
  169. }).Error; err != nil {
  170. log.Error("MangoDel ID %d, Mango %v, Err %v", req.ID, req, err)
  171. }
  172. return
  173. }
  174. // MangoPub publish the latest order of ids
  175. func (s *Service) MangoPub(c *bm.Context, ids []int64) (err error) {
  176. var order = 0
  177. for _, v := range ids {
  178. if !s.recomExist(v) {
  179. return errors.Wrap(ecode.Int(404), fmt.Sprintf("ID: %d", v))
  180. }
  181. }
  182. tx := s.DB.Begin()
  183. for _, v := range ids {
  184. order = order + 1
  185. if err = tx.Model(&model.MangoRecom{}).Where("id = ?", v).Update(map[string]int{"rorder": order}).Error; err != nil {
  186. log.Error("MangoPub ID %d, Err %v", v, err)
  187. tx.Rollback()
  188. return
  189. }
  190. }
  191. tx.Commit()
  192. err = s.dao.MangoRecom(c, ids)
  193. return
  194. }