up_year.go 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. package service
  2. import (
  3. "context"
  4. "fmt"
  5. "time"
  6. "go-common/library/log"
  7. xtime "go-common/library/time"
  8. )
  9. var (
  10. _upLevel = map[int]string{
  11. 0: "光腚激大励",
  12. 1: "奶瓶激大励",
  13. 2: "幼稚园激大励",
  14. 3: "小学生激大励",
  15. 4: "中学生激大励",
  16. 5: "社会人激大励",
  17. 6: "筋肉激大励",
  18. }
  19. _downshiftUps = map[int64]bool{
  20. 148834382: true,
  21. 3647169: true,
  22. 100238104: true,
  23. 42932619: true,
  24. 331782289: true,
  25. 40305856: true,
  26. 19419058: true,
  27. 4397552: true,
  28. 234891605: true,
  29. 310562646: true,
  30. 11793131: true,
  31. 316737962: true,
  32. 5326066: true,
  33. 669622: true,
  34. 312663583: true,
  35. 34028169: true,
  36. 288220100: true,
  37. 344838590: true,
  38. 25729281: true,
  39. 96506011: true,
  40. 312508662: true,
  41. 8888364: true,
  42. 238674714: true,
  43. 337061658: true,
  44. 321230684: true,
  45. 337376429: true,
  46. 85738972: true,
  47. 10094840: true,
  48. 291595967: true,
  49. 300676862: true,
  50. 298484242: true,
  51. 40426408: true,
  52. 2336206: true,
  53. 78839625: true,
  54. 291682397: true,
  55. 300576557: true,
  56. 61086273: true,
  57. 318078497: true,
  58. 291051169: true,
  59. 27174089: true,
  60. 274284147: true,
  61. 306198844: true,
  62. }
  63. )
  64. // UpYear up year
  65. func (s *Service) UpYear(c context.Context, mid int64) (data interface{}, err error) {
  66. redisKey := fmt.Sprintf("growup-up-year:%d", mid)
  67. res, err := s.dao.GetIncomeCache(c, redisKey)
  68. if err != nil {
  69. log.Error("s.dao.GetIncomeCache error(%v)", err)
  70. return
  71. }
  72. if res != nil {
  73. data = res["data"]
  74. return
  75. }
  76. data, err = s.upYear(c, mid)
  77. if err != nil {
  78. log.Error("s.upYear error(%v)", err)
  79. return
  80. }
  81. err = s.dao.SetIncomeCache(c, redisKey, map[string]interface{}{"data": data})
  82. if err != nil {
  83. log.Error("s.dao.SetIncomeCache error(%v)", err)
  84. }
  85. return
  86. }
  87. func (s *Service) upYear(c context.Context, mid int64) (data interface{}, err error) {
  88. up := new(struct {
  89. Name string `json:"name"`
  90. Face string `json:"face"`
  91. IsJoin bool `json:"is_join"`
  92. SignedAt xtime.Time `json:"signed_at"`
  93. FirstTime xtime.Time `json:"first_time"`
  94. HasIncome bool `json:"has_income"`
  95. Title string `json:"title"`
  96. Level int `json:"level"`
  97. TagIncome []int64 `json:"tag_income"`
  98. })
  99. defer func() {
  100. data = up
  101. }()
  102. // has signed
  103. up.SignedAt, err = s.getUpFirstSignedAt(c, mid)
  104. if err != nil {
  105. log.Error("s.dao.getUpFirstSignedAt error(%v)", err)
  106. return
  107. }
  108. if up.SignedAt == 0 {
  109. up.IsJoin = false
  110. return
  111. }
  112. up.IsJoin = true
  113. // first income
  114. up.FirstTime, err = s.dao.GetFirstUpIncome(c, mid)
  115. if err != nil {
  116. log.Error("s.dao.GetFirstUpIncome error(%v)", err)
  117. return
  118. }
  119. if up.FirstTime != 0 {
  120. up.HasIncome = true
  121. }
  122. earliestTime := xtime.Time(time.Date(2018, 2, 1, 0, 0, 0, 0, time.Local).Unix())
  123. if up.SignedAt < earliestTime {
  124. up.SignedAt = earliestTime
  125. }
  126. if up.FirstTime < earliestTime {
  127. up.FirstTime = earliestTime
  128. }
  129. // tag income
  130. var totalIncome int64
  131. up.TagIncome, totalIncome, err = s.dao.GetUpYearTag(c, mid)
  132. if err != nil {
  133. log.Error("s.dao.GetUpYearTag error(%v)", err)
  134. return
  135. }
  136. upInfos, err := s.dao.AccountInfos(c, []int64{mid})
  137. if err != nil {
  138. log.Error("s.dao.AccountInfos error(%v)", err)
  139. return
  140. }
  141. if info, ok := upInfos[mid]; ok {
  142. up.Name = info.Nickname
  143. up.Face = info.Face
  144. }
  145. switch {
  146. case totalIncome == 0:
  147. up.Level = 0
  148. case totalIncome > 0 && totalIncome <= 10000:
  149. up.Level = 1
  150. case totalIncome > 10000 && totalIncome <= 100000:
  151. up.Level = 2
  152. case totalIncome > 100000 && totalIncome <= 1000000:
  153. up.Level = 3
  154. case totalIncome > 1000000 && totalIncome <= 5000000:
  155. up.Level = 4
  156. case totalIncome > 5000000 && totalIncome <= 10000000:
  157. up.Level = 5
  158. case totalIncome > 10000000:
  159. up.Level = 6
  160. }
  161. if up.Level > 0 && _downshiftUps[mid] {
  162. up.Level--
  163. }
  164. up.Title = _upLevel[up.Level]
  165. return
  166. }
  167. func (s *Service) getUpFirstSignedAt(c context.Context, mid int64) (signedAt xtime.Time, err error) {
  168. video, err := s.dao.GetUpSignedAt(c, "up_info_video", mid)
  169. if err != nil {
  170. return
  171. }
  172. signedAt = video
  173. column, err := s.dao.GetUpSignedAt(c, "up_info_column", mid)
  174. if err != nil {
  175. return
  176. }
  177. if column != 0 && (signedAt > column || signedAt == 0) {
  178. signedAt = column
  179. }
  180. bgm, err := s.dao.GetUpSignedAt(c, "up_info_bgm", mid)
  181. if err != nil {
  182. return
  183. }
  184. if bgm != 0 && (signedAt > bgm || signedAt == 0) {
  185. signedAt = bgm
  186. }
  187. return
  188. }