124 lines
3.0 KiB
Go
124 lines
3.0 KiB
Go
package services
|
|
|
|
import (
|
|
"time"
|
|
|
|
"xorm.io/xorm"
|
|
|
|
"github.com/mayswind/lab/pkg/datastore"
|
|
"github.com/mayswind/lab/pkg/errs"
|
|
"github.com/mayswind/lab/pkg/models"
|
|
"github.com/mayswind/lab/pkg/uuid"
|
|
)
|
|
|
|
type AccountService struct {
|
|
ServiceUsingDB
|
|
ServiceUsingUuid
|
|
}
|
|
|
|
var (
|
|
Accounts = &AccountService{
|
|
ServiceUsingDB: ServiceUsingDB{
|
|
container: datastore.Container,
|
|
},
|
|
ServiceUsingUuid: ServiceUsingUuid{
|
|
container: uuid.Container,
|
|
},
|
|
}
|
|
)
|
|
|
|
func (s *AccountService) GetAllAccountsByUid(uid int64) ([]*models.Account, error) {
|
|
if uid <= 0 {
|
|
return nil, errs.ErrUserIdInvalid
|
|
}
|
|
|
|
var accounts []*models.Account
|
|
err := s.UserDataDB(uid).Where("uid=? AND deleted=?", uid, false).OrderBy("parent_account_id asc, display_order asc").Find(&accounts)
|
|
|
|
return accounts, err
|
|
}
|
|
|
|
func (s *AccountService) GetMaxDisplayOrder(uid int64) (int, error) {
|
|
if uid <= 0 {
|
|
return 0, errs.ErrUserIdInvalid
|
|
}
|
|
|
|
account := &models.Account{}
|
|
has, err := s.UserDataDB(uid).Cols("uid", "deleted", "parent_account_id", "display_order").Where("uid=? AND deleted=? AND parent_account_id=?", uid, false, models.ACCOUNT_PARENT_ID_LEVEL_ONE).OrderBy("display_order desc").Limit(1).Get(account)
|
|
|
|
if err != nil {
|
|
return 0, err
|
|
}
|
|
|
|
if has {
|
|
return account.DisplayOrder, nil
|
|
} else {
|
|
return 0, nil
|
|
}
|
|
}
|
|
|
|
func (s *AccountService) GetMaxSubAccountDisplayOrder(uid int64, parentAccountId int64) (int, error) {
|
|
if uid <= 0 {
|
|
return 0, errs.ErrUserIdInvalid
|
|
}
|
|
|
|
if parentAccountId <= 0 {
|
|
return 0, errs.ErrAccountIdInvalid
|
|
}
|
|
|
|
account := &models.Account{}
|
|
has, err := s.UserDataDB(uid).Cols("uid", "deleted", "parent_account_id", "display_order").Where("uid=? AND deleted=? AND parent_account_id=?", uid, false, parentAccountId).OrderBy("display_order desc").Limit(1).Get(account)
|
|
|
|
if err != nil {
|
|
return 0, err
|
|
}
|
|
|
|
if has {
|
|
return account.DisplayOrder, nil
|
|
} else {
|
|
return 0, nil
|
|
}
|
|
}
|
|
|
|
func (s *AccountService) CreateAccounts(mainAccount *models.Account, childrenAccounts []*models.Account) error {
|
|
if mainAccount.Uid <= 0 {
|
|
return errs.ErrUserIdInvalid
|
|
}
|
|
|
|
allAccounts := make([]*models.Account, len(childrenAccounts)+1)
|
|
|
|
mainAccount.AccountId = s.GenerateUuid(uuid.UUID_TYPE_ACCOUNT)
|
|
allAccounts[0] = mainAccount
|
|
|
|
if mainAccount.Type == models.ACCOUNT_TYPE_MULTI_SUB_ACCOUNTS {
|
|
for i := 0; i < len(childrenAccounts); i++ {
|
|
childAccount := childrenAccounts[i]
|
|
childAccount.AccountId = s.GenerateUuid(uuid.UUID_TYPE_ACCOUNT)
|
|
childAccount.ParentAccountId = mainAccount.AccountId
|
|
childAccount.Uid = mainAccount.Uid
|
|
childAccount.Type = models.ACCOUNT_TYPE_SINGLE_ACCOUNT
|
|
|
|
allAccounts[i+1] = childrenAccounts[i]
|
|
}
|
|
}
|
|
|
|
for i := 0; i < len(allAccounts); i++ {
|
|
allAccounts[i].Deleted = false
|
|
allAccounts[i].CreatedUnixTime = time.Now().Unix()
|
|
allAccounts[i].UpdatedUnixTime = time.Now().Unix()
|
|
}
|
|
|
|
return s.UserDataDB(mainAccount.Uid).DoTransaction(func(sess *xorm.Session) error {
|
|
for i := 0; i < len(allAccounts); i++ {
|
|
account := allAccounts[i]
|
|
_, err := sess.Insert(account)
|
|
|
|
if err != nil {
|
|
return err
|
|
}
|
|
}
|
|
|
|
return nil
|
|
})
|
|
}
|