You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
91 lines
2.0 KiB
91 lines
2.0 KiB
package hasaki_{{.app_name}}
|
|
|
|
import (
|
|
"errors"
|
|
"git.ouxuan.net/hasaki-service/hasaki-sdk/hsktime"
|
|
"strconv"
|
|
"xorm.io/xorm"
|
|
)
|
|
|
|
type Model struct {
|
|
Id int `json:"id" xorm:"not null pk autoincr INT(11)"`
|
|
CreatedAt hsktime.Time `json:"created_at" xorm:"created comment('创建时间')"`
|
|
UpdatedAt hsktime.Time `json:"updated_at" xorm:"updated comment('更新时间')"`
|
|
DeletedAt hsktime.Time `json:"-" xorm:"deleted comment('是否删除') TIMESTAMP INDEX"`
|
|
session *xorm.Session `xorm:"-"`
|
|
instance interface{} `xorm:"-"`
|
|
}
|
|
|
|
func (m *Model) checkInstance() error {
|
|
if m.instance == nil {
|
|
return errors.New("instance 未实例化")
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (m *Model) SetInstance(instance interface{}) {
|
|
m.instance = instance
|
|
}
|
|
|
|
// 增删改查
|
|
func (m *Model) Save(cols ...string) (int64, error) {
|
|
err := m.checkInstance()
|
|
if err != nil {
|
|
return 0, err
|
|
}
|
|
if m.Id > 0 {
|
|
if len(cols) > 0 {
|
|
return m.session.Cols(cols...).Update(m.instance)
|
|
} else {
|
|
return m.session.AllCols().Update(m.session)
|
|
}
|
|
} else {
|
|
return m.session.InsertOne(m.instance)
|
|
}
|
|
}
|
|
|
|
func (m *Model) Del() (int64, error) {
|
|
err := m.checkInstance()
|
|
if err != nil {
|
|
return 0, err
|
|
}
|
|
return m.session.ID(m.Id).Delete(m.instance)
|
|
}
|
|
|
|
func (m *Model) Get() (bool, error) {
|
|
err := m.checkInstance()
|
|
if err != nil {
|
|
return false, err
|
|
}
|
|
return m.session.Unscoped().Get(m.instance)
|
|
}
|
|
|
|
//分页 cond["page"] 第几页 cond["page_size"] 多少条
|
|
func Page(cond map[string]string, s *xorm.Session) *xorm.Session {
|
|
if cond["page_size"] != "" {
|
|
size, _ := strconv.Atoi(cond["page_size"])
|
|
page, _ := strconv.Atoi(cond["page"])
|
|
if page <= 0 {
|
|
page = 0
|
|
} else {
|
|
page = page - 1
|
|
}
|
|
s = s.Limit(size, page*size)
|
|
}
|
|
return s
|
|
}
|
|
|
|
func Like(col, key string, s *xorm.Session) *xorm.Session {
|
|
if key != "" {
|
|
s = s.Where(col+" like ?", "%"+key+"%")
|
|
}
|
|
return s
|
|
}
|
|
|
|
|
|
func JsonContains(col, key string, s *xorm.Session) *xorm.Session {
|
|
if key != "" {
|
|
s = s.Where(`JSON_CONTAINS(` + col + `, '` + key + `', '$')`)
|
|
}
|
|
return s
|
|
}
|