2021-11-18 08:27:28 +00:00
|
|
|
package script
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"encoding/json"
|
2021-11-19 09:54:02 +00:00
|
|
|
"github.com/gin-gonic/gin"
|
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-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 {
|
|
|
|
|
Acc string `json:"account"`
|
|
|
|
|
Commodity string `json:"commodity"`
|
|
|
|
|
StartDate string `json:"startDate"`
|
|
|
|
|
Type accountType `json:"type"`
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type accountType struct {
|
|
|
|
|
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-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 {
|
|
|
|
|
for _, config := range ledgerConfigMap {
|
|
|
|
|
accounts := make([]Account, 0)
|
|
|
|
|
err := BQLReport(&config, &accounts)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
//ledgerAccountsMap[ledgerId] = accounts
|
|
|
|
|
}
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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 "$"
|
|
|
|
|
}
|
|
|
|
|
return ""
|
|
|
|
|
}
|