public_host 4 years ago
parent
commit
45256e6787
  1. 7
      hasaki-gen-crud/hasaki-gen-crud.js
  2. 64
      hasaki-gen-crud/template.go
  3. 5
      main.go

7
hasaki-gen-crud/hasaki-gen-crud.js

@ -164,12 +164,15 @@ try {
let httpFileName = path.join(fileDir, `auto_generated_http_crud_${underscore_naming}_${struct}_${fileBaseName}`) let httpFileName = path.join(fileDir, `auto_generated_http_crud_${underscore_naming}_${struct}_${fileBaseName}`)
fs.writeFile(crudFileName, `${package_src}\n\n${tmp[2]}`, function () {
let mark = `/*该文件由插件自动生成,无特殊情况请勿改动*/`;
fs.writeFile(crudFileName, `${mark}\n${package_src}\n\n${tmp[2]}`, function () {
goplsfix(crudFileName); goplsfix(crudFileName);
}) })
fs.writeFile(httpFileName, `${package_src}\n\n${tmp[3]}`, function () {
fs.writeFile(httpFileName, `${mark}\n${package_src}\n\n${tmp[3]}`, function () {
goplsfix(httpFileName); goplsfix(httpFileName);
}) })

64
hasaki-gen-crud/template.go

@ -12,7 +12,7 @@ type Temp3A60 struct {
// ----------------------------temp------------------------------------ // ----------------------------temp------------------------------------
//wherePrimaryKey
//wherePrimaryKey 主键查询
func (TempLowHead3A60 *Temp3A60) wherePrimaryKey() (q interface{}, args []interface{}) { func (TempLowHead3A60 *Temp3A60) wherePrimaryKey() (q interface{}, args []interface{}) {
//todo 根据需求修改 //todo 根据需求修改
return "`id`=?", []interface{}{ return "`id`=?", []interface{}{
@ -20,16 +20,41 @@ func (TempLowHead3A60 *Temp3A60) wherePrimaryKey() (q interface{}, args []interf
} }
} }
//whereData 列表查询筛选
func (TempLowHead3A60 *Temp3A60) whereData() (q interface{}, args []interface{}) {
//todo
return "", nil
}
//orderData 列表查询排序
func (TempLowHead3A60 *Temp3A60) orderData() string {
//todo
return "`id` desc"
}
//init 数据初始化,可附加更多数据
func (TempLowHead3A60 *Temp3A60) init() {
//todo
}
//savePretreatment 保存前预处理
func (TempLowHead3A60 *Temp3A60) savePretreatment() {
//todo
}
func (TempLowHead3A60 *Temp3A60) getXorm() *xorm.Engine { func (TempLowHead3A60 *Temp3A60) getXorm() *xorm.Engine {
//todo
return hskdb.GetXormAuto() return hskdb.GetXormAuto()
} }
// ----------------------------temp------------------------------------ // ----------------------------temp------------------------------------
//Save 保存或者新增,通过wherePrimaryKey查不到数据时,新增数据,否则修改数据,参数:isAllCols...
func (TempLowHead3A60 *Temp3A60) Save(isAllCols ...bool) (err error) { func (TempLowHead3A60 *Temp3A60) Save(isAllCols ...bool) (err error) {
tmp := Temp3A60{} tmp := Temp3A60{}
whereQuery, whereArgs := TempLowHead3A60.wherePrimaryKey() whereQuery, whereArgs := TempLowHead3A60.wherePrimaryKey()
TempLowHead3A60.getXorm().Where(whereQuery, whereArgs...).Get(&tmp) TempLowHead3A60.getXorm().Where(whereQuery, whereArgs...).Get(&tmp)
TempLowHead3A60.savePretreatment()
if tmp.Id == 0 { if tmp.Id == 0 {
_, err = TempLowHead3A60.getXorm().Insert(TempLowHead3A60) _, err = TempLowHead3A60.getXorm().Insert(TempLowHead3A60)
} else { } else {
@ -42,22 +67,55 @@ func (TempLowHead3A60 *Temp3A60) Save(isAllCols ...bool) (err error) {
return err return err
} }
//Load 为实例载入数据,数据来源取决于wherePrimaryKey查找实例中的数据
func (TempLowHead3A60 *Temp3A60) Load() (err error) { func (TempLowHead3A60 *Temp3A60) Load() (err error) {
whereQuery, whereArgs := TempLowHead3A60.wherePrimaryKey() whereQuery, whereArgs := TempLowHead3A60.wherePrimaryKey()
_, err = TempLowHead3A60.getXorm().Where(whereQuery, whereArgs...).Get(TempLowHead3A60) _, err = TempLowHead3A60.getXorm().Where(whereQuery, whereArgs...).Get(TempLowHead3A60)
TempLowHead3A60.init()
return return
} }
//Data 不进行分页的数据列表
func (TempLowHead3A60 *Temp3A60) Data() (data []Temp3A60) { func (TempLowHead3A60 *Temp3A60) Data() (data []Temp3A60) {
TempLowHead3A60.getXorm().OrderBy("id desc").Find(&data)
q, args := TempLowHead3A60.whereData()
_xorm := TempLowHead3A60.getXorm()
if q != "" {
_xorm.Where(q, args...)
}
order := TempLowHead3A60.orderData()
if order != "" {
_xorm.OrderBy(order)
}
_xorm.Find(&data)
for i := range data {
data[i].init()
}
return return
} }
//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) {
count, _ = TempLowHead3A60.getXorm().OrderBy("id desc").Limit(pageSize, page*pageSize).FindAndCount(&data)
q, args := TempLowHead3A60.whereData()
_xorm := TempLowHead3A60.getXorm()
if q != "" {
_xorm.Where(q, args...)
}
order := TempLowHead3A60.orderData()
if order != "" {
_xorm.OrderBy(order)
}
count, _ = _xorm.Limit(pageSize, page*pageSize).FindAndCount(&data)
for i := range data {
data[i].init()
}
return return
} }
//Delete 删除数据
func (TempLowHead3A60 *Temp3A60) Delete() (err error) { func (TempLowHead3A60 *Temp3A60) Delete() (err error) {
whereQuery, whereArgs := TempLowHead3A60.wherePrimaryKey() whereQuery, whereArgs := TempLowHead3A60.wherePrimaryKey()
_, err = TempLowHead3A60.getXorm().Where(whereQuery, whereArgs...).Delete(TempLowHead3A60) _, err = TempLowHead3A60.getXorm().Where(whereQuery, whereArgs...).Delete(TempLowHead3A60)

5
main.go

@ -32,3 +32,8 @@ func (myTestr3 *MyTestr3) wherePrimaryKey() (q interface{}, args []interface{})
func (myTestr3 *MyTestr3) getXorm() *xorm.Engine { func (myTestr3 *MyTestr3) getXorm() *xorm.Engine {
return hskdb.GetXormAuto() return hskdb.GetXormAuto()
} }
func main() {
t := &MyTestr3{}
t.Data()
}
Loading…
Cancel
Save