package implement import ( "code.fnuoos.com/zhimeng/model.git/src/dao" "code.fnuoos.com/zhimeng/model.git/src/model" zhios_order_relate_logx "code.fnuoos.com/zhimeng/model.git/utils/logx" "reflect" "strings" "xorm.io/xorm" ) func NewMediumDb(engine *xorm.Engine) dao.MediumDao { return &MediumDb{Db: engine} } type MediumDb struct { Db *xorm.Engine } func (m MediumDb) UpdateMedium(medium *model.Medium, columns ...string) (int64, error) { affected, err := m.Db.Where("id =?", medium.Id).Cols(columns...).Update(m) if err != nil { return 0, err } return affected, nil } func (m MediumDb) GetMediumByUsername(username string) (medium *model.Medium, err error) { medium = new(model.Medium) has, err := m.Db.Where("username =?", username).Get(medium) if err != nil { return nil, zhios_order_relate_logx.Error(err) } if has == false { return nil, nil } return medium, nil } func (m MediumDb) MediumInsert(medium *model.Medium) (int64, error) { insertAffected, err := m.Db.InsertOne(medium) if err != nil { return 0, err } return insertAffected, nil } func (m MediumDb) MediumDeleteBySession(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 (m MediumDb) GetMediumRolePermission(id int) (list []*dao.MediumWithRolePermission, total int64, err error) { total, err = m.Db.Where("medium.id =?", id). Join("LEFT", "medium_with_role", "medium.id = medium_with_role.medium_id"). Join("LEFT", "medium_role", "admin_with_role.role_id = medium_role.id"). Join("LEFT", "medium_role_permission_group", "medium_role.id = medium_role_permission_group.role_id"). Join("LEFT", "medium_permission_group", "medium_role_permission_group.group_id = medium_permission_group.id"). Join("LEFT", "medium_permission_group_permission", "medium_permission_group.id = medium_permission_group_permission.group_id"). Join("LEFT", "medium_permission", "medium_permission_group_permission.permission_id = medium_permission.id"). FindAndCount(&list) return } func (m MediumDb) FindAdmin(username string, state, page, limit int) (list []model.Medium, total int64, err error) { sess := m.Db.Desc("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 (m MediumDb) GetMedium(id int) (mm *model.Medium, err error) { mm = new(model.Medium) has, err := m.Db.Where("id =?", id).Get(mm) if err != nil { return nil, zhios_order_relate_logx.Error(err) } if has == false { return nil, nil } return mm, nil } func (m MediumDb) FindMediumRolePermissionGroup(id int) (list []*dao.MediumWithRolePermissionGroup, total int64, err error) { total, err = m.Db.Where("medium.id =?", id). Join("LEFT", "medium_with_role", "medium.id = medium_with_role.medium_id"). Join("LEFT", "medium_role", "medium_with_role.role_id = medium_role.id"). FindAndCount(&list) return } func (m MediumDb) FindSuperAdmin(username, memo string, page, limit int) (list []model.Medium, total int64, err error) { sess := m.Db.Where("is_super_administrator=1").Desc("id").Limit(limit, (page-1)*limit) if username != "" { sess.And("username like ?", "%"+username+"%") } if memo != "" { sess.And("memo like ?", "%"+memo+"%") } total, err = sess.FindAndCount(&list) if err != nil { return nil, 0, err } return } func (m MediumDb) FindSuperAdminByMediumId(mediumId string, page, limit int) (list []model.Medium, total int64, err error) { sess := m.Db.Where("is_super_administrator=1").Desc("id").Limit(limit, (page-1)*limit) if mediumId != "" { sess.In("medium_id", strings.Split(mediumId, ",")) } total, err = sess.FindAndCount(&list) if err != nil { return nil, 0, err } return } func (m MediumDb) GetSuperAdmin(mediumId int) (list *model.Medium) { list = new(model.Medium) sess := m.Db.Where("is_super_administrator=1 and medium_id=?", mediumId) get, err := sess.Get(list) if err != nil || get == false { return nil } return list }