-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
154 lines (127 loc) · 3.27 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
package main
import (
"fmt"
"github.com/sdvdxl/auto-send-payroll/config"
"github.com/sdvdxl/auto-send-payroll/mail"
"github.com/axgle/pinyin"
"github.com/tealeg/xlsx"
"strings"
"os"
)
var (
cfg *config.Config
works chan bool
count int
)
func init() {
var err error
works = make(chan bool, 10)
configFile :="config/config.yaml"
fmt.Println("读取配置文件",configFile)
cfg, err = config.ReadConfig(configFile)
if cfg.Smtp_Server=="" {
fmt.Println("smtp server 没有配置")
os.Exit(1)
}
if cfg.Port==0 {
fmt.Println("smtp server port 没有配置")
os.Exit(1)
}
if cfg.Sender_Email =="" {
fmt.Println("发件人 没有配置")
os.Exit(1)
}
if cfg.Sender_Password=="" {
fmt.Println("发件人名称 没有配置")
os.Exit(1)
}
printErrorExit(err, "打开配置文件出错")
}
func main() {
var (
startRow, endRow int
found bool
title = [...]string{"部门", "姓名", "基本工资", "岗位工资", "通讯费",
"病假", "事假", "年休", "缺勤", "扣款", "应付工资", "社保基数",
"公积金基数", "养老", "失业", "医疗", "公积金", "个人合计",
"免税金额", "补扣款", "应发工资", "应税金额",
"代扣个税", "核发总额",
}
)
excelFileName := cfg.Execl_Path
xlFile, err := xlsx.OpenFile(excelFileName)
fmt.Println("读取Excel", excelFileName)
printErrorExit(err, "打开Excel文件出错")
for x, row := range xlFile.Sheets[0].Rows {
if found {
break
}
for _, cell := range row.Cells {
if "基本信息" == cell.Value {
startRow = x
break
}
if "合计" == cell.Value {
endRow = x
found = true
break
}
}
}
rows := xlFile.Sheets[0].Rows[startRow+2 : endRow]
count = len(rows)
fmt.Println(fmt.Sprintf("一共%v个人",count))
if count==0 {
fmt.Println("没有可以发送的人员")
os.Exit(0)
}
for i, row := range rows {
payRollInfo := `<!Doctype html><html xmlns=http://www.w3.org/1999/xhtml><meta http-equiv=Content-Type content="text/html;charset=utf-8"><body><table cellspacing='0' border='1'><thead>`
var name string
for _, v := range title {
if v == "" {
v = "0"
}
payRollInfo += "<td nowrap>" + v + "</td>"
}
payRollInfo += "</th></thead><tbody><tr>"
for j, cell := range row.Cells {
if j == 0 {
continue
}
if j == 2 {
name = cell.Value
}
payRollInfo += "<td nowrap>" + cell.Value + "</td>"
}
payRollInfo += "</tr></tbody></table></body></html>"
//将名字变换成拼音,
nameChars := []rune(name)
lastName := pinyin.Convert(string(nameChars[1:]))
firstName := pinyin.Convert(string(nameChars[:1]))
email := strings.ToLower(lastName + "." + firstName + "@fraudmetrix.cn")
works <- true
fmt.Println(fmt.Sprintf("正在给%s(%s)发送邮件", name, email))
mail := mail.Mail{SmtpServer: cfg.Smtp_Server, Port: cfg.Port,
SenderEmail: cfg.Sender_Email, SenderName: cfg.Sender_Name,
SenderPassword: cfg.Sender_Password, ToEmail: email,
Subject: cfg.Subject, Message: payRollInfo,
}
works<-true
go func(num int) {
mail.SendEmail()
if num+1 == count {
close(works)
}
}(i)
}
for _ = range works {
}
fmt.Println("发送完毕")
}
func printErrorExit(err error, msg string) {
if err!=nil {
fmt.Println(msg)
os.Exit(1)
}
}