beancount-gs/script/config.go

260 lines
6.5 KiB
Go
Raw Normal View History

2021-11-18 08:27:28 +00:00
package script
import (
"encoding/json"
"fmt"
2021-11-19 09:54:02 +00:00
"github.com/gin-gonic/gin"
2021-11-23 06:58:37 +00:00
"os"
"sort"
"strings"
2021-11-18 08:27:28 +00:00
)
var serverConfig Config
2021-11-22 14:50:10 +00:00
var ledgerConfigMap map[string]Config
var ledgerAccountsMap map[string][]Account
2021-11-23 06:58:37 +00:00
var ledgerAccountTypesMap map[string]map[string]string
2021-11-18 08:27:28 +00:00
var whiteList []string
type Config struct {
Id string `json:"id"`
Mail string `json:"mail"`
Title string `json:"title"`
DataPath string `json:"dataPath"`
OperatingCurrency string `json:"operatingCurrency"`
StartDate string `json:"startDate"`
IsBak bool `json:"isBak"`
}
2021-11-22 14:50:10 +00:00
type Account struct {
2021-11-23 14:42:03 +00:00
Acc string `json:"account"`
StartDate string `json:"startDate"`
Commodity string `json:"commodity,omitempty"`
PriceAmount string `json:"priceAmount,omitempty"`
PriceCommodity string `json:"priceCommodity,omitempty"`
PriceCommoditySymbol string `json:"priceCommoditySymbol,omitempty"`
EndDate string `json:"endDate,omitempty"`
Type *AccountType `json:"type,omitempty"`
2021-11-22 14:50:10 +00:00
}
2021-11-23 06:58:37 +00:00
type AccountType struct {
2021-11-22 14:50:10 +00:00
Key string `json:"key"`
Name string `json:"name"`
}
2021-11-18 08:27:28 +00:00
func GetServerConfig() Config {
return serverConfig
}
func GetLedgerConfigMap() map[string]Config {
return ledgerConfigMap
}
2021-11-18 10:10:19 +00:00
func GetLedgerConfig(ledgerId string) *Config {
for k, v := range ledgerConfigMap {
if k == ledgerId {
return &v
}
}
return nil
2021-11-18 08:27:28 +00:00
}
func GetLedgerConfigByMail(mail string) *Config {
for _, v := range ledgerConfigMap {
if v.Mail == mail {
return &v
}
}
return nil
}
2021-11-19 09:54:02 +00:00
func GetLedgerConfigFromContext(c *gin.Context) *Config {
ledgerConfig, _ := c.Get("LedgerConfig")
t, _ := ledgerConfig.(*Config)
return t
}
2021-11-22 14:50:10 +00:00
func GetLedgerAccounts(ledgerId string) []Account {
return ledgerAccountsMap[ledgerId]
}
2021-11-23 06:58:37 +00:00
func GetLedgerAccountTypes(ledgerId string) map[string]string {
return ledgerAccountTypesMap[ledgerId]
}
2021-11-23 14:42:03 +00:00
func GetAccountType(ledgerId string, acc string) AccountType {
accountTypes := ledgerAccountTypesMap[ledgerId]
accNodes := strings.Split(acc, ":")
accountType := AccountType{
Key: acc,
// 默认取最后一个节点
Name: accNodes[len(accNodes)-1],
}
var matchKey string = ""
for key, name := range accountTypes {
if strings.Contains(acc, key) && len(matchKey) < len(key) {
matchKey = key
accountType = AccountType{Key: key, Name: name}
}
}
return accountType
}
2021-11-18 08:27:28 +00:00
func IsInWhiteList(ledgerId string) bool {
// ledger white list is empty, return true
if whiteList == nil || len(whiteList) == 0 {
return true
}
for i := range whiteList {
if whiteList[i] == ledgerId {
return true
}
}
return false
}
func LoadServerConfig() error {
fileContent, err := ReadFile("./config/config.json")
if err != nil {
return err
}
err = json.Unmarshal(fileContent, &serverConfig)
if err != nil {
2021-11-21 14:37:13 +00:00
LogSystemError("Failed unmarshall config file (/config/config.json)")
2021-11-18 08:27:28 +00:00
return err
}
2021-11-21 14:37:13 +00:00
LogSystemInfo("Success load config file (/config/config.json)")
2021-11-18 08:27:28 +00:00
// load white list
fileContent, err = ReadFile("./config/white_list.json")
if err != nil {
return err
}
err = json.Unmarshal(fileContent, &whiteList)
if err != nil {
2021-11-21 14:37:13 +00:00
LogSystemError("Failed unmarshal whitelist file (/config/white_list.json)")
2021-11-18 08:27:28 +00:00
return err
}
2021-11-21 14:37:13 +00:00
LogSystemInfo("Success load whitelist file (/config/white_list.json)")
2021-11-18 08:27:28 +00:00
return nil
}
func LoadLedgerConfigMap() error {
path := GetServerLedgerConfigFilePath()
fileContent, err := ReadFile(path)
if err != nil {
return err
}
err = json.Unmarshal(fileContent, &ledgerConfigMap)
if err != nil {
2021-11-21 14:37:13 +00:00
LogSystemError("Failed unmarshal config file (" + path + ")")
2021-11-18 08:27:28 +00:00
return err
}
2021-11-21 14:37:13 +00:00
LogSystemInfo("Success load ledger_config file (" + path + ")")
2021-11-18 08:27:28 +00:00
return nil
}
2021-11-22 14:50:10 +00:00
func LoadLedgerAccountsMap() error {
2021-11-23 06:58:37 +00:00
if ledgerAccountsMap == nil {
ledgerAccountsMap = make(map[string][]Account)
}
2021-11-22 14:50:10 +00:00
for _, config := range ledgerConfigMap {
2021-11-23 06:58:37 +00:00
// 加载 account_type.json 到缓存(内存)
loadErr := LoadLedgerAccountTypesMap(config)
if loadErr != nil {
LogSystemError("Failed to load account types")
return loadErr
}
accountDirPath := config.DataPath + "/account"
dirs, err := os.ReadDir(accountDirPath)
2021-11-22 14:50:10 +00:00
if err != nil {
return err
}
2021-11-23 06:58:37 +00:00
accountMap := make(map[string]Account)
for _, dir := range dirs {
bytes, err := ReadFile(accountDirPath + "/" + dir.Name())
if err != nil {
return err
}
lines := strings.Split(string(bytes), "\n")
var temp Account
for _, line := range lines {
if line != "" {
words := strings.Fields(line)
if len(words) >= 3 {
key := words[2]
temp = accountMap[key]
2021-11-23 14:42:03 +00:00
account := Account{Acc: key, Type: nil}
2021-11-23 06:58:37 +00:00
// 货币单位
if len(words) >= 4 {
account.Commodity = words[3]
}
if words[1] == "open" {
account.StartDate = words[0]
} else if words[1] == "close" {
account.EndDate = words[0]
}
if temp.StartDate != "" {
account.StartDate = temp.StartDate
}
if temp.EndDate != "" {
account.EndDate = temp.EndDate
}
accountMap[key] = account
}
}
}
}
accounts := make([]Account, 0)
for _, account := range accountMap {
accounts = append(accounts, account)
}
// 账户按字母排序
sort.Sort(AccountSort(accounts))
ledgerAccountsMap[config.Id] = accounts
LogSystemInfo(fmt.Sprintf("Success load [%s] accounts cache", config.Mail))
2021-11-22 14:50:10 +00:00
}
return nil
}
2021-11-23 06:58:37 +00:00
func LoadLedgerAccountTypesMap(config Config) error {
path := GetLedgerAccountTypeFilePath(config.DataPath)
fileContent, err := ReadFile(path)
if err != nil {
return err
}
accountTypes := make(map[string]string)
err = json.Unmarshal(fileContent, &accountTypes)
if err != nil {
LogSystemError("Failed unmarshal config file (" + path + ")")
return err
}
if ledgerAccountTypesMap == nil {
ledgerAccountTypesMap = make(map[string]map[string]string)
}
ledgerAccountTypesMap[config.Id] = accountTypes
LogSystemInfo(fmt.Sprintf("Success load [%s] account type cache", config.Mail))
2021-11-23 06:58:37 +00:00
return nil
}
2021-11-22 14:50:10 +00:00
func WriteLedgerConfigMap(newLedgerConfigMap map[string]Config) error {
2021-11-18 08:27:28 +00:00
path := GetServerLedgerConfigFilePath()
mapBytes, err := json.Marshal(ledgerConfigMap)
if err != nil {
2021-11-21 14:37:13 +00:00
LogSystemError("Failed marshal ConfigMap")
2021-11-18 08:27:28 +00:00
return err
}
err = WriteFile(path, string(mapBytes))
ledgerConfigMap = newLedgerConfigMap
2021-11-21 14:37:13 +00:00
LogSystemInfo("Success write ledger_config file (" + path + ")")
2021-11-18 08:27:28 +00:00
return err
}
2021-11-22 08:47:49 +00:00
func GetCommoditySymbol(commodity string) string {
switch commodity {
case "CNY":
return "¥"
case "USD":
return "$"
}
2021-11-24 09:32:24 +00:00
return ""
2021-11-22 08:47:49 +00:00
}