package implement import ( "applet/app/db/dao" "applet/app/db/model" "applet/app/utils/logx" "reflect" "xorm.io/xorm" ) func NewAdminDb(engine *xorm.Engine) dao.AdminDao { return &AdminDb{Db: engine} } type AdminDb struct { Db *xorm.Engine } func (a AdminDb) CreateAdminId() (admId int, err error) { m := new(model.Admin) has, err := a.Db.Desc("adm_id").Get(m) if err != nil { return 0, logx.Error(err) } if has == false { return 0, nil } admId = m.AdmId + 1 return admId, nil } func (a AdminDb) AdminDeleteBySession(session *xorm.Session, admId interface{}) (int64, error) { if reflect.TypeOf(admId).Kind() == reflect.Slice { return session.In("adm_id", admId).Delete(model.Admin{}) } else { return session.Where("adm_id = ?", admId).Delete(model.Admin{}) } } func (a AdminDb) GetAdmin(id int) (m *model.Admin, err error) { m = new(model.Admin) has, err := a.Db.Where("adm_id =?", id).Get(m) if err != nil { return nil, logx.Error(err) } if has == false { return nil, nil } return m, nil } func (a AdminDb) GetAdminByUserName(userName string) (m *model.Admin, err error) { m = new(model.Admin) has, err := a.Db.Where("username =?", userName).Get(m) if err != nil { return nil, logx.Error(err) } if has == false { return nil, nil } return m, nil } func (a AdminDb) GetAdminRolePermission(admId int) (list []*dao.AdminRolePermission, total int64, err error) { total, err = a.Db.Where("admin.adm_id =?", admId). Join("LEFT", "admin_role", "admin.adm_id = admin_role.adm_id"). Join("LEFT", "role", "admin_role.role_id = role.id"). Join("LEFT", "role_permission_group", "role.id = role_permission_group.role_id"). Join("LEFT", "permission_group", "role_permission_group.group_id = permission_group.id"). Join("LEFT", "permission_group_permission", "permission_group.id = permission_group_permission.group_id"). Join("LEFT", "permission", "permission_group_permission.permission_id = permission.id"). FindAndCount(&list) return } func (a AdminDb) FindAdmin(username string, state, page, limit int) (list []model.Admin, total int64, err error) { sess := a.Db.Desc("adm_id").Limit(limit, (page-1)*limit) if username != "" { sess.And("username like ?", "%"+username+"%") } if state != 0 { sess.And("state = ?", state) } total, err = sess.FindAndCount(&list) if err != nil { return nil, 0, err } return } func (a AdminDb) UpdateAdmin(m *model.Admin, columns ...string) (int64, error) { affected, err := a.Db.Where("adm_id =?", m.AdmId).Cols(columns...).Update(m) if err != nil { return 0, err } return affected, nil } func (a AdminDb) AdminInsert(m *model.Admin) (int64, error) { insertAffected, err := a.Db.InsertOne(m) if err != nil { return 0, err } return insertAffected, nil } func (a AdminDb) FindAdminRolePermissionGroup(admId int) (list []*dao.AdminRolePermissionGroup, total int64, err error) { total, err = a.Db.Where("admin.adm_id =?", admId). Join("LEFT", "admin_role", "admin.adm_id = admin_role.adm_id"). Join("LEFT", "role", "admin_role.role_id = role.id"). //Join("LEFT", "role_permission_group", "role.id = role_permission_group.role_id"). //Join("LEFT", "permission_group", "role_permission_group.group_id = permission_group.id"). FindAndCount(&list) return }