forked from Mereithhh/van-nav
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtools.go
191 lines (171 loc) · 4.03 KB
/
tools.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
package service
import (
"sync"
"github.com/mereith/nav/database"
"github.com/mereith/nav/logger"
"github.com/mereith/nav/types"
"github.com/mereith/nav/utils"
)
func ImportTools(data []types.Tool) {
var catelogs []string
for _, v := range data {
if !utils.In(v.Catelog, catelogs) {
catelogs = append(catelogs, v.Catelog)
}
sql_add_tool := `
INSERT INTO nav_table (id, name, catelog, url, logo, desc)
VALUES (?, ?, ?, ?, ?, ?);
`
stmt, err := database.DB.Prepare(sql_add_tool)
utils.CheckErr(err)
res, err := stmt.Exec(v.Id, v.Name, v.Catelog, v.Url, v.Logo, v.Desc)
utils.CheckErr(err)
_, err = res.LastInsertId()
utils.CheckErr(err)
}
for _, catelog := range catelogs {
var addCatelogDto types.AddCatelogDto
addCatelogDto.Name = catelog
AddCatelog(addCatelogDto)
}
// 转存所有图片,异步
go func(data []types.Tool) {
for _, v := range data {
UpdateImg(v.Logo)
}
}(data)
}
func UpdateTool(data types.UpdateToolDto) {
// 除了更新工具本身之外,也要更新 img 表
sql_update_tool := `
UPDATE nav_table
SET name = ?, url = ?, logo = ?, catelog = ?, desc = ?, sort = ?, hide = ?
WHERE id = ?;
`
stmt, err := database.DB.Prepare(sql_update_tool)
utils.CheckErr(err)
res, err := stmt.Exec(data.Name, data.Url, data.Logo, data.Catelog, data.Desc, data.Sort, data.Hide, data.Id)
utils.CheckErr(err)
_, err = res.RowsAffected()
utils.CheckErr(err)
// 更新 img
UpdateImg(data.Logo)
}
func AddTool(data types.AddToolDto) (int64, error) {
// 创建一个互斥锁来保护数据库操作
var mu sync.Mutex
mu.Lock()
defer mu.Unlock()
tx, err := database.DB.Begin()
if err != nil {
return 0, err
}
defer func() {
if err != nil {
tx.Rollback()
}
}()
sql_add_tool := `
INSERT INTO nav_table (name, url, logo, catelog, desc, sort, hide)
VALUES (?, ?, ?, ?, ?, ?, ?);
`
stmt, err := tx.Prepare(sql_add_tool)
if err != nil {
return 0, err
}
defer stmt.Close()
res, err := stmt.Exec(data.Name, data.Url, data.Logo, data.Catelog, data.Desc, data.Sort, data.Hide)
if err != nil {
return 0, err
}
id, err := res.LastInsertId()
if err != nil {
return 0, err
}
err = tx.Commit()
if err != nil {
return 0, err
}
logger.LogInfo("新增工具: %s", data.Name)
// 在事务完成后再异步更新图片
if data.Logo != "" {
UpdateImg(data.Logo)
}
return id, nil
}
func GetAllTool() []types.Tool {
sql_get_all := `
SELECT id,name,url,logo,catelog,desc,sort,hide FROM nav_table order by sort;
`
results := make([]types.Tool, 0)
rows, err := database.DB.Query(sql_get_all)
utils.CheckErr(err)
for rows.Next() {
var tool types.Tool
var hide interface{}
var sort interface{}
err = rows.Scan(&tool.Id, &tool.Name, &tool.Url, &tool.Logo, &tool.Catelog, &tool.Desc, &sort, &hide)
if hide == nil {
tool.Hide = false
} else {
if hide.(int64) == 0 {
tool.Hide = false
} else {
tool.Hide = true
}
}
if sort == nil {
tool.Sort = 0
} else {
i64 := sort.(int64)
tool.Sort = int(i64)
}
utils.CheckErr(err)
results = append(results, tool)
}
defer rows.Close()
return results
}
func GetToolLogoUrlById(id int) string {
sql_get_tool := `
SELECT logo FROM nav_table WHERE id=?;
`
rows, err := database.DB.Query(sql_get_tool, id)
utils.CheckErr(err)
var tool types.Tool
for rows.Next() {
err = rows.Scan(&tool.Logo)
utils.CheckErr(err)
}
defer rows.Close()
return tool.Logo
}
func UpdateToolIcon(id int64, logo string) {
sql_update_tool := `
UPDATE nav_table SET logo=? WHERE id=?;
`
_, err := database.DB.Exec(sql_update_tool, logo, id)
utils.CheckErr(err)
UpdateImg(logo)
}
func UpdateToolsSort(updates []types.UpdateToolsSortDto) error {
tx, err := database.DB.Begin()
if err != nil {
return err
}
sql := `UPDATE nav_table SET sort = ? WHERE id = ?`
stmt, err := tx.Prepare(sql)
if err != nil {
tx.Rollback()
return err
}
defer stmt.Close()
for _, update := range updates {
_, err = stmt.Exec(update.Sort, update.Id)
if err != nil {
tx.Rollback()
return err
}
}
return tx.Commit()
}