common.go 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  1. package service
  2. import (
  3. "context"
  4. "errors"
  5. "fmt"
  6. "go-common/app/job/live/push-search/dao"
  7. "go-common/app/job/live/push-search/model"
  8. relationV1 "go-common/app/service/live/relation/api/liverpc/v1"
  9. roomV1 "go-common/app/service/live/room/api/liverpc/v1"
  10. roomV2 "go-common/app/service/live/room/api/liverpc/v2"
  11. userV3 "go-common/app/service/live/user/api/liverpc/v3"
  12. accountApi "go-common/app/service/main/account/api"
  13. "go-common/library/log"
  14. rpccontext "go-common/library/net/rpc/liverpc/context"
  15. "strconv"
  16. "time"
  17. )
  18. var (
  19. hbaseTable = "live:PushSearch"
  20. hbaseFamily = "search"
  21. fields = []string{
  22. "roomid",
  23. "short_id",
  24. "uid",
  25. "uname",
  26. "area",
  27. "title",
  28. "tags",
  29. "try_time",
  30. "cover",
  31. "user_cover",
  32. "lock_status",
  33. "hidden_status",
  34. "attentions",
  35. "online",
  36. "live_time",
  37. "area_v2_id",
  38. "area_v2_parent_id",
  39. "virtual",
  40. "round_status",
  41. "on_flag",
  42. "area_v2_name",
  43. "ctime",
  44. "mtime",
  45. }
  46. )
  47. func (s *Service) getBaseRoomInfo(uid int64) (roomInfo *roomV2.RoomGetByIdsResp_RoomInfo, err error) {
  48. roomIdResp, err := dao.RoomApi.V2Room.RoomIdByUid(rpccontext.WithTimeout(context.TODO(), 50*time.Millisecond), &roomV2.RoomRoomIdByUidReq{
  49. Uid: uid,
  50. })
  51. if err != nil {
  52. log.Error("[getBaseRoomInfo]RoomIdByUid rpc error, error:%+v", err)
  53. return
  54. }
  55. if roomIdResp.Code != 0 {
  56. log.Error("[getBaseRoomInfo]RoomIdByUid return error, code:%d, msg:%s", roomIdResp.Code, roomIdResp.Msg)
  57. err = errors.New("getRoomId return error")
  58. return
  59. }
  60. if roomIdResp.Data == nil {
  61. log.Error("[getBaseRoomInfo]GetMultiple empty data")
  62. err = errors.New("getRoomId empty error")
  63. return
  64. }
  65. if roomIdResp.Data.RoomId == 0 {
  66. log.Error("[getBaseRoomInfo]GetMultiple empty data")
  67. err = errors.New("roomId not found error")
  68. return
  69. }
  70. roomInfoResp := &roomV2.RoomGetByIdsResp{}
  71. roomInfoResp, err = dao.RoomApi.V2Room.GetByIds(rpccontext.WithTimeout(context.TODO(), 50*time.Millisecond), &roomV2.RoomGetByIdsReq{
  72. Ids: []int64{roomIdResp.Data.RoomId},
  73. From: "push-search",
  74. Fields: fields,
  75. })
  76. if err != nil {
  77. log.Error("[getBaseRoomInfo]GetByIds rpc error, error:%+v", err)
  78. return
  79. }
  80. if roomInfoResp.Code != 0 {
  81. log.Error("[getBaseRoomInfo]GetByIds return error, code:%d, msg:%s", roomInfoResp.Code, roomInfoResp.Msg)
  82. err = errors.New("GetByIds return error")
  83. return
  84. }
  85. if roomInfoResp.Data == nil {
  86. log.Error("[getBaseRoomInfo]GetByIds empty data")
  87. err = errors.New("GetByIds empty error")
  88. return
  89. }
  90. info, ok := roomInfoResp.Data[roomIdResp.Data.RoomId]
  91. if !ok {
  92. log.Error("[getBaseRoomInfo]GetByIds not found")
  93. err = errors.New("roomId not found error")
  94. return
  95. }
  96. roomInfo = info
  97. return
  98. }
  99. func (s *Service) getMultiUserInfo(uid int64) (userInfo *userV3.UserGetMultipleResp_Info, err error) {
  100. userInfo = &userV3.UserGetMultipleResp_Info{}
  101. pr, err := s.AccountClient.Profile3(context.TODO(), &accountApi.MidReq{Mid: uid})
  102. if err != nil {
  103. log.Error("[getMultiUserInfo]Profile3 rpc error, error:%+v", err)
  104. return
  105. }
  106. if pr == nil {
  107. log.Error("[getMultiUserInfo]Profile3 empty data")
  108. err = errors.New("user empty error")
  109. return
  110. }
  111. userInfo.Uid = uid
  112. userInfo.Uname = pr.GetProfile().GetName()
  113. userInfo.Face = pr.GetProfile().GetFace()
  114. if userInfo.Uname != "" {
  115. return
  116. }
  117. err = errors.New("user not found")
  118. log.Error("[getMultiUserInfo]GetMultiple no user, data:%+v", userInfo)
  119. return
  120. }
  121. func (s *Service) getFc(uid int64) (fc int, err error) {
  122. fcResp := &relationV1.FeedGetUserFcResp{}
  123. fcResp, err = dao.RelationApi.V1Feed.GetUserFc(rpccontext.WithTimeout(context.TODO(), 50*time.Millisecond), &relationV1.FeedGetUserFcReq{
  124. Follow: uid,
  125. })
  126. if err != nil {
  127. log.Error("[getFc]GetFc rpc error, error:%+v", err)
  128. return
  129. }
  130. if fcResp.Code != 0 {
  131. log.Error("[getFc]GetFc return error, code:%d, msg:%s", fcResp.Code, fcResp.Msg)
  132. err = errors.New("fc return error")
  133. return
  134. }
  135. if fcResp.Data == nil {
  136. log.Error("[getFc]GetFc empty data")
  137. err = errors.New("fc empty error")
  138. return
  139. }
  140. fc = int(fcResp.Data.Fc)
  141. return
  142. }
  143. func (s *Service) saveHBase(c context.Context, key string, columnInfo map[string][]byte) (err error) {
  144. var ctx, cancel = context.WithTimeout(c, time.Duration(s.c.SearchHBase.WriteTimeout)*time.Millisecond)
  145. defer cancel()
  146. values := map[string]map[string][]byte{hbaseFamily: columnInfo}
  147. if _, err = s.dao.SearchHBase.PutStr(ctx, hbaseTable, key, values); err != nil {
  148. log.Error("SearchHBase.PutStr error(%v), table(%s), values(%+v)", err, hbaseTable, values)
  149. }
  150. return
  151. }
  152. func (s *Service) getLockStatus(lockStatus string) int {
  153. status := 0
  154. if lockStatus != "0000-00-00 00:00:00" {
  155. status = 1
  156. }
  157. return status
  158. }
  159. func (s *Service) getHiddenStatus(HiddenStatus string) int {
  160. status := 0
  161. if HiddenStatus != "0000-00-00 00:00:00" {
  162. status = 1
  163. }
  164. return status
  165. }
  166. //hbase key roomID md5
  167. func (s *Service) rowKey(roomId int) string {
  168. key := fmt.Sprintf("%d_%d", roomId%10, roomId)
  169. return key
  170. }
  171. func (s *Service) generateSearchInfo(action string, table string, new *model.TableField, old *model.TableField) (ret map[string]interface{}, retByte map[string][]byte) {
  172. ret = make(map[string]interface{})
  173. ret["action"] = action
  174. ret["table"] = table
  175. //搜索字段转换
  176. newMap := make(map[string]interface{})
  177. newMap["id"] = new.RoomId
  178. newMap["short_id"] = new.ShortId
  179. newMap["uid"] = new.Uid
  180. newMap["uname"] = new.UName
  181. newMap["category"] = new.Area
  182. newMap["title"] = new.Title
  183. newMap["tag"] = new.Tag
  184. newMap["try_time"] = new.TryTime
  185. newMap["cover"] = new.Cover
  186. newMap["user_cover"] = new.UserCover
  187. newMap["lock_status"] = s.getLockStatus(new.LockStatus)
  188. newMap["hidden_status"] = s.getHiddenStatus(new.HiddenStatus)
  189. newMap["attentions"] = new.Attentions
  190. newMap["attention"] = new.Attentions
  191. newMap["online"] = new.Online
  192. newMap["live_time"] = new.LiveTime
  193. newMap["area_v2_id"] = new.AreaV2Id
  194. newMap["ord"] = new.AreaV2ParentId
  195. newMap["arcrank"] = new.Virtual
  196. newMap["lastupdate"] = s.getLastUpdate(new)
  197. newMap["is_live"] = s.getLiveStatus(new)
  198. newMap["s_category"] = new.AreaV2Name
  199. ret["new"] = newMap
  200. oldMap := make(map[string]interface{})
  201. if old != nil {
  202. oldMap["id"] = old.RoomId
  203. oldMap["short_id"] = old.ShortId
  204. oldMap["uid"] = old.Uid
  205. oldMap["uname"] = old.UName
  206. oldMap["category"] = old.Area
  207. oldMap["title"] = old.Title
  208. oldMap["tag"] = old.Tag
  209. oldMap["try_time"] = old.TryTime
  210. oldMap["cover"] = old.Cover
  211. oldMap["user_cover"] = old.UserCover
  212. oldMap["lock_status"] = s.getLockStatus(old.LockStatus)
  213. oldMap["hidden_status"] = s.getHiddenStatus(old.HiddenStatus)
  214. oldMap["attentions"] = old.Attentions
  215. oldMap["attention"] = old.Attentions
  216. oldMap["online"] = old.Online
  217. oldMap["live_time"] = old.LiveTime
  218. oldMap["area_v2_id"] = old.AreaV2Id
  219. oldMap["area_v2_name"] = old.AreaV2Name
  220. oldMap["ord"] = old.AreaV2ParentId
  221. oldMap["arcrank"] = old.Virtual
  222. oldMap["lastupdate"] = s.getLastUpdate(old)
  223. oldMap["is_live"] = s.getLiveStatus(old)
  224. }
  225. if action != "insert" && old == nil {
  226. oldMap["id"] = new.RoomId
  227. oldMap["short_id"] = new.ShortId
  228. oldMap["uid"] = new.Uid
  229. oldMap["uname"] = new.UName
  230. oldMap["category"] = new.Area
  231. oldMap["title"] = new.Title
  232. oldMap["tag"] = new.Tag
  233. oldMap["try_time"] = new.TryTime
  234. oldMap["cover"] = new.Cover
  235. oldMap["user_cover"] = new.UserCover
  236. oldMap["lock_status"] = s.getLockStatus(new.LockStatus)
  237. oldMap["hidden_status"] = s.getHiddenStatus(new.HiddenStatus)
  238. oldMap["attentions"] = new.Attentions
  239. oldMap["attention"] = new.Attentions
  240. oldMap["online"] = new.Online
  241. oldMap["live_time"] = new.LiveTime
  242. oldMap["area_v2_id"] = new.AreaV2Id
  243. oldMap["area_v2_name"] = new.AreaV2Name
  244. oldMap["ord"] = new.AreaV2ParentId
  245. oldMap["arcrank"] = new.Virtual
  246. oldMap["lastupdate"] = s.getLastUpdate(new)
  247. oldMap["is_live"] = s.getLiveStatus(new)
  248. }
  249. ret["old"] = oldMap
  250. newByteMap := make(map[string][]byte)
  251. newByteMap["id"] = []byte(strconv.Itoa(new.RoomId))
  252. newByteMap["short_id"] = []byte(strconv.Itoa(new.ShortId))
  253. newByteMap["uid"] = []byte(strconv.FormatInt(new.Uid, 10))
  254. newByteMap["uname"] = []byte(new.UName)
  255. newByteMap["category"] = []byte(strconv.Itoa(new.Area))
  256. newByteMap["title"] = []byte(new.Title)
  257. newByteMap["tag"] = []byte(new.Tag)
  258. newByteMap["try_time"] = []byte(new.TryTime)
  259. newByteMap["cover"] = []byte(new.Cover)
  260. newByteMap["user_cover"] = []byte(new.UserCover)
  261. newByteMap["lock_status"] = []byte(strconv.Itoa(s.getLockStatus(new.LockStatus)))
  262. newByteMap["hidden_status"] = []byte(strconv.Itoa(s.getHiddenStatus(new.HiddenStatus)))
  263. newByteMap["attentions"] = []byte(strconv.Itoa(new.Attentions))
  264. newByteMap["attention"] = []byte(strconv.Itoa(new.Attentions))
  265. newByteMap["online"] = []byte(strconv.Itoa(new.Online))
  266. newByteMap["live_time"] = []byte(new.LiveTime)
  267. newByteMap["area_v2_id"] = []byte(strconv.Itoa(new.AreaV2Id))
  268. newByteMap["ord"] = []byte(strconv.Itoa(new.AreaV2ParentId))
  269. newByteMap["arcrank"] = []byte(strconv.Itoa(new.Virtual))
  270. newByteMap["lastupdate"] = []byte(s.getLastUpdate(new))
  271. newByteMap["is_live"] = []byte(strconv.Itoa(s.getLiveStatus(new)))
  272. newByteMap["s_category"] = []byte(new.AreaV2Name)
  273. return ret, newByteMap
  274. }
  275. //获取直播状态
  276. func (s *Service) getLiveStatus(roomInfo *model.TableField) int {
  277. if roomInfo.LiveTime != "0000-00-00 00:00:00" {
  278. return 1
  279. }
  280. if roomInfo.RoundStatus == 1 && roomInfo.OnFlag == 1 {
  281. return 2
  282. }
  283. return 0
  284. }
  285. //获取房间最后更新时间
  286. func (s *Service) getLastUpdate(roomInfo *model.TableField) string {
  287. if roomInfo.MTime != "0000-00-00 00:00:00" {
  288. return roomInfo.MTime
  289. }
  290. return roomInfo.CTime
  291. }
  292. func (s *Service) getAreaV2Detail(areaV2Id int) (areaInfo *roomV1.AreaGetDetailResp_AreaInfo, err error) {
  293. areaResp, err := dao.RoomApi.V1Area.GetDetail(rpccontext.WithTimeout(context.TODO(), 50*time.Millisecond), &roomV1.AreaGetDetailReq{
  294. Id: int64(areaV2Id),
  295. })
  296. if err != nil {
  297. log.Error("[getAreaV2Detail]GetMultiple rpc error, error:%+v", err)
  298. return
  299. }
  300. if areaResp.Code != 0 {
  301. log.Error("[getAreaV2Detail]GetMultiple return error, code:%d, msg:%s", areaResp.Code, areaResp.Msg)
  302. err = errors.New("user return error")
  303. return
  304. }
  305. if areaResp.Data == nil {
  306. log.Error("[getAreaV2Detail]GetMultiple empty data")
  307. err = errors.New("area detail empty error")
  308. return
  309. }
  310. return areaResp.Data, err
  311. }