-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmonth.go
139 lines (118 loc) · 3.5 KB
/
month.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
// Copyright 2019 vogo. All rights reserved.
package aliwepaystat
type MonthStat struct {
TransMap map[string]Trans
YearMonth string
Investment *TransGroup
InnerTransfer *TransGroup
Income *TransGroup
IncomeTransfer *TransGroup
Loan *TransGroup
LoanRepayment *TransGroup
CreditRepayment *TransGroup
ExpenseTotal float64
ExpenseTransfer *TransGroup
ExpenseTravel *TransGroup
ExpenseEat *TransGroup
ExpenseWaterElectGas *TransGroup
ExpenseTel *TransGroup
ExpenseOther *TransGroup
}
func (ms *MonthStat) FormatExpenseTotal() float64 {
return RoundFloat(ms.ExpenseTotal)
}
func (ms *MonthStat) add(trans Trans) {
//ignore closed
if trans.IsClosed() {
return
}
// trans already exists
if _, ok := ms.TransMap[trans.GetID()]; ok {
return
}
ms.TransMap[trans.GetID()] = trans
// [1] 贷款放在收入之前判断
if EitherContainsAny(trans.GetProduct(), trans.GetTarget(), cfg.LoanKeyWords...) {
ms.Loan.add(trans)
return
}
// [2] 贷款还款
if EitherContainsAny(trans.GetProduct(), trans.GetTarget(), cfg.LoanRepaymentKeyWords...) {
ms.LoanRepayment.add(trans)
return
}
if IsInvestment(trans.GetProduct()) {
ms.Investment.add(trans)
return
}
// [2] 信用还款
if EitherContainsAny(trans.GetProduct(), trans.GetTarget(), cfg.RepaymentKeyWords...) {
ms.CreditRepayment.add(trans)
// 信用还款也作为支出的一部分
ms.ExpenseTotal += trans.GetAmount()
return
}
// [3] 内部转账
if trans.IsInnerTransfer() {
ms.InnerTransfer.add(trans)
return
}
// [4] 收入判断 (包括转入)
if trans.IsIncome() {
// 转账收入单独统计,不计入普通收入
if ContainsAny(trans.GetProduct(), cfg.TransferKeyWords...) {
ms.IncomeTransfer.add(trans)
return
}
ms.Income.add(trans)
return
}
// [5] 转账 (转账支出单独统计,不计入普通支出)
if trans.IsTransfer() {
ms.ExpenseTransfer.add(trans)
return
}
// [6] 开始统计支出
ms.ExpenseTotal += trans.GetAmount()
switch {
case EitherContainsAny(trans.GetProduct(), trans.GetTarget(), cfg.TravelKeyWords...):
ms.ExpenseTravel.add(trans)
case EitherContainsAny(trans.GetProduct(), trans.GetTarget(), cfg.EatKeyWords...) || IsWechatGroupAAExpense(trans):
ms.ExpenseEat.add(trans)
case EitherContainsAny(trans.GetProduct(), trans.GetTarget(), cfg.WaterElectGasKeyWords...):
ms.ExpenseWaterElectGas.add(trans)
case EitherContainsAny(trans.GetProduct(), trans.GetTarget(), cfg.TelKeyWords...):
ms.ExpenseTel.add(trans)
default:
ms.ExpenseOther.add(trans)
}
}
var (
monthStatsMap = make(map[string]*MonthStat)
yearMonths []string
)
func getMonthStat(yearMonth string) *MonthStat {
ms, ok := monthStatsMap[yearMonth]
if !ok {
ms = &MonthStat{
TransMap: make(map[string]Trans),
YearMonth: yearMonth,
Investment: &TransGroup{},
Loan: &TransGroup{},
ExpenseTransfer: &TransGroup{},
InnerTransfer: &TransGroup{},
IncomeTransfer: &TransGroup{},
Income: &TransGroup{},
LoanRepayment: &TransGroup{},
CreditRepayment: &TransGroup{},
ExpenseTravel: &TransGroup{},
ExpenseEat: &TransGroup{},
ExpenseWaterElectGas: &TransGroup{},
ExpenseTel: &TransGroup{},
ExpenseOther: &TransGroup{},
}
monthStatsMap[yearMonth] = ms
yearMonths = append(yearMonths, yearMonth)
}
return ms
}