others.go 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. package service
  2. import (
  3. "context"
  4. "database/sql"
  5. "fmt"
  6. "net/url"
  7. "strconv"
  8. "go-common/app/admin/main/tv/model"
  9. "go-common/library/ecode"
  10. "go-common/library/log"
  11. "go-common/library/xstr"
  12. "github.com/jinzhu/gorm"
  13. )
  14. // Playurl new playurl function, get url from API
  15. func (s *Service) Playurl(cid int) (playurl string, err error) {
  16. if playurl, err = s.dao.Playurl(ctx, cid); err != nil {
  17. log.Error("Playurl API Error(%d) (%v)", cid, err)
  18. return
  19. }
  20. if playurl, err = s.hostChange(playurl); err != nil {
  21. log.Error("hostChange Error(%s)-(%v)", playurl, err)
  22. return
  23. }
  24. log.Info("NewPlayURL cid = %d, playurl = %s", cid, playurl)
  25. return
  26. }
  27. // hostChange can change the url from playurl api to tvshenhe's host
  28. func (s *Service) hostChange(playurl string) (replacedURL string, err error) {
  29. u, err := url.Parse(playurl)
  30. if err != nil {
  31. log.Error("hostChange ParseURL error (%v)", err)
  32. return
  33. }
  34. log.Info("[hostChange] for URL: %s, Original Host: %s, Now we change it to: %s", playurl, u.Host, s.c.Cfg.Playpath)
  35. u.Host = s.c.Cfg.Playpath // replace the host
  36. u.RawQuery = "" // remove useless query
  37. replacedURL = u.String()
  38. return
  39. }
  40. // Upload can upload a file object: store the info in Redis, and transfer the file to Bfs
  41. func (s *Service) Upload(c context.Context, fileName string, fileType string, timing int64, body []byte) (location string, err error) {
  42. if location, err = s.dao.Upload(c, fileName, fileType, timing, body); err != nil {
  43. log.Error("s.upload.Upload() error(%v)", err)
  44. }
  45. return
  46. }
  47. // unshelveReqT treats the unshelve request to db ( for update ) and dbSel (for select )
  48. func (s *Service) unshelveReqT(req *model.ReqUnshelve) (db, dbSel *gorm.DB, err error) {
  49. if length := len(req.IDs); length == 0 || length > s.c.Cfg.AuditConsult.UnshelveNb {
  50. err = ecode.RequestErr
  51. return
  52. }
  53. switch req.Type {
  54. case 1: // sid
  55. db = s.DB.Model(&model.TVEpSeason{}).Where("is_deleted = 0").
  56. Where(fmt.Sprintf("id IN (%s)", xstr.JoinInts(req.IDs)))
  57. dbSel = db.Select("id")
  58. case 2: // epid
  59. db = s.DB.Model(&model.Content{}).Where("is_deleted = 0").
  60. Where(fmt.Sprintf("epid IN (%s)", xstr.JoinInts(req.IDs)))
  61. dbSel = db.Select("epid")
  62. case 3: // aid
  63. db = s.DB.Model(&model.Archive{}).Where("deleted = 0").
  64. Where(fmt.Sprintf("aid IN (%s)", xstr.JoinInts(req.IDs)))
  65. dbSel = db.Select("aid")
  66. case 4: // cid
  67. db = s.DB.Model(&model.Video{}).Where("deleted = 0").
  68. Where(fmt.Sprintf("cid IN (%s)", xstr.JoinInts(req.IDs)))
  69. dbSel = db.Select("cid")
  70. default:
  71. err = ecode.RequestErr
  72. }
  73. return
  74. }
  75. // Unshelve is to soft delete the media data
  76. func (s *Service) Unshelve(c context.Context, req *model.ReqUnshelve, username string) (resp *model.RespUnshelve, err error) {
  77. var (
  78. rows *sql.Rows
  79. existMap = make(map[int64]int, len(req.IDs))
  80. db, dbSelect *gorm.DB
  81. updField = make(map[string]int, 1)
  82. )
  83. log.Warn("Unshelve Req Type %d, IDs %v, Username %s", req.Type, req.IDs, username) // record user's action
  84. resp = &model.RespUnshelve{
  85. SuccIDs: make([]int64, 0),
  86. FailIDs: make([]int64, 0),
  87. }
  88. if db, dbSelect, err = s.unshelveReqT(req); err != nil {
  89. log.Error("unshelve ReqT Err %v", err)
  90. return
  91. }
  92. if rows, err = dbSelect.Rows(); err != nil {
  93. log.Error("db rows Ids %v, Err %v", req.IDs, err)
  94. return
  95. }
  96. defer rows.Close()
  97. for rows.Next() { // pick existing ids
  98. var sid int64
  99. if err = rows.Scan(&sid); err != nil {
  100. log.Error("rows.Scan error(%v)", err)
  101. return
  102. }
  103. resp.SuccIDs = append(resp.SuccIDs, sid)
  104. existMap[sid] = 1
  105. }
  106. if err = rows.Err(); err != nil {
  107. log.Error("rows.Err %v", err)
  108. return
  109. }
  110. for _, v := range req.IDs { // treat to have the non-existing ids
  111. if _, ok := existMap[v]; !ok {
  112. resp.FailIDs = append(resp.FailIDs, v)
  113. }
  114. }
  115. if len(resp.SuccIDs) == 0 { // there isn't any to update ids
  116. return
  117. }
  118. switch req.Type {
  119. case 1, 2: // sid, epid
  120. updField["is_deleted"] = 1
  121. case 3, 4: // aid, cid
  122. updField["deleted"] = 1
  123. }
  124. if err = db.Update(updField).Error; err != nil {
  125. log.Error("update Ids %v, err %v", req.IDs, err)
  126. }
  127. return
  128. }
  129. // ChlSplash gets channel's splash data
  130. func (s *Service) ChlSplash(c context.Context, req *model.ReqChannel) (res *model.ChannelPager, err error) {
  131. var (
  132. db = s.DB.Model(&model.Channel{}).Where("deleted!=?", _isDeleted)
  133. items []*model.ChannelFmt
  134. count int64
  135. )
  136. if req.Desc != "" {
  137. db = db.Where("`desc` LIKE ?", "%"+req.Desc+"%")
  138. }
  139. if req.Title != "" {
  140. db = db.Where("title = ?", req.Title)
  141. }
  142. db.Count(&count)
  143. if req.Order == model.OrderDesc {
  144. db = db.Order("mtime DESC")
  145. } else {
  146. db = db.Order("mtime ASC")
  147. }
  148. if err = db.Offset((req.Page - 1) * _pagesize).Limit(_pagesize).Find(&items).Error; err != nil {
  149. log.Error("chlList Error (%v)", err)
  150. return
  151. }
  152. for _, v := range items {
  153. v.MtimeFormat = s.TimeFormat(v.Mtime)
  154. v.Mtime = 0
  155. }
  156. res = &model.ChannelPager{
  157. TotalCount: count,
  158. Pn: req.Page,
  159. Ps: _pagesize,
  160. Items: items,
  161. }
  162. return
  163. }
  164. func atoi(str string) (res int) {
  165. res, _ = strconv.Atoi(str)
  166. return
  167. }