user_service.go 15 KB

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