weeklyhonor.go 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  1. package service
  2. import (
  3. "context"
  4. "strings"
  5. "time"
  6. wkhmdl "go-common/app/interface/main/creative/model/weeklyhonor"
  7. upgrpc "go-common/app/service/main/up/api/v1"
  8. "go-common/library/log"
  9. "go-common/library/sync/errgroup"
  10. )
  11. var (
  12. forbidMidMap = map[int64]struct{}{
  13. 24: {},
  14. 2: {},
  15. 517999: {},
  16. 9099524: {},
  17. 208259: {},
  18. 202466803: {},
  19. 40016273: {},
  20. 245482023: {},
  21. 84089650: {},
  22. 31465698: {},
  23. 22160843: {},
  24. 3098848: {},
  25. 39592268: {},
  26. 223931175: {},
  27. }
  28. honorMap map[int][]*wkhmdl.HonorWord
  29. )
  30. // SendMsg .
  31. func (s *Service) SendMsg() {
  32. log.Info("Start SendMsg")
  33. var (
  34. c = context.TODO()
  35. lastid int64
  36. size = 1000
  37. )
  38. for {
  39. var (
  40. upActives []*upgrpc.UpActivity
  41. newid int64
  42. err error
  43. )
  44. for i := 0; i < 5; i++ {
  45. upActives, newid, err = s.honDao.UpActivesList(c, lastid, size)
  46. if err == nil {
  47. break
  48. }
  49. }
  50. if err != nil {
  51. log.Error("s.honDao.UpActivesList(%d,%d) error(%v)", lastid, size, err)
  52. break
  53. }
  54. // filter mid
  55. mids, err := s.filterMids(c, upActives)
  56. if err != nil {
  57. continue
  58. }
  59. var errMids []int64
  60. if len(mids) > 0 {
  61. for i := 1; i < 3; i++ {
  62. if errMids, err = s.honDao.SendNotify(c, mids); err != nil {
  63. log.Error("s.honDao.SendNotify(%v) error(%v)", mids, err)
  64. continue
  65. }
  66. break
  67. }
  68. }
  69. go s.infocMsgStat(mids, errMids)
  70. if len(upActives) < size {
  71. break
  72. }
  73. lastid = newid
  74. time.Sleep(time.Second)
  75. }
  76. log.Info("Finish SendMsg")
  77. }
  78. // FlushHonor .
  79. func (s *Service) FlushHonor() {
  80. log.Info("FlushHonor Start")
  81. var (
  82. c = context.TODO()
  83. lastid int64
  84. size = 1000
  85. batchSize = s.c.HonorStep
  86. )
  87. for {
  88. upActives, newid, err := s.honDao.UpActivesList(c, lastid, size)
  89. if err != nil {
  90. log.Error("s.honDao.UpActivesList(%d,%d) error(%v)", lastid, size, err)
  91. time.Sleep(time.Millisecond * 100)
  92. continue
  93. }
  94. var mids []int64
  95. for _, v := range upActives {
  96. mids = append(mids, v.Mid)
  97. }
  98. g := new(errgroup.Group)
  99. var pmids []int64
  100. routines := len(mids)/batchSize + 1
  101. for i := 0; i < routines; i++ {
  102. if i == routines-1 {
  103. pmids = mids[i*batchSize:]
  104. } else {
  105. pmids = mids[i*batchSize : (i+1)*batchSize]
  106. }
  107. t := pmids
  108. g.Go(func() (err error) {
  109. err = s.upsertHonor(c, t)
  110. if err != nil {
  111. log.Error("s.upsertHonor(%v) error(%v)", t, err)
  112. return err
  113. }
  114. return nil
  115. })
  116. }
  117. g.Wait()
  118. if len(mids) < size {
  119. break
  120. }
  121. lastid = newid
  122. }
  123. log.Info("FlushHonor done")
  124. }
  125. func (s *Service) upsertHonor(c context.Context, mids []int64) error {
  126. now := time.Now()
  127. day := int(now.Weekday())
  128. date := now.AddDate(0, 0, -day-1)
  129. saturday := date.Format("20060102")
  130. LOOP:
  131. for _, mid := range mids {
  132. var hls map[int]*wkhmdl.HonorLog
  133. hls, err := s.honDao.HonorLogs(c, mid)
  134. if err != nil {
  135. log.Error("s.honDao.HonorLogs(%d) error(%v)", mid, err)
  136. time.Sleep(time.Millisecond * 100)
  137. continue
  138. }
  139. var lastHid int
  140. for _, v := range hls {
  141. if int64(v.MTime) > date.Unix() && int64(v.MTime) < date.AddDate(0, 0, 7).Unix() {
  142. continue LOOP
  143. }
  144. if int64(v.MTime) < date.Unix() && int64(v.MTime) > date.AddDate(0, 0, -7).Unix() {
  145. lastHid = v.HID
  146. }
  147. }
  148. var hs *wkhmdl.HonorStat
  149. for i := 0; i < 3; i++ {
  150. hs, err = s.honDao.HonorStat(c, mid, saturday)
  151. if err != nil {
  152. log.Error("s.honDao.HonorStat(%d,%v) error(%v)", mid, saturday, err)
  153. continue
  154. }
  155. if hs != nil {
  156. break
  157. }
  158. }
  159. if hs == nil {
  160. log.Error("FlushHonor nil hs mid(%d)", mid)
  161. time.Sleep(time.Millisecond * 100)
  162. continue
  163. }
  164. newHid := hs.GenHonor(mid, lastHid)
  165. affected, err := s.honDao.UpsertCount(c, mid, newHid)
  166. if err != nil || affected == 0 {
  167. log.Error("s.honDao.UpsertCount(%d,%d) affceted(%d) error(%v)", mid, newHid, affected, err)
  168. }
  169. log.Info("FlushHonor mid(%d)", mid)
  170. }
  171. return nil
  172. }
  173. // TestSendMsg .
  174. func (s *Service) TestSendMsg(c context.Context, mids []int64) (err error) {
  175. for i := 1; i < 3; i++ {
  176. if _, err = s.honDao.SendNotify(c, mids); err != nil {
  177. log.Error("s.honDao.SendNotify(%v) error(%v)", mids, err)
  178. continue
  179. } else {
  180. break
  181. }
  182. }
  183. return
  184. }
  185. func (s *Service) filterMids(c context.Context, upActives []*upgrpc.UpActivity) (mids []int64, err error) {
  186. if len(upActives) == 0 {
  187. return
  188. }
  189. var rawMids []int64
  190. for _, v := range upActives {
  191. rawMids = append(rawMids, v.Mid)
  192. }
  193. hls, err := s.honDao.LatestHonorLogs(c, rawMids)
  194. if err != nil {
  195. log.Error("failed to get latest honor logs err(%v)", err)
  196. return
  197. }
  198. midClickMap, err := s.honDao.ClickCounts(c, rawMids)
  199. if err != nil {
  200. log.Error("failed to get honor click count err (%v)", err)
  201. return
  202. }
  203. highLevMap := highLevMidMap(hls)
  204. for _, v := range upActives {
  205. var subState uint8
  206. if subState, err = s.honDao.GetUpSwitch(c, v.Mid); subState == wkhmdl.HonorUnSub {
  207. continue
  208. }
  209. if err != nil {
  210. log.Error("s.honDao.GetUpSwitch mid(%d) err(%v)", v.Mid, err)
  211. }
  212. var cnt int
  213. if cnt, err = s.honDao.UpCount(c, v.Mid); err == nil && cnt == 0 {
  214. continue
  215. }
  216. if highLevMap[v.Mid] {
  217. mids = append(mids, v.Mid)
  218. continue
  219. }
  220. if _, ok := forbidMidMap[v.Mid]; ok {
  221. continue
  222. }
  223. if v.Activity > 3 {
  224. continue
  225. }
  226. if _, ok := midClickMap[v.Mid]; ok {
  227. mids = append(mids, v.Mid)
  228. continue
  229. }
  230. if sunday := wkhmdl.LatestSunday(); s.c.SendEveryWeek || isOddWeek(sunday) {
  231. mids = append(mids, v.Mid)
  232. }
  233. }
  234. return mids, err
  235. }
  236. func isOddWeek(date time.Time) bool {
  237. _, w := date.ISOWeek()
  238. return w%2 != 0
  239. }
  240. func highLevMidMap(hls []*wkhmdl.HonorLog) (res map[int64]bool) {
  241. if honorMap == nil {
  242. honorMap = wkhmdl.HMap()
  243. }
  244. res = make(map[int64]bool)
  245. for _, h := range hls {
  246. hws, ok := honorMap[h.HID]
  247. if !ok || len(hws) == 0 {
  248. continue
  249. }
  250. last := len(hws) - 1
  251. res[h.MID] = isHighLev(hws[last].Priority)
  252. }
  253. return
  254. }
  255. func isHighLev(p string) bool {
  256. return strings.HasPrefix(p, "A") || strings.HasPrefix(p, "R") || strings.HasPrefix(p, "S")
  257. }
  258. func (s *Service) infocMsgStat(mids, errMids []int64) {
  259. errMidsMap := make(map[int64]bool)
  260. for _, mid := range errMids {
  261. errMidsMap[mid] = true
  262. }
  263. for _, mid := range mids {
  264. var success int32
  265. if !errMidsMap[mid] {
  266. success = 1
  267. }
  268. err := s.honDao.HonorInfoc(context.Background(), mid, success)
  269. if err != nil {
  270. log.Error("failed to log honor infoc,mid(%d),success(%d),err(%v)", mid, success, err)
  271. }
  272. }
  273. }