|
- package implement
-
- import (
- "applet/app/db/dao"
- "applet/app/db/model"
- "applet/app/utils/logx"
- "reflect"
- "xorm.io/xorm"
- )
-
- func NewAgentDb(engine *xorm.Engine) dao.AgentDao {
- return &AgentDb{Db: engine}
- }
-
- type AgentDb struct {
- Db *xorm.Engine
- }
-
- func (a AgentDb) GetAgentByUsername(username string) (agent *model.Agent, err error) {
- agent = new(model.Agent)
- has, err := a.Db.Where("username =?", username).Get(agent)
- if err != nil {
- return nil, logx.Error(err)
- }
- if has == false {
- return nil, nil
- }
- return agent, nil
- }
-
- func (a AgentDb) AgentInsert(m *model.Agent) (int64, error) {
- insertAffected, err := a.Db.InsertOne(m)
- if err != nil {
- return 0, err
- }
- return insertAffected, nil
- }
-
- func (a AgentDb) AgentDeleteBySession(session *xorm.Session, id interface{}) (int64, error) {
- if reflect.TypeOf(id).Kind() == reflect.Slice {
- return session.In("id", id).Delete(model.Admin{})
- } else {
- return session.Where("id = ?", id).Delete(model.Admin{})
- }
- }
-
- func (a AgentDb) UpdateAgent(m *model.Agent, columns ...string) (int64, error) {
- affected, err := a.Db.Where("id =?", m.Id).Cols(columns...).Update(m)
- if err != nil {
- return 0, err
- }
- return affected, nil
- }
|