mirror of
https://gitee.com/XM-GO/PandaX.git
synced 2026-04-23 02:48:34 +08:00
【新增】自动生成代码
This commit is contained in:
@@ -1,86 +1,23 @@
|
||||
// ==========================================================================
|
||||
// GFast自动生成dao操作代码,无需手动修改,重新生成不会自动覆盖.
|
||||
// 生成日期:{{.table.CreateTime}}
|
||||
// 生成路径: {{.table.PackageName}}/dao/{{.table.TableName}}.go
|
||||
// 生成人:{{.table.FunctionAuthor}}
|
||||
// 生成日期:{{.CreatedAt}}
|
||||
// 生成路径: apps/{{.PackageName}}/entity/{{.TableName}}.go
|
||||
// 生成人:{{.FunctionAuthor}}
|
||||
// ==========================================================================
|
||||
////
|
||||
package dao
|
||||
|
||||
{{$hasGTime:=false}}
|
||||
{{range $index, $column := .table.Columns}}
|
||||
{{if eq $column.GoType "Time"}}
|
||||
{{$hasGTime = true}}
|
||||
{{end}}
|
||||
{{end}}
|
||||
package entity
|
||||
|
||||
import (
|
||||
comModel "gfast/app/common/model"
|
||||
"{{.table.PackageName}}/dao/internal"
|
||||
{{if $hasGTime}}
|
||||
"github.com/gogf/gf/os/gtime"
|
||||
{{end}}
|
||||
import "pandax/base/model"
|
||||
)
|
||||
|
||||
|
||||
// {{.table.BusinessName | CaseCamelLower}}Dao is the manager for logic model data accessing and custom defined data operations functions management.
|
||||
// You can define custom methods on it to extend its functionality as you wish.
|
||||
type {{.table.BusinessName | CaseCamelLower}}Dao struct {
|
||||
*internal.{{.table.BusinessName | CaseCamel}}Dao
|
||||
type {{.FunctionName}} struct {
|
||||
model.BaseAutoModel
|
||||
{{- range .Columns -}}
|
||||
{{- if eq .IsQuery "1" }}
|
||||
{{.GoField}} {{.GoType}} `json:"{{.JsonField}}" gorm:"type:{{.ColumnType}};comment:{{.ColumnComment}}" binding:"required"`
|
||||
{{- end }}
|
||||
{{- end }}
|
||||
}
|
||||
|
||||
var (
|
||||
// {{.table.ClassName}} is globally public accessible object for table tools_gen_table operations.
|
||||
{{.table.ClassName}} = {{.table.BusinessName | CaseCamelLower}}Dao{
|
||||
internal.New{{.table.ClassName}}Dao(),
|
||||
}
|
||||
)
|
||||
|
||||
////
|
||||
// Fill with you ideas below.
|
||||
////
|
||||
|
||||
|
||||
// {{.table.ClassName}}SearchReq 分页请求参数
|
||||
type {{.table.ClassName}}SearchReq struct {
|
||||
{{range $index, $column := .table.Columns}}
|
||||
{{if eq $column.IsQuery "1"}}
|
||||
{{$column.GoField}} {{if eq $column.GoType "Time"}}*gtime.Time{{else if or (eq $column.GoType "int") (eq $column.GoType "int64") (eq $column.GoType "uint") (eq $column.GoType "uint64")}}string{{else}}{{$column.GoType}}{{end}} `p:"{{$column.HtmlField}}"` //{{$column.ColumnComment}}
|
||||
{{end}}
|
||||
{{end}}
|
||||
comModel.PageReq
|
||||
func ({{.FunctionName}}) TableName() string {
|
||||
return "{{.TableName}}"
|
||||
}
|
||||
|
||||
|
||||
// {{.table.ClassName}}AddReq 添加操作请求参数
|
||||
type {{.table.ClassName}}AddReq struct {
|
||||
{{range $index, $column := .table.Columns}}
|
||||
{{if and (eq $column.IsInsert "1") (ne $column.IsPk "1")}}
|
||||
{{$column.GoField}} {{if eq $column.GoType "Time"}}*gtime.Time{{else}}{{$column.GoType}}{{end}} `p:"{{$column.HtmlField}}" {{if eq $column.IsRequired "1"}}v:"required#{{$column.ColumnComment}}不能为空"{{end}}`
|
||||
{{end}}
|
||||
{{if eq $column.ColumnName "created_by"}}CreatedBy uint64 {{end}}
|
||||
{{end}}
|
||||
}
|
||||
|
||||
|
||||
// {{.table.ClassName}}EditReq 修改操作请求参数
|
||||
type {{.table.ClassName}}EditReq struct {
|
||||
{{.table.PkColumn.GoField}} {{.table.PkColumn.GoType}} `p:"{{.table.PkColumn.HtmlField}}" v:"required#主键ID不能为空"`
|
||||
{{range $index, $column := .table.Columns}}
|
||||
{{if eq $column.IsEdit "1"}}
|
||||
{{$column.GoField}} {{if eq $column.GoType "Time"}}*gtime.Time{{else}}{{$column.GoType}}{{end}} `p:"{{$column.HtmlField}}" {{if eq $column.IsRequired "1"}}v:"required#{{$column.ColumnComment}}不能为空"{{end}}`{{end}}
|
||||
{{if eq $column.ColumnName "updated_by"}}UpdatedBy uint64 {{end}}
|
||||
{{end}}
|
||||
}
|
||||
|
||||
|
||||
|
||||
{{range $index,$column:= .table.Columns}}
|
||||
{{if and (HasSuffix $column.ColumnName "status") (eq $column.IsList "1") }}
|
||||
// {{$.table.ClassName}}{{$column.GoField}}Req 设置用户状态参数
|
||||
type {{$.table.ClassName}}{{$column.GoField}}Req struct {
|
||||
{{$.table.PkColumn.GoField}} {{$.table.PkColumn.GoType}} `p:"{{$.table.PkColumn.HtmlField}}" v:"required#主键ID不能为空"`
|
||||
{{$column.GoField}} {{$column.GoType}} `p:"{{$column.HtmlField}}" v:"required#{{$column.ColumnComment}}不能为空"`
|
||||
}
|
||||
{{end}}
|
||||
{{end}}
|
||||
@@ -1,198 +1,86 @@
|
||||
// ==========================================================================
|
||||
// Panda自动生成业务逻辑层相关代码,只生成一次,按需修改,再次生成不会覆盖.
|
||||
// 生成日期:{{.table.CreateTime}}
|
||||
// 生成路径: {{.table.PackageName}}/service/{{.table.BusinessName}}.go
|
||||
// 生成人:{{.table.FunctionAuthor}}
|
||||
// 生成日期:{{.CreatedAt}}
|
||||
// 生成路径: apps/{{.PackageName}}/services/{{.TableName}}.go
|
||||
// 生成人:{{.FunctionAuthor}}
|
||||
// ==========================================================================
|
||||
////
|
||||
{{$structName := .table.BusinessName | CaseCamelLower}}
|
||||
|
||||
package service
|
||||
|
||||
|
||||
import (
|
||||
"context"
|
||||
comModel "gfast/app/common/model"
|
||||
"pandax/apps/{{.table.PackageName}}/entity"
|
||||
"pandax/base/biz"
|
||||
"pandax/apps/{{.PackageName}}/entity"
|
||||
"pandax/base/biz"
|
||||
"pandax/base/global"
|
||||
)
|
||||
|
||||
type (
|
||||
{{.FunctionName}}Model interface {
|
||||
Insert(data entity.{{.FunctionName}}) *entity.{{.FunctionName}}
|
||||
FindOne({{.PkJsonField}} int64) *entity.{{.FunctionName}}
|
||||
FindListPage(page, pageSize int, data entity.{{.FunctionName}}) (*[]entity.{{.FunctionName}}, int64)
|
||||
FindList(data entity.{{ .FunctionName }}) *[]entity.{{.FunctionName}}
|
||||
Update(data entity.{{.FunctionName}}) *entity.{{.FunctionName}}
|
||||
Delete({{.PkJsonField}}s []int64)
|
||||
}
|
||||
|
||||
type {{$structName}} struct {
|
||||
{{.BusinessName}}ModelImpl struct {
|
||||
table string
|
||||
}
|
||||
)
|
||||
|
||||
{{$model := .FunctionName }}
|
||||
|
||||
var {{.FunctionName}}ModelDao {{.FunctionName}}Model = &{{.BusinessName}}ModelImpl{
|
||||
table: `{{.TableName}}`,
|
||||
}
|
||||
|
||||
var {{.table.ClassName}} = new({{$structName}})
|
||||
|
||||
|
||||
{{$pk:=""}}
|
||||
{{$pkGoField:=""}}
|
||||
|
||||
{{$createdAt:=""}}
|
||||
{{$createdAtGoField:=""}}
|
||||
|
||||
{{range $index, $column := .table.Columns}}
|
||||
{{if eq $column.IsPk "1"}}
|
||||
{{$pk = $column.ColumnName}}
|
||||
{{$pkGoField = $column.GoField}}
|
||||
{{end}}
|
||||
{{if eq $column.ColumnName "created_at"}}
|
||||
{{$createdAt = $column.ColumnName}}
|
||||
{{$createdAtGoField = $column.GoField}}
|
||||
{{end}}
|
||||
{{end}}
|
||||
|
||||
// GetList 获取任务列表
|
||||
func (s *{{$structName}}) GetList(req *dao.{{.table.ClassName}}SearchReq) (total, page int, list []*model.{{.table.ClassName}}, err error) {
|
||||
m := dao.{{.table.ClassName}}.Ctx(req.Ctx)
|
||||
{{range $index, $column := .table.Columns}} {{if eq $column.IsQuery "1"}}
|
||||
{{if eq $column.QueryType "LIKE"}}
|
||||
if req.{{$column.GoField}} != "" {
|
||||
m = m.Where(dao.{{$.table.ClassName}}.Columns.{{$column.GoField}}+" like ?", "%"+req.{{$column.GoField}}+"%")
|
||||
} {{end}}
|
||||
{{if eq $column.QueryType "EQ"}} {{if eq $column.GoType "string"}}
|
||||
if req.{{$column.GoField}} != "" {
|
||||
m = m.Where(dao.{{$.table.ClassName}}.Columns.{{$column.GoField}}+" = ?", req.{{$column.GoField}})
|
||||
}
|
||||
{{else if and (eq $column.GoType "Time") (eq $column.ColumnName "created_at")}}
|
||||
if req.BeginTime != "" {
|
||||
m = m.Where(dao.{{$.table.ClassName}}.Columns.{{$column.GoField}}+" >=", req.BeginTime)
|
||||
}
|
||||
if req.EndTime != "" {
|
||||
m = m.Where(dao.{{$.table.ClassName}}.Columns.{{$column.GoField}}+" <", req.EndTime)
|
||||
}
|
||||
{{else if or (eq $column.GoType "int") (eq $column.GoType "int64") (eq $column.GoType "uint") (eq $column.GoType "uint64") }}
|
||||
if req.{{$column.GoField}} != "" {
|
||||
m = m.Where(dao.{{$.table.ClassName}}.Columns.{{$column.GoField}}+" = ?", req.{{$column.GoField}})
|
||||
}
|
||||
{{end}} {{end}}
|
||||
{{if and (eq $column.QueryType "BETWEEN") (eq $column.ColumnType "datetime") }}
|
||||
if req.{{$column.GoField}} != nil {
|
||||
m = m.Where(dao.{{$.table.ClassName}}.Columns.{{$column.GoField}}+" >= ? AND "+dao.{{$.table.ClassName}}.Columns.{{$column.GoField}}+" < ?", req.{{$column.GoField}}, req.{{$column.GoField}}.Add(gtime.D))
|
||||
}
|
||||
{{end}}
|
||||
{{end}}
|
||||
{{end}}
|
||||
|
||||
total, err = m.Count()
|
||||
if err != nil {
|
||||
g.Log().Error(err)
|
||||
err = gerror.New("获取总行数失败")
|
||||
return
|
||||
}
|
||||
{{if ne .table.TplCategory "tree"}}
|
||||
if req.PageNum == 0 {
|
||||
req.PageNum = 1
|
||||
}
|
||||
page = req.PageNum
|
||||
if req.PageSize == 0 {
|
||||
req.PageSize = comModel.PageSize
|
||||
}
|
||||
order:= "{{$pk}} asc"
|
||||
if req.OrderBy!=""{
|
||||
order = req.OrderBy
|
||||
}
|
||||
err = m.Page(page, req.PageSize).Order(order).Scan(&list)
|
||||
{{else}}
|
||||
order:= "{{$pk}} asc"
|
||||
if req.OrderBy!=""{
|
||||
order = req.OrderBy
|
||||
}
|
||||
err = m.Order(order).Scan(&list)
|
||||
{{end}}
|
||||
if err != nil {
|
||||
g.Log().Error(err)
|
||||
err = gerror.New("获取数据失败")
|
||||
}
|
||||
return
|
||||
func (m *{{.BusinessName}}ModelImpl) Insert(data entity.{{$model}}) *entity.{{$model}} {
|
||||
err := global.Db.Table(m.table).Create(&data).Error
|
||||
biz.ErrIsNil(err, "添加{{.TableComment}}失败")
|
||||
return &data
|
||||
}
|
||||
|
||||
|
||||
// GetInfoById 通过id获取
|
||||
func (s *{{$structName}}) GetInfoById(ctx context.Context,id {{$.table.PkColumn.GoType}}) (info *model.{{.table.ClassName}}, err error) {
|
||||
if id == 0 {
|
||||
err = gerror.New("参数错误")
|
||||
return
|
||||
}
|
||||
err = dao.{{.table.ClassName}}.Ctx(ctx).Where(dao.{{.table.ClassName}}.Columns.{{$pkGoField}}, id).Scan(&info)
|
||||
if err != nil {
|
||||
g.Log().Error(err)
|
||||
}
|
||||
if info == nil || err != nil {
|
||||
err = gerror.New("获取信息失败")
|
||||
}
|
||||
return
|
||||
func (m *{{.BusinessName}}ModelImpl) FindOne({{.PkJsonField}} int64) *entity.{{$model}} {
|
||||
resData := new(entity.{{$model}})
|
||||
err := global.Db.Table(m.table).Where("{{.PkColumn}} = ?", {{.PkJsonField}}).First(resData).Error
|
||||
biz.ErrIsNil(err, "查询{{.TableComment}}失败")
|
||||
return resData
|
||||
}
|
||||
|
||||
// Add 添加
|
||||
func (s *{{$structName}}) Add(ctx context.Context,req *dao.{{.table.ClassName}}AddReq) (err error) {
|
||||
_, err = dao.{{.table.ClassName}}.Ctx(ctx).Insert(req)
|
||||
return
|
||||
func (m *{{.BusinessName}}ModelImpl) FindListPage(page, pageSize int, data entity.{{$model}}) (*[]entity.{{$model}}, int64) {
|
||||
list := make([]entity.{{$model}}, 0)
|
||||
var total int64 = 0
|
||||
offset := pageSize * (page - 1)
|
||||
db := global.Db.Table(m.table)
|
||||
// 此处填写 where参数判断
|
||||
if data.{{.PkGoField}} != 0 {
|
||||
db = db.Where("{{.PkColumn}} = ?", data.{{.PkGoField}})
|
||||
}
|
||||
|
||||
db.Where("delete_time IS NULL")
|
||||
err := db.Count(&total).Error
|
||||
err = db.Order("create_time").Limit(pageSize).Offset(offset).Find(&list).Error
|
||||
biz.ErrIsNil(err, "查询{{.TableComment}}分页列表失败")
|
||||
return &list, total
|
||||
}
|
||||
|
||||
// Edit 修改
|
||||
func (s *{{$structName}}) Edit(ctx context.Context,req *dao.{{.table.ClassName}}EditReq) error {
|
||||
{{ $fieldsEx:= concat "dao." $.table.ClassName ".Columns." $pkGoField }}
|
||||
{{if ne $createdAt ""}}
|
||||
{{$fieldsEx = concat "dao." $.table.ClassName ".Columns." $pkGoField "," "dao." $.table.ClassName ".Columns." $createdAtGoField}}
|
||||
{{end}}
|
||||
_, err := dao.{{.table.ClassName}}.Ctx(ctx).FieldsEx({{$fieldsEx}}).Where(dao.{{.table.ClassName}}.Columns.{{$pkGoField}}, req.{{$pkGoField}}).
|
||||
Update(req)
|
||||
return err
|
||||
func (m *{{.BusinessName}}ModelImpl) FindList(data entity.{{$model}}) *[]entity.{{$model}} {
|
||||
list := make([]entity.{{$model}}, 0)
|
||||
db := global.Db.Table(m.table)
|
||||
// 此处填写 where参数判断
|
||||
if data.{{.PkGoField}} != 0 {
|
||||
db = db.Where("{{.PkColumn}} = ?", data.{{.PkGoField}})
|
||||
}
|
||||
|
||||
db.Where("delete_time IS NULL")
|
||||
biz.ErrIsNil(db.Order("create_time").Find(&list).Error, "查询{{.TableComment}}列表失败")
|
||||
return &list
|
||||
}
|
||||
|
||||
|
||||
// DeleteByIds 删除
|
||||
func (s *{{$structName}}) DeleteByIds(ctx context.Context,ids []int) (err error) {
|
||||
if len(ids) == 0 {
|
||||
err = gerror.New("参数错误")
|
||||
return
|
||||
}
|
||||
{{if eq .table.TplCategory "tree"}}
|
||||
ids, err = s.GetChildrenIds(ctx,ids)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
{{end}}
|
||||
_, err = dao.{{.table.ClassName}}.Ctx(ctx).Delete(dao.{{.table.ClassName}}.Columns.{{$pkGoField}}+" in (?)", ids)
|
||||
if err != nil {
|
||||
g.Log().Error(err)
|
||||
err = gerror.New("删除失败")
|
||||
}
|
||||
return
|
||||
func (m *{{.BusinessName}}ModelImpl) Update(data entity.{{$model}}) *entity.{{$model}} {
|
||||
biz.ErrIsNil(global.Db.Table(m.table).Updates(&data).Error, "修改{{.TableComment}}失败")
|
||||
return &data
|
||||
}
|
||||
|
||||
|
||||
{{range $index,$column:= .table.Columns}}
|
||||
{{if and (HasSuffix $column.ColumnName "status") (eq $column.IsList "1") }}
|
||||
// Change{{$column.GoField}} 修改状态
|
||||
func (s *{{$structName}}) Change{{$column.GoField}}(ctx context.Context,req *dao.{{$.table.ClassName}}{{$column.GoField}}Req) error {
|
||||
_, err := dao.{{$.table.ClassName}}.Ctx(ctx).WherePri(req.{{$pkGoField}}).Update(g.Map{
|
||||
dao.{{$.table.ClassName}}.Columns.{{$column.GoField}}: req.{{$column.GoField}},
|
||||
})
|
||||
return err
|
||||
func (m *{{.BusinessName}}ModelImpl) Delete({{.PkJsonField}}s []int64) {
|
||||
biz.ErrIsNil(global.Db.Table(m.table).Delete(&entity.{{$model}}{}, "{{.PkColumn}} in (?)", {{.PkJsonField}}s).Error, "删除{{.TableComment}}失败")
|
||||
}
|
||||
{{end}}
|
||||
{{end}}
|
||||
|
||||
{{if eq .table.TplCategory "tree"}}
|
||||
// GetChildrenIds 通过ID获取子级ID
|
||||
func (s *{{$structName}})GetChildrenIds(ctx context.Context,ids []int) ([]int, error) {
|
||||
//获取所有
|
||||
_,_,all, err := s.GetList(&dao.{{.table.ClassName}}SearchReq{PageReq:comModel.PageReq{Ctx: ctx}})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
list := make(g.List, len(all))
|
||||
for k, info := range all {
|
||||
list[k] = gconv.Map(info)
|
||||
}
|
||||
for _, id := range ids {
|
||||
children := library.FindSonByParentId(list, id, "{{.table.TreeParentCode}}", "{{.table.TreeCode}}")
|
||||
for _, cid := range children {
|
||||
ids = append(ids, gconv.Int(cid["{{.table.TreeCode}}"]))
|
||||
}
|
||||
}
|
||||
return ids, nil
|
||||
}
|
||||
{{end}}
|
||||
Reference in New Issue
Block a user