sub_account.go 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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: 3}
  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. // FindSubAccountById 根据Id查询子账号
  53. func FindSubAccountById(ctx context.Context, subAccountId int) (*gorm_model.YounggeeSubAccount, error) {
  54. db := GetReadDB(ctx)
  55. var total int64
  56. var subAccount *gorm_model.YounggeeSubAccount
  57. whereCondition := gorm_model.YounggeeSubAccount{SubAccountId: subAccountId, SubAccountType: 1}
  58. err := db.Model(gorm_model.YounggeeSubAccount{}).Where(whereCondition).Find(&subAccount).Count(&total).Error
  59. if err != nil {
  60. return nil, err
  61. }
  62. fmt.Println(total)
  63. if total == 0 {
  64. return nil, err
  65. }
  66. return subAccount, nil
  67. }
  68. // FindSubAccountBySupplierId 根据服务商ID查找包含的所有子账号信息
  69. func FindSubAccountBySupplierId(ctx context.Context, supplierId int, jobId int, accountStatus int) ([]*gorm_model.YounggeeSubAccount, int64, error) {
  70. db := GetReadDB(ctx)
  71. var total int64
  72. var subAccount []*gorm_model.YounggeeSubAccount
  73. whereCondition := gorm_model.YounggeeSubAccount{SupplierId: supplierId, SubAccountType: 3, JobId: jobId, AccountStatus: accountStatus}
  74. err := db.Model(gorm_model.YounggeeSubAccount{}).Where(whereCondition).Find(&subAccount).Count(&total).Error
  75. if err != nil {
  76. return nil, 0, err
  77. }
  78. if total == 0 {
  79. return nil, 0, err
  80. }
  81. return subAccount, total, nil
  82. }