sub_account.go 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. package db
  2. import (
  3. "context"
  4. "fmt"
  5. "github.com/sirupsen/logrus"
  6. "youngee_b_api/model/gorm_model"
  7. )
  8. // CreateSubAccount 新建子账号
  9. func CreateSubAccount(ctx context.Context, account gorm_model.YounggeeSubAccount) error {
  10. db := GetWriteDB(ctx)
  11. err := db.Create(&account).Error
  12. if err != nil {
  13. return err
  14. }
  15. return nil
  16. }
  17. // UpdateSubAccount 更新子账号
  18. func UpdateSubAccount(ctx context.Context, account *gorm_model.YounggeeSubAccount) error {
  19. db := GetWriteDB(ctx)
  20. whereCondition := gorm_model.YounggeeSubAccount{SubAccountId: account.SubAccountId}
  21. err := db.Model(&gorm_model.YounggeeSubAccount{}).Where(whereCondition).Updates(account).Error
  22. if err != nil {
  23. return err
  24. }
  25. return nil
  26. }
  27. // DeleteSubAccount 删除子账号
  28. func DeleteSubAccount(ctx context.Context, account gorm_model.YounggeeSubAccount) error {
  29. db := GetWriteDB(ctx)
  30. whereCondition := gorm_model.YounggeeSubAccount{SubAccountId: account.SubAccountId}
  31. err := db.Where(whereCondition).Delete(&gorm_model.YounggeeSubAccount{}).Error
  32. if err != nil {
  33. return err
  34. }
  35. return nil
  36. }
  37. // FindSubAccountByPhone 根据手机号码查询子账号
  38. func FindSubAccountByPhone(ctx context.Context, phone string) (*gorm_model.YounggeeSubAccount, error) {
  39. db := GetReadDB(ctx)
  40. var total int64
  41. var subAccount *gorm_model.YounggeeSubAccount
  42. whereCondition := gorm_model.YounggeeSubAccount{PhoneNumber: phone, SubAccountType: 3}
  43. err := db.Model(gorm_model.YounggeeSubAccount{}).Where(whereCondition).Find(&subAccount).Count(&total).Error
  44. if err != nil {
  45. return nil, err
  46. }
  47. fmt.Println(total)
  48. if total == 0 {
  49. return nil, err
  50. }
  51. return subAccount, nil
  52. }
  53. // FindSubAccountById 根据Id查询子账号
  54. func FindSubAccountById(ctx context.Context, subAccountId int) (*gorm_model.YounggeeSubAccount, error) {
  55. db := GetReadDB(ctx)
  56. var total int64
  57. var subAccount *gorm_model.YounggeeSubAccount
  58. whereCondition := gorm_model.YounggeeSubAccount{SubAccountId: subAccountId, SubAccountType: 3}
  59. err := db.Model(gorm_model.YounggeeSubAccount{}).Where(whereCondition).Find(&subAccount).Count(&total).Error
  60. if err != nil {
  61. return nil, err
  62. }
  63. fmt.Println(total)
  64. if total == 0 {
  65. return nil, err
  66. }
  67. return subAccount, nil
  68. }
  69. /*
  70. // FindSubAccountBySupplierId 根据服务商ID查找包含的所有子账号信息
  71. func FindSubAccountBySupplierId(ctx context.Context, pageNum int32, pageSize int32, supplierId int, jobId int, accountStatus int, condition string) ([]*gorm_model.YounggeeSubAccount, int64, error) {
  72. db := GetReadDB(ctx)
  73. var total int64
  74. var subAccount []*gorm_model.YounggeeSubAccount
  75. whereCondition := gorm_model.YounggeeSubAccount{SupplierId: supplierId, SubAccountType: 3, JobId: jobId, AccountStatus: accountStatus}
  76. err := db.Model(gorm_model.YounggeeSubAccount{}).Where(whereCondition).Find(&subAccount).Count(&total).Error
  77. if err != nil {
  78. return nil, 0, err
  79. }
  80. if total == 0 {
  81. return nil, 0, err
  82. }
  83. return subAccount, total, nil
  84. }
  85. */
  86. // FindSubAccountBySupplierId 根据服务商ID查找包含的所有子账号信息
  87. func FindSubAccountBySupplierId(ctx context.Context, pageNum int32, pageSize int32, supplierId int, jobId int, accountStatus int, condition string) ([]*gorm_model.YounggeeSubAccount, int64, error) {
  88. db := GetReadDB(ctx)
  89. query := db.Model(&gorm_model.YounggeeSubAccount{}).
  90. Where("supplier_id = ? and sub_account_type = ? and account_status = 1", supplierId, 3)
  91. if jobId != 0 {
  92. query = query.Where("job_id = ?", jobId)
  93. }
  94. if accountStatus != 0 {
  95. query = query.Where("account_status = ?", accountStatus)
  96. }
  97. if condition != "" {
  98. query = query.Where("sub_account_name LIKE ?", "%"+condition+"%")
  99. }
  100. // Get total count
  101. var total int64
  102. if err := query.Count(&total).Error; err != nil {
  103. return nil, 0, err
  104. }
  105. if total == 0 {
  106. return nil, 0, nil
  107. }
  108. var subAccounts []*gorm_model.YounggeeSubAccount
  109. offset := (pageNum - 1) * pageSize
  110. if err := query.Offset(int(offset)).Limit(int(pageSize)).Find(&subAccounts).Error; err != nil {
  111. return nil, 0, err
  112. }
  113. return subAccounts, total, nil
  114. }
  115. // CountSubAccountUserByPhone 按照手机号码统计子账号数量
  116. func CountSubAccountUserByPhone(ctx context.Context, phone string) (int64, error) {
  117. db := GetReadDB(ctx)
  118. var taskNum int64
  119. err := db.Model(gorm_model.YounggeeSubAccount{}).Where("phone_number = ? and sub_account_type = 3", phone).Count(&taskNum).Error
  120. if err != nil {
  121. logrus.WithContext(ctx).Errorf("[CountSubAccountUserByPhone] error read mysql, err:%+v", err)
  122. return 0, err
  123. }
  124. return taskNum, nil
  125. }