feat: 多班级版班级管理系统 v2.0
技术栈:Go (Gin + GORM) + PHP + MySQL 5.7 + Redis 主要功能: - 多班级完全隔离(class_id 贯穿全系统) - 后端从 Python FastAPI 重写为 Go Gin(端口 56789) - 超级管理员独立登录(env 配置路径,默认账密 admin/Admin123) - 科任老师/课代表新角色 - 课代表作业管理页面 - 排行榜分项排行(操行分/考勤/作业) - 角色加减分上下限由班主任配置 - 家长改密功能(可开关) - 班级角色按需开关 - 宿舍号格式:南0-000 - 周度/月度重置功能 - MySQL 5.7 兼容 - Nginx 反向代理部署 开发者: Canglan 版权归属: Sea Network Technology Studio 许可证: Apache License 2.0
This commit is contained in:
112
backend-go/internal/repository/admin_role_repo.go
Normal file
112
backend-go/internal/repository/admin_role_repo.go
Normal file
@@ -0,0 +1,112 @@
|
||||
// ===========================================
|
||||
// 多班级版班级管理系统 - Go 后端
|
||||
//
|
||||
// 开发者: Canglan
|
||||
// 联系方式: admin@sea-studio.top
|
||||
// 版权归属: Sea Network Technology Studio
|
||||
// 许可证: Apache License 2.0
|
||||
//
|
||||
// 版权所有 © Sea Network Technology Studio
|
||||
// ===========================================
|
||||
|
||||
package repository
|
||||
|
||||
import (
|
||||
"gorm.io/gorm"
|
||||
|
||||
"hz-gitea.sea-studio.top/canglan/SharedClassManager/internal/model"
|
||||
)
|
||||
|
||||
// AdminRoleRepo 管理员角色数据访问层
|
||||
type AdminRoleRepo struct {
|
||||
db *gorm.DB
|
||||
}
|
||||
|
||||
// NewAdminRoleRepo 创建管理员角色 Repository
|
||||
func NewAdminRoleRepo(db *gorm.DB) *AdminRoleRepo {
|
||||
return &AdminRoleRepo{db: db}
|
||||
}
|
||||
|
||||
// GetByUserID 获取用户的管理员角色(取第一个,含科目名称)
|
||||
func (r *AdminRoleRepo) GetByUserID(userID int) (*model.AdminRole, error) {
|
||||
var role model.AdminRole
|
||||
if err := r.db.Table("admin_roles ar").
|
||||
Select("ar.*, s.subject_name").
|
||||
Joins("LEFT JOIN subjects s ON ar.subject_id = s.subject_id").
|
||||
Where("ar.user_id = ?", userID).
|
||||
Order("ar.admin_role_id ASC").
|
||||
Limit(1).
|
||||
First(&role).Error; err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &role, nil
|
||||
}
|
||||
|
||||
// GetByUserIDAndClass 获取用户在指定班级的管理员角色
|
||||
func (r *AdminRoleRepo) GetByUserIDAndClass(userID int, classID int) (*model.AdminRole, error) {
|
||||
var role model.AdminRole
|
||||
if err := r.db.Table("admin_roles ar").
|
||||
Select("ar.*, s.subject_name").
|
||||
Joins("LEFT JOIN subjects s ON ar.subject_id = s.subject_id").
|
||||
Where("ar.user_id = ? AND ar.class_id = ?", userID, classID).
|
||||
Limit(1).
|
||||
First(&role).Error; err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &role, nil
|
||||
}
|
||||
|
||||
// GetAllByClass 获取指定班级的所有管理员列表(含用户和科目信息)
|
||||
func (r *AdminRoleRepo) GetAllByClass(classID int) ([]model.AdminRole, error) {
|
||||
var roles []model.AdminRole
|
||||
if err := r.db.Table("admin_roles ar").
|
||||
Select("ar.*, u.real_name, u.username, u.status as user_status, s.subject_name").
|
||||
Joins("JOIN users u ON ar.user_id = u.user_id AND u.status = 1").
|
||||
Joins("LEFT JOIN subjects s ON ar.subject_id = s.subject_id").
|
||||
Where("ar.class_id = ?", classID).
|
||||
Order("ar.role_type").
|
||||
Find(&roles).Error; err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return roles, nil
|
||||
}
|
||||
|
||||
// Create 创建管理员角色
|
||||
func (r *AdminRoleRepo) Create(role *model.AdminRole) (int, error) {
|
||||
if err := r.db.Create(role).Error; err != nil {
|
||||
return 0, err
|
||||
}
|
||||
return role.AdminRoleID, nil
|
||||
}
|
||||
|
||||
// Delete 删除管理员角色(可指定班级)
|
||||
func (r *AdminRoleRepo) Delete(userID int, classID int) error {
|
||||
query := r.db.Where("user_id = ?", userID)
|
||||
if classID > 0 {
|
||||
query = query.Where("class_id = ?", classID)
|
||||
}
|
||||
return query.Delete(&model.AdminRole{}).Error
|
||||
}
|
||||
|
||||
// UpdateRole 更新管理员角色类型和关联科目
|
||||
func (r *AdminRoleRepo) UpdateRole(userID int, roleType string, classID int, subjectID *int) error {
|
||||
query := r.db.Model(&model.AdminRole{}).Where("user_id = ?", userID)
|
||||
if classID > 0 {
|
||||
query = query.Where("class_id = ?", classID)
|
||||
}
|
||||
return query.Updates(map[string]interface{}{
|
||||
"role_type": roleType,
|
||||
"subject_id": subjectID,
|
||||
}).Error
|
||||
}
|
||||
|
||||
// GetUserRoleAndClassID 获取用户的角色类型和所属班级ID
|
||||
func (r *AdminRoleRepo) GetUserRoleAndClassID(userID int) (string, int, error) {
|
||||
var role model.AdminRole
|
||||
if err := r.db.Where("user_id = ?", userID).
|
||||
Limit(1).
|
||||
First(&role).Error; err != nil {
|
||||
return "", 0, err
|
||||
}
|
||||
return role.RoleType, role.ClassID, nil
|
||||
}
|
||||
Reference in New Issue
Block a user