user_service.go 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. package user
  2. import (
  3. logger "eta/eta_mini_ht_api/common/component/log"
  4. "eta/eta_mini_ht_api/common/exception"
  5. analystService "eta/eta_mini_ht_api/domian/financial_analyst"
  6. userService "eta/eta_mini_ht_api/domian/user"
  7. "sort"
  8. "sync"
  9. "time"
  10. )
  11. type User struct {
  12. Id int `json:"id"`
  13. Username string `json:"username"`
  14. Mobile string `json:"mobile"`
  15. OpenId string `json:"openId,omitempty"`
  16. }
  17. type AnalystDetail struct {
  18. AnalystName string `json:"AnalystName"`
  19. HeadImgUrl string `json:"HeadImgUrl"`
  20. Introduction string `json:"Introduction"`
  21. Followed string `json:"Followed"`
  22. }
  23. func GetAnalystDetail(userId int, analystId int) (analystDetail AnalystDetail, err error) {
  24. analyst, err := analystService.GetAnalystById(analystId)
  25. if err != nil {
  26. logger.Error("研究员信息不存在:%v", err)
  27. err = exception.New(exception.AnalystNotFound)
  28. }
  29. analystDetail = convertToAnalystDetail(analyst)
  30. //研究员关注状态
  31. analystDetail.Followed = userService.GetFollowed(userId, analystId)
  32. return
  33. }
  34. func convertToAnalystDetail(dto analystService.FinancialAnalystDTO) AnalystDetail {
  35. return AnalystDetail{
  36. AnalystName: dto.Name,
  37. HeadImgUrl: dto.HeadImgUrl,
  38. Introduction: dto.Introduction,
  39. }
  40. }
  41. func FollowAnalystsByName(userId int, analystNames []string, followType string) (err error) {
  42. var followlist []userService.FollowDTO
  43. for _, analystName := range analystNames {
  44. FinancialAnalystDTO, followErr := analystService.GetAnalystByName(analystName)
  45. if followErr != nil {
  46. err = exception.New(exception.AnalystNotFound)
  47. }
  48. followDTO := userService.FollowDTO{
  49. UserId: userId,
  50. AnalystId: FinancialAnalystDTO.Id,
  51. AnalystName: FinancialAnalystDTO.Name,
  52. FollowType: followType,
  53. }
  54. followlist = append(followlist, followDTO)
  55. }
  56. err = userService.FollowAnalystsByName(userId, followlist, followType)
  57. if err != nil {
  58. logger.Error("批量关注研究员失败:%v", err)
  59. err = exception.New(exception.BatchFollowingAnalystFailed)
  60. }
  61. return
  62. }
  63. func CheckFollowStatusByNames(userId int, names []string) (list []userService.FollowDTO, err error) {
  64. list, err = userService.CheckFollowStatusByNames(userId, names)
  65. if err != nil {
  66. logger.Error("获取关注状态失败:%v", err)
  67. err = exception.New(exception.CheckFollowStatusByNamesFailed)
  68. }
  69. return
  70. }
  71. func FollowAnalyst(userId int, analystId int, followType string) (err error) {
  72. FinancialAnalystDTO, err := analystService.GetAnalystById(analystId)
  73. if err != nil {
  74. err = exception.New(exception.AnalystNotFound)
  75. }
  76. followDTO := userService.FollowDTO{
  77. UserId: userId,
  78. AnalystId: analystId,
  79. AnalystName: FinancialAnalystDTO.Name,
  80. FollowType: followType,
  81. }
  82. err = userService.FollowAnalyst(followDTO)
  83. if err != nil {
  84. logger.Error("关注研究员失败:%v", err)
  85. err = exception.New(exception.UserFollowAnalystFailed)
  86. }
  87. return
  88. }
  89. func GetFollowingAnalystList(userId int) (analysts []FollowAnalystDTO, err error) {
  90. dtoList, err := userService.GetFollowingAnalystList(userId)
  91. if err != nil {
  92. err = exception.New(exception.GetFollowingAnalystListFailed)
  93. }
  94. analysts, err = convertToAnalystList(dtoList)
  95. var wg sync.WaitGroup
  96. wg.Add(len(analysts))
  97. for i := 0; i < len(analysts); i++ {
  98. go func(followDTo *FollowAnalystDTO) {
  99. defer wg.Done()
  100. followDTo.NeedNotice = userService.NeedNotice(userId, followDTo.AnalystId)
  101. }(&analysts[i])
  102. }
  103. wg.Wait()
  104. //排序
  105. sort.Slice(analysts, func(i, j int) bool {
  106. // 首先按 NeedNotice 排序
  107. if analysts[i].NeedNotice == analysts[j].NeedNotice {
  108. // 对于 NeedNotice 相同的情况下,进行倒序排列
  109. return analysts[i].FollowedTime.After(analysts[j].FollowedTime)
  110. }
  111. // NeedNotice 为 true 的排在 false 的前面
  112. return analysts[i].NeedNotice
  113. })
  114. if err != nil {
  115. logger.Error("转换研究员列表失败:%v", err)
  116. err = exception.New(exception.TransferFollowingAnalystListFailed)
  117. }
  118. return
  119. }
  120. func GetUnReadMessageList(userId int) (messages []userService.MyMessage, err error) {
  121. messages, err = userService.GetUnReadMessageList(userId)
  122. if err != nil {
  123. err = exception.New(exception.GetUserUnReadMsgFailed)
  124. }
  125. return
  126. }
  127. func ReadMessage(userId int, messageId int) bool {
  128. return userService.ReadMessage(userId, messageId)
  129. }
  130. func ReadMessages(userId int, analystId int) bool {
  131. return userService.ReadMessages(userId, analystId)
  132. }
  133. type FollowAnalystDTO struct {
  134. AnalystId int `json:"analystId"`
  135. AnalystName string `json:"analystName"`
  136. HeadImgUrl string `json:"headImgUrl"`
  137. FollowedTime time.Time `json:"followedTime"`
  138. NeedNotice bool `json:"needNotice"`
  139. }
  140. func convertToAnalystList(dtoList []userService.FollowDTO) (list []FollowAnalystDTO, err error) {
  141. for _, dto := range dtoList {
  142. analyst := FollowAnalystDTO{
  143. AnalystId: dto.AnalystId,
  144. AnalystName: dto.AnalystName,
  145. FollowedTime: dto.FollowedTime,
  146. NeedNotice: false,
  147. }
  148. list = append(list, analyst)
  149. }
  150. return
  151. }
  152. func FollowAnalystByName(userId int, analystName string, followType string) (err error) {
  153. FinancialAnalystDTO, err := analystService.GetAnalystByName(analystName)
  154. if err != nil {
  155. err = exception.New(exception.AnalystNotFound)
  156. }
  157. followDTO := userService.FollowDTO{
  158. UserId: userId,
  159. AnalystId: FinancialAnalystDTO.Id,
  160. AnalystName: FinancialAnalystDTO.Name,
  161. FollowType: followType,
  162. }
  163. err = userService.FollowAnalyst(followDTO)
  164. if err != nil {
  165. logger.Error("关注研究员失败:%v", err)
  166. err = exception.New(exception.UserFollowAnalystFailed)
  167. }
  168. return
  169. }
  170. func FeedBack(userId int, mobile string, message string) (err error) {
  171. feedback := userService.FeedbackDTO{
  172. UserId: userId,
  173. Mobile: mobile,
  174. Message: message,
  175. }
  176. err = userService.FeedBack(feedback)
  177. if err != nil {
  178. err = exception.New(exception.FeedBackError)
  179. }
  180. return
  181. }
  182. func GetUserByMobile(mobile string) (user User, err error) {
  183. userDTO, err := userService.GetUserByMobile(mobile)
  184. if err == nil {
  185. user = convertToUser(userDTO)
  186. }
  187. return
  188. }
  189. func GetUserByOpenId(openId string) (user User, err error) {
  190. userDTO, err := userService.GetUserByOpenId(openId)
  191. if err == nil {
  192. user = convertToUser(userDTO)
  193. }
  194. return
  195. }
  196. func convertToUser(userDTO userService.UserDTO) User {
  197. return User{
  198. Id: userDTO.Id,
  199. Username: userDTO.Username,
  200. Mobile: userDTO.Mobile,
  201. }
  202. }