update model template
This commit is contained in:
1 parent
8260581be1
commit
570afbbe12
1 file changed
+34
-18
+34
-18
@@ -1,9 +1,9 @@
|
|||||||
package models
|
package models
|
||||||
|
|
||||||
import (
|
import (
|
||||||
orm "go-admin/database"
|
orm "go-admin/database"
|
||||||
"go-admin/utils"
|
"go-admin/tools"
|
||||||
"time"
|
_ "time"
|
||||||
)
|
)
|
||||||
|
|
||||||
type {{.ClassName}} struct {
|
type {{.ClassName}} struct {
|
||||||
@@ -11,12 +11,19 @@ type {{.ClassName}} struct {
|
|||||||
{{$x := .Pk}}
|
{{$x := .Pk}}
|
||||||
{{if ($x)}}{{.GoField}} {{.GoType}} `json:"{{.JsonField}}" gorm:"type:{{.ColumnType}};primary_key"`{{else}}{{.GoField}} {{.GoType}} `json:"{{.JsonField}}" gorm:"type:{{.ColumnType}};"`{{end}} // {{.ColumnComment}}
|
{{if ($x)}}{{.GoField}} {{.GoType}} `json:"{{.JsonField}}" gorm:"type:{{.ColumnType}};primary_key"`{{else}}{{.GoField}} {{.GoType}} `json:"{{.JsonField}}" gorm:"type:{{.ColumnType}};"`{{end}} // {{.ColumnComment}}
|
||||||
{{- end }}
|
{{- end }}
|
||||||
|
DataScope string `json:"dataScope" gorm:"-"`
|
||||||
|
Params string `json:"params" gorm:"-"`
|
||||||
|
BaseModel
|
||||||
|
}
|
||||||
|
|
||||||
|
func ({{.ClassName}}) TableName() string {
|
||||||
|
return "{{.TBName}}"
|
||||||
}
|
}
|
||||||
|
|
||||||
// 创建{{.ClassName}}
|
// 创建{{.ClassName}}
|
||||||
func (e *{{.ClassName}}) Create() ({{.ClassName}}, error) {
|
func (e *{{.ClassName}}) Create() ({{.ClassName}}, error) {
|
||||||
var doc {{.ClassName}}
|
var doc {{.ClassName}}
|
||||||
result := orm.Eloquent.Table("{{.TableName}}").Create(&e)
|
result := orm.Eloquent.Table(e.TableName()).Create(&e)
|
||||||
if result.Error != nil {
|
if result.Error != nil {
|
||||||
err := result.Error
|
err := result.Error
|
||||||
return doc, err
|
return doc, err
|
||||||
@@ -25,11 +32,12 @@ func (e *{{.ClassName}}) Create() ({{.ClassName}}, error) {
|
|||||||
return doc, nil
|
return doc, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// 获取{{.ClassName}}
|
// 获取{{.ClassName}}
|
||||||
func (e *{{.ClassName}}) Get() ({{.ClassName}}, error) {
|
func (e *{{.ClassName}}) Get() ({{.ClassName}}, error) {
|
||||||
var doc {{.ClassName}}
|
var doc {{.ClassName}}
|
||||||
|
|
||||||
table := orm.Eloquent.Table("{{.TableName}}")
|
table := orm.Eloquent.Table(e.TableName())
|
||||||
{{ range .Columns -}}
|
{{ range .Columns -}}
|
||||||
{{$z := .IsQuery}}
|
{{$z := .IsQuery}}
|
||||||
{{- if ($z) -}}
|
{{- if ($z) -}}
|
||||||
@@ -46,10 +54,10 @@ func (e *{{.ClassName}}) Get() ({{.ClassName}}, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// 获取{{.ClassName}}带分页
|
// 获取{{.ClassName}}带分页
|
||||||
func (e *{{.ClassName}}) GetPage(pageSize int, pageIndex int) ([]{{.ClassName}}, int32, error) {
|
func (e *{{.ClassName}}) GetPage(pageSize int, pageIndex int) ([]{{.ClassName}}, int, error) {
|
||||||
var doc []{{.ClassName}}
|
var doc []{{.ClassName}}
|
||||||
|
|
||||||
table := orm.Eloquent.Select("*").Table("{{.TableName}}")
|
table := orm.Eloquent.Select("*").Table(e.TableName())
|
||||||
{{ range .Columns -}}
|
{{ range .Columns -}}
|
||||||
{{$z := .IsQuery}}
|
{{$z := .IsQuery}}
|
||||||
{{- if ($z) -}}
|
{{- if ($z) -}}
|
||||||
@@ -59,14 +67,14 @@ func (e *{{.ClassName}}) GetPage(pageSize int, pageIndex int) ([]{{.ClassName}},
|
|||||||
{{ end -}}
|
{{ end -}}
|
||||||
{{ end }}
|
{{ end }}
|
||||||
|
|
||||||
// 数据权限控制
|
// 数据权限控制(如果不需要数据权限请将此处去掉)
|
||||||
dataPermission := new(DataPermission)
|
dataPermission := new(DataPermission)
|
||||||
dataPermission.UserId, _ = utils.StringToInt(e.DataScope)
|
dataPermission.UserId, _ = tools.StringToInt(e.DataScope)
|
||||||
table = dataPermission.GetDataScope("{{.TableName}}", table)
|
table = dataPermission.GetDataScope(e.TableName(), table)
|
||||||
|
|
||||||
var count int32
|
var count int
|
||||||
table = table.Offset((pageIndex - 1) * pageSize).Limit(pageSize)
|
|
||||||
if err := table.Find(&doc).Error; err != nil {
|
if err := table.Offset((pageIndex - 1) * pageSize).Limit(pageSize).Find(&doc).Error; err != nil {
|
||||||
return nil, 0, err
|
return nil, 0, err
|
||||||
}
|
}
|
||||||
table.Count(&count)
|
table.Count(&count)
|
||||||
@@ -74,22 +82,22 @@ func (e *{{.ClassName}}) GetPage(pageSize int, pageIndex int) ([]{{.ClassName}},
|
|||||||
}
|
}
|
||||||
|
|
||||||
// 更新{{.ClassName}}
|
// 更新{{.ClassName}}
|
||||||
func (e *{{.ClassName}}) Update(id int64) (update {{.ClassName}}, err error) {
|
func (e *{{.ClassName}}) Update(id int) (update {{.ClassName}}, err error) {
|
||||||
if err = orm.Eloquent.Table("{{.TableName}}").Where("{{.PkColumn}} = ?", id).First(&update).Error; err != nil {
|
if err = orm.Eloquent.Table(e.TableName()).Where("{{.PkColumn}} = ?", id).First(&update).Error; err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
//参数1:是要修改的数据
|
//参数1:是要修改的数据
|
||||||
//参数2:是修改的数据
|
//参数2:是修改的数据
|
||||||
if err = orm.Eloquent.Table("{{.TableName}}").Model(&update).Updates(&e).Error; err != nil {
|
if err = orm.Eloquent.Table(e.TableName()).Model(&update).Updates(&e).Error; err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// 删除{{.ClassName}}
|
// 删除{{.ClassName}}
|
||||||
func (e *{{.ClassName}}) Delete(id int64) (success bool, err error) {
|
func (e *{{.ClassName}}) Delete(id int) (success bool, err error) {
|
||||||
if err = orm.Eloquent.Table("{{.TableName}}").Where("{{.PkColumn}} = ?", id).Delete(&{{.ClassName}}{}).Error; err != nil {
|
if err = orm.Eloquent.Table(e.TableName()).Where("{{.PkColumn}} = ?", id).Delete(&{{.ClassName}}{}).Error; err != nil {
|
||||||
success = false
|
success = false
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@@ -97,3 +105,11 @@ func (e *{{.ClassName}}) Delete(id int64) (success bool, err error) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//批量删除
|
||||||
|
func (e *{{.ClassName}}) BatchDelete(id []int) (Result bool, err error) {
|
||||||
|
if err = orm.Eloquent.Table(e.TableName()).Where("{{.PkColumn}} in (?)", id).Delete(&{{.ClassName}}{}).Error; err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
Result = true
|
||||||
|
return
|
||||||
|
}
|
||||||
Reference in new issue
Block a user