package db import ( "applet/app/db/model" "applet/app/utils/logx" "reflect" "xorm.io/xorm" ) type EnterpriseDb struct { Db *xorm.Engine `json:"db"` } func (enterpriseDb *EnterpriseDb) Set() { // set方法 enterpriseDb.Db = Db } func (enterpriseDb *EnterpriseDb) GetEnterprise(id int) (m *model.Enterprise, err error) { m = new(model.Enterprise) has, err := enterpriseDb.Db.Where("id =?", id).Get(m) if err != nil { return nil, logx.Error(err) } if has == false { return nil, nil } return m, nil } func (enterpriseDb *EnterpriseDb) EnterpriseDeleteBySession(session *xorm.Session, id interface{}) (int64, error) { if reflect.TypeOf(id).Kind() == reflect.Slice { return session.In("id", id).Delete(model.Enterprise{}) } else { return session.Where("id = ?", id).Delete(model.Enterprise{}) } } func (enterpriseDb *EnterpriseDb) FindEnterprise(limit, start int) (*[]model.Enterprise, error) { var m []model.Enterprise if limit == 0 || start == 0 { if err := enterpriseDb.Db.Desc("id").Find(&m); err != nil { return nil, logx.Error(err) } } else { if err := enterpriseDb.Db.Desc("id").Limit(limit, start).Find(m); err != nil { return nil, logx.Error(err) } } return &m, nil } func (enterpriseDb *EnterpriseDb) GetEnterpriseByName(name string) (m *model.Enterprise, err error) { m = new(model.Enterprise) has, err := enterpriseDb.Db.Where("name =?", name).Get(m) if err != nil { return nil, logx.Error(err) } if has == false { return nil, nil } return m, nil } func (enterpriseDb *EnterpriseDb) EnterpriseInsert(m *model.Enterprise) (int, error) { _, err := enterpriseDb.Db.InsertOne(m) if err != nil { return 0, err } return m.Id, nil } func (enterpriseDb *EnterpriseDb) EnterpriseUpdate(m *model.Enterprise, columns ...string) (int64, error) { affected, err := enterpriseDb.Db.Where("id =?", m.Id).Cols(columns...).Update(m) if err != nil { return 0, err } return affected, nil }