account multi commodity
This commit is contained in:
parent
a339e1afe8
commit
0024debf08
4
go.mod
4
go.mod
|
|
@ -5,10 +5,12 @@ go 1.17
|
||||||
require (
|
require (
|
||||||
github.com/gin-gonic/gin v1.7.4
|
github.com/gin-gonic/gin v1.7.4
|
||||||
github.com/shopspring/decimal v1.3.1
|
github.com/shopspring/decimal v1.3.1
|
||||||
|
github.com/stretchr/testify v1.7.0
|
||||||
golang.org/x/text v0.3.7
|
golang.org/x/text v0.3.7
|
||||||
)
|
)
|
||||||
|
|
||||||
require (
|
require (
|
||||||
|
github.com/davecgh/go-spew v1.1.1 // indirect
|
||||||
github.com/gin-contrib/sse v0.1.0 // indirect
|
github.com/gin-contrib/sse v0.1.0 // indirect
|
||||||
github.com/go-playground/locales v0.14.0 // indirect
|
github.com/go-playground/locales v0.14.0 // indirect
|
||||||
github.com/go-playground/universal-translator v0.18.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/mattn/go-isatty v0.0.14 // indirect
|
||||||
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
|
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
|
||||||
github.com/modern-go/reflect2 v1.0.2 // 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
|
github.com/ugorji/go/codec v1.2.6 // indirect
|
||||||
golang.org/x/crypto v0.0.0-20211117183948-ae814b36b871 // indirect
|
golang.org/x/crypto v0.0.0-20211117183948-ae814b36b871 // indirect
|
||||||
golang.org/x/sys v0.0.0-20211117180635-dee7805ff2e1 // indirect
|
golang.org/x/sys v0.0.0-20211117180635-dee7805ff2e1 // indirect
|
||||||
google.golang.org/protobuf v1.27.1 // indirect
|
google.golang.org/protobuf v1.27.1 // indirect
|
||||||
gopkg.in/yaml.v2 v2.4.0 // indirect
|
gopkg.in/yaml.v2 v2.4.0 // indirect
|
||||||
|
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b // indirect
|
||||||
)
|
)
|
||||||
|
|
|
||||||
|
|
@ -36,10 +36,11 @@ type Account struct {
|
||||||
Acc string `json:"account"`
|
Acc string `json:"account"`
|
||||||
StartDate string `json:"startDate"`
|
StartDate string `json:"startDate"`
|
||||||
Currency string `json:"currency,omitempty"` // 货币
|
Currency string `json:"currency,omitempty"` // 货币
|
||||||
CurrencySymbol string `json:"currencySymbol,omitempty"` // 货币符号
|
//CurrencySymbol string `json:"currencySymbol,omitempty"` // 货币符号
|
||||||
Price string `json:"price,omitempty"` // 汇率
|
Currencies []AccountCurrency `json:"currencies,omitempty"` // 多个货币单位
|
||||||
PriceDate string `json:"priceDate,omitempty"` // 汇率日期
|
//Price string `json:"price,omitempty"` // 汇率
|
||||||
IsAnotherCurrency bool `json:"isAnotherCurrency,omitempty"` // 其他币种标识
|
//PriceDate string `json:"priceDate,omitempty"` // 汇率日期
|
||||||
|
//IsAnotherCurrency bool `json:"isAnotherCurrency,omitempty"` // 其他币种标识
|
||||||
IsCurrent bool `json:"isCurrent,omitempty"`
|
IsCurrent bool `json:"isCurrent,omitempty"`
|
||||||
Positions []AccountPosition `json:"positions,omitempty"`
|
Positions []AccountPosition `json:"positions,omitempty"`
|
||||||
MarketNumber string `json:"marketNumber,omitempty"`
|
MarketNumber string `json:"marketNumber,omitempty"`
|
||||||
|
|
@ -49,6 +50,14 @@ type Account struct {
|
||||||
Type *AccountType `json:"type,omitempty"`
|
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 {
|
type AccountPosition struct {
|
||||||
Number string `json:"number,omitempty"`
|
Number string `json:"number,omitempty"`
|
||||||
Currency string `json:"currency,omitempty"`
|
Currency string `json:"currency,omitempty"`
|
||||||
|
|
@ -325,6 +334,8 @@ func LoadLedgerAccounts(ledgerId string) error {
|
||||||
}
|
}
|
||||||
if words[1] == "open" {
|
if words[1] == "open" {
|
||||||
account.StartDate = words[0]
|
account.StartDate = words[0]
|
||||||
|
// fix: 处理已关闭又打开的账户
|
||||||
|
account.EndDate = ""
|
||||||
} else if words[1] == "close" {
|
} else if words[1] == "close" {
|
||||||
account.EndDate = words[0]
|
account.EndDate = words[0]
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -18,17 +18,10 @@ func QueryValidAccount(c *gin.Context) {
|
||||||
result := make([]script.Account, 0)
|
result := make([]script.Account, 0)
|
||||||
for _, account := range allAccounts {
|
for _, account := range allAccounts {
|
||||||
if account.EndDate == "" {
|
if account.EndDate == "" {
|
||||||
// 货币实时汇率(忽略账本主货币)
|
// 多个货币处理
|
||||||
if account.Currency != ledgerConfig.OperatingCurrency && account.Currency != "" {
|
multiCurrency := strings.Split(account.Currency, ",")
|
||||||
// 从 map 中获取对应货币的实时汇率和符号
|
account.Currency = multiCurrency[0]
|
||||||
currency, ok := currencyMap[account.Currency]
|
account.Currencies = multiCurrencies(*ledgerConfig, multiCurrency, currencyMap)
|
||||||
if ok {
|
|
||||||
account.CurrencySymbol = currency.Symbol
|
|
||||||
account.Price = currency.Price
|
|
||||||
account.PriceDate = currency.PriceDate
|
|
||||||
account.IsAnotherCurrency = true
|
|
||||||
}
|
|
||||||
}
|
|
||||||
result = append(result, account)
|
result = append(result, account)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -66,17 +59,11 @@ func QueryAllAccount(c *gin.Context) {
|
||||||
if account.EndDate != "" {
|
if account.EndDate != "" {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
// 货币实时汇率(忽略账本主货币)
|
// 多个货币处理
|
||||||
if account.Currency != ledgerConfig.OperatingCurrency && account.Currency != "" {
|
multiCurrency := strings.Split(account.Currency, ",")
|
||||||
// 从 map 中获取对应货币的实时汇率和符号
|
account.Currency = multiCurrency[0]
|
||||||
currency, ok := currencyMap[account.Currency]
|
account.Currencies = multiCurrencies(*ledgerConfig, multiCurrency, currencyMap)
|
||||||
if ok {
|
|
||||||
account.CurrencySymbol = currency.Symbol
|
|
||||||
account.Price = currency.Price
|
|
||||||
account.PriceDate = currency.PriceDate
|
|
||||||
account.IsAnotherCurrency = true
|
|
||||||
}
|
|
||||||
}
|
|
||||||
key := account.Acc
|
key := account.Acc
|
||||||
typ := script.GetAccountType(ledgerConfig.Id, key)
|
typ := script.GetAccountType(ledgerConfig.Id, key)
|
||||||
account.Type = &typ
|
account.Type = &typ
|
||||||
|
|
@ -96,6 +83,26 @@ func QueryAllAccount(c *gin.Context) {
|
||||||
OK(c, result)
|
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 {
|
func parseAccountPositions(ledgerId string, input string) []script.AccountPosition {
|
||||||
// 使用正则表达式提取数字、货币代码和金额
|
// 使用正则表达式提取数字、货币代码和金额
|
||||||
re := regexp.MustCompile(`(-?\d+\.\d+) (\w+)`)
|
re := regexp.MustCompile(`(-?\d+\.\d+) (\w+)`)
|
||||||
|
|
|
||||||
|
|
@ -15,11 +15,6 @@ func TestPingRoute(t *testing.T) {
|
||||||
// 创建一个Gin引擎
|
// 创建一个Gin引擎
|
||||||
r := gin.Default()
|
r := gin.Default()
|
||||||
|
|
||||||
// 定义测试路由处理程序
|
|
||||||
r.GET("/ping", func(c *gin.Context) {
|
|
||||||
c.JSON(http.StatusOK, gin.H{"message": "pong"})
|
|
||||||
})
|
|
||||||
|
|
||||||
// 创建一个模拟的HTTP请求
|
// 创建一个模拟的HTTP请求
|
||||||
req, err := http.NewRequest(http.MethodGet, "/ping", nil)
|
req, err := http.NewRequest(http.MethodGet, "/ping", nil)
|
||||||
assert.NoError(t, err)
|
assert.NoError(t, err)
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue