rpc.go 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. package service
  2. import (
  3. "context"
  4. "go-common/app/service/main/archive/api"
  5. arcmdl "go-common/app/service/main/archive/model/archive"
  6. favpb "go-common/app/service/main/favorite/api"
  7. favmdl "go-common/app/service/main/favorite/model"
  8. "go-common/library/ecode"
  9. "go-common/library/log"
  10. "go-common/library/time"
  11. )
  12. // ArcRPC find archive by rpc
  13. func (s *Service) ArcRPC(c context.Context, aid int64) (a *api.Arc, err error) {
  14. argAid := &arcmdl.ArgAid2{
  15. Aid: aid,
  16. }
  17. if a, err = s.arcRPC.Archive3(c, argAid); err != nil {
  18. log.Error("s.arcRPC.Archive3(%v), error(%v)", argAid, err)
  19. }
  20. if !a.IsNormal() {
  21. err = ecode.ArchiveNotExist
  22. }
  23. return
  24. }
  25. // ArcsRPC find archives by rpc
  26. func (s *Service) ArcsRPC(c context.Context, aids []int64) (as map[int64]*api.Arc, err error) {
  27. if len(aids) == 0 {
  28. return
  29. }
  30. argAids := &arcmdl.ArgAids2{
  31. Aids: aids,
  32. }
  33. if as, err = s.arcRPC.Archives3(c, argAids); err != nil {
  34. log.Error("s.arcRPC.Archives3(%v, archives), err(%v)", argAids, err)
  35. }
  36. return
  37. }
  38. // AddFavRPC add favorite .
  39. func (s *Service) AddFavRPC(c context.Context, typ int8, mid, aid, fid int64) (err error) {
  40. arg := &favpb.AddFavReq{Tp: int32(typ), Mid: mid, Oid: aid, Fid: fid}
  41. _, err = s.favClient.AddFav(c, arg)
  42. if err != nil {
  43. log.Error("s.favClient.AddFav(%+v) error(%v)", arg, err)
  44. }
  45. return
  46. }
  47. // DelFavRPC del favorite .
  48. func (s *Service) DelFavRPC(c context.Context, typ int8, mid, aid, fid int64) (err error) {
  49. arg := &favpb.DelFavReq{Tp: int32(typ), Mid: mid, Oid: aid, Fid: fid}
  50. if _, err = s.favClient.DelFav(c, arg); err != nil {
  51. log.Error("s.favClient.DelFavRPC(%+v) error(%v)", arg, err)
  52. }
  53. return
  54. }
  55. // FavoritesRPC favorites list.
  56. func (s *Service) FavoritesRPC(c context.Context, typ int8, mid, vmid, fid int64, tid int, keyword, order string, pn, ps int) (favs *favmdl.Favorites, err error) {
  57. arg := &favpb.FavoritesReq{Tp: int32(typ), Mid: mid, Uid: vmid, Fid: fid, Tid: int32(tid), Keyword: keyword, Order: order, Pn: int32(pn), Ps: int32(ps)}
  58. var reply *favpb.FavoritesReply
  59. if reply, err = s.favClient.Favorites(c, arg); err != nil {
  60. log.Error("s.favClient.Favorites(%+v) error(%v)", arg, err)
  61. return
  62. }
  63. favs = &favmdl.Favorites{}
  64. favs.Page.Count = int(reply.Res.Page.Count)
  65. favs.Page.Num = int(reply.Res.Page.Num)
  66. favs.Page.Size = int(reply.Res.Page.Size_)
  67. for _, data := range reply.Res.List {
  68. favs.List = append(favs.List, &favmdl.Favorite{
  69. ID: data.Id,
  70. Oid: data.Oid,
  71. Mid: data.Mid,
  72. Fid: data.Fid,
  73. Type: int8(data.Type),
  74. State: int8(data.State),
  75. CTime: time.Time(data.Ctime),
  76. MTime: time.Time(data.Mtime),
  77. })
  78. }
  79. return
  80. }
  81. // IsFavByFidRPC return user whether favored
  82. func (s *Service) IsFavByFidRPC(c context.Context, typ int8, mid, aid int64, fid int64) (res bool, err error) {
  83. arg := &favpb.IsFavoredByFidReq{Type: int32(typ), Mid: mid, Oid: aid, Fid: fid}
  84. var reply *favpb.IsFavoredReply
  85. if reply, err = s.favClient.IsFavoredByFid(c, arg); err != nil {
  86. log.Error("s.favClient.IsFavoredByFid(%+v) error(%v)", arg, err)
  87. return
  88. }
  89. res = reply.Faved
  90. return
  91. }
  92. // IsFavRPC return user whether favored
  93. func (s *Service) IsFavRPC(c context.Context, typ int8, mid, aid int64) (res bool, err error) {
  94. arg := &favpb.IsFavoredReq{Typ: int32(typ), Mid: mid, Oid: aid}
  95. var reply *favpb.IsFavoredReply
  96. if reply, err = s.favClient.IsFavored(c, arg); err != nil {
  97. log.Error("s.favClient.IsFavored(%+v) error(%v)", arg, err)
  98. return
  99. }
  100. res = reply.Faved
  101. return
  102. }
  103. // InDefaultRPC return aid whether in default fodler.
  104. func (s *Service) InDefaultRPC(c context.Context, typ int8, mid, aid int64) (res bool, err error) {
  105. arg := &favpb.InDefaultFolderReq{Typ: int32(typ), Mid: mid, Oid: aid}
  106. var reply *favpb.InDefaultFolderReply
  107. if reply, err = s.favClient.InDefault(c, arg); err != nil {
  108. log.Error("s.favClient.IsFavored(%+v) error(%v)", arg, err)
  109. return
  110. }
  111. res = reply.IsIn
  112. return
  113. }
  114. // IsFavsRPC return user's oids whether favored
  115. func (s *Service) IsFavsRPC(c context.Context, typ int8, mid int64, aids []int64) (res map[int64]bool, err error) {
  116. arg := &favpb.IsFavoredsReq{Typ: int32(typ), Mid: mid, Oids: aids}
  117. var reply *favpb.IsFavoredsReply
  118. if reply, err = s.favClient.IsFavoreds(c, arg); err != nil {
  119. log.Error("s.favClient.IsFavoreds(%+v) error(%v)", arg, err)
  120. return
  121. }
  122. res = reply.Faveds
  123. return
  124. }
  125. // AllFoldersRPC user's folders list.
  126. func (s *Service) AllFoldersRPC(c context.Context, typ int8, mid, vmid, oid int64, ip string) (fs []*favmdl.Folder, err error) {
  127. arg := &favpb.UserFoldersReq{Typ: int32(typ), Mid: mid, Vmid: vmid, Oid: oid}
  128. var reply *favpb.UserFoldersReply
  129. if reply, err = s.favClient.UserFolders(c, arg); err != nil {
  130. log.Error("s.favClient.UserFolders(%+v) error(%v)", arg, err)
  131. return
  132. }
  133. fs = reply.GetRes()
  134. return
  135. }
  136. // FolderRPC user's folder .
  137. func (s *Service) FolderRPC(c context.Context, typ int8, fid, mid, vmid int64) (f *favmdl.Folder, err error) {
  138. arg := &favpb.UserFolderReq{Typ: int32(typ), Mid: mid, Vmid: vmid, Fid: fid}
  139. var reply *favpb.UserFolderReply
  140. if reply, err = s.favClient.UserFolder(c, arg); err != nil {
  141. log.Error("s.favClient.UserFolder(%+v) error(%v)", arg, err)
  142. return
  143. }
  144. f = reply.GetRes()
  145. return
  146. }
  147. // TlistsRPC archive type list.
  148. func (s *Service) TlistsRPC(c context.Context, typ int8, mid, vmid, fid int64) (ps []*favmdl.Partition, err error) {
  149. arg := &favpb.TlistsReq{Tp: int32(typ), Mid: mid, Uid: vmid, Fid: fid}
  150. var reply *favpb.TlistsReply
  151. if reply, err = s.favClient.Tlists(c, arg); err != nil {
  152. log.Error("s.favClient.Tlists(%+v) error(%v)", arg, err)
  153. return
  154. }
  155. ps = make([]*favmdl.Partition, 0)
  156. for _, v := range reply.Res {
  157. ps = append(ps, &favmdl.Partition{
  158. Tid: int(v.Tid),
  159. Name: v.Name,
  160. Count: int(v.Count),
  161. })
  162. }
  163. return
  164. }
  165. // RecentsRPC recent favs .
  166. func (s *Service) RecentsRPC(c context.Context, typ int8, mid int64, size int) (aids []int64, err error) {
  167. arg := &favpb.RecentFavsReq{Tp: int32(typ), Mid: mid, Size_: int32(size)}
  168. var reply *favpb.RecentFavsReply
  169. if reply, err = s.favClient.RecentFavs(c, arg); err != nil {
  170. log.Error("s.favClient.RecentFavs(%+v) error(%v)", arg, err)
  171. return
  172. }
  173. aids = reply.GetRes()
  174. return
  175. }
  176. // TypesRPC find all archives's type by rpc
  177. func (s *Service) TypesRPC(c context.Context) (ats map[int16]*arcmdl.ArcType, err error) {
  178. if ats, err = s.arcRPC.Types2(c); err != nil {
  179. log.Error("s.arcRPC.Types2(), error(%v)", err)
  180. }
  181. return
  182. }