-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
260 lines (234 loc) · 6.07 KB
/
main.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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
package main
import (
"crypto/tls"
"database/sql"
"fmt"
"github.com/BurntSushi/toml"
"github.com/ftlynx/tsx"
"github.com/ftlynx/tsx/mysqlx"
_ "github.com/go-sql-driver/mysql"
"github.com/tealeg/xlsx"
"gopkg.in/gomail.v2"
"io/ioutil"
"mime"
"os"
"path/filepath"
"strings"
"time"
)
type Config struct {
Data *DataConfig `toml:"data"`
EMail *EmailConf `toml:"email"`
}
type DataConfig struct {
Datasource string `toml:"datasource"`
Sql string `toml:"sql"`
Subject string `toml:"subject"`
Mailto string `toml:"mailto"`
Cc string `toml:"cc"`
PlainBody string `toml:"plain_body"`
AttachName string `toml:"attach_name"`
}
type EmailConf struct {
Smtp string `toml:"smtp"`
Port int `toml:"port"`
User string `toml:"user"`
Passwd string `toml:"passwd"`
}
func parseConfig(configpath string) (*Config, error) {
cfg := new(Config)
configPath, err := filepath.Abs(configpath)
if err != nil {
return cfg, fmt.Errorf("get config file absolute path failed, %s", err.Error())
}
file, err := os.Open(configPath)
defer file.Close()
if err != nil {
return cfg, fmt.Errorf("open config file error, %s", err.Error())
}
fd, err := ioutil.ReadAll(file)
if err != nil {
return cfg, fmt.Errorf("read config file error, %s", err.Error())
}
cfg.Data = new(DataConfig)
cfg.EMail = new(EmailConf)
if err := toml.Unmarshal(fd, cfg); err != nil {
return cfg, fmt.Errorf("load config file error, %s", err.Error())
}
return cfg, nil
}
type myDB struct {
DB *sql.DB
}
type RowData []interface{}
func (d *myDB) QueryDataToMap(sql string) ([]string, []map[string]interface{}, error) {
result := make([]map[string]interface{}, 0)
rows, err := d.DB.Query(sql)
if err != nil {
return make([]string, 0), result, err
}
columns, err := rows.Columns()
if err != nil {
return columns, result, err
}
r := make(RowData, len(columns))
for index, _ := range r {
var tmp interface{}
r[index] = &tmp
}
for rows.Next() {
if err := rows.Scan(r...); err != nil {
return columns, result, err
}
item := make(map[string]interface{})
for i, data := range r {
item[columns[i]] = *data.(*interface{})
}
result = append(result, item)
}
return columns, result, err
}
func (d *myDB) QueryDataToSlice(sql string) ([]string, []RowData, error) {
result := make([]RowData, 0)
rows, err := d.DB.Query(sql)
if err != nil {
return make([]string, 0), result, err
}
columns, err := rows.Columns()
if err != nil {
return columns, result, err
}
for rows.Next() {
//初始化
r := make(RowData, len(columns))
for index, _ := range r {
var tmp interface{}
r[index] = &tmp
}
if err := rows.Scan(r...); err != nil {
return columns, result, err
}
result = append(result, r)
}
return columns, result, err
}
func CreateExcelFromMap(columns []string, data []map[string]interface{}, filename string) error {
file := xlsx.NewFile()
sheet, err := file.AddSheet("sheet1")
if err != nil {
return err
}
row := sheet.AddRow()
row.SetHeightCM(1)
for _, v := range columns {
cell := row.AddCell()
cell.Value = v
}
for _, v1 := range data {
row := sheet.AddRow()
row.SetHeightCM(1)
for _, column := range columns {
cell := row.AddCell()
cell.Value = fmt.Sprintf("%s", v1[column])
}
}
return file.Save(filename)
}
func CreateExcelFromSlice(columns []string, data []RowData, filename string) error {
file := xlsx.NewFile()
sheet, err := file.AddSheet("sheet1")
if err != nil {
return err
}
row := sheet.AddRow()
row.SetHeightCM(1)
for _, v := range columns {
cell := row.AddCell()
cell.Value = v
}
for k1, _ := range data {
row := sheet.AddRow()
row.SetHeightCM(1)
for k2, _ := range columns {
cell := row.AddCell()
cell.Value = fmt.Sprintf("%s", *data[k1][k2].(*interface{}))
}
}
return file.Save(filename)
}
type connMail struct {
User string `json:"user"`
Passwd string `json:"passwd"`
Smtp string `json:"smtp"`
Port int `json:"port"`
}
func (m *connMail) Send(to string, cc string, subject string, plain_body string, attach_file string) error {
d := gomail.NewDialer(m.Smtp, m.Port, m.User, m.Passwd)
d.TLSConfig = &tls.Config{InsecureSkipVerify: true}
//设置消息
msg := gomail.NewMessage(gomail.SetCharset("UTF-8"), gomail.SetEncoding(gomail.Base64))
msg.SetHeader("From", msg.FormatAddress(m.User, "系统邮件"))
msg.SetHeader("To", strings.Split(to, ";")...)
if cc != "" {
msg.SetHeader("Cc", strings.Split(cc, ";")...)
}
msg.SetHeader("Subject", subject)
msg.SetBody("text/plain", plain_body)
if tsx.FileExists(attach_file) {
names := strings.Split(attach_file, "/")
name := names[len(names)-1]
//处理附件文件名乱码
fileHeader := map[string][]string{
"Content-Type": {fmt.Sprintf(`text/plain; name="%s"`, mime.BEncoding.Encode("UTF-8", name))},
//"Content-Disposition": {fmt.Sprintf(`attachment; filename="%s"`, mime.QEncoding.Encode("UTF-8", name))},
}
msg.Attach(attach_file, gomail.Rename(name), gomail.SetHeader(fileHeader))
}
return d.DialAndSend(msg)
}
func main() {
if len(os.Args) < 2 {
panic(fmt.Sprintf("usage %s config.ini", os.Args[0]))
}
cfg, err := parseConfig(os.Args[1])
if err != nil {
panic(err)
}
maxConnNum := 10
maxIdleConn := 5
maxLifeTime := time.Duration(int64(86400))
db, err := mysqlx.SqlDB(cfg.Data.Datasource, maxConnNum, maxIdleConn, maxLifeTime)
if err != nil {
panic(err)
}
filename := fmt.Sprintf("/tmp/%s.xlsx", cfg.Data.AttachName)
sqldata := myDB{DB: db}
// 使用slice
columns, result, err := sqldata.QueryDataToSlice(cfg.Data.Sql)
if err != nil {
panic(err)
}
if err := CreateExcelFromSlice(columns, result, filename); err != nil {
panic(err)
}
/*
// 使用map
columns, result, err := sqldata.QueryDataToMap(cfg.Data.Sql)
if err != nil {
panic(err)
}
if err := CreateExcelFromMap(columns, result, filename); err != nil {
panic(err)
}
*/
email := connMail{
User: cfg.EMail.User,
Passwd: cfg.EMail.Passwd,
Smtp: cfg.EMail.Smtp,
Port: cfg.EMail.Port,
}
if err := email.Send(cfg.Data.Mailto, cfg.Data.Cc, cfg.Data.Subject, cfg.Data.PlainBody, filename); err != nil {
panic(err)
}
return
}