mirror of
https://github.com/mayswind/ezbookkeeping.git
synced 2026-05-14 06:57:35 +08:00
transaction list api supports filtering by multiple account / category
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
package services
|
||||
|
||||
import (
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"xorm.io/xorm"
|
||||
@@ -86,6 +87,48 @@ func (s *AccountService) GetSubAccountsByAccountId(c *core.Context, uid int64, a
|
||||
return accounts, err
|
||||
}
|
||||
|
||||
// GetSubAccountsByAccountIds returns sub-account models according to account ids
|
||||
func (s *AccountService) GetSubAccountsByAccountIds(c *core.Context, uid int64, accountIds []int64) ([]*models.Account, error) {
|
||||
if uid <= 0 {
|
||||
return nil, errs.ErrUserIdInvalid
|
||||
}
|
||||
|
||||
if len(accountIds) <= 0 {
|
||||
return nil, errs.ErrAccountIdInvalid
|
||||
}
|
||||
|
||||
condition := "uid=? AND deleted=?"
|
||||
conditionParams := make([]any, 0, len(accountIds)+2)
|
||||
conditionParams = append(conditionParams, uid)
|
||||
conditionParams = append(conditionParams, false)
|
||||
|
||||
var accountIdConditions strings.Builder
|
||||
|
||||
for i := 0; i < len(accountIds); i++ {
|
||||
if accountIds[i] <= 0 {
|
||||
return nil, errs.ErrAccountIdInvalid
|
||||
}
|
||||
|
||||
if accountIdConditions.Len() > 0 {
|
||||
accountIdConditions.WriteString(",")
|
||||
}
|
||||
|
||||
accountIdConditions.WriteString("?")
|
||||
conditionParams = append(conditionParams, accountIds[i])
|
||||
}
|
||||
|
||||
if accountIdConditions.Len() > 1 {
|
||||
condition = condition + " AND parent_account_id IN (" + accountIdConditions.String() + ")"
|
||||
} else {
|
||||
condition = condition + " AND parent_account_id = " + accountIdConditions.String()
|
||||
}
|
||||
|
||||
var accounts []*models.Account
|
||||
err := s.UserDataDB(uid).NewSession(c).Where(condition, conditionParams...).OrderBy("display_order asc").Find(&accounts)
|
||||
|
||||
return accounts, err
|
||||
}
|
||||
|
||||
// GetAccountsByAccountIds returns account models according to account ids
|
||||
func (s *AccountService) GetAccountsByAccountIds(c *core.Context, uid int64, accountIds []int64) (map[int64]*models.Account, error) {
|
||||
if uid <= 0 {
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package services
|
||||
|
||||
import (
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"xorm.io/xorm"
|
||||
@@ -68,6 +69,48 @@ func (s *TransactionCategoryService) GetAllCategoriesByUid(c *core.Context, uid
|
||||
return categories, err
|
||||
}
|
||||
|
||||
// GetSubCategoriesByCategoryIds returns sub-category models according to category ids
|
||||
func (s *TransactionCategoryService) GetSubCategoriesByCategoryIds(c *core.Context, uid int64, categoryIds []int64) ([]*models.TransactionCategory, error) {
|
||||
if uid <= 0 {
|
||||
return nil, errs.ErrUserIdInvalid
|
||||
}
|
||||
|
||||
if len(categoryIds) <= 0 {
|
||||
return nil, errs.ErrTransactionCategoryIdInvalid
|
||||
}
|
||||
|
||||
condition := "uid=? AND deleted=?"
|
||||
conditionParams := make([]any, 0, len(categoryIds)+2)
|
||||
conditionParams = append(conditionParams, uid)
|
||||
conditionParams = append(conditionParams, false)
|
||||
|
||||
var categoryIdConditions strings.Builder
|
||||
|
||||
for i := 0; i < len(categoryIds); i++ {
|
||||
if categoryIds[i] <= 0 {
|
||||
return nil, errs.ErrTransactionCategoryIdInvalid
|
||||
}
|
||||
|
||||
if categoryIdConditions.Len() > 0 {
|
||||
categoryIdConditions.WriteString(",")
|
||||
}
|
||||
|
||||
categoryIdConditions.WriteString("?")
|
||||
conditionParams = append(conditionParams, categoryIds[i])
|
||||
}
|
||||
|
||||
if categoryIdConditions.Len() > 1 {
|
||||
condition = condition + " AND parent_category_id IN (" + categoryIdConditions.String() + ")"
|
||||
} else {
|
||||
condition = condition + " AND parent_category_id = " + categoryIdConditions.String()
|
||||
}
|
||||
|
||||
var categories []*models.TransactionCategory
|
||||
err := s.UserDataDB(uid).NewSession(c).Where(condition, conditionParams...).OrderBy("display_order asc").Find(&categories)
|
||||
|
||||
return categories, err
|
||||
}
|
||||
|
||||
// GetCategoryByCategoryId returns a transaction category model according to transaction category id
|
||||
func (s *TransactionCategoryService) GetCategoryByCategoryId(c *core.Context, uid int64, categoryId int64) (*models.TransactionCategory, error) {
|
||||
if uid <= 0 {
|
||||
|
||||
Reference in New Issue
Block a user