package implement import ( "applet/app/db/dao" "applet/app/db/model" "applet/app/utils/logx" "reflect" "xorm.io/xorm" ) func NewMediumRoleDb(engine *xorm.Engine) dao.MediumRoleDao { return &MediumRoleDb{Db: engine} } type MediumRoleDb struct { Db *xorm.Engine } func (m MediumRoleDb) FindMediumRole() (*[]model.MediumRole, error) { var mm []model.MediumRole if err := m.Db.Desc("id").Find(&mm); err != nil { return nil, logx.Error(err) } return &mm, nil } func (m MediumRoleDb) GetMediumRole() (mm *model.MediumRole, err error) { mm = new(model.MediumRole) has, err := m.Db.Where("id = ?", mm.Id).Get(mm) if err != nil { return nil, logx.Error(err) } if has == false { return nil, nil } return mm, nil } func (m MediumRoleDb) UpdateMediumRole(mm *model.MediumRole, columns ...string) (int64, error) { affected, err := m.Db.Where("id =?", mm.Id).Cols(columns...).Update(m) if err != nil { return 0, err } return affected, nil } func (m MediumRoleDb) MediumRoleDeleteBySession(session *xorm.Session, id interface{}) (int64, error) { if reflect.TypeOf(id).Kind() == reflect.Slice { return session.In("id", id).Delete(model.MediumRole{}) } else { return session.Where("id = ?", id).Delete(model.MediumRole{}) } } func (m MediumRoleDb) MediumRoleInsert(mm *model.MediumRole) (int, error) { _, err := m.Db.InsertOne(mm) if err != nil { return 0, err } return mm.Id, nil } func (m MediumRoleDb) FindPermissionGroupByMediumRole(roleId int) (list []*dao.MediumRolePermissionGroup, total int64, err error) { total, err = m.Db.Where("medium_role.id =?", roleId). Join("LEFT", "medium_role_permission_group", "medium_role_permission_group.role_id = medium_role.id"). Join("LEFT", "medium_permission_group", "medium_permission_group.id = medium_role_permission_group.group_id"). FindAndCount(&list) return }