account multi commodity

This commit is contained in:
BaoXuebin 2024-05-28 18:17:37 +08:00
parent a339e1afe8
commit 0024debf08
4 changed files with 51 additions and 34 deletions

4
go.mod
View File

@ -5,10 +5,12 @@ go 1.17
require (
github.com/gin-gonic/gin v1.7.4
github.com/shopspring/decimal v1.3.1
github.com/stretchr/testify v1.7.0
golang.org/x/text v0.3.7
)
require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/gin-contrib/sse v0.1.0 // indirect
github.com/go-playground/locales v0.14.0 // indirect
github.com/go-playground/universal-translator v0.18.0 // indirect
@ -19,9 +21,11 @@ require (
github.com/mattn/go-isatty v0.0.14 // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/ugorji/go/codec v1.2.6 // indirect
golang.org/x/crypto v0.0.0-20211117183948-ae814b36b871 // indirect
golang.org/x/sys v0.0.0-20211117180635-dee7805ff2e1 // indirect
google.golang.org/protobuf v1.27.1 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b // indirect
)

View File

@ -33,13 +33,14 @@ type Config struct {
}
type Account struct {
Acc string `json:"account"`
StartDate string `json:"startDate"`
Currency string `json:"currency,omitempty"` // 货币
CurrencySymbol string `json:"currencySymbol,omitempty"` // 货币符号
Price string `json:"price,omitempty"` // 汇率
PriceDate string `json:"priceDate,omitempty"` // 汇率日期
IsAnotherCurrency bool `json:"isAnotherCurrency,omitempty"` // 其他币种标识
Acc string `json:"account"`
StartDate string `json:"startDate"`
Currency string `json:"currency,omitempty"` // 货币
//CurrencySymbol string `json:"currencySymbol,omitempty"` // 货币符号
Currencies []AccountCurrency `json:"currencies,omitempty"` // 多个货币单位
//Price string `json:"price,omitempty"` // 汇率
//PriceDate string `json:"priceDate,omitempty"` // 汇率日期
//IsAnotherCurrency bool `json:"isAnotherCurrency,omitempty"` // 其他币种标识
IsCurrent bool `json:"isCurrent,omitempty"`
Positions []AccountPosition `json:"positions,omitempty"`
MarketNumber string `json:"marketNumber,omitempty"`
@ -49,6 +50,14 @@ type Account struct {
Type *AccountType `json:"type,omitempty"`
}
type AccountCurrency struct {
Currency string `json:"currency,omitempty"` // 货币
CurrencySymbol string `json:"currencySymbol,omitempty"` // 货币符号
IsAnotherCurrency bool `json:"isAnotherCurrency,omitempty"` // 其他币种标识
Price string `json:"price,omitempty"` // 汇率
PriceDate string `json:"priceDate,omitempty"` // 汇率日期
}
type AccountPosition struct {
Number string `json:"number,omitempty"`
Currency string `json:"currency,omitempty"`
@ -325,6 +334,8 @@ func LoadLedgerAccounts(ledgerId string) error {
}
if words[1] == "open" {
account.StartDate = words[0]
// fix: 处理已关闭又打开的账户
account.EndDate = ""
} else if words[1] == "close" {
account.EndDate = words[0]
}

View File

@ -18,17 +18,10 @@ func QueryValidAccount(c *gin.Context) {
result := make([]script.Account, 0)
for _, account := range allAccounts {
if account.EndDate == "" {
// 货币实时汇率(忽略账本主货币)
if account.Currency != ledgerConfig.OperatingCurrency && account.Currency != "" {
// 从 map 中获取对应货币的实时汇率和符号
currency, ok := currencyMap[account.Currency]
if ok {
account.CurrencySymbol = currency.Symbol
account.Price = currency.Price
account.PriceDate = currency.PriceDate
account.IsAnotherCurrency = true
}
}
// 多个货币处理
multiCurrency := strings.Split(account.Currency, ",")
account.Currency = multiCurrency[0]
account.Currencies = multiCurrencies(*ledgerConfig, multiCurrency, currencyMap)
result = append(result, account)
}
}
@ -66,17 +59,11 @@ func QueryAllAccount(c *gin.Context) {
if account.EndDate != "" {
continue
}
// 货币实时汇率(忽略账本主货币)
if account.Currency != ledgerConfig.OperatingCurrency && account.Currency != "" {
// 从 map 中获取对应货币的实时汇率和符号
currency, ok := currencyMap[account.Currency]
if ok {
account.CurrencySymbol = currency.Symbol
account.Price = currency.Price
account.PriceDate = currency.PriceDate
account.IsAnotherCurrency = true
}
}
// 多个货币处理
multiCurrency := strings.Split(account.Currency, ",")
account.Currency = multiCurrency[0]
account.Currencies = multiCurrencies(*ledgerConfig, multiCurrency, currencyMap)
key := account.Acc
typ := script.GetAccountType(ledgerConfig.Id, key)
account.Type = &typ
@ -96,6 +83,26 @@ func QueryAllAccount(c *gin.Context) {
OK(c, result)
}
func multiCurrencies(ledgerConfig script.Config, multiCurrencyStr []string, currencyMap map[string]script.LedgerCurrency) []script.AccountCurrency {
currencies := make([]script.AccountCurrency, 0)
for i := 0; i < len(multiCurrencyStr); i++ {
accCurrency := script.AccountCurrency{
Currency: multiCurrencyStr[i],
CurrencySymbol: script.GetCommoditySymbol(ledgerConfig.Id, multiCurrencyStr[i]),
}
// 从 map 中获取对应货币的实时汇率和符号
currency, ok := currencyMap[multiCurrencyStr[i]]
if ok {
accCurrency.CurrencySymbol = currency.Symbol
accCurrency.Price = currency.Price
accCurrency.PriceDate = currency.PriceDate
accCurrency.IsAnotherCurrency = multiCurrencyStr[i] != ledgerConfig.OperatingCurrency
}
currencies = append(currencies, accCurrency)
}
return currencies
}
func parseAccountPositions(ledgerId string, input string) []script.AccountPosition {
// 使用正则表达式提取数字、货币代码和金额
re := regexp.MustCompile(`(-?\d+\.\d+) (\w+)`)

View File

@ -15,11 +15,6 @@ func TestPingRoute(t *testing.T) {
// 创建一个Gin引擎
r := gin.Default()
// 定义测试路由处理程序
r.GET("/ping", func(c *gin.Context) {
c.JSON(http.StatusOK, gin.H{"message": "pong"})
})
// 创建一个模拟的HTTP请求
req, err := http.NewRequest(http.MethodGet, "/ping", nil)
assert.NoError(t, err)