user_service.go 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519
  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. "eta/eta_mini_ht_api/common/utils/page"
  7. permissionService "eta/eta_mini_ht_api/domian/config"
  8. analystService "eta/eta_mini_ht_api/domian/financial_analyst"
  9. userService "eta/eta_mini_ht_api/domian/user"
  10. merchantDao "eta/eta_mini_ht_api/models/merchant"
  11. userDao "eta/eta_mini_ht_api/models/user"
  12. "eta/eta_mini_ht_api/service/config"
  13. "gorm.io/gorm"
  14. "sort"
  15. "sync"
  16. "time"
  17. )
  18. const (
  19. RiskValid = "valid"
  20. RiskExpired = "expired"
  21. RiskUnTest = "unTest"
  22. SubscribeExpired = "expired"
  23. UnSubscribe = "unSubscribe"
  24. Subscribing = "Subscribing"
  25. )
  26. type User struct {
  27. Id int `json:"id"`
  28. Username string `json:"username"`
  29. AreaCode string `json:"areaCode"`
  30. Mobile string `json:"mobile"`
  31. OpenId string `json:"openId,omitempty"`
  32. }
  33. type AnalystDetail struct {
  34. AnalystName string `json:"analystName"`
  35. HeadImgUrl string `json:"headImgUrl"`
  36. Introduction string `json:"introduction"`
  37. Followed string `json:"followed"`
  38. Position string `json:"position"`
  39. InvestmentCertificate string `json:"investmentCertificate"`
  40. ProfessionalCertificate string `json:"professionalCertificate"`
  41. }
  42. type UserProfile struct {
  43. Mobile string `json:"mobile"`
  44. RiskLevel string `json:"riskLevel"`
  45. RiskLevelStatus string `json:"riskLevelStatus"`
  46. UserName string `json:"userName"`
  47. }
  48. func CheckUserRiskMatchStatus(userId int) (riskLevel string, userRiskLevelStatus string, err error) {
  49. if userId <= 0 {
  50. err = exception.New(exception.IllegalTemplateUserId)
  51. return
  52. }
  53. userProfile, userErr := GetUserProfile(userId)
  54. if userErr != nil {
  55. if errors.Is(userErr, gorm.ErrRecordNotFound) {
  56. err = exception.New(exception.TemplateUserNotFound)
  57. } else {
  58. err = exception.New(exception.TemplateUserFoundFailed)
  59. }
  60. logger.Error("获取临时客户信息失败:%v", err)
  61. return
  62. }
  63. userRiskLevelStatus = userProfile.RiskLevelStatus
  64. //获取产品风险等级
  65. if userProfile.RiskLevelStatus == RiskUnTest {
  66. logger.Warn("客户未做风险等级测评,mobile:%v", userProfile.Mobile)
  67. }
  68. if userProfile.RiskLevelStatus == RiskExpired {
  69. logger.Warn("客户风险等级已过期,mobile:%v", userProfile.Mobile)
  70. }
  71. if userProfile.RiskLevel != "" && userProfile.RiskLevelStatus == RiskValid {
  72. var mapping permissionService.CustomerProductRiskMappingDTO
  73. mapping, err = permissionService.GetRiskMappingByCustomerRiskLevel(userProfile.RiskLevel)
  74. if err != nil {
  75. logger.Error("查询产品风险等级映射失败:%v", err)
  76. return
  77. }
  78. riskLevel = mapping.ProductRiskLevel
  79. }
  80. return
  81. }
  82. // GetRiskLevelPermissionList 删选掉没有配置风险等级的品种,并校验客户的风险等级,riskLevel只有在客户
  83. func GetRiskLevelPermissionList(permissionIds []int, isLogin bool, userId int) (filterPermissionIds []int, riskLevel string, userRiskStatus string, err error) {
  84. if isLogin {
  85. userProfile, userErr := GetUserProfile(userId)
  86. if userErr != nil {
  87. if errors.Is(userErr, gorm.ErrRecordNotFound) {
  88. err = exception.New(exception.TemplateUserNotFound)
  89. } else {
  90. err = exception.New(exception.TemplateUserFoundFailed)
  91. }
  92. logger.Error("获取临时客户信息失败:%v", err)
  93. return
  94. }
  95. var permissionList []permissionService.PermissionDTO
  96. if len(permissionIds) == 0 {
  97. //获取所有设置风险等级的品种
  98. permissionList, err = permissionService.GetPermissionListWithRisk()
  99. } else {
  100. //更具id过滤设置了风险等级的品种
  101. permissionList, err = permissionService.GetPermissionListByIds(permissionIds)
  102. }
  103. if err != nil {
  104. logger.Error("查询有风险等级的品种列表失败:%v", err)
  105. return
  106. }
  107. userRiskStatus = userProfile.RiskLevelStatus
  108. //获取产品风险等级
  109. if userProfile.RiskLevelStatus == RiskUnTest {
  110. logger.Warn("客户未做风险等级测评,mobile:%v", userProfile.Mobile)
  111. //err = exception.New(exception.RiskUnTestError)
  112. }
  113. if userProfile.RiskLevelStatus == RiskExpired {
  114. logger.Warn("客户风险等级已过期,mobile:%v", userProfile.Mobile)
  115. //err = exception.New(exception.RiskExpiredError)
  116. }
  117. if userProfile.RiskLevel != "" && userProfile.RiskLevelStatus == RiskValid {
  118. var mapping permissionService.CustomerProductRiskMappingDTO
  119. mapping, err = permissionService.GetRiskMappingByCustomerRiskLevel(userProfile.RiskLevel)
  120. if err != nil {
  121. logger.Error("查询产品风险等级映射失败:%v", err)
  122. return
  123. }
  124. permissionList = filterPermissionsByRisk(permissionList, mapping.ProductRiskLevel)
  125. riskLevel = mapping.ProductRiskLevel
  126. }
  127. for _, permission := range permissionList {
  128. filterPermissionIds = append(filterPermissionIds, permission.PermissionId)
  129. }
  130. return
  131. } else { //没有登录的时候展示所有设置了风险等级的品种报告,筛选的时候过滤传入ID中没有设置风险等级的品种
  132. var permissionList []permissionService.PermissionDTO
  133. if len(permissionIds) == 0 {
  134. //获取所有设置风险等级的品种
  135. permissionList, err = permissionService.GetPermissionListWithRisk()
  136. } else {
  137. //更具id过滤设置了风险等级的品种
  138. permissionList, err = permissionService.GetPermissionListByIds(permissionIds)
  139. }
  140. if err != nil {
  141. logger.Error("查询有风险等级的品种列表失败:%v", err)
  142. return
  143. }
  144. for _, permission := range permissionList {
  145. filterPermissionIds = append(filterPermissionIds, permission.PermissionId)
  146. }
  147. return
  148. }
  149. }
  150. func filterPermissionsByRisk(permissionList []permissionService.PermissionDTO, riskLevel string) (resultList []permissionService.PermissionDTO) {
  151. if riskLevel != "" {
  152. riskLevelNum, err := config.ParseRiskLevel(riskLevel)
  153. if err != nil {
  154. logger.Error("风险等级解析失败:%v", err)
  155. return
  156. }
  157. for _, permission := range permissionList {
  158. pRiskNum, riskErr := config.ParseRiskLevel(permission.RiskLevel)
  159. if riskErr != nil {
  160. logger.Error("解析品种风险等级失败 permission:%d,risk:%v", permission.PermissionId, permission.RiskLevel)
  161. continue
  162. }
  163. if pRiskNum <= riskLevelNum {
  164. resultList = append(resultList, permission)
  165. }
  166. }
  167. } else {
  168. resultList = permissionList
  169. }
  170. return
  171. }
  172. func convertUserDTOToProfile(dto userService.UserDTO) (profile UserProfile) {
  173. profile = UserProfile{
  174. Mobile: dto.Mobile,
  175. RiskLevel: dto.RiskLevel,
  176. UserName: dto.Username,
  177. }
  178. if profile.UserName == "" {
  179. profile.UserName = dto.Mobile
  180. }
  181. if dto.RiskLevel == "" {
  182. profile.RiskLevelStatus = RiskUnTest
  183. return
  184. }
  185. date, err := time.Parse(time.DateOnly, dto.RiskValidEndDate)
  186. if err != nil {
  187. logger.Error("解析日期失败:%v", err)
  188. profile.RiskLevelStatus = RiskExpired
  189. return
  190. }
  191. currentDate := time.Now().Truncate(24 * time.Hour)
  192. expiryDate := date.Truncate(24 * time.Hour)
  193. if expiryDate.Before(currentDate) {
  194. profile.RiskLevelStatus = RiskExpired
  195. return
  196. }
  197. profile.RiskLevelStatus = RiskValid
  198. return
  199. }
  200. func GetUserProfile(userId int) (userProfile UserProfile, err error) {
  201. userDTO, err := userService.GetUserById(userId)
  202. if err != nil {
  203. if errors.Is(err, gorm.ErrRecordNotFound) {
  204. logger.Error("用户不存在,用户Id:%d", userId)
  205. err = exception.New(exception.TemplateUserNotFound)
  206. } else {
  207. logger.Error("获取用户信息失败:%v", err)
  208. err = exception.New(exception.TemplateUserFoundFailed)
  209. }
  210. return
  211. }
  212. userProfile = convertUserDTOToProfile(userDTO)
  213. return
  214. }
  215. func GetAnalystDetail(userId int, analystId int) (analystDetail AnalystDetail, err error) {
  216. analyst, err := analystService.GetAnalystById(analystId)
  217. if err != nil {
  218. logger.Error("研究员信息不存在:%v", err)
  219. err = exception.New(exception.AnalystNotFound)
  220. }
  221. analystDetail = convertToAnalystDetail(analyst)
  222. //研究员关注状态
  223. analystDetail.Followed = userService.GetFollowed(userId, analystId)
  224. return
  225. }
  226. func convertToAnalystDetail(dto analystService.FinancialAnalystDTO) AnalystDetail {
  227. return AnalystDetail{
  228. AnalystName: dto.Name,
  229. HeadImgUrl: dto.HeadImgUrl,
  230. Introduction: dto.Introduction,
  231. Position: dto.Position,
  232. InvestmentCertificate: dto.InvestmentCertificate,
  233. ProfessionalCertificate: dto.ProfessionalCertificate,
  234. }
  235. }
  236. func FollowAnalystsByName(userId int, analystNames []string, followType string) (err error) {
  237. var followlist []userService.FollowDTO
  238. for _, analystName := range analystNames {
  239. FinancialAnalystDTO, followErr := analystService.GetAnalystByName(analystName)
  240. if followErr != nil {
  241. err = exception.New(exception.AnalystNotFound)
  242. }
  243. if FinancialAnalystDTO.Id == 0 || FinancialAnalystDTO.Name == "" {
  244. continue
  245. }
  246. followDTO := userService.FollowDTO{
  247. UserId: userId,
  248. AnalystId: FinancialAnalystDTO.Id,
  249. AnalystName: FinancialAnalystDTO.Name,
  250. FollowType: followType,
  251. }
  252. followlist = append(followlist, followDTO)
  253. }
  254. err = userService.FollowAnalystsByName(userId, followlist, followType)
  255. if err != nil {
  256. logger.Error("批量关注研究员失败:%v", err)
  257. err = exception.New(exception.BatchFollowingAnalystFailed)
  258. }
  259. return
  260. }
  261. func CheckFollowStatusByNames(userId int, names []string) (list []userService.FollowDTO, err error) {
  262. list, err = userService.CheckFollowStatusByNames(userId, names)
  263. if err != nil {
  264. logger.Error("获取关注状态失败:%v", err)
  265. err = exception.New(exception.CheckFollowStatusByNamesFailed)
  266. }
  267. return
  268. }
  269. func FollowAnalyst(userId int, analystId int, followType string) (err error) {
  270. FinancialAnalystDTO, err := analystService.GetAnalystById(analystId)
  271. if err != nil {
  272. err = exception.New(exception.AnalystNotFound)
  273. }
  274. if FinancialAnalystDTO.Id == 0 || FinancialAnalystDTO.Name == "" {
  275. err = exception.New(exception.AnalystNotFound)
  276. return
  277. }
  278. followDTO := userService.FollowDTO{
  279. UserId: userId,
  280. AnalystId: analystId,
  281. AnalystName: FinancialAnalystDTO.Name,
  282. FollowType: followType,
  283. }
  284. err = userService.FollowAnalyst(followDTO)
  285. if err != nil {
  286. logger.Error("关注研究员失败:%v", err)
  287. err = exception.New(exception.UserFollowAnalystFailed)
  288. }
  289. return
  290. }
  291. func GetFollowingAnalystList(userId int) (analysts []FollowAnalystDTO, err error) {
  292. logger.Info("用户ID:%d", userId)
  293. dtoList, err := userService.GetFollowingAnalystList(userId)
  294. if err != nil {
  295. logger.Error("获取关注列表失败:%v", err)
  296. err = exception.New(exception.GetFollowingAnalystListFailed)
  297. return
  298. }
  299. analysts, err = convertToAnalystList(dtoList)
  300. var wg sync.WaitGroup
  301. wg.Add(len(analysts))
  302. for i := 0; i < len(analysts); i++ {
  303. go func(followDTo *FollowAnalystDTO) {
  304. defer wg.Done()
  305. followDTo.NeedNotice = userService.NeedNotice(userId, followDTo.AnalystId)
  306. var analystsDTO analystService.FinancialAnalystDTO
  307. analystsDTO, err = analystService.GetAnalystById(followDTo.AnalystId)
  308. if err != nil {
  309. logger.Error("获取研究员信息失败")
  310. } else {
  311. followDTo.HeadImgUrl = analystsDTO.HeadImgUrl
  312. }
  313. }(&analysts[i])
  314. }
  315. wg.Wait()
  316. //排序
  317. sort.Slice(analysts, func(i, j int) bool {
  318. // 首先按 NeedNotice 排序
  319. if analysts[i].NeedNotice == analysts[j].NeedNotice {
  320. // 对于 NeedNotice 相同的情况下,进行倒序排列
  321. return analysts[i].FollowedTime.After(analysts[j].FollowedTime)
  322. }
  323. // NeedNotice 为 true 的排在 false 的前面
  324. return analysts[i].NeedNotice
  325. })
  326. //if err != nil {
  327. // logger.Error("转换研究员列表失败:%v", err)
  328. // err = exception.New(exception.TransferFollowingAnalystListFailed)
  329. //}
  330. return
  331. }
  332. func GetUnReadMessageList(userId int) (messages []userService.MyMessage, err error) {
  333. messages, err = userService.GetUnReadMessageList(userId)
  334. if err != nil {
  335. err = exception.New(exception.GetUserUnReadMsgFailed)
  336. }
  337. return
  338. }
  339. func ReadMessage(userId int, messageId int) bool {
  340. return userService.ReadMessage(userId, messageId)
  341. }
  342. func ReadMessages(userId int, analystId int) bool {
  343. return userService.ReadMessages(userId, analystId)
  344. }
  345. type FollowAnalystDTO struct {
  346. AnalystId int `json:"analystId"`
  347. AnalystName string `json:"analystName"`
  348. HeadImgUrl string `json:"headImgUrl"`
  349. FollowedTime time.Time `json:"followedTime"`
  350. NeedNotice bool `json:"needNotice"`
  351. }
  352. func convertToAnalystList(dtoList []userService.FollowDTO) (list []FollowAnalystDTO, err error) {
  353. for _, dto := range dtoList {
  354. analyst := FollowAnalystDTO{
  355. AnalystId: dto.AnalystId,
  356. AnalystName: dto.AnalystName,
  357. FollowedTime: dto.FollowedTime,
  358. NeedNotice: false,
  359. }
  360. list = append(list, analyst)
  361. }
  362. return
  363. }
  364. func FollowAnalystByName(userId int, analystName string, followType string) (err error) {
  365. FinancialAnalystDTO, err := analystService.GetAnalystByName(analystName)
  366. if err != nil {
  367. err = exception.New(exception.AnalystNotFound)
  368. }
  369. if FinancialAnalystDTO.Id == 0 || FinancialAnalystDTO.Name == "" {
  370. err = exception.New(exception.AnalystNotFound)
  371. return
  372. }
  373. followDTO := userService.FollowDTO{
  374. UserId: userId,
  375. AnalystId: FinancialAnalystDTO.Id,
  376. AnalystName: FinancialAnalystDTO.Name,
  377. FollowType: followType,
  378. }
  379. err = userService.FollowAnalyst(followDTO)
  380. if err != nil {
  381. logger.Error("关注研究员失败:%v", err)
  382. err = exception.New(exception.UserFollowAnalystFailed)
  383. }
  384. return
  385. }
  386. func FeedBack(userId int, mobile string, message string) (err error) {
  387. feedback := userService.FeedbackDTO{
  388. UserId: userId,
  389. Mobile: mobile,
  390. Message: message,
  391. }
  392. err = userService.FeedBack(feedback)
  393. if err != nil {
  394. err = exception.New(exception.FeedBackError)
  395. }
  396. return
  397. }
  398. func GetUserByMobile(mobile string) (user User, err error) {
  399. userDTO, err := userService.GetUserByMobile(mobile)
  400. if err != nil {
  401. if errors.Is(err, gorm.ErrRecordNotFound) {
  402. err = exception.New(exception.TemplateUserNotFound)
  403. } else {
  404. err = exception.New(exception.TemplateUserFoundFailed)
  405. }
  406. }
  407. user = convertToUser(userDTO)
  408. return
  409. }
  410. func GetUserByOpenId(openId string) (user User, err error) {
  411. userDTO, err := userService.GetUserByOpenId(openId)
  412. if err != nil {
  413. return
  414. }
  415. user = convertToUser(userDTO)
  416. return
  417. }
  418. func convertToUser(userDTO userService.UserDTO) User {
  419. return User{
  420. Id: userDTO.Id,
  421. Username: userDTO.Username,
  422. OpenId: userDTO.OpenId,
  423. AreaCode: userDTO.AreaCode,
  424. Mobile: userDTO.Mobile,
  425. }
  426. }
  427. func GetUserByTemplateUserId(templateUserId int) (officialUser userService.OfficialUserDTO, err error) {
  428. officialUser, err = userService.GetUserByTemplateUserId(templateUserId)
  429. if err != nil {
  430. if errors.Is(err, gorm.ErrRecordNotFound) {
  431. err = exception.New(exception.OfficialUserNotFound)
  432. logger.Info("用户未开户:%v", templateUserId)
  433. } else {
  434. err = exception.NewWithException(exception.OfficialUserFoundError, err.Error())
  435. logger.Error("获取正式用户信息失败:%v", err)
  436. }
  437. }
  438. return
  439. }
  440. func GetUserScribeStatus(productId int, templateUserId int) (subscribe string) {
  441. userSubscribe, err := userService.GetUserSubscribe([]int{productId}, templateUserId)
  442. if err != nil {
  443. logger.Error("获取用户订阅状态失败:%v", err)
  444. return UnSubscribe
  445. }
  446. if len(userSubscribe) == 0 {
  447. return UnSubscribe
  448. }
  449. if userSubscribe[0].ProductType != merchantDao.Package && userSubscribe[0].Status == merchantDao.SubscribeExpired {
  450. logger.Error("用户订阅状态异常:%v,单品状态为过期,productId:%d", productId)
  451. return UnSubscribe
  452. }
  453. switch userSubscribe[0].Status {
  454. case
  455. merchantDao.SubscribeClose:
  456. return UnSubscribe
  457. case merchantDao.SubscribeExpired:
  458. return SubscribeExpired
  459. case merchantDao.SubscribeValid:
  460. return Subscribing
  461. default:
  462. return UnSubscribe
  463. }
  464. }
  465. func BookMark(templateUserId int, sourceId int, sourceType string) error {
  466. return userService.BookMark(templateUserId, sourceId, sourceType)
  467. }
  468. func UnBookMark(templateUserId int, sourceId int, sourceType string) error {
  469. return userService.UnBookMark(templateUserId, sourceId, sourceType)
  470. }
  471. func CheckBookMarkStatus(templateUserId int, sourceId int, sourceType string) (isBookMarked bool, err error) {
  472. status, err := userService.CheckBookMarkStatus(templateUserId, sourceId, sourceType)
  473. if err != nil {
  474. logger.Error("获取收藏状态失败:%v", err)
  475. return
  476. }
  477. isBookMarked = status == string(userDao.Marked)
  478. return
  479. }
  480. func GetTotalBookMarkPageBySourceType(userId int, sourceType string) (total int64, sourceIds []int, err error) {
  481. return userService.GetTotalBookMarkPageBySourceType(userId, sourceType)
  482. }
  483. func GetBookMarkPageBySourceType(userId int, pageInfo page.PageInfo, sourceType string) (sourceIds []int, err error) {
  484. return userService.GetBookMarkPageBySourceType(userId, sourceType, pageInfo)
  485. }
  486. func GetBookMarkPageRangeBySourceType(userId int, pageInfo page.PageInfo, sourceType string, sourceIds []int) (filterSourceIds []int, err error) {
  487. return userService.GetBookMarkPageRangeBySourceType(userId, sourceType, pageInfo, sourceIds)
  488. }