subject.go 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. package like
  2. import (
  3. "context"
  4. "time"
  5. ldao "go-common/app/interface/main/activity/dao/like"
  6. "go-common/app/interface/main/activity/model/like"
  7. "go-common/library/ecode"
  8. "go-common/library/log"
  9. "go-common/library/sync/errgroup"
  10. )
  11. // SubjectInitialize act_subject data initialize .
  12. func (s *Service) SubjectInitialize(c context.Context, minSid int64) (err error) {
  13. if minSid < 0 {
  14. minSid = 0
  15. }
  16. var actSub []*like.SubjectItem
  17. for {
  18. if actSub, err = s.dao.SubjectListMoreSid(c, minSid); err != nil {
  19. log.Error("dao.subjectListMoreSid(%d) error(%+v)", minSid, err)
  20. break
  21. }
  22. // empty slice or nil
  23. if len(actSub) == 0 {
  24. log.Info("SubjectInitialize end success")
  25. break
  26. }
  27. for _, sub := range actSub {
  28. item := sub
  29. if minSid < item.ID {
  30. minSid = item.ID
  31. }
  32. id := item.ID
  33. //the activity offline is stored with empty data
  34. if item.State != ldao.SubjectValidState {
  35. item = &like.SubjectItem{}
  36. }
  37. s.cache.Do(c, func(c context.Context) {
  38. s.dao.AddCacheActSubject(c, id, item)
  39. })
  40. }
  41. }
  42. s.cache.Do(c, func(c context.Context) {
  43. s.SubjectMaxIDInitialize(c)
  44. })
  45. return
  46. }
  47. // SubjectMaxIDInitialize Initialize act_subject max id data .
  48. func (s *Service) SubjectMaxIDInitialize(c context.Context) (err error) {
  49. var actSub *like.SubjectItem
  50. if actSub, err = s.dao.SubjectMaxID(c); err != nil {
  51. log.Error(" s.dao.SubjectMaxID() error(%+v)", err)
  52. return
  53. }
  54. if actSub.ID >= 0 {
  55. if err = s.dao.AddCacheActSubjectMaxID(c, actSub.ID); err != nil {
  56. log.Error("s.dao.AddCacheActSubjectMaxID(%d) error(%v)", actSub.ID, err)
  57. }
  58. }
  59. return
  60. }
  61. // SubjectUp up act_subject cahce info .
  62. func (s *Service) SubjectUp(c context.Context, sid int64) (err error) {
  63. var (
  64. actSub *like.SubjectItem
  65. maxSubID int64
  66. )
  67. group, ctx := errgroup.WithContext(c)
  68. group.Go(func() (e error) {
  69. if actSub, e = s.dao.RawActSubject(ctx, sid); e != nil {
  70. log.Error("dao.RawActSubject(%d) error(%+v)", sid, e)
  71. }
  72. return
  73. })
  74. group.Go(func() (e error) {
  75. if maxSubID, e = s.dao.CacheActSubjectMaxID(ctx); e != nil {
  76. log.Error("dao.RawActSubject(%d) error(%v)", sid, e)
  77. }
  78. return
  79. })
  80. if err = group.Wait(); err != nil {
  81. log.Error("SubjectUp error(%v)", err)
  82. return
  83. }
  84. if actSub.ID == 0 || actSub.State != ldao.SubjectValidState {
  85. actSub = &like.SubjectItem{}
  86. }
  87. if maxSubID < sid {
  88. s.cache.Do(c, func(c context.Context) {
  89. s.dao.AddCacheActSubjectMaxID(context.Background(), sid)
  90. })
  91. }
  92. s.cache.Do(c, func(c context.Context) {
  93. s.dao.AddCacheActSubject(context.Background(), sid, actSub)
  94. })
  95. return
  96. }
  97. // SubjectLikeListInitialize Initialize likes list .
  98. func (s *Service) SubjectLikeListInitialize(c context.Context, sid int64) (err error) {
  99. var (
  100. actSub *like.SubjectItem
  101. items []*like.Item
  102. lid = int64(0)
  103. )
  104. if actSub, err = s.dao.RawActSubject(c, sid); err != nil {
  105. log.Error("dao.RawActSubject(%d) error(%+v)", sid, err)
  106. return
  107. }
  108. if actSub.ID == 0 {
  109. log.Info("SubjectSLikeListInitialize end success")
  110. return
  111. }
  112. for {
  113. if items, err = s.dao.LikesBySid(c, lid, sid); err != nil {
  114. log.Error("dao.LikesBySid(%d,%d) error(%+v)", lid, sid, err)
  115. break
  116. }
  117. // empty slice or nil
  118. if len(items) == 0 {
  119. log.Info("SubjectSLikeListInitialize end success")
  120. break
  121. }
  122. //Initialize likes ctime cache
  123. cItems := items
  124. s.cache.Do(c, func(c context.Context) {
  125. s.dao.LikeListCtime(c, sid, cItems)
  126. })
  127. for _, val := range items {
  128. if lid < val.ID {
  129. lid = val.ID
  130. }
  131. }
  132. }
  133. return
  134. }
  135. // LikeActCountInitialize Initialize like_action cache data .
  136. func (s *Service) LikeActCountInitialize(c context.Context, sid int64) (err error) {
  137. var (
  138. actSub *like.SubjectItem
  139. items []*like.Item
  140. lid = int64(0)
  141. types = make(map[int64]int)
  142. likeSumItem []*like.LidLikeSum
  143. )
  144. if actSub, err = s.dao.RawActSubject(c, sid); err != nil {
  145. log.Error("dao.RawActSubject(%d) error(%+v)", sid, err)
  146. return
  147. }
  148. if actSub.ID == 0 {
  149. log.Info("SubjectSLikeListInitialize end success")
  150. return
  151. }
  152. for {
  153. if items, err = s.dao.LikesBySid(c, lid, sid); err != nil {
  154. log.Error("dao.LikesBySid(%d,%d) error(%+v)", lid, sid, err)
  155. break
  156. }
  157. if len(items) == 0 {
  158. log.Info("SubjectSLikeListInitialize end success")
  159. break
  160. }
  161. lidList := make([]int64, 0, len(items))
  162. for _, val := range items {
  163. if lid < val.ID {
  164. lid = val.ID
  165. }
  166. lidList = append(lidList, val.ID)
  167. types[val.ID] = val.Type
  168. }
  169. if likeSumItem, err = s.dao.LikeActSums(c, sid, lidList); err != nil {
  170. log.Error(" s.dao.LikeActSums(%d,%v) error(%+v)", sid, lidList, err)
  171. return
  172. }
  173. if len(likeSumItem) == 0 {
  174. continue
  175. }
  176. lidLike := make(map[int64]int64, len(likeSumItem))
  177. for _, v := range likeSumItem {
  178. lidLike[v.Lid] = v.Likes
  179. }
  180. eg, errCtx := errgroup.WithContext(c)
  181. eg.Go(func() (e error) {
  182. e = s.dao.SetInitializeLikeCache(errCtx, sid, lidLike, types)
  183. return
  184. })
  185. eg.Go(func() (e error) {
  186. e = s.SetLikeActSum(errCtx, lidLike)
  187. return
  188. })
  189. if err = eg.Wait(); err != nil {
  190. log.Error("LikeActCountInitialize:eg.Wait() error(%+v)", err)
  191. return
  192. }
  193. }
  194. return
  195. }
  196. // SetLikeActSum set like_extend sum data
  197. func (s *Service) SetLikeActSum(c context.Context, lidLikes map[int64]int64) (err error) {
  198. var (
  199. AddLids []*like.Extend
  200. )
  201. if len(lidLikes) == 0 {
  202. return
  203. }
  204. for k, v := range lidLikes {
  205. AddLids = append(AddLids, &like.Extend{Like: v, Lid: k})
  206. }
  207. _, err = s.BatchInsertLikeExtend(c, AddLids)
  208. return
  209. }
  210. // ActSubject .
  211. func (s *Service) ActSubject(c context.Context, sid int64) (res *like.SubjectItem, err error) {
  212. if res, err = s.dao.ActSubject(c, sid); err != nil {
  213. return
  214. }
  215. if res == nil {
  216. err = ecode.NothingFound
  217. }
  218. return
  219. }
  220. // ActProtocol .
  221. func (s *Service) ActProtocol(c context.Context, a *like.ArgActProtocol) (res *like.SubProtocol, err error) {
  222. res = new(like.SubProtocol)
  223. if res.SubjectItem, err = s.dao.ActSubject(c, a.Sid); err != nil {
  224. log.Error("s.dao.ActSubject() error(%+v)", err)
  225. return
  226. }
  227. if res.SubjectItem.ID == 0 {
  228. err = ecode.NothingFound
  229. return
  230. }
  231. now := time.Now().Unix()
  232. if int64(res.SubjectItem.Stime) <= now && int64(res.SubjectItem.Etime) >= now {
  233. if res.ActSubjectProtocol, err = s.dao.ActSubjectProtocol(c, a.Sid); err != nil {
  234. log.Error("s.dao.ActSubjectProtocol(%d) error(%+v)", a.Sid, err)
  235. }
  236. }
  237. return
  238. }