public_host 4 years ago
parent
commit
da37e21e63
  1. 107
      hasaki-gen-crud/template.go

107
hasaki-gen-crud/template.go

@ -3,11 +3,13 @@ package main
import ( import (
"git.ouxuan.net/hasaki-service/hasaki-sdk/hskdb" "git.ouxuan.net/hasaki-service/hasaki-sdk/hskdb"
"git.ouxuan.net/hasaki-service/hasaki-sdk/hskgin" "git.ouxuan.net/hasaki-service/hasaki-sdk/hskgin"
"xorm.io/builder"
"xorm.io/xorm" "xorm.io/xorm"
) )
type Temp3A60 struct { type Temp3A60 struct {
Id int
Id int
service string
} }
// ----------------------------temp------------------------------------ // ----------------------------temp------------------------------------
@ -20,6 +22,18 @@ func (TempLowHead3A60 *Temp3A60) wherePrimaryKey() (q interface{}, args []interf
} }
} }
//wherePrimaryKeyWithCond 主键查询
func (TempLowHead3A60 *Temp3A60) wherePrimaryKeyWithCond() builder.Cond {
//todo 根据需求修改
return nil
}
//whereDataWithCond 列表查询
func (TempLowHead3A60 *Temp3A60) whereDataWithCond() builder.Cond {
//todo 根据需求修改
return nil
}
//whereData 列表查询筛选 //whereData 列表查询筛选
func (TempLowHead3A60 *Temp3A60) whereData() (q interface{}, args []interface{}) { func (TempLowHead3A60 *Temp3A60) whereData() (q interface{}, args []interface{}) {
//todo //todo
@ -42,8 +56,14 @@ func (TempLowHead3A60 *Temp3A60) isSaveAllCols() bool {
return false return false
} }
//savePretreatment 保存前预处理
func (TempLowHead3A60 *Temp3A60) savePretreatment() error {
//insertPretreatment 新增前预处理
func (TempLowHead3A60 *Temp3A60) insertPretreatment() error {
//todo
return nil
}
//updatePretreatment 修改前预处理
func (TempLowHead3A60 *Temp3A60) updatePretreatment() error {
//todo //todo
return nil return nil
} }
@ -63,16 +83,32 @@ func (TempLowHead3A60 *Temp3A60) getXorm() *xorm.Engine {
//Save 保存或者新增,通过wherePrimaryKey查不到数据时,新增数据,否则修改数据,参数:isAllCols... //Save 保存或者新增,通过wherePrimaryKey查不到数据时,新增数据,否则修改数据,参数:isAllCols...
func (TempLowHead3A60 *Temp3A60) Save(isAllCols ...bool) (err error) { func (TempLowHead3A60 *Temp3A60) Save(isAllCols ...bool) (err error) {
if TempLowHead3A60.service == "" {
TempLowHead3A60.service = "none"
}
tmp := Temp3A60{} tmp := Temp3A60{}
whereQuery, whereArgs := TempLowHead3A60.wherePrimaryKey() whereQuery, whereArgs := TempLowHead3A60.wherePrimaryKey()
TempLowHead3A60.getXorm().Where(whereQuery, whereArgs...).Get(&tmp)
err = TempLowHead3A60.savePretreatment()
whereCond := TempLowHead3A60.wherePrimaryKeyWithCond()
session := TempLowHead3A60.getXorm().NewSession()
defer session.Close()
if whereQuery != "" {
session = session.Where(whereQuery, whereArgs...)
}
if whereCond != nil {
session = session.Where(whereCond)
}
_, err = session.Get(&tmp)
if err != nil { if err != nil {
return return
} }
if tmp.Id == 0 { if tmp.Id == 0 {
err = TempLowHead3A60.insertPretreatment()
_, err = TempLowHead3A60.getXorm().Insert(TempLowHead3A60) _, err = TempLowHead3A60.getXorm().Insert(TempLowHead3A60)
} else { } else {
err = TempLowHead3A60.updatePretreatment()
if len(isAllCols) > 0 { if len(isAllCols) > 0 {
if isAllCols[0] { if isAllCols[0] {
_, err = TempLowHead3A60.getXorm().AllCols().Where(whereQuery, whereArgs...).Update(TempLowHead3A60) _, err = TempLowHead3A60.getXorm().AllCols().Where(whereQuery, whereArgs...).Update(TempLowHead3A60)
@ -92,24 +128,53 @@ func (TempLowHead3A60 *Temp3A60) Save(isAllCols ...bool) (err error) {
//Load 为实例载入数据,数据来源取决于wherePrimaryKey查找实例中的数据 //Load 为实例载入数据,数据来源取决于wherePrimaryKey查找实例中的数据
func (TempLowHead3A60 *Temp3A60) Load() (err error) { func (TempLowHead3A60 *Temp3A60) Load() (err error) {
if TempLowHead3A60.service == "" {
TempLowHead3A60.service = "none"
}
whereQuery, whereArgs := TempLowHead3A60.wherePrimaryKey() whereQuery, whereArgs := TempLowHead3A60.wherePrimaryKey()
_, err = TempLowHead3A60.getXorm().Where(whereQuery, whereArgs...).Get(TempLowHead3A60)
whereCond := TempLowHead3A60.wherePrimaryKeyWithCond()
session := TempLowHead3A60.getXorm().NewSession()
defer session.Close()
if whereQuery != "" {
session = session.Where(whereQuery, whereArgs...)
}
if whereCond != nil {
session = session.Where(whereCond)
}
_, err = session.Get(TempLowHead3A60)
if err != nil {
return
}
TempLowHead3A60.init() TempLowHead3A60.init()
return return
} }
//Data 不进行分页的数据列表 //Data 不进行分页的数据列表
func (TempLowHead3A60 *Temp3A60) Data() (data []Temp3A60) { func (TempLowHead3A60 *Temp3A60) Data() (data []Temp3A60) {
if TempLowHead3A60.service == "" {
TempLowHead3A60.service = "none"
}
q, args := TempLowHead3A60.whereData() q, args := TempLowHead3A60.whereData()
_xorm := TempLowHead3A60.getXorm() _xorm := TempLowHead3A60.getXorm()
session := _xorm.NewSession() session := _xorm.NewSession()
defer func() { defer func() {
session.Close() session.Close()
}() }()
if q != "" { if q != "" {
session = session.Where(q, args...) session = session.Where(q, args...)
} }
whereCond := TempLowHead3A60.whereDataWithCond()
if whereCond != nil {
session = session.Where(whereCond)
}
order := TempLowHead3A60.orderData() order := TempLowHead3A60.orderData()
if order != "" { if order != "" {
session = session.OrderBy(order) session = session.OrderBy(order)
@ -124,6 +189,10 @@ func (TempLowHead3A60 *Temp3A60) Data() (data []Temp3A60) {
//PageData 进行分页的数据列表,page从0开始 //PageData 进行分页的数据列表,page从0开始
func (TempLowHead3A60 *Temp3A60) PageData(page, pageSize int) (data []Temp3A60, count int64) { func (TempLowHead3A60 *Temp3A60) PageData(page, pageSize int) (data []Temp3A60, count int64) {
if TempLowHead3A60.service == "" {
TempLowHead3A60.service = "none"
}
q, args := TempLowHead3A60.whereData() q, args := TempLowHead3A60.whereData()
_xorm := TempLowHead3A60.getXorm() _xorm := TempLowHead3A60.getXorm()
session := _xorm.NewSession() session := _xorm.NewSession()
@ -134,6 +203,11 @@ func (TempLowHead3A60 *Temp3A60) PageData(page, pageSize int) (data []Temp3A60,
session = session.Where(q, args...) session = session.Where(q, args...)
} }
whereCond := TempLowHead3A60.whereDataWithCond()
if whereCond != nil {
session = session.Where(whereCond)
}
order := TempLowHead3A60.orderData() order := TempLowHead3A60.orderData()
if order != "" { if order != "" {
session = session.OrderBy(order) session = session.OrderBy(order)
@ -148,12 +222,26 @@ func (TempLowHead3A60 *Temp3A60) PageData(page, pageSize int) (data []Temp3A60,
//Delete 删除数据 //Delete 删除数据
func (TempLowHead3A60 *Temp3A60) Delete() (err error) { func (TempLowHead3A60 *Temp3A60) Delete() (err error) {
if TempLowHead3A60.service == "" {
TempLowHead3A60.service = "none"
}
whereQuery, whereArgs := TempLowHead3A60.wherePrimaryKey() whereQuery, whereArgs := TempLowHead3A60.wherePrimaryKey()
whereCond := TempLowHead3A60.wherePrimaryKeyWithCond()
session := TempLowHead3A60.getXorm().NewSession()
defer session.Close()
if whereQuery != "" {
session = session.Where(whereQuery, whereArgs...)
}
if whereCond != nil {
session = session.Where(whereCond)
}
err = TempLowHead3A60.deletePretreatment() err = TempLowHead3A60.deletePretreatment()
if err != nil { if err != nil {
return return
} }
_, err = TempLowHead3A60.getXorm().Where(whereQuery, whereArgs...).Delete(TempLowHead3A60)
_, err = session.Delete(TempLowHead3A60)
return return
} }
@ -170,6 +258,7 @@ func InitializationHttpCrudPrefixHigHead3A60Temp3A60(router *hskgin.GinHelper, a
req := &Temp3A60{} req := &Temp3A60{}
ctx.Bind(req) ctx.Bind(req)
authority(ctx, req) authority(ctx, req)
req.service = "Prefix3A60"
err := req.Load() err := req.Load()
ctx.CheckErrDisplayByError(err) ctx.CheckErrDisplayByError(err)
ctx.DisplayByData(req) ctx.DisplayByData(req)
@ -179,6 +268,7 @@ func InitializationHttpCrudPrefixHigHead3A60Temp3A60(router *hskgin.GinHelper, a
req := &Temp3A60{} req := &Temp3A60{}
ctx.Bind(&req) ctx.Bind(&req)
authority(ctx, req) authority(ctx, req)
req.service = "Prefix3A60"
err := req.Save() err := req.Save()
ctx.CheckErrDisplayByError(err) ctx.CheckErrDisplayByError(err)
err = req.Load() err = req.Load()
@ -190,6 +280,7 @@ func InitializationHttpCrudPrefixHigHead3A60Temp3A60(router *hskgin.GinHelper, a
req := &Temp3A60{} req := &Temp3A60{}
ctx.Bind(&req) ctx.Bind(&req)
authority(ctx, req) authority(ctx, req)
req.service = "Prefix3A60"
data := req.Data() data := req.Data()
ctx.DisplayByData(data) ctx.DisplayByData(data)
}) })
@ -199,6 +290,7 @@ func InitializationHttpCrudPrefixHigHead3A60Temp3A60(router *hskgin.GinHelper, a
ctx.Bind(&req) ctx.Bind(&req)
authority(ctx, req) authority(ctx, req)
page := ctx.ParamRequired2Page(true) page := ctx.ParamRequired2Page(true)
req.service = "Prefix3A60"
data, count := req.PageData(page, 10) data, count := req.PageData(page, 10)
ctx.DisplayByPage(data, count) ctx.DisplayByPage(data, count)
}) })
@ -207,6 +299,7 @@ func InitializationHttpCrudPrefixHigHead3A60Temp3A60(router *hskgin.GinHelper, a
req := &Temp3A60{} req := &Temp3A60{}
ctx.Bind(&req) ctx.Bind(&req)
authority(ctx, req) authority(ctx, req)
req.service = "Prefix3A60"
err := req.Delete() err := req.Delete()
ctx.CheckErrDisplayByError(err) ctx.CheckErrDisplayByError(err)
ctx.DisplayBySuccess("删除成功") ctx.DisplayBySuccess("删除成功")

Loading…
Cancel
Save