sub_account.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. package service
  2. import (
  3. "context"
  4. "fmt"
  5. log "github.com/sirupsen/logrus"
  6. "time"
  7. "youngee_b_api/db"
  8. "youngee_b_api/model/gorm_model"
  9. "youngee_b_api/model/http_model"
  10. )
  11. var SubAccount *subaccount
  12. type subaccount struct {
  13. }
  14. // CreateSubAccount 新增子账号
  15. func (*subaccount) CreateSubAccount(ctx context.Context, request *http_model.AddNewSubAccountRequest) error {
  16. user := gorm_model.YounggeeUser{
  17. Phone: request.PhoneNumber,
  18. User: "1002",
  19. Username: request.PhoneNumber,
  20. Password: "1002",
  21. RealName: "",
  22. Role: "4",
  23. Email: "",
  24. LastLoginTime: time.Now().UTC().Local(),
  25. }
  26. userId, err := db.CreateUser(ctx, user)
  27. if err != nil {
  28. log.Infof("[CreateEnterpriseSubUser] fail,err:%+v", err)
  29. return err
  30. }
  31. fmt.Println("userId: ", userId)
  32. var curr = int(*userId)
  33. var newSubAccount = gorm_model.YounggeeSubAccount{
  34. PhoneNumber: request.PhoneNumber,
  35. SubAccountName: request.SubAccountName,
  36. JobId: request.JobId,
  37. EnterpriseId: request.EnterpriseId,
  38. AccountStatus: 1,
  39. UserId: curr,
  40. SubAccountType: 1,
  41. }
  42. err1 := db.CreateSubAccount(ctx, newSubAccount)
  43. if err1 != nil {
  44. return err1
  45. }
  46. return nil
  47. }
  48. // UpdateSubAccount 修改子账号信息
  49. func (*subaccount) UpdateSubAccount(ctx context.Context, request *http_model.UpdateJobRequest) error {
  50. //var newSubAccount = gorm_model.YounggeeSubAccount{}
  51. //err := db.UpdateSubAccount(ctx, newSubAccount)
  52. //if err != nil {
  53. // return err
  54. //}
  55. return nil
  56. }
  57. // DeleteSubAccount 删除子账号
  58. func (*subaccount) DeleteSubAccount(ctx context.Context, request http_model.DeleteSubAccountRequest) error {
  59. var newSubAccount = gorm_model.YounggeeSubAccount{
  60. SubAccountId: request.SubAccountId,
  61. }
  62. err := db.DeleteSubAccount(ctx, newSubAccount)
  63. if err != nil {
  64. return err
  65. }
  66. return nil
  67. }
  68. // FindSubAccountByEnterpriseId 根据商家ID查找包含的所有子账号信息
  69. func (*subaccount) FindSubAccountByEnterpriseId(ctx context.Context, request http_model.FindAllSubAccountRequest) (*http_model.FindAllSubAccountData, error) {
  70. var subAccountResp *http_model.FindAllSubAccountData
  71. subAccountResp = &http_model.FindAllSubAccountData{}
  72. // 1. 取出子账号基本信息
  73. newSubAccount, total, subaccountErr := db.FindSubAccountByEnterpriseId(ctx, request.EnterpriseId, request.JobId, request.AccountStatus)
  74. if subaccountErr != nil {
  75. return nil, subaccountErr
  76. }
  77. if newSubAccount != nil {
  78. for _, s := range newSubAccount {
  79. var subAccountInfo *http_model.FindAllSubAccountInfo
  80. subAccountInfo = &http_model.FindAllSubAccountInfo{}
  81. // fmt.Println("s:", s.SubAccountId)
  82. subAccountInfo.SubAccountId = s.SubAccountId
  83. subAccountInfo.SubAccountName = s.SubAccountName
  84. subAccountInfo.JobId = s.JobId
  85. subAccountInfo.UserId = s.UserId
  86. subAccountInfo.PhoneNumber = s.PhoneNumber
  87. subAccountInfo.EnterpriseId = s.EnterpriseId
  88. subAccountInfo.EnterpriseName = s.EnterpriseId
  89. subAccountInfo.AccountStatus = s.AccountStatus
  90. // 2. 岗位信息
  91. jobInfo, jobErr := db.FindJobByJobId(ctx, s.JobId)
  92. if jobErr != nil {
  93. return nil, jobErr
  94. }
  95. if jobInfo != nil {
  96. subAccountInfo.JobName = jobInfo.JobName
  97. }
  98. subAccountResp.SubAccountInfo = append(subAccountResp.SubAccountInfo, subAccountInfo)
  99. }
  100. }
  101. subAccountResp.Total = total
  102. return subAccountResp, nil
  103. }