From 7b71d44a7174726e841dfd2e18ff65385348d7de Mon Sep 17 00:00:00 2001 From: qianqianzyk <3265569512@qq.com> Date: Sat, 5 Oct 2024 23:49:17 +0800 Subject: [PATCH] =?UTF-8?q?feat(theme):=E5=A2=9E=E5=8A=A0=E7=94=A8?= =?UTF-8?q?=E6=88=B7=E7=AB=AF=E8=8E=B7=E5=8F=96=E4=B8=BB=E9=A2=98=E8=89=B2?= =?UTF-8?q?=E5=88=97=E8=A1=A8,=E7=AE=A1=E7=90=86=E7=AB=AF=E5=A2=9E?= =?UTF-8?q?=E5=8A=A0=E5=88=A0=E9=99=A4=E4=BF=AE=E6=94=B9=E6=9F=A5=E6=89=BE?= =?UTF-8?q?=E4=B8=BB=E9=A2=98=E8=89=B2=E5=92=8C=E4=B8=BB=E9=A2=98=E8=89=B2?= =?UTF-8?q?=E7=94=A8=E6=88=B7=E6=9D=83=E9=99=90=E7=9A=84=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/config/key.go | 6 + app/config/term.go | 6 +- app/controllers/adminController/init.go | 3 +- app/controllers/adminController/theme.go | 180 +++++++++++++++ .../themeController/themeController.go | 66 ++++++ .../systemController/controlInfo.go | 23 ++ app/controllers/userController/auth.go | 1 - app/controllers/userController/del.go | 7 + app/controllers/userController/reg.go | 13 ++ app/models/theme.go | 8 + app/models/themePermission.go | 12 + .../themeServices/themePermissionService.go | 210 ++++++++++++++++++ app/services/themeServices/themeService.go | 109 +++++++++ .../themeServices/themeStudentService.go | 19 ++ config/database/migrations.go | 2 + config/router/adminRouter.go | 9 + config/router/funcRouter.go | 9 +- 17 files changed, 679 insertions(+), 4 deletions(-) create mode 100644 app/controllers/adminController/theme.go create mode 100644 app/controllers/funcControllers/themeController/themeController.go create mode 100644 app/models/theme.go create mode 100644 app/models/themePermission.go create mode 100644 app/services/themeServices/themePermissionService.go create mode 100644 app/services/themeServices/themeService.go create mode 100644 app/services/themeServices/themeStudentService.go diff --git a/app/config/key.go b/app/config/key.go index cb22bf4..66a0053 100644 --- a/app/config/key.go +++ b/app/config/key.go @@ -8,6 +8,8 @@ const fileUrlKey = "fileUrlKey" const registerTipsKey = "registerTipsKey" +const defaultThemeKey = "defaultThemeKey" + func GetSchoolBusUrl() string { return getConfig(schoolBusUrlKey) } @@ -35,3 +37,7 @@ func SetFileUrlKey(url string) error { func GetRegisterTipsKey() string { return getConfig(registerTipsKey) } func SetRegisterTipsKey(url string) error { return setConfig(registerTipsKey, url) } + +func GetDefaultThemeKey() string { return getConfig(defaultThemeKey) } + +func SetDefaultThemeKey(url string) error { return setConfig(defaultThemeKey, url) } diff --git a/app/config/term.go b/app/config/term.go index 75559d5..73cba4a 100644 --- a/app/config/term.go +++ b/app/config/term.go @@ -6,7 +6,7 @@ const termStartDate = "termStartDate" const scoreTermYearKey = "scoreTermYearKey" const scoreTermKey = "scoreTermKey" -func SetSystemInfo(yearValue, termValue, termStartDateValue, scoreYearValue, scoreTermValue, jpgUrlValue, fileUrlValue, registerTipsValue, schoolBusUrlValue string) error { +func SetSystemInfo(yearValue, termValue, termStartDateValue, scoreYearValue, scoreTermValue, jpgUrlValue, fileUrlValue, registerTipsValue, schoolBusUrlValue, defaultThemeValue string) error { err := setConfig(termYearKey, yearValue) if err != nil { return err @@ -43,6 +43,10 @@ func SetSystemInfo(yearValue, termValue, termStartDateValue, scoreYearValue, sco if err != nil { return err } + err = setConfig(defaultThemeKey, defaultThemeValue) + if err != nil { + return err + } return err } diff --git a/app/controllers/adminController/init.go b/app/controllers/adminController/init.go index 0f77024..8ff239d 100644 --- a/app/controllers/adminController/init.go +++ b/app/controllers/adminController/init.go @@ -18,6 +18,7 @@ type SystemInfoForm struct { JpgUrlValue string `json:"jpgUrlValue"` FileUrlValue string `json:"fileUrlValue"` RegisterTips string `json:"registerTips"` + DefaultThemeValue string `json:"defaultThemeValue"` } type encryptForm struct { @@ -71,7 +72,7 @@ func SetSystemInfo(c *gin.Context) { return } - err = config.SetSystemInfo(postForm.YearValue, postForm.TermValue, postForm.TermStartDateValue, postForm.ScoreYearValue, postForm.ScoreTermValue, postForm.JpgUrlValue, postForm.FileUrlValue, postForm.RegisterTips, postForm.SchoolBusUrlValue) + err = config.SetSystemInfo(postForm.YearValue, postForm.TermValue, postForm.TermStartDateValue, postForm.ScoreYearValue, postForm.ScoreTermValue, postForm.JpgUrlValue, postForm.FileUrlValue, postForm.RegisterTips, postForm.SchoolBusUrlValue, postForm.DefaultThemeValue) if err != nil { _ = c.AbortWithError(200, apiException.ServerError) return diff --git a/app/controllers/adminController/theme.go b/app/controllers/adminController/theme.go new file mode 100644 index 0000000..680921d --- /dev/null +++ b/app/controllers/adminController/theme.go @@ -0,0 +1,180 @@ +package adminController + +import ( + "github.com/gin-gonic/gin" + "wejh-go/app/apiException" + "wejh-go/app/models" + "wejh-go/app/services/themeServices" + "wejh-go/app/utils" +) + +type CreateThemeData struct { + Name string `json:"name" binding:"required"` + Type string `json:"type" binding:"required"` + ThemeConfig string `json:"theme_config"` +} + +// 管理员创建主题色 +func CreateTheme(c *gin.Context) { + var data CreateThemeData + err := c.ShouldBindJSON(&data) + if err != nil { + _ = c.AbortWithError(200, apiException.ParamError) + return + } + + record := models.Theme{ + Name: data.Name, + Type: data.Type, + ThemeConfig: data.ThemeConfig, + } + themeID, err := themeServices.CreateTheme(record) + if err != nil { + _ = c.AbortWithError(200, apiException.ServerError) + return + } + if data.Type == "all" { + studentIDs, err := themeServices.GetAllStudentIDs() + if err != nil { + _ = c.AbortWithError(200, apiException.ServerError) + return + } + + _, err = themeServices.AddThemePermission(themeID, studentIDs) + if err != nil { + _ = c.AbortWithError(200, apiException.ServerError) + return + } + } + + utils.JsonSuccessResponse(c, nil) +} + +type UpdateThemeData struct { + ID int `json:"id" binding:"required"` + Name string `json:"name" binding:"required"` + ThemeConfig string `json:"theme_config" binding:"required"` +} + +// 管理员更新主题色 +func UpdateTheme(c *gin.Context) { + var data UpdateThemeData + err := c.ShouldBindJSON(&data) + if err != nil { + _ = c.AbortWithError(200, apiException.ParamError) + return + } + + record := models.Theme{ + Name: data.Name, + ThemeConfig: data.ThemeConfig, + } + err = themeServices.UpdateTheme(data.ID, record) + if err != nil { + _ = c.AbortWithError(200, apiException.ServerError) + return + } + + utils.JsonSuccessResponse(c, nil) +} + +// 管理员获取主题色列表 +func GetThemes(c *gin.Context) { + themes, err := themeServices.GetThemes() + if err != nil { + _ = c.AbortWithError(200, apiException.ServerError) + return + } + + utils.JsonSuccessResponse(c, gin.H{ + "theme_list": themes, + }) +} + +type DeleteThemeData struct { + ID int `form:"id" binding:"required"` +} + +// 管理员根据id删除主题色 +func DeleteTheme(c *gin.Context) { + var data DeleteThemeData + err := c.ShouldBindQuery(&data) + if err != nil { + _ = c.AbortWithError(200, apiException.ParamError) + return + } + + err = themeServices.CheckThemeExist(data.ID) + if err != nil { + _ = c.AbortWithError(200, apiException.ServerError) + return + } + + err = themeServices.DeleteTheme(data.ID) + if err != nil { + _ = c.AbortWithError(200, apiException.ServerError) + return + } + utils.JsonSuccessResponse(c, nil) +} + +type AddThemePermissionData struct { + ThemeID int `json:"theme_id" binding:"required"` + StudentID []string `json:"student_id" binding:"required"` +} + +// 管理员添加用户主题色权限 +func AddThemePermission(c *gin.Context) { + var data AddThemePermissionData + err := c.ShouldBindJSON(&data) + if err != nil { + _ = c.AbortWithError(200, apiException.ParamError) + return + } + + err = themeServices.CheckThemeExist(data.ThemeID) + if err != nil { + _ = c.AbortWithError(200, apiException.ServerError) + return + } + + invalidStudentIDs, err := themeServices.AddThemePermission(data.ThemeID, data.StudentID) + if err != nil { + _ = c.AbortWithError(200, apiException.ServerError) + return + } + + utils.JsonSuccessResponse(c, gin.H{ + "invalid_student_ids": invalidStudentIDs, + }) +} + +type GetThemePermissionData struct { + StudentID string `form:"student_id" binding:"required"` +} + +// 管理员根据学号查询用户主题色权限 +func GetThemePermission(c *gin.Context) { + var data GetThemePermissionData + err := c.ShouldBindQuery(&data) + if err != nil { + _ = c.AbortWithError(200, apiException.ParamError) + return + } + + themePermission, err := themeServices.GetThemePermissionByStudentID(data.StudentID) + if err != nil { + _ = c.AbortWithError(200, apiException.ServerError) + return + } + + themeNames, err := themeServices.GetThemeNameByID(themePermission) + if err != nil { + _ = c.AbortWithError(200, apiException.ServerError) + return + } + + utils.JsonSuccessResponse(c, gin.H{ + "theme_name": themeNames, + }) +} diff --git a/app/controllers/funcControllers/themeController/themeController.go b/app/controllers/funcControllers/themeController/themeController.go new file mode 100644 index 0000000..145f880 --- /dev/null +++ b/app/controllers/funcControllers/themeController/themeController.go @@ -0,0 +1,66 @@ +package themeController + +import ( + "github.com/gin-gonic/gin" + "wejh-go/app/apiException" + "wejh-go/app/services/sessionServices" + "wejh-go/app/services/themeServices" + "wejh-go/app/utils" +) + +func GetThemeList(c *gin.Context) { + user, err := sessionServices.GetUserSession(c) + if err != nil { + _ = c.AbortWithError(200, apiException.NotLogin) + return + } + + themePermission, err := themeServices.GetThemePermissionByStudentID(user.StudentID) + if err != nil { + _ = c.AbortWithError(200, apiException.ServerError) + return + } + + themes, err := themeServices.GetThemesByID(themePermission) + if err != nil { + _ = c.AbortWithError(200, apiException.ServerError) + return + } + + utils.JsonSuccessResponse(c, gin.H{ + "theme_list": themes, + "current_theme_id": themePermission.CurrentThemeID, + }) +} + +type ChooseCurrentThemeData struct { + ID int `json:"id" binding:"required"` +} + +func ChooseCurrentTheme(c *gin.Context) { + var data ChooseCurrentThemeData + err := c.ShouldBindJSON(&data) + if err != nil { + _ = c.AbortWithError(200, apiException.ParamError) + return + } + + user, err := sessionServices.GetUserSession(c) + if err != nil { + _ = c.AbortWithError(200, apiException.NotLogin) + return + } + + err = themeServices.CheckThemeExist(data.ID) + if err != nil { + _ = c.AbortWithError(200, apiException.ServerError) + return + } + + err = themeServices.UpdateCurrentTheme(data.ID, user.StudentID) + if err != nil { + _ = c.AbortWithError(200, apiException.ServerError) + return + } + utils.JsonSuccessResponse(c, nil) +} diff --git a/app/controllers/systemController/controlInfo.go b/app/controllers/systemController/controlInfo.go index 3ce3a02..a3fbbe3 100644 --- a/app/controllers/systemController/controlInfo.go +++ b/app/controllers/systemController/controlInfo.go @@ -2,8 +2,12 @@ package systemController import ( "github.com/gin-gonic/gin" + "strconv" "time" + "wejh-go/app/apiException" "wejh-go/app/config" + "wejh-go/app/models" + "wejh-go/app/services/themeServices" "wejh-go/app/utils" ) @@ -15,11 +19,29 @@ func Info(c *gin.Context) { jpgUrl := config.GetWebpUrlKey() fileUrl := config.GetFileUrlKey() registerTips := config.GetRegisterTipsKey() + defaultThemeIDStr := config.GetDefaultThemeKey() week := ((currentTime.Unix()-startTime.Unix())/3600+8)/24/7 + 1 if currentTime.Unix() < startTime.Unix()-8*3600 { week = -1 } + + var defaultTheme models.Theme + if defaultThemeIDStr != "" { + defaultThemeID, err := strconv.Atoi(defaultThemeIDStr) + if err == nil { + defaultTheme, err = themeServices.GetThemeByID(defaultThemeID) + if err != nil { + _ = c.AbortWithError(200, apiException.ServerError) + return + } + if defaultTheme.Type != "all" { + _ = c.AbortWithError(200, apiException.ServerError) + return + } + } + } + response := gin.H{ "time": time.Now(), "is_begin": week > 0, @@ -33,6 +55,7 @@ func Info(c *gin.Context) { "jpgUrl": jpgUrl, "fileUrl": fileUrl, "registerTips": registerTips, + "defaultTheme": defaultTheme, } utils.JsonSuccessResponse(c, response) diff --git a/app/controllers/userController/auth.go b/app/controllers/userController/auth.go index 87025cd..6eecc49 100644 --- a/app/controllers/userController/auth.go +++ b/app/controllers/userController/auth.go @@ -70,7 +70,6 @@ func AuthByPassword(c *gin.Context) { "createTime": user.CreateTime, }, }) - } func AuthBySession(c *gin.Context) { diff --git a/app/controllers/userController/del.go b/app/controllers/userController/del.go index bea1db1..160218d 100644 --- a/app/controllers/userController/del.go +++ b/app/controllers/userController/del.go @@ -4,6 +4,7 @@ import ( "github.com/gin-gonic/gin" "wejh-go/app/apiException" "wejh-go/app/services/sessionServices" + "wejh-go/app/services/themeServices" "wejh-go/app/services/userServices" "wejh-go/app/utils" ) @@ -32,6 +33,12 @@ func DelAccount(c *gin.Context) { return } + err = themeServices.DeleteThemePermission(user.StudentID) + if err != nil { + _ = c.AbortWithError(200, apiException.ServerError) + return + } + if err = userServices.DelAccount(user, postForm.IDCard); err != nil { if err == apiException.StudentNumAndIidError { _ = c.AbortWithError(200, apiException.StudentNumAndIidError) diff --git a/app/controllers/userController/reg.go b/app/controllers/userController/reg.go index d5b50b7..b15f33e 100644 --- a/app/controllers/userController/reg.go +++ b/app/controllers/userController/reg.go @@ -5,6 +5,7 @@ import ( "strings" "wejh-go/app/apiException" "wejh-go/app/services/sessionServices" + "wejh-go/app/services/themeServices" "wejh-go/app/services/userServices" "wejh-go/app/utils" "wejh-go/config/wechat" @@ -57,6 +58,12 @@ func BindOrCreateStudentUserFromWechat(c *gin.Context) { return } + err = themeServices.AddDefaultThemePermission(postForm.StudentID) + if err != nil { + _ = c.AbortWithError(200, apiException.ServerError) + return + } + err = sessionServices.SetUserSession(c, user) if err != nil { _ = c.AbortWithError(200, apiException.ServerError) @@ -86,6 +93,12 @@ func CreateStudentUser(c *gin.Context) { return } + err = themeServices.AddDefaultThemePermission(postForm.StudentID) + if err != nil { + _ = c.AbortWithError(200, apiException.ServerError) + return + } + err = sessionServices.SetUserSession(c, user) if err != nil { _ = c.AbortWithError(200, apiException.ServerError) diff --git a/app/models/theme.go b/app/models/theme.go new file mode 100644 index 0000000..6c02886 --- /dev/null +++ b/app/models/theme.go @@ -0,0 +1,8 @@ +package models + +type Theme struct { + ID int `json:"id"` + Name string `json:"name"` + Type string `json:"type"` + ThemeConfig string `json:"theme_config"` +} diff --git a/app/models/themePermission.go b/app/models/themePermission.go new file mode 100644 index 0000000..32b98a5 --- /dev/null +++ b/app/models/themePermission.go @@ -0,0 +1,12 @@ +package models + +type ThemePermission struct { + ID int `json:"id"` + StudentID string `json:"student_id"` + CurrentThemeID int `json:"current_theme_id"` + ThemePermission string `json:"theme_permission"` +} + +type ThemePermissionData struct { + ThemeIDs []int `json:"theme_id"` +} diff --git a/app/services/themeServices/themePermissionService.go b/app/services/themeServices/themePermissionService.go new file mode 100644 index 0000000..57a99ec --- /dev/null +++ b/app/services/themeServices/themePermissionService.go @@ -0,0 +1,210 @@ +package themeServices + +import ( + "encoding/json" + "errors" + "gorm.io/gorm" + "wejh-go/app/models" + "wejh-go/config/database" +) + +func AddThemePermission(themeID int, reqStudentIDs []string) ([]string, error) { + var studentIDs []string + var invalidStudentIDs []string + + if len(reqStudentIDs) > 0 { + var existingUsers []models.User + err := database.DB.Select("student_id").Where("student_id IN ?", reqStudentIDs).Find(&existingUsers).Error + if err != nil { + return nil, err + } + + existingStudentIDMap := make(map[string]bool) + for _, user := range existingUsers { + existingStudentIDMap[user.StudentID] = true + } + + for _, studentID := range reqStudentIDs { + if existingStudentIDMap[studentID] { + studentIDs = append(studentIDs, studentID) + } else { + invalidStudentIDs = append(invalidStudentIDs, studentID) + } + } + } else { + var users []models.User + err := database.DB.Select("student_id").Find(&users).Error + if err != nil { + return nil, err + } + for _, user := range users { + studentIDs = append(studentIDs, user.StudentID) + } + } + + var permissions []models.ThemePermission + err := database.DB.Where("student_id IN ?", studentIDs).Find(&permissions).Error + if err != nil { + return nil, err + } + permissionMap := make(map[string]*models.ThemePermission) + for i, permission := range permissions { + permissionMap[permission.StudentID] = &permissions[i] + } + + for _, studentID := range studentIDs { + permission, exist := permissionMap[studentID] + if !exist { + themePermissionData := models.ThemePermissionData{ + ThemeIDs: []int{themeID}, + } + newPermissionData, err := json.Marshal(themePermissionData) + if err != nil { + return nil, err + } + newPermission := models.ThemePermission{ + StudentID: studentID, + CurrentThemeID: themeID, + ThemePermission: string(newPermissionData), + } + permission = &newPermission + permissions = append(permissions, newPermission) + } + + var themePermissionData models.ThemePermissionData + err = json.Unmarshal([]byte(permission.ThemePermission), &themePermissionData) + if err != nil { + return nil, err + } + if !containThemeID(themePermissionData.ThemeIDs, themeID) { + themePermissionData.ThemeIDs = append(themePermissionData.ThemeIDs, themeID) + newPermission, err := json.Marshal(themePermissionData) + if err != nil { + return nil, err + } + permission.ThemePermission = string(newPermission) + } + } + + err = database.DB.Save(&permissions).Error + if err != nil { + return nil, err + } + return invalidStudentIDs, nil +} + +func UpdateCurrentTheme(id int, studentID string) error { + var permission models.ThemePermission + err := database.DB.Where("student_id = ?", studentID).First(&permission).Error + if err != nil { + return err + } + + var themePermissionData models.ThemePermissionData + err = json.Unmarshal([]byte(permission.ThemePermission), &themePermissionData) + if err != nil { + return err + } + + if !containThemeID(themePermissionData.ThemeIDs, id) { + return errors.New("the theme ID is not in the user's permission list") + } + + result := database.DB.Model(models.ThemePermission{}). + Where("student_id = ?", studentID). + Update("current_theme_id", id) + return result.Error +} + +func DeleteThemePermission(studentID string) error { + result := database.DB.Where("student_id = ?", studentID).Delete(&models.ThemePermission{}) + return result.Error +} + +func GetThemePermissionByStudentID(studentID string) (models.ThemePermission, error) { + var record models.ThemePermission + result := database.DB.Model(models.ThemePermission{}).Where("student_id = ?", studentID).First(&record) + return record, result.Error +} + +func GetThemeNameByID(themePermission models.ThemePermission) ([]string, error) { + var themePermissionData models.ThemePermissionData + err := json.Unmarshal([]byte(themePermission.ThemePermission), &themePermissionData) + if err != nil { + return nil, err + } + var themes []models.Theme + err = database.DB.Where("id IN ?", themePermissionData.ThemeIDs).Find(&themes).Error + if err != nil { + return nil, err + } + var themeNames []string + for _, theme := range themes { + themeNames = append(themeNames, theme.Name) + } + return themeNames, nil +} + +func GetThemesByID(themePermission models.ThemePermission) ([]models.Theme, error) { + var themePermissionData models.ThemePermissionData + err := json.Unmarshal([]byte(themePermission.ThemePermission), &themePermissionData) + if err != nil { + return nil, err + } + var themes []models.Theme + err = database.DB.Where("id IN ?", themePermissionData.ThemeIDs).Find(&themes).Error + if err != nil { + return nil, err + } + return themes, nil +} + +func AddDefaultThemePermission(studentID string) error { + var existingPermission models.ThemePermission + err := database.DB.Where("student_id = ?", studentID).First(&existingPermission).Error + if err != nil { + if err == gorm.ErrRecordNotFound { + var themes []models.Theme + err := database.DB.Where("type = ?", "all").Find(&themes).Error + if err != nil { + return err + } + if len(themes) == 0 { + return errors.New("no themes found with type 'all'") + } + + var themeIDs []int + for _, theme := range themes { + themeIDs = append(themeIDs, theme.ID) + } + var themePermissionData models.ThemePermissionData + themePermissionData.ThemeIDs = themeIDs + permission, err := json.Marshal(themePermissionData) + if err != nil { + return err + } + + newPermission := models.ThemePermission{ + StudentID: studentID, + CurrentThemeID: themeIDs[0], + ThemePermission: string(permission), + } + + result := database.DB.Create(&newPermission) + return result.Error + } else { + return err + } + } else { + return nil + } +} + +func containThemeID(themeIDs []int, id int) bool { + for _, themeID := range themeIDs { + if themeID == id { + return true + } + } + return false +} diff --git a/app/services/themeServices/themeService.go b/app/services/themeServices/themeService.go new file mode 100644 index 0000000..2a58c93 --- /dev/null +++ b/app/services/themeServices/themeService.go @@ -0,0 +1,109 @@ +package themeServices + +import ( + "encoding/json" + "wejh-go/app/models" + "wejh-go/config/database" +) + +func CheckThemeExist(id int) error { + var theme models.Theme + result := database.DB.Model(models.Theme{}).Where("id = ?", id).First(&theme) + return result.Error +} + +func CreateTheme(record models.Theme) (int, error) { + result := database.DB.Create(&record) + return record.ID, result.Error +} + +func UpdateTheme(id int, record models.Theme) error { + result := database.DB.Model(models.Theme{}).Where(&models.Theme{ID: id}).Updates(&record) + return result.Error +} + +func GetThemeByID(id int) (models.Theme, error) { + var record models.Theme + result := database.DB.Model(models.Theme{}).Where(&models.Theme{ID: id}).First(&record) + return record, result.Error +} + +func GetThemes() ([]models.Theme, error) { + var themes []models.Theme + result := database.DB.Model(models.Theme{}).Find(&themes) + return themes, result.Error +} + +func DeleteTheme(id int) error { + tx := database.DB.Begin() + if err := tx.Delete(&models.Theme{}, id).Error; err != nil { + tx.Rollback() + return err + } + + var theme models.Theme + if err := tx.Where("type = ?", "all").First(&theme).Error; err != nil { + tx.Rollback() + return err + } + + var permissions []models.ThemePermission + if err := tx.Where("current_theme_id = ?", id).Find(&permissions).Error; err != nil { + tx.Rollback() + return err + } + + if err := tx.Model(&models.ThemePermission{}). + Where("current_theme_id = ?", id). + Update("current_theme_id", theme.ID).Error; err != nil { + tx.Rollback() + return err + } + + result := tx.Find(&permissions) + if result.Error != nil { + tx.Rollback() + return result.Error + } + + updatedPermissionMap := make(map[string]models.ThemePermissionData) + for _, permission := range permissions { + var themePermissionData models.ThemePermissionData + err := json.Unmarshal([]byte(permission.ThemePermission), &themePermissionData) + if err != nil { + tx.Rollback() + return err + } + updatedThemeIDs := removeThemeID(themePermissionData.ThemeIDs, id) + if len(updatedThemeIDs) != len(themePermissionData.ThemeIDs) { + themePermissionData.ThemeIDs = updatedThemeIDs + updatedPermissionMap[permission.StudentID] = themePermissionData + } + } + for studentID, data := range updatedPermissionMap { + newPermission, err := json.Marshal(data) + if err != nil { + tx.Rollback() + return err + } + if err := tx.Model(&models.ThemePermission{}). + Where("student_id = ?", studentID). + Update("theme_permission", string(newPermission)).Error; err != nil { + tx.Rollback() + return err + } + } + + tx.Commit() + return nil +} + +func removeThemeID(themeIDs []int, id int) []int { + var updatedThemeIDs []int + for _, themeID := range themeIDs { + if themeID != id { + updatedThemeIDs = append(updatedThemeIDs, themeID) + } + } + return updatedThemeIDs +} diff --git a/app/services/themeServices/themeStudentService.go b/app/services/themeServices/themeStudentService.go new file mode 100644 index 0000000..6de319a --- /dev/null +++ b/app/services/themeServices/themeStudentService.go @@ -0,0 +1,19 @@ +package themeServices + +import ( + "wejh-go/app/models" + "wejh-go/config/database" +) + +func GetAllStudentIDs() ([]string, error) { + var studentIDs []string + var users []models.User + err := database.DB.Select("student_id").Find(&users).Error + if err != nil { + return nil, err + } + for _, user := range users { + studentIDs = append(studentIDs, user.StudentID) + } + return studentIDs, nil +} diff --git a/config/database/migrations.go b/config/database/migrations.go index b89b9de..6e83bc5 100755 --- a/config/database/migrations.go +++ b/config/database/migrations.go @@ -24,5 +24,7 @@ func autoMigrate(db *gorm.DB) error { &models.PersonalInfo{}, &models.Supplies{}, &models.BorrowRecord{}, + &models.Theme{}, + &models.ThemePermission{}, ) } diff --git a/config/router/adminRouter.go b/config/router/adminRouter.go index 21da6a9..bddca5a 100755 --- a/config/router/adminRouter.go +++ b/config/router/adminRouter.go @@ -45,6 +45,15 @@ func adminRouterInit(r *gin.RouterGroup) { user.POST("/create", adminController.CreateAdminAccount) user.GET("/status", adminController.GetUserBindStatus) } + theme := admin.Group("/theme") + { + theme.POST("/create", adminController.CreateTheme) + theme.PUT("/update", adminController.UpdateTheme) + theme.GET("/get", adminController.GetThemes) + theme.DELETE("/delete", adminController.DeleteTheme) + theme.POST("/permission/create", adminController.AddThemePermission) + theme.GET("/permission/get", adminController.GetThemePermission) + } } forU := r.Group("/foru", midwares.CheckLostAndFoundAdmin) diff --git a/config/router/funcRouter.go b/config/router/funcRouter.go index 9ec8131..cc035a9 100755 --- a/config/router/funcRouter.go +++ b/config/router/funcRouter.go @@ -9,6 +9,7 @@ import ( "wejh-go/app/controllers/funcControllers/noticeController" "wejh-go/app/controllers/funcControllers/schoolBusController" "wejh-go/app/controllers/funcControllers/suppliesController" + "wejh-go/app/controllers/funcControllers/themeController" "wejh-go/app/controllers/funcControllers/zfController" "wejh-go/app/controllers/yxyController/electricityController" "wejh-go/app/controllers/yxyController/schoolCardController" @@ -101,7 +102,13 @@ func funcRouterInit(r *gin.RouterGroup) { borrow.GET("", suppliesController.GetBorrowRecord) borrow.DELETE("", suppliesController.CancelBorrow) } - + } + + // 主题色 + theme := fun.Group("/theme", midwares.CheckLogin) + { + theme.GET("/get", themeController.GetThemeList) + theme.POST("/choose", themeController.ChooseCurrentTheme) } } }