forked from go-admin-team/go-admin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig.go
120 lines (102 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
package models
import (
orm "go-admin/database"
"go-admin/utils"
"time"
)
type Config struct {
//编码
ConfigId int64 `json:"configId" gorm:"column:configId;primary_key"`
//参数名称
ConfigName string `json:"configName" gorm:"column:configName"`
//参数键名
ConfigKey string `json:"configKey" gorm:"column:configKey"`
//参数键值
ConfigValue string `json:"configValue" gorm:"column:configValue"`
//是否系统内置
ConfigType string `json:"configType" gorm:"column:configType"`
//备注
Remark string `json:"remark" gorm:"column:remark"`
CreateBy string `json:"createBy" gorm:"column:create_by"`
CreateTime string `json:"createTime" gorm:"column:create_time"`
UpdateBy string `json:"updateBy" gorm:"column:update_by"`
UpdateTime string `json:"updateTime" gorm:"column:update_time"`
DataScope string `json:"dataScope" gorm:"_"`
Params string `json:"params" gorm:"column:params"`
IsDel string `json:"isDel" gorm:"column:is_del"`
}
// Config 创建
func (e *Config) Create() (Config, error) {
var doc Config
doc.IsDel = "0"
e.CreateTime = time.Now().String()
result := orm.Eloquent.Table("sys_config").Create(&e)
if result.Error != nil {
err := result.Error
return doc, err
}
doc = *e
return doc, nil
}
// 获取 Config
func (e *Config) Get() (Config, error) {
var doc Config
table := orm.Eloquent.Table("sys_config")
if e.ConfigId != 0 {
table = table.Where("configId = ?", e.ConfigId)
}
if e.ConfigKey != "" {
table = table.Where("configKey = ?", e.ConfigKey)
}
if err := table.Where("is_del = 0").First(&doc).Error; err != nil {
return doc, err
}
return doc, nil
}
func (e *Config) GetPage(pageSize int, pageIndex int) ([]Config, int32, error) {
var doc []Config
table := orm.Eloquent.Select("*").Table("sys_config")
if e.ConfigName != "" {
table = table.Where("configName = ?", e.ConfigName)
}
if e.ConfigKey != "" {
table = table.Where("configKey = ?", e.ConfigKey)
}
if e.ConfigType != "" {
table = table.Where("configType = ?", e.ConfigType)
}
// 数据权限控制
dataPermission := new(DataPermission)
dataPermission.UserId, _ = utils.StringToInt64(e.DataScope)
table = dataPermission.GetDataScope("sys_config", table)
var count int32
if err := table.Where("is_del = 0").Offset((pageIndex - 1) * pageSize).Limit(pageSize).Find(&doc).Error; err != nil {
return nil, 0, err
}
table.Where("is_del = 0").Count(&count)
return doc, count, nil
}
func (e *Config) Update(id int64) (update Config, err error) {
e.UpdateTime = utils.GetCurrntTime()
if err = orm.Eloquent.Table("sys_config").Where("configId = ?", id).First(&update).Error; err != nil {
return
}
//参数1:是要修改的数据
//参数2:是修改的数据
if err = orm.Eloquent.Table("sys_config").Model(&update).Updates(&e).Error; err != nil {
return
}
return
}
func (e *Config) Delete(id int64) (success bool, err error) {
var mp = map[string]string{}
mp["is_del"] = "1"
mp["update_time"] = utils.GetCurrntTime()
mp["update_by"] = e.UpdateBy
if err = orm.Eloquent.Table("sys_config").Where("configId = ?", id).Update(mp).Error; err != nil {
success = false
return
}
success = true
return
}