sub_account.go 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. package db
  2. import (
  3. "context"
  4. "fmt"
  5. "youngee_b_api/model/gorm_model"
  6. )
  7. // CreateSubAccount 新建子账号
  8. func CreateSubAccount(ctx context.Context, account gorm_model.YounggeeSubAccount) error {
  9. db := GetWriteDB(ctx)
  10. err := db.Create(&account).Error
  11. if err != nil {
  12. return err
  13. }
  14. return nil
  15. }
  16. // UpdateSubAccount 更新子账号
  17. func UpdateSubAccount(ctx context.Context, account gorm_model.YounggeeSubAccount) error {
  18. db := GetWriteDB(ctx)
  19. whereCondition := gorm_model.YounggeeSubAccount{SubAccountId: account.SubAccountId}
  20. err := db.Model(&gorm_model.YounggeeSubAccount{}).Where(whereCondition).Updates(account).Error
  21. if err != nil {
  22. return err
  23. }
  24. return nil
  25. }
  26. // DeleteSubAccount 删除子账号
  27. func DeleteSubAccount(ctx context.Context, account gorm_model.YounggeeSubAccount) error {
  28. db := GetWriteDB(ctx)
  29. whereCondition := gorm_model.YounggeeSubAccount{SubAccountId: account.SubAccountId}
  30. err := db.Where(whereCondition).Delete(&gorm_model.YounggeeSubAccount{}).Error
  31. if err != nil {
  32. return err
  33. }
  34. return nil
  35. }
  36. // FindSubAccountByPhone 根据手机号码查询子账号
  37. func FindSubAccountByPhone(ctx context.Context, phone string) (*gorm_model.YounggeeSubAccount, error) {
  38. db := GetReadDB(ctx)
  39. var total int64
  40. var subAccount *gorm_model.YounggeeSubAccount
  41. whereCondition := gorm_model.YounggeeSubAccount{PhoneNumber: phone, SubAccountType: 1}
  42. err := db.Model(gorm_model.YounggeeSubAccount{}).Where(whereCondition).Find(&subAccount).Count(&total).Error
  43. if err != nil {
  44. return nil, err
  45. }
  46. fmt.Println(total)
  47. if total == 0 {
  48. return nil, err
  49. }
  50. return subAccount, nil
  51. }
  52. // FindSubAccountByEnterpriseId 根据商家ID查找包含的所有子账号信息
  53. func FindSubAccountByEnterpriseId(ctx context.Context, enterpriseId string, jobId int, accountStatus int) ([]*gorm_model.YounggeeSubAccount, int64, error) {
  54. db := GetReadDB(ctx)
  55. var total int64
  56. var subAccount []*gorm_model.YounggeeSubAccount
  57. whereCondition := gorm_model.YounggeeSubAccount{EnterpriseId: enterpriseId, SubAccountType: 1, JobId: jobId, AccountStatus: accountStatus}
  58. err := db.Model(gorm_model.YounggeeSubAccount{}).Where(whereCondition).Find(&subAccount).Count(&total).Error
  59. if err != nil {
  60. return nil, 0, err
  61. }
  62. if total == 0 {
  63. return nil, 0, err
  64. }
  65. return subAccount, total, nil
  66. }