user_service.go 7.2 KB

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