forked from phachon/mm-wiki
/
config.go
139 lines (122 loc) · 3.12 KB
/
config.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
package models
import (
"github.com/snail007/go-activerecord/mysql"
"time"
)
const Table_Config_Name = "config"
const (
Config_Key_MainTitle = "main_title"
Config_Key_MainDescription = "main_description"
Config_Key_AutoFollowDoc = "auto_follow_doc_open"
Config_Key_SendEmail = "send_email_open"
Config_Key_AuthLogin = "sso_open"
Config_Key_SystemVersion = "system_version"
)
type Config struct {
}
var ConfigModel = Config{}
// get config by config_id
func (c *Config) GetConfigByConfigId(configId string) (config map[string]string, err error) {
db := G.DB()
var rs *mysql.ResultSet
rs, err = db.Query(db.AR().From(Table_Config_Name).Where(map[string]interface{}{
"config_id": configId,
}))
if err != nil {
return
}
config = rs.Row()
return
}
// update config by config_id
func (c *Config) Update(configId string, configValue map[string]interface{}) (id int64, err error) {
db := G.DB()
var rs *mysql.ResultSet
configValue["update_time"] = time.Now().Unix()
rs, err = db.Exec(db.AR().Update(Table_Config_Name, configValue, map[string]interface{}{
"config_id": configId,
}))
if err != nil {
return
}
id = rs.LastInsertId
return
}
// update config by key
func (c *Config) UpdateByKey(key string, value string) (id int64, err error) {
db := G.DB()
var rs *mysql.ResultSet
configValue := map[string]interface{}{}
configValue["value"] = value
configValue["update_time"] = time.Now().Unix()
rs, err = db.Exec(db.AR().Update(Table_Config_Name, configValue, map[string]interface{}{
"key": key,
}))
if err != nil {
return
}
id = rs.LastInsertId
return
}
// get all configs
func (c *Config) GetConfigs() (configs []map[string]string, err error) {
db := G.DB()
var rs *mysql.ResultSet
rs, err = db.Query(
db.AR().From(Table_Config_Name))
if err != nil {
return
}
configs = rs.Rows()
return
}
// get config by many config_id
func (c *Config) GetConfigByConfigIds(configIds []string) (configs []map[string]string, err error) {
db := G.DB()
var rs *mysql.ResultSet
rs, err = db.Query(db.AR().From(Table_Config_Name).Where(map[string]interface{}{
"config_id": configIds,
}))
if err != nil {
return
}
configs = rs.Rows()
return
}
// insert batch configs
func (c *Config) InsertBatch(insertValues []map[string]interface{}) (id int64, err error) {
db := G.DB()
var rs *mysql.ResultSet
rs, err = db.Exec(db.AR().InsertBatch(Table_Config_Name, insertValues))
if err != nil {
return
}
id = rs.LastInsertId
return
}
// insert config
func (c *Config) Insert(insertValue map[string]interface{}) (id int64, err error) {
insertValue["create_time"] = time.Now().Unix()
insertValue["update_time"] = time.Now().Unix()
db := G.DB()
var rs *mysql.ResultSet
rs, err = db.Exec(db.AR().Insert(Table_Config_Name, insertValue))
if err != nil {
return
}
id = rs.LastInsertId
return
}
// get config by config key
func (c *Config) GetConfigByKey(key string) (config map[string]string, err error) {
db := G.DB()
var rs *mysql.ResultSet
rs, err = db.Query(db.AR().From(Table_Config_Name).Where(map[string]interface{}{
"key": key,
}).Limit(0, 1))
if err != nil {
return
}
config = rs.Row()
return
}