enterprise.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. package service
  2. import (
  3. "context"
  4. "math/rand"
  5. "time"
  6. "youngee_b_api/db"
  7. "youngee_b_api/model/gorm_model"
  8. "youngee_b_api/model/http_model"
  9. "github.com/issue9/conv"
  10. log "github.com/sirupsen/logrus"
  11. )
  12. var Enterprise *enterprise
  13. type enterprise struct {
  14. }
  15. func (*enterprise) CreateEnterpriseUser(ctx context.Context, newEnterprise http_model.RegisterRequest) (*http_model.RegisterData, error) {
  16. user := gorm_model.YounggeeUser{
  17. Phone: newEnterprise.Phone,
  18. User: "1001",
  19. Username: newEnterprise.BusinessName,
  20. Password: "1001",
  21. RealName: newEnterprise.RealName,
  22. Role: "3",
  23. Email: newEnterprise.Email,
  24. LastLoginTime: time.Now().UTC().Local(),
  25. }
  26. userId, err := db.CreateUser(ctx, user)
  27. if err != nil {
  28. log.Infof("[CreateEnterpriseUser] fail,err:%+v", err)
  29. return nil, err
  30. } else {
  31. rand.Seed(time.Now().UnixNano())
  32. th := conv.MustString(time.Now().Hour())
  33. tm := conv.MustString(time.Now().Minute())
  34. if len(tm) < 2 {
  35. tm = "0" + tm
  36. }
  37. if len(tm) < 2 {
  38. th = "0" + th
  39. }
  40. ShowEnterpriseID := "1" + th + tm + conv.MustString(rand.Intn(10000-1000)+1000)
  41. enterprise := &gorm_model.Enterprise{
  42. EnterpriseID: ShowEnterpriseID,
  43. Industry: newEnterprise.Industry,
  44. BusinessName: newEnterprise.BusinessName,
  45. UserID: *userId,
  46. Balance: 0,
  47. FrozenBalance: 0,
  48. AvailableBalance: 0,
  49. }
  50. enterpriseId, err := db.CreateEnterprise(ctx, *enterprise)
  51. if err != nil {
  52. log.Infof("[CreateEnterpriseUser] fail,err:%+v", err)
  53. return nil, err
  54. }
  55. res := &http_model.RegisterData{
  56. EnterpriseId: *enterpriseId,
  57. UserID: *userId,
  58. }
  59. return res, nil
  60. }
  61. }
  62. func (*enterprise) CreateEnterprise(ctx context.Context, phone string) (*http_model.RegisterData, error) {
  63. user := gorm_model.YounggeeUser{
  64. Phone: phone,
  65. User: "1001",
  66. Username: phone,
  67. Password: "1001",
  68. RealName: "",
  69. Role: "3",
  70. Email: "",
  71. LastLoginTime: time.Now().UTC().Local(),
  72. }
  73. userId, err := db.CreateUser(ctx, user)
  74. if err != nil {
  75. log.Infof("[CreateEnterpriseUser] fail,err:%+v", err)
  76. return nil, err
  77. } else {
  78. rand.Seed(time.Now().UnixNano())
  79. th := conv.MustString(time.Now().Hour())
  80. tm := conv.MustString(time.Now().Minute())
  81. if len(tm) < 2 {
  82. tm = "0" + tm
  83. }
  84. if len(tm) < 2 {
  85. th = "0" + th
  86. }
  87. ShowEnterpriseID := "1" + th + tm + conv.MustString(rand.Intn(10000-1000)+1000)
  88. enterprise := &gorm_model.Enterprise{
  89. EnterpriseID: ShowEnterpriseID,
  90. Industry: 1,
  91. BusinessName: phone,
  92. UserID: *userId,
  93. Balance: 0,
  94. FrozenBalance: 0,
  95. AvailableBalance: 0,
  96. }
  97. enterpriseId, err := db.CreateEnterprise(ctx, *enterprise)
  98. if err != nil {
  99. log.Infof("[CreateEnterpriseUser] fail,err:%+v", err)
  100. return nil, err
  101. }
  102. res := &http_model.RegisterData{
  103. EnterpriseId: *enterpriseId,
  104. UserID: *userId,
  105. }
  106. return res, nil
  107. }
  108. }