sub_account.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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: 1}
  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. // FindSubAccountByEnterpriseId 根据商家ID查找包含的所有子账号信息
  54. func FindSubAccountByEnterpriseId(ctx context.Context, enterpriseId string, jobId int, accountStatus int) ([]*gorm_model.YounggeeSubAccount, int64, error) {
  55. db := GetReadDB(ctx)
  56. var total int64
  57. var subAccount []*gorm_model.YounggeeSubAccount
  58. whereCondition := gorm_model.YounggeeSubAccount{EnterpriseId: enterpriseId, SubAccountType: 1, JobId: jobId, AccountStatus: accountStatus}
  59. err := db.Model(gorm_model.YounggeeSubAccount{}).Where(whereCondition).Find(&subAccount).Count(&total).Error
  60. if err != nil {
  61. return nil, 0, err
  62. }
  63. if total == 0 {
  64. return nil, 0, err
  65. }
  66. return subAccount, total, nil
  67. }
  68. // FindSubAccountById 根据Id查询子账号
  69. func FindSubAccountById(ctx context.Context, subAccountId int) (*gorm_model.YounggeeSubAccount, error) {
  70. db := GetReadDB(ctx)
  71. var total int64
  72. var subAccount *gorm_model.YounggeeSubAccount
  73. whereCondition := gorm_model.YounggeeSubAccount{SubAccountId: subAccountId, SubAccountType: 1}
  74. err := db.Model(gorm_model.YounggeeSubAccount{}).Where(whereCondition).Find(&subAccount).Count(&total).Error
  75. if err != nil {
  76. return nil, err
  77. }
  78. // fmt.Println(total)
  79. if total == 0 {
  80. return nil, err
  81. }
  82. return subAccount, nil
  83. }
  84. // CountSubAccountUserByPhone 按照手机号码统计子账号数量
  85. func CountSubAccountUserByPhone(ctx context.Context, phone string) (int64, error) {
  86. db := GetReadDB(ctx)
  87. var taskNum int64
  88. err := db.Model(gorm_model.YounggeeSubAccount{}).Where("phone_number = ? and sub_account_type = 1", phone).Count(&taskNum).Error
  89. if err != nil {
  90. logrus.WithContext(ctx).Errorf("[CountSubAccountUserByPhone] error read mysql, err:%+v", err)
  91. return 0, err
  92. }
  93. return taskNum, nil
  94. }