forked from appleboy/go-admin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodel.go.template
104 lines (89 loc) · 2.62 KB
/
model.go.template
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
package models
import (
"go-admin/global/orm"
"go-admin/utils"
"time"
)
type {{.ClassName}} struct {
{{ range .Columns -}}
{{$x := .Pk}}
// {{.ColumnComment}}
{{if ($x)}}{{.GoField}} {{.GoType}} `json:"{{.JsonField}}" gorm:"column:{{.ColumnName}};primary_key"`{{else}}{{.GoField}} {{.GoType}} `json:"{{.JsonField}}" gorm:"column:{{.ColumnName}};"`{{end}}
{{ end -}}
}
// 创建{{.ClassName}}
func (e *{{.ClassName}}) Create() ({{.ClassName}}, error) {
var doc {{.ClassName}}
doc.IsDel = "0"
e.CreateTime = time.Now().String()
result := orm.Eloquent.Table("{{.TableName}}").Create(&e)
if result.Error != nil {
err := result.Error
return doc, err
}
doc = *e
return doc, nil
}
// 获取{{.ClassName}}
func (e *{{.ClassName}}) Get() ({{.ClassName}}, error) {
var doc {{.ClassName}}
table := orm.Eloquent.Table("{{.TableName}}")
{{ range .Columns -}}
{{$z := .IsQuery}}
{{- if ($z) -}}if e.{{.GoField}} != "" {
table = table.Where("{{.ColumnName}} = ?", e.{{.GoField}})
}
{{ end }}
{{- end -}}
if err := table.First(&doc).Error; err != nil {
return doc, err
}
return doc, nil
}
// 获取{{.ClassName}}带分页
func (e *{{.ClassName}}) GetPage(pageSize int, pageIndex int) ([]{{.ClassName}}, int32, error) {
var doc []{{.ClassName}}
table := orm.Eloquent.Select("*").Table("{{.TableName}}")
{{ range .Columns -}}
{{$z := .IsQuery}}
{{- if ($z) -}}if e.{{.GoField}} != "" {
table = table.Where("{{.ColumnName}} = ?", e.{{.GoField}})
}
{{ end }}
{{- end -}}
// 数据权限控制
dataPermission := new(DataPermission)
dataPermission.UserId, _ = utils.StringToInt(e.DataScope)
table,err := dataPermission.GetDataScope("{{.TableName}}", table)
if err != nil {
return nil, 0, err
}
var count int32
table = table.Offset((pageIndex - 1) * pageSize).Limit(pageSize)
if err := table.Find(&doc).Error; err != nil {
return nil, 0, err
}
table.Count(&count)
return doc, count, nil
}
// 更新{{.ClassName}}
func (e *{{.ClassName}}) Update(id int) (update {{.ClassName}}, err error) {
if err = orm.Eloquent.Table("{{.TableName}}").Where("{{.PkColumn}} = ?", id).First(&update).Error; err != nil {
return
}
//参数1:是要修改的数据
//参数2:是修改的数据
if err = orm.Eloquent.Table("{{.TableName}}").Model(&update).Updates(&e).Error; err != nil {
return
}
return
}
// 删除{{.ClassName}}
func (e *{{.ClassName}}) Delete(id int) (success bool, err error) {
if err = orm.Eloquent.Table("{{.TableName}}").Where("{{.PkColumn}} = ?", id).Delete(&{{.ClassName}}{}).Error; err != nil {
success = false
return
}
success = true
return
}