-
Notifications
You must be signed in to change notification settings - Fork 1
/
wechat.go
113 lines (94 loc) · 3.16 KB
/
wechat.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
// Copyright 2019 vogo. All rights reserved.
package aliwepaystat
import (
"log"
"strconv"
"strings"
"golang.org/x/text/encoding"
"golang.org/x/text/encoding/unicode"
)
const (
WechatCsvHeader = "交易时间,交易类型,交易对方,商品,收/支,金额(元),支付方式,当前状态,交易单号,商户单号,备注"
WechatCsvFieldNum = 11
)
// WechatTrans Wechat transaction
type WechatTrans struct {
CreatedTime string `csv:"created_time"`
Type string `csv:"type"`
Target string `csv:"target"`
Product string `csv:"product"`
FinType string `csv:"fin_type"`
Amount string `csv:"amount"`
amt float64
Source string `csv:"source"`
Status string `csv:"status"`
ID string `csv:"id"`
OrderID string `csv:"order_id"`
Comment string `csv:"comment"`
refund float64
}
func (t *WechatTrans) IsIncome() bool {
return Contains(t.FinType, "收入") ||
ContainsAny(t.Product, cfg.IncomeKeyWords...)
}
func (t *WechatTrans) IsInnerTransfer() bool {
return ContainsAny(t.Target, cfg.FamilyMembers...) ||
ContainsAny(t.Product, cfg.InnerTransferKeyWords...)
}
func (t *WechatTrans) IsTransfer() bool {
return Contains(t.Type, "转账") ||
EitherContainsAny(t.Product, t.Target, cfg.TransferKeyWords...) ||
ContainsAny(t.Target, cfg.FamilyMembers...)
}
func (t *WechatTrans) IsClosed() bool {
return ContainsAny(t.Status, "失败", "交易关闭")
}
func (t *WechatTrans) YearMonth() string {
return t.CreatedTime[0:4] + t.CreatedTime[5:7]
}
func (t *WechatTrans) GetID() string { return t.ID }
func (t *WechatTrans) GetOrderID() string { return t.OrderID }
func (t *WechatTrans) GetCreatedTime() string { return t.CreatedTime }
func (t *WechatTrans) GetSource() string { return t.Source }
func (t *WechatTrans) GetType() string { return t.Type }
func (t *WechatTrans) GetTarget() string { return t.Target }
func (t *WechatTrans) GetProduct() string { return t.Product }
func (t *WechatTrans) GetAmount() float64 {
if t.amt == 0 && t.Amount != "" {
var err error
t.amt, err = strconv.ParseFloat(strings.ReplaceAll(t.Amount, "¥", ""), 32)
if err != nil {
log.Fatalf("无法解析金额: %v", t.Amount)
}
}
return t.amt
}
func (t *WechatTrans) GetFormatAmount() float64 {
return RoundFloat(t.GetAmount())
}
func (t *WechatTrans) GetFinType() string { return t.FinType }
func (t *WechatTrans) GetStatus() string { return t.Status }
func (t *WechatTrans) GetRefund() float64 { return t.refund }
func (t *WechatTrans) GetComment() string { return t.Comment }
func (t *WechatTrans) IsShowInList() bool { return t.GetAmount() > cfg.ListMinAmount }
type wechatTransParser struct {
}
func (p *wechatTransParser) NewTrans() Trans {
return &WechatTrans{}
}
func (p *wechatTransParser) CsvHeader() string {
return WechatCsvHeader
}
func (p *wechatTransParser) FieldNum() int {
return WechatCsvFieldNum
}
func (p *wechatTransParser) Enc() encoding.Encoding {
return unicode.UTF8
}
var TransParserWechat = &wechatTransParser{}
func IsWechatGroupAAExpense(trans Trans) bool {
if we, ok := trans.(*WechatTrans); ok {
return we.Type == "群收款" && we.FinType == "支出"
}
return false
}