diff --git a/pkg/api/accounts.go b/pkg/api/accounts.go index 43fdf92e..06cce45c 100644 --- a/pkg/api/accounts.go +++ b/pkg/api/accounts.go @@ -135,6 +135,11 @@ func (a *AccountsApi) AccountCreateHandler(c *core.Context) (interface{}, *errs. return nil, errs.ErrParentAccountCannotSetCurrency } + if accountCreateReq.Balance != 0 { + log.WarnfWithRequestId(c, "[accounts.AccountCreateHandler] parent account cannot set balance") + return nil, errs.ErrParentAccountCannotSetBalance + } + for i := 0; i < len(accountCreateReq.SubAccounts); i++ { subAccount := accountCreateReq.SubAccounts[i] @@ -345,6 +350,7 @@ func (a *AccountsApi) createNewAccountModel(uid int64, accountCreateReq *models. Icon: accountCreateReq.Icon, Color: accountCreateReq.Color, Currency: accountCreateReq.Currency, + Balance: accountCreateReq.Balance, Comment: accountCreateReq.Comment, } } diff --git a/pkg/errs/account.go b/pkg/errs/account.go index 006bc561..084b66e4 100644 --- a/pkg/errs/account.go +++ b/pkg/errs/account.go @@ -9,9 +9,10 @@ var ( ErrAccountHaveNoSubAccount = NewNormalError(NORMAL_SUBCATEGORY_ACCOUNT, 3, http.StatusBadRequest, "account must have at least one sub account") ErrAccountCannotHaveSubAccounts = NewNormalError(NORMAL_SUBCATEGORY_ACCOUNT, 4, http.StatusBadRequest, "account cannot have sub accounts") ErrParentAccountCannotSetCurrency = NewNormalError(NORMAL_SUBCATEGORY_ACCOUNT, 5, http.StatusBadRequest, "parent account cannot set currency") - ErrSubAccountCategoryNotEqualsToParent = NewNormalError(NORMAL_SUBCATEGORY_ACCOUNT, 6, http.StatusBadRequest, "sub account category not equals to parent") - ErrSubAccountTypeInvalid = NewNormalError(NORMAL_SUBCATEGORY_ACCOUNT, 7, http.StatusBadRequest, "sub account type invalid") - ErrCannotAddOrDeleteSubAccountsWhenModify = NewNormalError(NORMAL_SUBCATEGORY_ACCOUNT, 8, http.StatusBadRequest, "cannot add or delete sub accounts when modify account") - ErrSourceAccountNotFound = NewNormalError(NORMAL_SUBCATEGORY_ACCOUNT, 9, http.StatusBadRequest, "source account not found") - ErrDestinationAccountNotFound = NewNormalError(NORMAL_SUBCATEGORY_ACCOUNT, 10, http.StatusBadRequest, "destination account not found") + ErrParentAccountCannotSetBalance = NewNormalError(NORMAL_SUBCATEGORY_ACCOUNT, 6, http.StatusBadRequest, "parent account cannot set balance") + ErrSubAccountCategoryNotEqualsToParent = NewNormalError(NORMAL_SUBCATEGORY_ACCOUNT, 7, http.StatusBadRequest, "sub account category not equals to parent") + ErrSubAccountTypeInvalid = NewNormalError(NORMAL_SUBCATEGORY_ACCOUNT, 8, http.StatusBadRequest, "sub account type invalid") + ErrCannotAddOrDeleteSubAccountsWhenModify = NewNormalError(NORMAL_SUBCATEGORY_ACCOUNT, 9, http.StatusBadRequest, "cannot add or delete sub accounts when modify account") + ErrSourceAccountNotFound = NewNormalError(NORMAL_SUBCATEGORY_ACCOUNT, 10, http.StatusBadRequest, "source account not found") + ErrDestinationAccountNotFound = NewNormalError(NORMAL_SUBCATEGORY_ACCOUNT, 11, http.StatusBadRequest, "destination account not found") ) diff --git a/pkg/models/account.go b/pkg/models/account.go index 05cced9c..6df58ddf 100644 --- a/pkg/models/account.go +++ b/pkg/models/account.go @@ -73,6 +73,7 @@ type AccountCreateRequest struct { Icon int64 `json:"icon,string" binding:"required,min=1"` Color string `json:"color" binding:"required,len=6,validHexRGBColor"` Currency string `json:"currency" binding:"required,len=3,validCurrency"` + Balance int64 `json:"balance"` Comment string `json:"comment" binding:"max=255"` SubAccounts []*AccountCreateRequest `json:"subAccounts" binding:"omitempty"` } diff --git a/pkg/services/accounts.go b/pkg/services/accounts.go index 284478c4..1e5528ec 100644 --- a/pkg/services/accounts.go +++ b/pkg/services/accounts.go @@ -1,6 +1,7 @@ package services import ( + "github.com/mayswind/lab/pkg/utils" "time" "xorm.io/xorm" @@ -100,7 +101,10 @@ func (s *AccountService) CreateAccounts(mainAccount *models.Account, childrenAcc return errs.ErrUserIdInvalid } + now := time.Now().Unix() + allAccounts := make([]*models.Account, len(childrenAccounts)+1) + var allInitTransactions []*models.Transaction mainAccount.AccountId = s.GenerateUuid(uuid.UUID_TYPE_ACCOUNT) allAccounts[0] = mainAccount @@ -117,10 +121,31 @@ func (s *AccountService) CreateAccounts(mainAccount *models.Account, childrenAcc } } + transactionTime := utils.GetTransactionTimeFromUnixTime(now) + for i := 0; i < len(allAccounts); i++ { allAccounts[i].Deleted = false - allAccounts[i].CreatedUnixTime = time.Now().Unix() - allAccounts[i].UpdatedUnixTime = time.Now().Unix() + allAccounts[i].CreatedUnixTime = now + allAccounts[i].UpdatedUnixTime = now + + if allAccounts[i].Balance != 0 { + newTransaction := &models.Transaction{ + TransactionId: s.GenerateUuid(uuid.UUID_TYPE_TRANSACTION), + Uid: allAccounts[i].Uid, + Deleted: false, + Type: models.TRANSACTION_TYPE_MODIFY_BALANCE, + TransactionTime: transactionTime, + SourceAccountId: allAccounts[i].AccountId, + DestinationAccountId: allAccounts[i].AccountId, + SourceAmount: allAccounts[i].Balance, + DestinationAmount: allAccounts[i].Balance, + CreatedUnixTime: now, + UpdatedUnixTime: now, + } + + transactionTime++ + allInitTransactions = append(allInitTransactions, newTransaction) + } } return s.UserDataDB(mainAccount.Uid).DoTransaction(func(sess *xorm.Session) error { @@ -133,6 +158,15 @@ func (s *AccountService) CreateAccounts(mainAccount *models.Account, childrenAcc } } + for i := 0; i < len(allInitTransactions); i++ { + transaction := allInitTransactions[i] + _, err := sess.Insert(transaction) + + if err != nil { + return err + } + } + return nil }) } @@ -172,7 +206,7 @@ func (s *AccountService) HideAccount(uid int64, ids []int64, hidden bool) error now := time.Now().Unix() updateModel := &models.Account{ - Hidden: hidden, + Hidden: hidden, UpdatedUnixTime: now, } @@ -222,7 +256,7 @@ func (s *AccountService) DeleteAccounts(uid int64, ids []int64) error { now := time.Now().Unix() updateModel := &models.Account{ - Deleted: true, + Deleted: true, DeletedUnixTime: now, } diff --git a/pkg/utils/datetimes.go b/pkg/utils/datetimes.go index 89b9f602..927f7ae3 100644 --- a/pkg/utils/datetimes.go +++ b/pkg/utils/datetimes.go @@ -11,3 +11,7 @@ func FormatToLongDateTime(t time.Time) string { func ParseFromLongDateTime(t string) (time.Time, error) { return time.Parse(LongDateTimeFormat, t) } + +func GetTransactionTimeFromUnixTime(unixTime int64) int64 { + return unixTime * 1000 +} diff --git a/src/locales/en.js b/src/locales/en.js index efb957cf..40f1f564 100644 --- a/src/locales/en.js +++ b/src/locales/en.js @@ -300,6 +300,7 @@ export default { 'account must have at least one sub account': 'Account must have at least one sub account', 'account cannot have sub accounts': 'Account cannot have sub accounts', 'parent account cannot set currency': 'Parent account cannot set currency', + 'parent account cannot set balance': 'Parent account cannot set balance', 'sub account category not equals to parent': 'Sub account category does not equal to parent', 'sub account type invalid': 'Sub account type is invalid', 'cannot add or delete sub accounts when modify account': 'You cannot add or delete sub accounts when modify account', diff --git a/src/locales/zh_Hans.js b/src/locales/zh_Hans.js index 6fa31df1..4b2310c5 100644 --- a/src/locales/zh_Hans.js +++ b/src/locales/zh_Hans.js @@ -300,6 +300,7 @@ export default { 'account must have at least one sub account': '账户必须包含至少一个子账户', 'account cannot have sub accounts': '账户不能包含子账户', 'parent account cannot set currency': '父账户不能设置货币', + 'parent account cannot set balance': '父账户不能设置余额', 'sub account category not equals to parent': '子账户类别与父账户不同', 'sub account type invalid': '子账户类型无效', 'cannot add or delete sub accounts when modify account': '您不能在修改账户时添加或删除子账户',