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

Loading…
Cancel
Save