123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102 |
- package db
- import (
- "context"
- "fmt"
- "github.com/sirupsen/logrus"
- "youngee_b_api/model/gorm_model"
- )
- // CreateSubAccount 新建子账号
- func CreateSubAccount(ctx context.Context, account gorm_model.YounggeeSubAccount) error {
- db := GetWriteDB(ctx)
- err := db.Create(&account).Error
- if err != nil {
- return err
- }
- return nil
- }
- // UpdateSubAccount 更新子账号
- func UpdateSubAccount(ctx context.Context, account *gorm_model.YounggeeSubAccount) error {
- db := GetWriteDB(ctx)
- whereCondition := gorm_model.YounggeeSubAccount{SubAccountId: account.SubAccountId}
- err := db.Model(&gorm_model.YounggeeSubAccount{}).Where(whereCondition).Updates(account).Error
- if err != nil {
- return err
- }
- return nil
- }
- // DeleteSubAccount 删除子账号
- func DeleteSubAccount(ctx context.Context, account gorm_model.YounggeeSubAccount) error {
- db := GetWriteDB(ctx)
- whereCondition := gorm_model.YounggeeSubAccount{SubAccountId: account.SubAccountId}
- err := db.Where(whereCondition).Delete(&gorm_model.YounggeeSubAccount{}).Error
- if err != nil {
- return err
- }
- return nil
- }
- // FindSubAccountByPhone 根据手机号码查询子账号
- func FindSubAccountByPhone(ctx context.Context, phone string) (*gorm_model.YounggeeSubAccount, error) {
- db := GetReadDB(ctx)
- var total int64
- var subAccount *gorm_model.YounggeeSubAccount
- whereCondition := gorm_model.YounggeeSubAccount{PhoneNumber: phone, SubAccountType: 1}
- err := db.Model(gorm_model.YounggeeSubAccount{}).Where(whereCondition).Find(&subAccount).Count(&total).Error
- if err != nil {
- return nil, err
- }
- fmt.Println(total)
- if total == 0 {
- return nil, err
- }
- return subAccount, nil
- }
- // FindSubAccountByEnterpriseId 根据商家ID查找包含的所有子账号信息
- func FindSubAccountByEnterpriseId(ctx context.Context, enterpriseId string, jobId int, accountStatus int) ([]*gorm_model.YounggeeSubAccount, int64, error) {
- db := GetReadDB(ctx)
- var total int64
- var subAccount []*gorm_model.YounggeeSubAccount
- whereCondition := gorm_model.YounggeeSubAccount{EnterpriseId: enterpriseId, SubAccountType: 1, JobId: jobId, AccountStatus: accountStatus}
- err := db.Model(gorm_model.YounggeeSubAccount{}).Where(whereCondition).Find(&subAccount).Count(&total).Error
- if err != nil {
- return nil, 0, err
- }
- if total == 0 {
- return nil, 0, err
- }
- return subAccount, total, nil
- }
- // FindSubAccountById 根据Id查询子账号
- func FindSubAccountById(ctx context.Context, subAccountId int) (*gorm_model.YounggeeSubAccount, error) {
- db := GetReadDB(ctx)
- var total int64
- var subAccount *gorm_model.YounggeeSubAccount
- whereCondition := gorm_model.YounggeeSubAccount{SubAccountId: subAccountId, SubAccountType: 1}
- err := db.Model(gorm_model.YounggeeSubAccount{}).Where(whereCondition).Find(&subAccount).Count(&total).Error
- if err != nil {
- return nil, err
- }
- // fmt.Println(total)
- if total == 0 {
- return nil, err
- }
- return subAccount, nil
- }
- // CountSubAccountUserByPhone 按照手机号码统计子账号数量
- func CountSubAccountUserByPhone(ctx context.Context, phone string) (int64, error) {
- db := GetReadDB(ctx)
- var taskNum int64
- err := db.Model(gorm_model.YounggeeSubAccount{}).Where("phone_number = ? and sub_account_type = 1", phone).Count(&taskNum).Error
- if err != nil {
- logrus.WithContext(ctx).Errorf("[CountSubAccountUserByPhone] error read mysql, err:%+v", err)
- return 0, err
- }
- return taskNum, nil
- }
|