user_service.go 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  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. var analystsDTO analystService.FinancialAnalystDTO
  102. analystsDTO, err = analystService.GetAnalystById(followDTo.AnalystId)
  103. if err != nil {
  104. logger.Error("获取研究员信息失败")
  105. } else {
  106. followDTo.HeadImgUrl = analystsDTO.HeadImgUrl
  107. }
  108. }(&analysts[i])
  109. }
  110. wg.Wait()
  111. //排序
  112. sort.Slice(analysts, func(i, j int) bool {
  113. // 首先按 NeedNotice 排序
  114. if analysts[i].NeedNotice == analysts[j].NeedNotice {
  115. // 对于 NeedNotice 相同的情况下,进行倒序排列
  116. return analysts[i].FollowedTime.After(analysts[j].FollowedTime)
  117. }
  118. // NeedNotice 为 true 的排在 false 的前面
  119. return analysts[i].NeedNotice
  120. })
  121. if err != nil {
  122. logger.Error("转换研究员列表失败:%v", err)
  123. err = exception.New(exception.TransferFollowingAnalystListFailed)
  124. }
  125. return
  126. }
  127. func GetUnReadMessageList(userId int) (messages []userService.MyMessage, err error) {
  128. messages, err = userService.GetUnReadMessageList(userId)
  129. if err != nil {
  130. err = exception.New(exception.GetUserUnReadMsgFailed)
  131. }
  132. return
  133. }
  134. func ReadMessage(userId int, messageId int) bool {
  135. return userService.ReadMessage(userId, messageId)
  136. }
  137. func ReadMessages(userId int, analystId int) bool {
  138. return userService.ReadMessages(userId, analystId)
  139. }
  140. type FollowAnalystDTO struct {
  141. AnalystId int `json:"analystId"`
  142. AnalystName string `json:"analystName"`
  143. HeadImgUrl string `json:"headImgUrl"`
  144. FollowedTime time.Time `json:"followedTime"`
  145. NeedNotice bool `json:"needNotice"`
  146. }
  147. func convertToAnalystList(dtoList []userService.FollowDTO) (list []FollowAnalystDTO, err error) {
  148. for _, dto := range dtoList {
  149. analyst := FollowAnalystDTO{
  150. AnalystId: dto.AnalystId,
  151. AnalystName: dto.AnalystName,
  152. FollowedTime: dto.FollowedTime,
  153. NeedNotice: false,
  154. }
  155. list = append(list, analyst)
  156. }
  157. return
  158. }
  159. func FollowAnalystByName(userId int, analystName string, followType string) (err error) {
  160. FinancialAnalystDTO, err := analystService.GetAnalystByName(analystName)
  161. if err != nil {
  162. err = exception.New(exception.AnalystNotFound)
  163. }
  164. followDTO := userService.FollowDTO{
  165. UserId: userId,
  166. AnalystId: FinancialAnalystDTO.Id,
  167. AnalystName: FinancialAnalystDTO.Name,
  168. FollowType: followType,
  169. }
  170. err = userService.FollowAnalyst(followDTO)
  171. if err != nil {
  172. logger.Error("关注研究员失败:%v", err)
  173. err = exception.New(exception.UserFollowAnalystFailed)
  174. }
  175. return
  176. }
  177. func FeedBack(userId int, mobile string, message string) (err error) {
  178. feedback := userService.FeedbackDTO{
  179. UserId: userId,
  180. Mobile: mobile,
  181. Message: message,
  182. }
  183. err = userService.FeedBack(feedback)
  184. if err != nil {
  185. err = exception.New(exception.FeedBackError)
  186. }
  187. return
  188. }
  189. func GetUserByMobile(mobile string) (user User, err error) {
  190. userDTO, err := userService.GetUserByMobile(mobile)
  191. if err == nil {
  192. user = convertToUser(userDTO)
  193. }
  194. return
  195. }
  196. func GetUserByOpenId(openId string) (user User, err error) {
  197. userDTO, err := userService.GetUserByOpenId(openId)
  198. if err != nil {
  199. return
  200. }
  201. user = convertToUser(userDTO)
  202. return
  203. }
  204. func convertToUser(userDTO userService.UserDTO) User {
  205. return User{
  206. Id: userDTO.Id,
  207. Username: userDTO.Username,
  208. Mobile: userDTO.Mobile,
  209. }
  210. }