get_user_logic.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. package user
  2. import (
  3. "context"
  4. "fmt"
  5. "git.i2edu.net/i2/i2-bill-api/internal/svc"
  6. "git.i2edu.net/i2/i2-bill-api/internal/types"
  7. "git.i2edu.net/i2/i2-bill-api/internal/utils"
  8. "git.i2edu.net/i2/i2-bill-api/model"
  9. "git.i2edu.net/i2/i2-bill-erp/transform"
  10. "strings"
  11. "git.i2edu.net/i2/go-zero/core/logx"
  12. )
  13. type GetUserLogic struct {
  14. logx.Logger
  15. ctx context.Context
  16. svcCtx *svc.ServiceContext
  17. UserId int64
  18. SessionKey string
  19. }
  20. func NewGetUserLogic(ctx context.Context, svcCtx *svc.ServiceContext) GetUserLogic {
  21. return GetUserLogic{
  22. Logger: logx.WithContext(ctx),
  23. ctx: ctx,
  24. svcCtx: svcCtx,
  25. }
  26. }
  27. func (l *GetUserLogic) GetUser() (*types.InfoResponse, error) {
  28. var user model.User
  29. info := &types.InfoResponse{}
  30. err := l.svcCtx.SqlConn.QueryRowPartial(&user, "select id, erp_id, avatar, birthday, username, nickname, gender, mobile from i2bill_user where `id` = ? limit 1", l.UserId)
  31. if err != nil {
  32. logx.Error(err)
  33. return nil, err
  34. }
  35. // 每次都去查找, 暂时没有绑定 erp mk
  36. // resp, err := l.svcCtx.Transformer.GetUser(l.ctx, &transform.UserRequest{Id: user.ErpId, Phone: user.Mobile})
  37. resp, err := l.svcCtx.Transformer.GetUser(l.ctx, &transform.UserRequest{Phone: user.Mobile})
  38. if err != nil {
  39. logx.Error(err)
  40. return nil, err
  41. }
  42. if resp.ErpRoleType != 1 {
  43. //是不是 兼职
  44. res, err := l.svcCtx.DB.SQL("select * from i2bill_mkt_part_time_user where del_flag = 0 and user_id = ? and check_state = ?", l.UserId, 57).Query().List()
  45. if err != nil {
  46. logx.Error(err)
  47. return nil, err
  48. }
  49. if len(res) > 0 {
  50. //兼职信息
  51. resp.UserName, _ = res[0]["name"].(string)
  52. cityId, _ := res[0]["city_id"].(int64)
  53. cityNodes, err := l.svcCtx.GetErpCity(l.ctx)
  54. if err != nil {
  55. logx.Error(err)
  56. return nil, err
  57. }
  58. var idPath, namePath string
  59. if utils.TreePath(cityNodes, cityId, &idPath, &namePath) {
  60. cityNames := strings.Split(namePath, ",")
  61. if len(cityNames) > 0 {
  62. resp.CityName = cityNames[len(cityNames)-1]
  63. }
  64. }
  65. for _, node := range cityNodes {
  66. if node.Id == cityId {
  67. resp.CityName = node.Text
  68. break
  69. }
  70. }
  71. resp.Gender = res[0]["sex"].(int64)
  72. resp.ErpRoleType = 2
  73. } else {
  74. resp.ErpRoleType = 0
  75. }
  76. } else {
  77. user.ErpId = resp.Id
  78. l.svcCtx.DB.Table("i2bill_user").Where("id = ?", user.Id).Cols("erp_id").Update(map[string]interface{}{"erp_id": user.ErpId})
  79. }
  80. if !strings.HasPrefix(user.Avatar, "http") && user.Avatar != "" {
  81. domain := strings.TrimRight(l.svcCtx.Config.AliYunOss.FileUrl, "/")
  82. filePath := strings.TrimLeft(user.Avatar, "/")
  83. user.Avatar = fmt.Sprintf("%s/%s", domain, filePath)
  84. }
  85. info.ID = user.Id
  86. info.Avatar = user.Avatar
  87. info.Birthday = user.Birthday
  88. info.NickName = user.Nickname
  89. info.Mobile = user.Mobile
  90. info.UserName = resp.UserName
  91. info.Gender = resp.Gender
  92. info.CityName = resp.CityName
  93. info.ErpRoleType = resp.ErpRoleType
  94. return info, nil
  95. }