From 46ff0ecd3bf6d8a83ae25557f9607029acde5e40 Mon Sep 17 00:00:00 2001 From: MaysWind Date: Sat, 17 Jan 2026 23:32:13 +0800 Subject: [PATCH] place the account at the end of the account category after changing account category --- pkg/api/accounts.go | 31 +++++++++++++++++++++---------- pkg/services/accounts.go | 2 +- 2 files changed, 22 insertions(+), 11 deletions(-) diff --git a/pkg/api/accounts.go b/pkg/api/accounts.go index 98331517..c0bb32b4 100644 --- a/pkg/api/accounts.go +++ b/pkg/api/accounts.go @@ -437,6 +437,17 @@ func (a *AccountsApi) AccountModifyHandler(c *core.WebContext) (any, *errs.Error toUpdateAccount := a.getToUpdateAccount(uid, &accountModifyReq, mainAccount, false) if toUpdateAccount != nil { + if toUpdateAccount.Category != mainAccount.Category { + maxOrderId, err := a.accounts.GetMaxDisplayOrder(c, uid, toUpdateAccount.Category) + + if err != nil { + log.Errorf(c, "[accounts.AccountModifyHandler] failed to get max display order for user \"uid:%d\", because %s", uid, err.Error()) + return nil, errs.Or(err, errs.ErrOperationFailed) + } + + toUpdateAccount.DisplayOrder = maxOrderId + 1 + } + anythingUpdate = true toUpdateAccounts = append(toUpdateAccounts, toUpdateAccount) } @@ -542,7 +553,6 @@ func (a *AccountsApi) AccountModifyHandler(c *core.WebContext) (any, *errs.Error account.Type = oldAccount.Type account.ParentAccountId = oldAccount.ParentAccountId - account.DisplayOrder = oldAccount.DisplayOrder account.Currency = oldAccount.Currency account.Balance = oldAccount.Balance @@ -762,15 +772,16 @@ func (a *AccountsApi) getToUpdateAccount(uid int64, accountModifyReq *models.Acc } newAccount := &models.Account{ - AccountId: oldAccount.AccountId, - Uid: uid, - Name: accountModifyReq.Name, - Category: accountModifyReq.Category, - Icon: accountModifyReq.Icon, - Color: accountModifyReq.Color, - Comment: accountModifyReq.Comment, - Extend: newAccountExtend, - Hidden: accountModifyReq.Hidden, + AccountId: oldAccount.AccountId, + Uid: uid, + Name: accountModifyReq.Name, + DisplayOrder: oldAccount.DisplayOrder, + Category: accountModifyReq.Category, + Icon: accountModifyReq.Icon, + Color: accountModifyReq.Color, + Comment: accountModifyReq.Comment, + Extend: newAccountExtend, + Hidden: accountModifyReq.Hidden, } if newAccount.Name != oldAccount.Name || diff --git a/pkg/services/accounts.go b/pkg/services/accounts.go index 4d03965e..25441a27 100644 --- a/pkg/services/accounts.go +++ b/pkg/services/accounts.go @@ -445,7 +445,7 @@ func (s *AccountService) ModifyAccounts(c core.Context, mainAccount *models.Acco // update accounts for i := 0; i < len(updateAccounts); i++ { account := updateAccounts[i] - updatedRows, err := sess.ID(account.AccountId).Cols("name", "category", "icon", "color", "comment", "extend", "hidden", "updated_unix_time").Where("uid=? AND deleted=?", account.Uid, false).Update(account) + updatedRows, err := sess.ID(account.AccountId).Cols("name", "display_order", "category", "icon", "color", "comment", "extend", "hidden", "updated_unix_time").Where("uid=? AND deleted=?", account.Uid, false).Update(account) if err != nil { return err