upper.go 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. package service
  2. import (
  3. "context"
  4. "go-common/app/admin/main/tv/model"
  5. accmdl "go-common/app/service/main/account/api"
  6. account "go-common/app/service/main/account/model"
  7. "go-common/library/ecode"
  8. "go-common/library/log"
  9. "github.com/jinzhu/gorm"
  10. )
  11. const (
  12. _removeArcs = 2
  13. _deleted = 1
  14. _toinit = 1
  15. _requestedInit = 2
  16. _onlineAct = "1"
  17. _offlineAct = "0"
  18. _validOnline = 1
  19. _validOffline = 0
  20. )
  21. // AddMids CheckMids checks the mids, whether all the uppers exist
  22. func (s *Service) AddMids(mids []int64) (res *model.AddResp, err error) {
  23. var (
  24. accsReply *accmdl.InfosReply
  25. accsInfo map[int64]*account.Info
  26. midExist bool
  27. )
  28. // init the response
  29. res = &model.AddResp{
  30. Succ: []int64{},
  31. Exist: []int64{},
  32. Invalids: []int64{},
  33. }
  34. // rpc get all the mids' info
  35. if accsReply, err = s.accClient.Infos3(ctx, &accmdl.MidsReq{
  36. Mids: mids,
  37. }); err != nil {
  38. log.Error("CheckMids Mids: %v, Error: %v", mids, err)
  39. return
  40. }
  41. accsInfo = accsReply.Infos
  42. for _, v := range mids {
  43. // if invalid account
  44. if _, ok := accsInfo[v]; !ok {
  45. res.Invalids = append(res.Invalids, v)
  46. continue
  47. }
  48. if midExist, err = s.existMid(v); err != nil {
  49. log.Error("AddMid %d, Error %v", v, err)
  50. return
  51. }
  52. // if the account is existing in our DB
  53. if midExist {
  54. res.Exist = append(res.Exist, v)
  55. continue
  56. }
  57. // add the upper
  58. if err = s.dao.UpAdd(v); err != nil {
  59. log.Error("AddMid %d, Error %v", v, err)
  60. return
  61. }
  62. res.Succ = append(res.Succ, v)
  63. }
  64. return
  65. }
  66. // existMid checks whether the mid is existing in our DB
  67. func (s *Service) existMid(mid int64) (exist bool, err error) {
  68. if err = s.DB.Where("deleted = 0").Where("mid = ?", mid).First(&model.Upper{}).Error; err != nil {
  69. if err == gorm.ErrRecordNotFound {
  70. return false, nil
  71. }
  72. log.Error("existMid %d, Error %v", mid, err)
  73. return
  74. }
  75. return true, nil
  76. }
  77. // ImportMids is for updating the data to tell the tv-job to import the uppers' all videos
  78. func (s *Service) ImportMids(mids []int64) (res *model.ImportResp, err error) {
  79. var midExist bool
  80. res = &model.ImportResp{
  81. NotExist: []int64{},
  82. Succ: []int64{},
  83. }
  84. for _, v := range mids {
  85. if midExist, err = s.existMid(v); err != nil {
  86. log.Error("ImportMids %d, Error %v", v, err)
  87. return
  88. }
  89. if !midExist {
  90. res.NotExist = append(res.NotExist, v)
  91. continue
  92. }
  93. if err = s.DB.Model(&model.Upper{}).Where("mid = ?", v).Update(map[string]int{"toinit": _toinit, "state": _requestedInit}).Error; err != nil {
  94. log.Error("ImportMids %d, Error %v", v, err)
  95. return
  96. }
  97. res.Succ = append(res.Succ, v)
  98. }
  99. return
  100. }
  101. // DelMid is for updating remove one upper from the list
  102. func (s *Service) DelMid(mid int64) (err error) {
  103. var exist bool
  104. if exist, err = s.existMid(mid); err != nil {
  105. return
  106. }
  107. if !exist {
  108. return ecode.TvUpperNotInList
  109. }
  110. if err = s.DB.Model(&model.Upper{}).Where("mid = ?", mid).Update(map[string]int{"deleted": _deleted, "toinit": _removeArcs}).Error; err != nil {
  111. log.Error("DelMid %d, Error %v", mid, err)
  112. }
  113. return
  114. }
  115. // UpList shows the upper list
  116. func (s *Service) UpList(order int, page int, name string, id int) (pager *model.UpperPager, err error) {
  117. var (
  118. source []*model.Upper
  119. mids []int64
  120. match map[int64]*account.Info
  121. info *account.Info
  122. ok bool
  123. namesReply, infosReply *accmdl.InfosReply
  124. nameRes map[int64]*account.Info
  125. ids []int64
  126. )
  127. pager = &model.UpperPager{}
  128. // id treatmnet
  129. if id != 0 {
  130. ids = append(ids, int64(id))
  131. }
  132. // name treatment
  133. if name != "" {
  134. if namesReply, err = s.accClient.InfosByName3(ctx, &accmdl.NamesReq{
  135. Names: []string{name},
  136. }); err != nil {
  137. log.Error("accRPC InfosByName3 %s, Err %v", name, err)
  138. return
  139. }
  140. nameRes = namesReply.Infos
  141. for k := range nameRes {
  142. ids = append(ids, k)
  143. }
  144. }
  145. if source, pager.Page, err = s.dao.UpList(order, page, ids); err != nil {
  146. return
  147. }
  148. // pick upper's name from AccRPC
  149. for _, v := range source {
  150. mids = append(mids, v.MID)
  151. }
  152. if infosReply, err = s.accClient.Infos3(ctx, &accmdl.MidsReq{
  153. Mids: mids,
  154. }); err != nil {
  155. log.Error("accRPC Infos3, %v, Error %v", mids, err)
  156. return
  157. }
  158. match = infosReply.Infos
  159. for _, v := range source { // arrange the data to output
  160. if info, ok = match[v.MID]; !ok {
  161. log.Error("Mid %v, AccRPC info is nil", v.MID)
  162. continue
  163. }
  164. pager.Items = append(pager.Items, &model.UpperR{
  165. MID: v.MID,
  166. State: v.State,
  167. Name: info.Name,
  168. Ctime: s.TimeFormat(v.Ctime),
  169. Mtime: s.TimeFormat(v.Mtime),
  170. })
  171. }
  172. return
  173. }
  174. // CmsList is the upper cms list service function
  175. func (s *Service) CmsList(ctx context.Context, req *model.ReqUpCms) (pager *model.CmsUpperPager, err error) {
  176. ups, page, errList := s.dao.UpCmsList(req)
  177. if errList != nil {
  178. return nil, errList
  179. }
  180. for _, v := range ups {
  181. v.MtimeStr = v.Mtime.Time().Format("2006-01-02 15:04:05")
  182. }
  183. pager = &model.CmsUpperPager{
  184. Items: ups,
  185. Page: page,
  186. }
  187. return
  188. }
  189. // CmsAudit updates the mids' valid status
  190. func (s *Service) CmsAudit(ctx context.Context, mids []int64, action string) (resp *model.RespUpAudit, err error) {
  191. var (
  192. okMids map[int64]*model.UpMC
  193. validAct int
  194. )
  195. resp = &model.RespUpAudit{
  196. Succ: mids,
  197. }
  198. if okMids, err = s.dao.VerifyIds(mids); err != nil {
  199. return
  200. }
  201. if len(okMids) != len(mids) {
  202. succ := []int64{}
  203. for _, v := range mids {
  204. if _, ok := okMids[v]; !ok {
  205. resp.Invalid = append(resp.Invalid, v)
  206. } else {
  207. succ = append(succ, v)
  208. }
  209. }
  210. resp.Succ = succ
  211. }
  212. if action == _onlineAct {
  213. validAct = _validOnline
  214. } else if action == _offlineAct {
  215. validAct = _validOffline
  216. }
  217. if err = s.dao.AuditIds(resp.Succ, validAct); err != nil {
  218. return
  219. }
  220. for _, v := range resp.Succ {
  221. s.dao.DelCache(ctx, v) // delete cache from MC because their status has been updated
  222. }
  223. return
  224. }
  225. // CmsEdit updates the upper's info in both DB and cache
  226. func (s *Service) CmsEdit(ctx context.Context, req *model.ReqUpEdit) (err error) {
  227. var (
  228. okMids map[int64]*model.UpMC
  229. upInfo *model.UpMC
  230. ok bool
  231. )
  232. if okMids, err = s.dao.VerifyIds([]int64{req.MID}); err != nil {
  233. return
  234. }
  235. if len(okMids) == 0 {
  236. return ecode.TvUpperNotInList
  237. }
  238. if upInfo, ok = okMids[req.MID]; !ok {
  239. log.Error("okMids %v, Mid %d, not found", okMids, req.MID)
  240. return ecode.NothingFound
  241. }
  242. if err = s.dao.SetUpper(req); err != nil { // update upper cms info in DB
  243. return
  244. }
  245. upInfo.CMSFace = req.Face
  246. upInfo.CMSName = req.Name
  247. err = s.dao.SetUpMetaCache(ctx, upInfo) // update Cache
  248. return
  249. }