sub_account.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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.UpdateJobRequest) error {
  50. var newSubAccount *gorm_model.YounggeeSubAccount
  51. newSubAccount = &gorm_model.YounggeeSubAccount{}
  52. err := db.UpdateSubAccount(ctx, newSubAccount)
  53. if err != nil {
  54. return err
  55. }
  56. return nil
  57. }
  58. // DeleteSubAccount 删除子账号
  59. func (*subaccount) DeleteSubAccount(ctx context.Context, request http_model.DeleteSubAccountRequest) error {
  60. var newSubAccount = gorm_model.YounggeeSubAccount{
  61. SubAccountId: request.SubAccountId,
  62. }
  63. err := db.DeleteSubAccount(ctx, newSubAccount)
  64. if err != nil {
  65. return err
  66. }
  67. return nil
  68. }
  69. // FindSubAccountBySupplierId 根据服务商ID查找包含的所有子账号信息
  70. func (*subaccount) FindSubAccountBySupplierId(ctx context.Context, request http_model.FindAllSubAccountRequest) (*http_model.FindAllSubAccountData, error) {
  71. var subAccountResp *http_model.FindAllSubAccountData
  72. subAccountResp = &http_model.FindAllSubAccountData{}
  73. // 1. 取出子账号基本信息
  74. newSubAccount, total, subaccountErr := db.FindSubAccountBySupplierId(ctx, request.SupplierId, request.JobId, request.AccountStatus)
  75. if subaccountErr != nil {
  76. return nil, subaccountErr
  77. }
  78. if newSubAccount != nil {
  79. for _, s := range newSubAccount {
  80. var subAccountInfo *http_model.FindAllSubAccountInfo
  81. subAccountInfo = &http_model.FindAllSubAccountInfo{}
  82. // fmt.Println("s:", s.SubAccountId)
  83. subAccountInfo.SubAccountId = s.SubAccountId
  84. subAccountInfo.SubAccountName = s.SubAccountName
  85. subAccountInfo.JobId = s.JobId
  86. subAccountInfo.UserId = s.UserId
  87. subAccountInfo.PhoneNumber = s.PhoneNumber
  88. subAccountInfo.SupplierId = s.SupplierId
  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. // 3. 服务商信息
  99. supplierInfo, supplierErr := db.GetSupplierById(ctx, s.SupplierId)
  100. if supplierErr != nil {
  101. return nil, supplierErr
  102. }
  103. if supplierInfo != nil {
  104. subAccountInfo.SupplierName = supplierInfo.SupplierName
  105. }
  106. subAccountResp.SubAccountInfo = append(subAccountResp.SubAccountInfo, subAccountInfo)
  107. }
  108. }
  109. subAccountResp.Total = total
  110. return subAccountResp, nil
  111. }