This commit is contained in:
parent
037e17569f
commit
5875a7872b
|
|
@ -47,6 +47,7 @@ type Account struct {
|
||||||
MarketCurrencySymbol string `json:"marketCurrencySymbol,omitempty"`
|
MarketCurrencySymbol string `json:"marketCurrencySymbol,omitempty"`
|
||||||
EndDate string `json:"endDate,omitempty"`
|
EndDate string `json:"endDate,omitempty"`
|
||||||
Type *AccountType `json:"type,omitempty"`
|
Type *AccountType `json:"type,omitempty"`
|
||||||
|
Status bool `json:"status,omitempty"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type AccountPosition struct {
|
type AccountPosition struct {
|
||||||
|
|
@ -313,46 +314,43 @@ func LoadLedgerAccounts(ledgerId string) error {
|
||||||
lines := strings.Split(string(bytes), "\n")
|
lines := strings.Split(string(bytes), "\n")
|
||||||
var temp Account
|
var temp Account
|
||||||
for _, line := range lines {
|
for _, line := range lines {
|
||||||
|
line = strings.TrimSpace(line) //去除文本前后空白
|
||||||
if line != "" {
|
if line != "" {
|
||||||
words := strings.Fields(line)
|
if line[0] == ';' {
|
||||||
if len(words) >= 3 {
|
// 跳过注释行
|
||||||
key := words[2]
|
continue
|
||||||
temp = accountMap[key]
|
} else {
|
||||||
account := Account{Acc: key, Type: nil}
|
//非注释行
|
||||||
|
words := strings.Fields(line)
|
||||||
if words[1] == "open" {
|
if len(words) >= 3 {
|
||||||
account.StartDate = words[0]
|
key := words[2]
|
||||||
if account.StartDate != "" && temp.StartDate != "" && strings.Compare(account.StartDate, temp.StartDate) < 0 {
|
temp = accountMap[key]
|
||||||
// 重复定义的账户,取最早的开始时间为准
|
account := Account{Acc: key, Type: nil, StartDate: "", EndDate: ""}
|
||||||
account.StartDate = temp.StartDate
|
if words[1] == "open" {
|
||||||
|
// 最晚的开户日期设置为账户开户日期
|
||||||
|
account.StartDate = getMaxDate(words[0], temp.StartDate)
|
||||||
|
// 货币单位
|
||||||
|
if len(words) >= 4 {
|
||||||
|
account.Currency = words[3]
|
||||||
|
}
|
||||||
|
} else if words[1] == "close" {
|
||||||
|
//账户最晚的关闭日期设置为账户关闭日期
|
||||||
|
account.EndDate = getMaxDate(words[0], temp.EndDate)
|
||||||
}
|
}
|
||||||
// 货币单位
|
if account.EndDate != "" && account.StartDate == getMaxDate(account.StartDate, account.EndDate) {
|
||||||
if len(words) >= 4 {
|
// 如果结束时间非空,且 开户日期>关闭日期,则清空账户结束日期,设置此账户为有效账户
|
||||||
account.Currency = words[3]
|
account.EndDate = ""
|
||||||
|
account.Status = true
|
||||||
}
|
}
|
||||||
} else if words[1] == "close" {
|
// 现在如果结束日期非空,肯定满足有开始时间<结束时间
|
||||||
account.EndDate = words[0]
|
if account.EndDate != "" {
|
||||||
if account.EndDate != "" && temp.EndDate != "" && strings.Compare(account.EndDate, temp.EndDate) > 0 {
|
account.Status = false
|
||||||
// 重复定义的账户,取最晚的开始时间为准
|
|
||||||
account.EndDate = temp.EndDate
|
|
||||||
}
|
}
|
||||||
|
if account.Currency == "" {
|
||||||
|
account.Currency = temp.Currency
|
||||||
|
}
|
||||||
|
accountMap[key] = account
|
||||||
}
|
}
|
||||||
|
|
||||||
if account.StartDate == "" {
|
|
||||||
account.StartDate = temp.StartDate
|
|
||||||
}
|
|
||||||
if account.EndDate == "" {
|
|
||||||
account.EndDate = temp.EndDate
|
|
||||||
}
|
|
||||||
if account.Currency == "" {
|
|
||||||
account.Currency = temp.Currency
|
|
||||||
}
|
|
||||||
|
|
||||||
// 如果结束时间小于开始时间,则结束时间为空
|
|
||||||
if account.EndDate != "" && strings.Compare(account.StartDate, account.EndDate) > 0 {
|
|
||||||
account.EndDate = ""
|
|
||||||
}
|
|
||||||
accountMap[key] = account
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -29,3 +29,78 @@ func RandChar(size int) string {
|
||||||
}
|
}
|
||||||
return s.String()
|
return s.String()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type Timestamp int64
|
||||||
|
|
||||||
|
const time_layout string = "2006-01-02 15:04:05"
|
||||||
|
|
||||||
|
// 日期字符串转为时间戳 工具函数
|
||||||
|
func getTimeStamp(str_date string) Timestamp {
|
||||||
|
if len(str_date) == 10 {
|
||||||
|
str_date = str_date + " 00:00:00"
|
||||||
|
}
|
||||||
|
// 获取时区
|
||||||
|
loc, err := time.LoadLocation("Local")
|
||||||
|
if err != nil {
|
||||||
|
return 0
|
||||||
|
}
|
||||||
|
// 转换为时间戳
|
||||||
|
the_time, err := time.ParseInLocation(time_layout, str_date, loc)
|
||||||
|
if err != nil {
|
||||||
|
return 0
|
||||||
|
}
|
||||||
|
// 返回时间戳
|
||||||
|
return Timestamp(the_time.Unix())
|
||||||
|
}
|
||||||
|
|
||||||
|
//获取1到2个日期字符串中更大的日期
|
||||||
|
func getMaxDate(str_date1 string, str_date2 string) string {
|
||||||
|
var max_date string
|
||||||
|
if str_date1 != "" && str_date2 == "" {
|
||||||
|
// 只定义了第一个账户,取第一个账户的日期为准
|
||||||
|
max_date = str_date1
|
||||||
|
} else if str_date1 == "" && str_date2 != "" {
|
||||||
|
// 只定义了第二个账户,取第二个账户的日期为准
|
||||||
|
max_date = str_date2
|
||||||
|
} else if str_date1 != "" && str_date2 != "" {
|
||||||
|
// 重复定义的账户,取最晚的时间为准
|
||||||
|
t1 := getTimeStamp(str_date1)
|
||||||
|
t2 := getTimeStamp(str_date2)
|
||||||
|
if t1 > t2 {
|
||||||
|
max_date = str_date1
|
||||||
|
} else {
|
||||||
|
max_date = str_date2
|
||||||
|
}
|
||||||
|
} else if str_date1 == "" && str_date2 == "" {
|
||||||
|
// 没有定义账户,取当前日期为准
|
||||||
|
max_date = time.Now().Format(time_layout)
|
||||||
|
}
|
||||||
|
return max_date
|
||||||
|
}
|
||||||
|
|
||||||
|
// 获取1-2个日期字符串中最小的日期值
|
||||||
|
// 如果双参数均为空,则返回账簿开始记账日期
|
||||||
|
func getMinDate(str_date1 string, str_date2 string) string {
|
||||||
|
//time_layout := "2006-01-02 15:04:05"
|
||||||
|
var min_date string
|
||||||
|
if str_date1 != "" && str_date2 == "" {
|
||||||
|
// 只定义了第一个账户,取第一个账户的日期为准
|
||||||
|
min_date = str_date1
|
||||||
|
} else if str_date1 == "" && str_date2 != "" {
|
||||||
|
// 只定义了第二个账户,取第二个账户的日期为准
|
||||||
|
min_date = str_date2
|
||||||
|
} else if str_date1 != "" && str_date2 != "" {
|
||||||
|
// 重复定义的账户,取最早的时间
|
||||||
|
t1 := getTimeStamp(str_date1)
|
||||||
|
t2 := getTimeStamp(str_date2)
|
||||||
|
if t1 < t2 {
|
||||||
|
min_date = str_date1
|
||||||
|
} else {
|
||||||
|
min_date = str_date2
|
||||||
|
}
|
||||||
|
} else if str_date1 == "" && str_date2 == "" {
|
||||||
|
// 没有定义账户,取固定日期"1970-01-01"
|
||||||
|
min_date = "1970-01-01"
|
||||||
|
}
|
||||||
|
return min_date
|
||||||
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue