package implement import ( "applet/app/db/dao" "applet/app/db/model" "applet/app/utils/logx" "reflect" "xorm.io/xorm" ) func NewAgentRoleDb(engine *xorm.Engine) dao.AgentRoleDao { return &AgentRoleDb{Db: engine} } type AgentRoleDb struct { Db *xorm.Engine } func (a AgentRoleDb) FindAgentRole() (*[]model.AgentRole, error) { var mm []model.AgentRole if err := a.Db.Desc("id").Find(&mm); err != nil { return nil, logx.Error(err) } return &mm, nil } func (a AgentRoleDb) GetAgentRole() (m *model.AgentRole, err error) { m = new(model.AgentRole) has, err := a.Db.Where("id = ?", m.Id).Get(m) if err != nil { return nil, logx.Error(err) } if has == false { return nil, nil } return m, nil } func (a AgentRoleDb) UpdateAgentRole(m *model.AgentRole, 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 } func (a AgentRoleDb) AgentRoleDeleteBySession(session *xorm.Session, id interface{}) (int64, error) { if reflect.TypeOf(id).Kind() == reflect.Slice { return session.In("id", id).Delete(model.Role{}) } else { return session.Where("id = ?", id).Delete(model.Role{}) } } func (a AgentRoleDb) AgentRoleInsert(m *model.AgentRole) (int, error) { _, err := a.Db.InsertOne(m) if err != nil { return 0, err } return m.Id, nil } func (a AgentRoleDb) FindPermissionGroupByAgentRole(roleId int) (list []*dao.AgentRolePermissionGroup, total int64, err error) { total, err = a.Db.Where("agent_role.id =?", roleId). Join("LEFT", "agent_role_permission_group", "agent_role_permission_group.role_id = agent_role.id"). Join("LEFT", "agent_permission_group", "agent_permission_group.id = agent_role_permission_group.group_id"). FindAndCount(&list) return }