|
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768 |
- package db
-
- import (
- "applet/app/db/model"
- "applet/app/utils/logx"
- "reflect"
- "xorm.io/xorm"
- )
-
- type SchemeDb struct {
- Db *xorm.Engine `json:"db"`
- }
-
- func (schemeDb *SchemeDb) Set() { // set方法
- schemeDb.Db = Db
- }
-
- func (schemeDb *SchemeDb) FindScheme() (*[]model.Scheme, error) {
- var m []model.Scheme
- if err := schemeDb.Db.Desc("id").Find(&m); err != nil {
- return nil, logx.Error(err)
- }
- return &m, nil
- }
-
- func (schemeDb *SchemeDb) GetScheme(id int) (m *model.Scheme, err error) {
- m = new(model.Scheme)
- has, err := schemeDb.Db.Where("id =?", id).Get(m)
- if err != nil {
- return nil, logx.Error(err)
- }
- if has == false {
- return nil, nil
- }
- return m, nil
- }
-
- func (schemeDb *SchemeDb) SchemeDeleteBySession(session *xorm.Session, id interface{}) (int64, error) {
- if reflect.TypeOf(id).Kind() == reflect.Slice {
- return session.In("id", id).Delete(model.Scheme{})
- } else {
- return session.Where("id = ?", id).Delete(model.Scheme{})
- }
- }
-
- func (schemeDb *SchemeDb) BatchAddSchemeBySession(session *xorm.Session, mm []*model.Scheme) (int64, error) {
- affected, err := session.Insert(mm)
- if err != nil {
- return 0, err
- }
- return affected, nil
- }
-
- func (schemeDb *SchemeDb) SchemeInsert(m *model.Scheme) (int, error) {
- _, err := schemeDb.Db.InsertOne(m)
- if err != nil {
- return 0, err
- }
- return m.Id, nil
- }
-
- func (schemeDb *SchemeDb) UpdateScheme(m *model.Scheme, columns ...string) (int64, error) {
- affected, err := schemeDb.Db.Where("id =?", m.Id).Cols(columns...).Update(m)
- if err != nil {
- return 0, err
- }
- return affected, nil
- }
|