126 lines
4.6 KiB
Plaintext
126 lines
4.6 KiB
Plaintext
package models
|
|
|
|
import (
|
|
"ferry/global/orm"
|
|
"ferry/tools"
|
|
_ "time"
|
|
)
|
|
|
|
type {{.ClassName}} struct {
|
|
{{ range .Columns -}}
|
|
{{$x := .Pk}}
|
|
{{- if ($x) }}
|
|
{{.GoField}} {{.GoType}} `json:"{{.JsonField}}" gorm:"type:{{.ColumnType}};primary_key"` // {{.ColumnComment}}
|
|
{{- else if eq .GoField "CreatedAt" -}}
|
|
{{- else if eq .GoField "UpdatedAt" -}}
|
|
{{- else if eq .GoField "DeletedAt" -}}
|
|
{{- else }}
|
|
{{.GoField}} {{.GoType}} `json:"{{.JsonField}}" gorm:"type:{{.ColumnType}};"` // {{.ColumnComment}}{{end -}}
|
|
{{- end }}
|
|
DataScope string `json:"dataScope" gorm:"-"`
|
|
Params string `json:"params" gorm:"-"`
|
|
BaseModel
|
|
}
|
|
|
|
func ({{.ClassName}}) TableName() string {
|
|
return "{{.TBName}}"
|
|
}
|
|
|
|
// 创建{{.ClassName}}
|
|
func (e *{{.ClassName}}) Create() ({{.ClassName}}, error) {
|
|
var doc {{.ClassName}}
|
|
result := orm.Eloquent.Table(e.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(e.TableName())
|
|
{{ range .Columns }}
|
|
{{$x := .Pk}}
|
|
{{- if ($x) }}
|
|
if e.{{.GoField}} != {{if eq .GoType "string" -}} "" {{ else if eq .GoType "int" -}} 0 {{- end}} {
|
|
table = table.Where("{{.ColumnName}}{{if eq .QueryType "EQ"}} = {{else if eq .QueryType "NE"}} != {{else if eq .QueryType "GT"}} > {{else if eq .QueryType "GTE"}} >= {{else if eq .QueryType "LT"}} < {{else if eq .QueryType "LTE"}} <= {{else if eq .QueryType "LIKE"}} like {{end}}?", {{ if eq .QueryType "LIKE"}}"%"+e.{{.GoField}}+"%"{{else}}e.{{.GoField}}{{end}})
|
|
}
|
|
{{- else if .IsQuery }}
|
|
if e.{{.GoField}} != {{if eq .GoType "string" -}} "" {{ else if eq .GoType "int" -}} 0 {{- end}} {
|
|
table = table.Where("{{.ColumnName}}{{if eq .QueryType "EQ"}} = {{else if eq .QueryType "NE"}} != {{else if eq .QueryType "GT"}} > {{else if eq .QueryType "GTE"}} >= {{else if eq .QueryType "LT"}} < {{else if eq .QueryType "LTE"}} <= {{else if eq .QueryType "LIKE"}} like {{end}}?", {{ if eq .QueryType "LIKE"}}"%"+e.{{.GoField}}+"%"{{else}}e.{{.GoField}}{{end}})
|
|
}
|
|
{{ 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}}, int, error) {
|
|
var doc []{{.ClassName}}
|
|
|
|
table := orm.Eloquent.Select("*").Table(e.TableName())
|
|
{{ range .Columns }}
|
|
{{- if .IsQuery }}
|
|
if e.{{.GoField}} != {{if eq .GoType "string" -}} "" {{ else if eq .GoType "int" -}} 0 {{- end}} {
|
|
table = table.Where("{{.ColumnName}}{{if eq .QueryType "EQ"}} = {{else if eq .QueryType "NE"}} != {{else if eq .QueryType "GT"}} > {{else if eq .QueryType "GTE"}} >= {{else if eq .QueryType "LT"}} < {{else if eq .QueryType "LTE"}} <= {{else if eq .QueryType "LIKE"}} like {{end}}?", {{ if eq .QueryType "LIKE"}}"%"+e.{{.GoField}}+"%"{{else}}e.{{.GoField}}{{end}})
|
|
}
|
|
{{ end -}}
|
|
{{- end }}
|
|
|
|
// 数据权限控制(如果不需要数据权限请将此处去掉)
|
|
dataPermission := new(DataPermission)
|
|
dataPermission.UserId, _ = tools.StringToInt(e.DataScope)
|
|
table,err := dataPermission.GetDataScope(e.TableName(), table)
|
|
if err != nil {
|
|
return nil, 0, err
|
|
}
|
|
var count int
|
|
|
|
if err := table.Offset((pageIndex - 1) * pageSize).Limit(pageSize).Find(&doc).Error; err != nil {
|
|
return nil, 0, err
|
|
}
|
|
table.Where("`deleted_at` IS NULL").Count(&count)
|
|
return doc, count, nil
|
|
}
|
|
|
|
// 更新{{.ClassName}}
|
|
func (e *{{.ClassName}}) Update(id int) (update {{.ClassName}}, err error) {
|
|
if err = orm.Eloquent.Table(e.TableName()).Where("{{.PkColumn}} = ?", id).First(&update).Error; err != nil {
|
|
return
|
|
}
|
|
|
|
//参数1:是要修改的数据
|
|
//参数2:是修改的数据
|
|
if err = orm.Eloquent.Table(e.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(e.TableName()).Where("{{.PkColumn}} = ?", id).Delete(&{{.ClassName}}{}).Error; err != nil {
|
|
success = false
|
|
return
|
|
}
|
|
success = true
|
|
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
|
|
}
|