sub_account.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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) (*gorm_model.YounggeeSubAccount, error) {
  16. user := gorm_model.YounggeeUser{
  17. Phone: request.PhoneNumber,
  18. User: "1002",
  19. Username: request.PhoneNumber,
  20. Password: "1002",
  21. RealName: "",
  22. Role: "7",
  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 nil, 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. SupplierId: request.SupplierId,
  38. AccountStatus: 1,
  39. UserId: curr,
  40. SubAccountType: 3,
  41. }
  42. err1 := db.CreateSubAccount(ctx, newSubAccount)
  43. if err1 != nil {
  44. return nil, err1
  45. }
  46. return &newSubAccount, nil
  47. }
  48. // UpdateSubAccount 修改子账号
  49. func (*subaccount) UpdateSubAccount(ctx context.Context, request http_model.UpdateSubAccountRequest) (*http_model.UpdateSubAccountInfo, error) {
  50. //var newSubAccount *gorm_model.YounggeeSubAccount
  51. //newSubAccount = &gorm_model.YounggeeSubAccount{}
  52. //// 1. 若修改绑定手机号
  53. //if request.Phone != "" {
  54. //}
  55. return nil, 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. // FindSubAccountBySupplierId 根据服务商ID查找包含的所有子账号信息
  69. func (*subaccount) FindSubAccountBySupplierId(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.FindSubAccountBySupplierId(ctx, request.PageNum, request.PageSize, request.SupplierId, request.JobId, request.AccountStatus, request.Condition)
  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.SupplierId = s.SupplierId
  88. subAccountInfo.AccountStatus = s.AccountStatus
  89. // 2. 岗位信息
  90. jobInfo, jobErr := db.FindJobByJobId(ctx, s.JobId)
  91. if jobErr != nil {
  92. return nil, jobErr
  93. }
  94. if jobInfo != nil {
  95. subAccountInfo.JobName = jobInfo.JobName
  96. }
  97. // 3. 服务商信息
  98. supplierInfo, supplierErr := db.GetSupplierById(ctx, s.SupplierId)
  99. if supplierErr != nil {
  100. return nil, supplierErr
  101. }
  102. if supplierInfo != nil {
  103. subAccountInfo.SupplierName = supplierInfo.SupplierName
  104. }
  105. subAccountResp.SubAccountInfo = append(subAccountResp.SubAccountInfo, subAccountInfo)
  106. }
  107. }
  108. subAccountResp.Total = total
  109. return subAccountResp, nil
  110. }
  111. // ShutDownSubAccount 停用子账号
  112. func (*subaccount) ShutDownSubAccount(ctx context.Context, request http_model.ShutDownSubAccountRequest) error {
  113. var newSubAccount *gorm_model.YounggeeSubAccount
  114. newSubAccount = &gorm_model.YounggeeSubAccount{}
  115. newSubAccount.SubAccountId = request.SubAccountId
  116. newSubAccount.AccountStatus = 2
  117. err := db.UpdateSubAccount(ctx, newSubAccount)
  118. if err != nil {
  119. return err
  120. }
  121. return nil
  122. }