user_service.go 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  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. const (
  14. RiskValid = "valid"
  15. RiskExpired = "expired"
  16. RiskUnTest = "unTest"
  17. )
  18. type User struct {
  19. Id int `json:"id"`
  20. Username string `json:"username"`
  21. AreaCode string `json:"areaCode"`
  22. Mobile string `json:"mobile"`
  23. OpenId string `json:"openId,omitempty"`
  24. }
  25. type AnalystDetail struct {
  26. AnalystName string `json:"AnalystName"`
  27. HeadImgUrl string `json:"HeadImgUrl"`
  28. Introduction string `json:"Introduction"`
  29. Followed string `json:"Followed"`
  30. }
  31. type UserProfile struct {
  32. Mobile string `json:"mobile"`
  33. RiskLevel string `json:"riskLevel"`
  34. RiskLevelStatus string `json:"riskLevelStatus"`
  35. UserName string `json:"userName"`
  36. }
  37. func convertUserDTOToProfile(dto userService.UserDTO) (profile UserProfile) {
  38. profile = UserProfile{
  39. Mobile: dto.Mobile,
  40. RiskLevel: dto.RiskLevel,
  41. UserName: dto.Username,
  42. }
  43. if profile.UserName == "" {
  44. profile.UserName = dto.Mobile
  45. }
  46. if dto.RiskLevel == "" {
  47. profile.RiskLevelStatus = RiskUnTest
  48. return
  49. }
  50. date, err := time.Parse(time.DateOnly, dto.RiskValidEndDate)
  51. if err != nil {
  52. logger.Error("解析日期失败:%v", err)
  53. profile.RiskLevelStatus = RiskExpired
  54. return
  55. }
  56. currentDate := time.Now().Truncate(24 * time.Hour)
  57. expiryDate := date.Truncate(24 * time.Hour)
  58. if expiryDate.Before(currentDate) {
  59. profile.RiskLevelStatus = RiskExpired
  60. return
  61. }
  62. profile.RiskLevelStatus = RiskValid
  63. return
  64. }
  65. func GetUserProfile(userId int) (userProfile UserProfile, err error) {
  66. userDTO, err := userService.GetUserById(userId)
  67. if err != nil {
  68. if errors.Is(err, gorm.ErrRecordNotFound) {
  69. logger.Error("用户不存在,用户Id:%d", userId)
  70. err = exception.New(exception.TemplateUserNotFound)
  71. } else {
  72. logger.Error("获取用户信息失败:%v", err)
  73. err = exception.New(exception.TemplateUserFoundFailed)
  74. }
  75. return
  76. }
  77. userProfile = convertUserDTOToProfile(userDTO)
  78. return
  79. }
  80. func GetAnalystDetail(userId int, analystId int) (analystDetail AnalystDetail, err error) {
  81. analyst, err := analystService.GetAnalystById(analystId)
  82. if err != nil {
  83. logger.Error("研究员信息不存在:%v", err)
  84. err = exception.New(exception.AnalystNotFound)
  85. }
  86. analystDetail = convertToAnalystDetail(analyst)
  87. //研究员关注状态
  88. analystDetail.Followed = userService.GetFollowed(userId, analystId)
  89. return
  90. }
  91. func convertToAnalystDetail(dto analystService.FinancialAnalystDTO) AnalystDetail {
  92. return AnalystDetail{
  93. AnalystName: dto.Name,
  94. HeadImgUrl: dto.HeadImgUrl,
  95. Introduction: dto.Introduction,
  96. }
  97. }
  98. func FollowAnalystsByName(userId int, analystNames []string, followType string) (err error) {
  99. var followlist []userService.FollowDTO
  100. for _, analystName := range analystNames {
  101. FinancialAnalystDTO, followErr := analystService.GetAnalystByName(analystName)
  102. if followErr != nil {
  103. err = exception.New(exception.AnalystNotFound)
  104. }
  105. if FinancialAnalystDTO.Id == 0 || FinancialAnalystDTO.Name == "" {
  106. continue
  107. }
  108. followDTO := userService.FollowDTO{
  109. UserId: userId,
  110. AnalystId: FinancialAnalystDTO.Id,
  111. AnalystName: FinancialAnalystDTO.Name,
  112. FollowType: followType,
  113. }
  114. followlist = append(followlist, followDTO)
  115. }
  116. err = userService.FollowAnalystsByName(userId, followlist, followType)
  117. if err != nil {
  118. logger.Error("批量关注研究员失败:%v", err)
  119. err = exception.New(exception.BatchFollowingAnalystFailed)
  120. }
  121. return
  122. }
  123. func CheckFollowStatusByNames(userId int, names []string) (list []userService.FollowDTO, err error) {
  124. list, err = userService.CheckFollowStatusByNames(userId, names)
  125. if err != nil {
  126. logger.Error("获取关注状态失败:%v", err)
  127. err = exception.New(exception.CheckFollowStatusByNamesFailed)
  128. }
  129. return
  130. }
  131. func FollowAnalyst(userId int, analystId int, followType string) (err error) {
  132. FinancialAnalystDTO, err := analystService.GetAnalystById(analystId)
  133. if err != nil {
  134. err = exception.New(exception.AnalystNotFound)
  135. }
  136. if FinancialAnalystDTO.Id == 0 || FinancialAnalystDTO.Name == "" {
  137. err = exception.New(exception.AnalystNotFound)
  138. return
  139. }
  140. followDTO := userService.FollowDTO{
  141. UserId: userId,
  142. AnalystId: analystId,
  143. AnalystName: FinancialAnalystDTO.Name,
  144. FollowType: followType,
  145. }
  146. err = userService.FollowAnalyst(followDTO)
  147. if err != nil {
  148. logger.Error("关注研究员失败:%v", err)
  149. err = exception.New(exception.UserFollowAnalystFailed)
  150. }
  151. return
  152. }
  153. func GetFollowingAnalystList(userId int) (analysts []FollowAnalystDTO, err error) {
  154. logger.Info("用户ID:%d", userId)
  155. dtoList, err := userService.GetFollowingAnalystList(userId)
  156. if err != nil {
  157. logger.Error("获取关注列表失败:%v", err)
  158. err = exception.New(exception.GetFollowingAnalystListFailed)
  159. return
  160. }
  161. analysts, err = convertToAnalystList(dtoList)
  162. var wg sync.WaitGroup
  163. wg.Add(len(analysts))
  164. for i := 0; i < len(analysts); i++ {
  165. go func(followDTo *FollowAnalystDTO) {
  166. defer wg.Done()
  167. followDTo.NeedNotice = userService.NeedNotice(userId, followDTo.AnalystId)
  168. var analystsDTO analystService.FinancialAnalystDTO
  169. analystsDTO, err = analystService.GetAnalystById(followDTo.AnalystId)
  170. if err != nil {
  171. logger.Error("获取研究员信息失败")
  172. } else {
  173. followDTo.HeadImgUrl = analystsDTO.HeadImgUrl
  174. }
  175. }(&analysts[i])
  176. }
  177. wg.Wait()
  178. //排序
  179. sort.Slice(analysts, func(i, j int) bool {
  180. // 首先按 NeedNotice 排序
  181. if analysts[i].NeedNotice == analysts[j].NeedNotice {
  182. // 对于 NeedNotice 相同的情况下,进行倒序排列
  183. return analysts[i].FollowedTime.After(analysts[j].FollowedTime)
  184. }
  185. // NeedNotice 为 true 的排在 false 的前面
  186. return analysts[i].NeedNotice
  187. })
  188. //if err != nil {
  189. // logger.Error("转换研究员列表失败:%v", err)
  190. // err = exception.New(exception.TransferFollowingAnalystListFailed)
  191. //}
  192. return
  193. }
  194. func GetUnReadMessageList(userId int) (messages []userService.MyMessage, err error) {
  195. messages, err = userService.GetUnReadMessageList(userId)
  196. if err != nil {
  197. err = exception.New(exception.GetUserUnReadMsgFailed)
  198. }
  199. return
  200. }
  201. func ReadMessage(userId int, messageId int) bool {
  202. return userService.ReadMessage(userId, messageId)
  203. }
  204. func ReadMessages(userId int, analystId int) bool {
  205. return userService.ReadMessages(userId, analystId)
  206. }
  207. type FollowAnalystDTO struct {
  208. AnalystId int `json:"analystId"`
  209. AnalystName string `json:"analystName"`
  210. HeadImgUrl string `json:"headImgUrl"`
  211. FollowedTime time.Time `json:"followedTime"`
  212. NeedNotice bool `json:"needNotice"`
  213. }
  214. func convertToAnalystList(dtoList []userService.FollowDTO) (list []FollowAnalystDTO, err error) {
  215. for _, dto := range dtoList {
  216. analyst := FollowAnalystDTO{
  217. AnalystId: dto.AnalystId,
  218. AnalystName: dto.AnalystName,
  219. FollowedTime: dto.FollowedTime,
  220. NeedNotice: false,
  221. }
  222. list = append(list, analyst)
  223. }
  224. return
  225. }
  226. func FollowAnalystByName(userId int, analystName string, followType string) (err error) {
  227. FinancialAnalystDTO, err := analystService.GetAnalystByName(analystName)
  228. if err != nil {
  229. err = exception.New(exception.AnalystNotFound)
  230. }
  231. if FinancialAnalystDTO.Id == 0 || FinancialAnalystDTO.Name == "" {
  232. err = exception.New(exception.AnalystNotFound)
  233. return
  234. }
  235. followDTO := userService.FollowDTO{
  236. UserId: userId,
  237. AnalystId: FinancialAnalystDTO.Id,
  238. AnalystName: FinancialAnalystDTO.Name,
  239. FollowType: followType,
  240. }
  241. err = userService.FollowAnalyst(followDTO)
  242. if err != nil {
  243. logger.Error("关注研究员失败:%v", err)
  244. err = exception.New(exception.UserFollowAnalystFailed)
  245. }
  246. return
  247. }
  248. func FeedBack(userId int, mobile string, message string) (err error) {
  249. feedback := userService.FeedbackDTO{
  250. UserId: userId,
  251. Mobile: mobile,
  252. Message: message,
  253. }
  254. err = userService.FeedBack(feedback)
  255. if err != nil {
  256. err = exception.New(exception.FeedBackError)
  257. }
  258. return
  259. }
  260. func GetUserByMobile(mobile string) (user User, err error) {
  261. userDTO, err := userService.GetUserByMobile(mobile)
  262. if err != nil {
  263. if errors.Is(err, gorm.ErrRecordNotFound) {
  264. err = exception.New(exception.TemplateUserNotFound)
  265. } else {
  266. err = exception.New(exception.TemplateUserFoundFailed)
  267. }
  268. }
  269. user = convertToUser(userDTO)
  270. return
  271. }
  272. func GetUserByOpenId(openId string) (user User, err error) {
  273. userDTO, err := userService.GetUserByOpenId(openId)
  274. if err != nil {
  275. return
  276. }
  277. user = convertToUser(userDTO)
  278. return
  279. }
  280. func convertToUser(userDTO userService.UserDTO) User {
  281. return User{
  282. Id: userDTO.Id,
  283. Username: userDTO.Username,
  284. OpenId: userDTO.OpenId,
  285. AreaCode: userDTO.AreaCode,
  286. Mobile: userDTO.Mobile,
  287. }
  288. }
  289. func GetUserByTemplateUserId(templateUserId int) (officialUser userService.OfficialUserDTO, err error) {
  290. officialUser, err = userService.GetUserByTemplateUserId(templateUserId)
  291. if err != nil {
  292. if errors.Is(err, gorm.ErrRecordNotFound) {
  293. err = exception.New(exception.OfficialUserNotFound)
  294. logger.Info("用户未开户:%v", templateUserId)
  295. } else {
  296. err = exception.NewWithException(exception.OfficialUserFoundError, err.Error())
  297. logger.Error("获取正式用户信息失败:%v", err)
  298. }
  299. }
  300. return
  301. }