|
|
@ -1,103 +1,9 @@ |
|
|
|
package main |
|
|
|
|
|
|
|
import ( |
|
|
|
"git.ouxuan.net/hasaki-service/hasaki-sdk/hskdb" |
|
|
|
"git.ouxuan.net/hasaki-service/hasaki-sdk/hskgin" |
|
|
|
) |
|
|
|
|
|
|
|
type MyTestr struct { |
|
|
|
Id int |
|
|
|
} |
|
|
|
|
|
|
|
func (that *MyTestr) where() (q interface{}, args []interface{}) { |
|
|
|
//todo 根据需求修改
|
|
|
|
return "`id`=?", []interface{}{ |
|
|
|
that.Id, |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
func (that *MyTestr) Save(isAllCols ...bool) (err error) { |
|
|
|
tmp := MyTestr{} |
|
|
|
whereQuery, whereArgs := that.where() |
|
|
|
hskdb.GetXormAuto().Where(whereQuery, whereArgs...).Get(&tmp) |
|
|
|
if tmp.Id == 0 { |
|
|
|
_, err = hskdb.GetXormAuto().Insert(that) |
|
|
|
} else { |
|
|
|
if len(isAllCols) > 0 && isAllCols[0] { |
|
|
|
_, err = hskdb.GetXormAuto().AllCols().Where(whereQuery, whereArgs...).Update(that) |
|
|
|
} else { |
|
|
|
_, err = hskdb.GetXormAuto().Where(whereQuery, whereArgs...).Update(that) |
|
|
|
} |
|
|
|
} |
|
|
|
return err |
|
|
|
} |
|
|
|
|
|
|
|
func (that *MyTestr) Load() (err error) { |
|
|
|
whereQuery, whereArgs := that.where() |
|
|
|
_, err = hskdb.GetXormAuto().Where(whereQuery, whereArgs...).Get(&that) |
|
|
|
return |
|
|
|
} |
|
|
|
|
|
|
|
func (that *MyTestr) Data() (data []MyTestr) { |
|
|
|
hskdb.GetXormAuto().OrderBy("id desc").Find(&data) |
|
|
|
return |
|
|
|
} |
|
|
|
|
|
|
|
func (that *MyTestr) PageData(page, pageSize int) (data []MyTestr, count int64) { |
|
|
|
count, _ = hskdb.GetXormAuto().OrderBy("id desc").Limit(pageSize, page*pageSize).FindAndCount(&data) |
|
|
|
return |
|
|
|
} |
|
|
|
|
|
|
|
func (that *MyTestr) Delete() (err error) { |
|
|
|
whereQuery, whereArgs := that.where() |
|
|
|
_, err = hskdb.GetXormAuto().Where(whereQuery, whereArgs...).Delete(&that) |
|
|
|
return |
|
|
|
} |
|
|
|
|
|
|
|
//该方法请不要放在db文件中
|
|
|
|
func InitializationMyTestr(router *hskgin.GinHelper) { |
|
|
|
var authority = func(ctx *hskgin.GinContextHelper) { |
|
|
|
//todo 权限控制
|
|
|
|
} |
|
|
|
|
|
|
|
router.Any("/admin/MyTestr/get", func(ctx *hskgin.GinContextHelper) { |
|
|
|
authority(ctx) |
|
|
|
req := MyTestr{} |
|
|
|
ctx.Bind(&req) |
|
|
|
err := req.Load() |
|
|
|
ctx.CheckErrDisplayByError(err) |
|
|
|
ctx.DisplayByData(req) |
|
|
|
}) |
|
|
|
|
|
|
|
router.Any("/admin/MyTestr/save", func(ctx *hskgin.GinContextHelper) { |
|
|
|
authority(ctx) |
|
|
|
req := MyTestr{} |
|
|
|
ctx.Bind(&req) |
|
|
|
err := req.Save() |
|
|
|
ctx.CheckErrDisplayByError(err) |
|
|
|
err = req.Load() |
|
|
|
ctx.CheckErrDisplayByError(err) |
|
|
|
ctx.DisplayByData(req) |
|
|
|
}) |
|
|
|
|
|
|
|
router.Any("/admin/MyTestr/data", func(ctx *hskgin.GinContextHelper) { |
|
|
|
authority(ctx) |
|
|
|
req := MyTestr{} |
|
|
|
ctx.Bind(&req) |
|
|
|
data := req.Data() |
|
|
|
ctx.DisplayByData(data) |
|
|
|
}) |
|
|
|
|
|
|
|
router.Any("/admin/MyTestr/list", func(ctx *hskgin.GinContextHelper) { |
|
|
|
authority(ctx) |
|
|
|
req := MyTestr{} |
|
|
|
ctx.Bind(&req) |
|
|
|
page := ctx.ParamRequired2Page(true) |
|
|
|
data, count := req.PageData(page, 10) |
|
|
|
ctx.DisplayByPage(data, count) |
|
|
|
}) |
|
|
|
} |
|
|
|
|
|
|
|
type MyTestr2 struct { |
|
|
|
Id int |
|
|
|
} |
|
|
|