user_service.go 16 KB

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