user_service.go 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  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. if FinancialAnalystDTO.Id == 0 || FinancialAnalystDTO.Name == "" {
  49. continue
  50. }
  51. followDTO := userService.FollowDTO{
  52. UserId: userId,
  53. AnalystId: FinancialAnalystDTO.Id,
  54. AnalystName: FinancialAnalystDTO.Name,
  55. FollowType: followType,
  56. }
  57. followlist = append(followlist, followDTO)
  58. }
  59. err = userService.FollowAnalystsByName(userId, followlist, followType)
  60. if err != nil {
  61. logger.Error("批量关注研究员失败:%v", err)
  62. err = exception.New(exception.BatchFollowingAnalystFailed)
  63. }
  64. return
  65. }
  66. func CheckFollowStatusByNames(userId int, names []string) (list []userService.FollowDTO, err error) {
  67. list, err = userService.CheckFollowStatusByNames(userId, names)
  68. if err != nil {
  69. logger.Error("获取关注状态失败:%v", err)
  70. err = exception.New(exception.CheckFollowStatusByNamesFailed)
  71. }
  72. return
  73. }
  74. func FollowAnalyst(userId int, analystId int, followType string) (err error) {
  75. FinancialAnalystDTO, err := analystService.GetAnalystById(analystId)
  76. if err != nil {
  77. err = exception.New(exception.AnalystNotFound)
  78. }
  79. if FinancialAnalystDTO.Id == 0 || FinancialAnalystDTO.Name == "" {
  80. err = exception.New(exception.AnalystNotFound)
  81. return
  82. }
  83. followDTO := userService.FollowDTO{
  84. UserId: userId,
  85. AnalystId: analystId,
  86. AnalystName: FinancialAnalystDTO.Name,
  87. FollowType: followType,
  88. }
  89. err = userService.FollowAnalyst(followDTO)
  90. if err != nil {
  91. logger.Error("关注研究员失败:%v", err)
  92. err = exception.New(exception.UserFollowAnalystFailed)
  93. }
  94. return
  95. }
  96. func GetFollowingAnalystList(userId int) (analysts []FollowAnalystDTO, err error) {
  97. logger.Info("用户ID:%d", userId)
  98. dtoList, err := userService.GetFollowingAnalystList(userId)
  99. if err != nil {
  100. logger.Error("获取关注列表失败:%v", err)
  101. err = exception.New(exception.GetFollowingAnalystListFailed)
  102. return
  103. }
  104. analysts, err = convertToAnalystList(dtoList)
  105. var wg sync.WaitGroup
  106. wg.Add(len(analysts))
  107. for i := 0; i < len(analysts); i++ {
  108. go func(followDTo *FollowAnalystDTO) {
  109. defer wg.Done()
  110. followDTo.NeedNotice = userService.NeedNotice(userId, followDTo.AnalystId)
  111. var analystsDTO analystService.FinancialAnalystDTO
  112. analystsDTO, err = analystService.GetAnalystById(followDTo.AnalystId)
  113. if err != nil {
  114. logger.Error("获取研究员信息失败")
  115. } else {
  116. followDTo.HeadImgUrl = analystsDTO.HeadImgUrl
  117. }
  118. }(&analysts[i])
  119. }
  120. wg.Wait()
  121. //排序
  122. sort.Slice(analysts, func(i, j int) bool {
  123. // 首先按 NeedNotice 排序
  124. if analysts[i].NeedNotice == analysts[j].NeedNotice {
  125. // 对于 NeedNotice 相同的情况下,进行倒序排列
  126. return analysts[i].FollowedTime.After(analysts[j].FollowedTime)
  127. }
  128. // NeedNotice 为 true 的排在 false 的前面
  129. return analysts[i].NeedNotice
  130. })
  131. //if err != nil {
  132. // logger.Error("转换研究员列表失败:%v", err)
  133. // err = exception.New(exception.TransferFollowingAnalystListFailed)
  134. //}
  135. return
  136. }
  137. func GetUnReadMessageList(userId int) (messages []userService.MyMessage, err error) {
  138. messages, err = userService.GetUnReadMessageList(userId)
  139. if err != nil {
  140. err = exception.New(exception.GetUserUnReadMsgFailed)
  141. }
  142. return
  143. }
  144. func ReadMessage(userId int, messageId int) bool {
  145. return userService.ReadMessage(userId, messageId)
  146. }
  147. func ReadMessages(userId int, analystId int) bool {
  148. return userService.ReadMessages(userId, analystId)
  149. }
  150. type FollowAnalystDTO struct {
  151. AnalystId int `json:"analystId"`
  152. AnalystName string `json:"analystName"`
  153. HeadImgUrl string `json:"headImgUrl"`
  154. FollowedTime time.Time `json:"followedTime"`
  155. NeedNotice bool `json:"needNotice"`
  156. }
  157. func convertToAnalystList(dtoList []userService.FollowDTO) (list []FollowAnalystDTO, err error) {
  158. for _, dto := range dtoList {
  159. analyst := FollowAnalystDTO{
  160. AnalystId: dto.AnalystId,
  161. AnalystName: dto.AnalystName,
  162. FollowedTime: dto.FollowedTime,
  163. NeedNotice: false,
  164. }
  165. list = append(list, analyst)
  166. }
  167. return
  168. }
  169. func FollowAnalystByName(userId int, analystName string, followType string) (err error) {
  170. FinancialAnalystDTO, err := analystService.GetAnalystByName(analystName)
  171. if err != nil {
  172. err = exception.New(exception.AnalystNotFound)
  173. }
  174. if FinancialAnalystDTO.Id == 0 || FinancialAnalystDTO.Name == "" {
  175. err = exception.New(exception.AnalystNotFound)
  176. return
  177. }
  178. followDTO := userService.FollowDTO{
  179. UserId: userId,
  180. AnalystId: FinancialAnalystDTO.Id,
  181. AnalystName: FinancialAnalystDTO.Name,
  182. FollowType: followType,
  183. }
  184. err = userService.FollowAnalyst(followDTO)
  185. if err != nil {
  186. logger.Error("关注研究员失败:%v", err)
  187. err = exception.New(exception.UserFollowAnalystFailed)
  188. }
  189. return
  190. }
  191. func FeedBack(userId int, mobile string, message string) (err error) {
  192. feedback := userService.FeedbackDTO{
  193. UserId: userId,
  194. Mobile: mobile,
  195. Message: message,
  196. }
  197. err = userService.FeedBack(feedback)
  198. if err != nil {
  199. err = exception.New(exception.FeedBackError)
  200. }
  201. return
  202. }
  203. func GetUserByMobile(mobile string) (user User, err error) {
  204. userDTO, err := userService.GetUserByMobile(mobile)
  205. if err == nil {
  206. user = convertToUser(userDTO)
  207. }
  208. return
  209. }
  210. func GetUserByOpenId(openId string) (user User, err error) {
  211. userDTO, err := userService.GetUserByOpenId(openId)
  212. if err != nil {
  213. return
  214. }
  215. user = convertToUser(userDTO)
  216. return
  217. }
  218. func convertToUser(userDTO userService.UserDTO) User {
  219. return User{
  220. Id: userDTO.Id,
  221. Username: userDTO.Username,
  222. Mobile: userDTO.Mobile,
  223. }
  224. }