golang-yitisheng-server/server/modules/user/controller/user_volunteer_controller.go

200 lines
6.5 KiB
Go
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package controller
import (
"server/common"
"server/modules/user/dto"
"server/modules/user/service"
"server/modules/user/vo"
yxDto "server/modules/yx/dto"
"server/modules/yx/entity"
yx_service "server/modules/yx/service"
"time" // 用于时间处理
"github.com/gin-gonic/gin"
)
// _ 确保包被导入(用于类型引用)
var _ = yxDto.SchoolMajorDTO{}
var _ = entity.YxVolunteerRecord{}
var _ = time.Now()
var _ = vo.VolunteerDetailVO{}
type UserVolunteerController struct {
userScoreService *service.UserScoreService
yxVolunteerService *yx_service.YxVolunteerService
yxVolunteerRecordService *yx_service.YxVolunteerRecordService
yxCalculationMajorService *yx_service.YxCalculationMajorService
}
func NewUserVolunteerController() *UserVolunteerController {
return &UserVolunteerController{
userScoreService: service.NewUserScoreService(),
yxVolunteerService: yx_service.NewYxVolunteerService(),
yxVolunteerRecordService: yx_service.NewYxVolunteerRecordService(),
yxCalculationMajorService: yx_service.NewYxCalculationMajorService(),
}
}
func (ctrl *UserVolunteerController) RegisterRoutes(rg *gin.RouterGroup) {
group := rg.Group("/user/volunteer")
{
group.POST("/save", ctrl.SaveVolunteer)
group.GET("/detail", ctrl.GetVolunteerDetail)
group.PUT("/updateName", ctrl.UpdateVolunteerName)
group.GET("/list", ctrl.GetUserVolunteerList)
group.DELETE("/delete", ctrl.DeleteVolunteer)
group.POST("/switch", ctrl.SwitchVolunteer)
}
}
// SaveVolunteer 保存志愿明细
// @Summary 保存志愿明细
// @Tags 用户志愿
// @Param request body dto.SaveVolunteerRequest true "志愿键列表"
// @Success 200 {object} common.Response
// @Router /user/volunteer/save [post]
func (ctrl *UserVolunteerController) SaveVolunteer(c *gin.Context) {
var req dto.SaveVolunteerRequest
if err := c.ShouldBindJSON(&req); err != nil {
common.Error(c, 500, err.Error())
return
}
loginUserId := common.GetLoginUser(c).ID
if err := ctrl.userScoreService.SaveVolunteer(loginUserId, &req); err != nil {
common.Error(c, 500, err.Error())
return
}
common.Success(c, "保存成功")
}
// GetVolunteerDetail 获取当前志愿单详情
// @Summary 获取当前志愿单详情
// @Tags 用户志愿
// @Success 200 {object} common.Response{data=vo.VolunteerDetailResponse}
// @Router /user/volunteer/detail [get]
func (ctrl *UserVolunteerController) GetVolunteerDetail(c *gin.Context) {
loginUserId := common.GetLoginUser(c).ID
response, err := ctrl.userScoreService.GetVolunteerDetail(loginUserId)
if err != nil {
common.Error(c, 500, err.Error())
return
}
common.Success(c, response)
}
// UpdateVolunteerName 编辑志愿单名称
// @Summary 编辑志愿单名称
// @Tags 用户志愿
// @Param id query string true "志愿单ID"
// @Param name query string true "志愿单名称"
// @Success 200 {object} common.Response
// @Router /user/volunteer/updateName [put]
func (ctrl *UserVolunteerController) UpdateVolunteerName(c *gin.Context) {
id := c.Query("id")
name := c.Query("name")
if id == "" || name == "" {
common.Error(c, 400, "参数错误")
return
}
loginUserID := common.GetLoginUser(c).ID
if err := ctrl.yxVolunteerService.UpdateName(id, name, loginUserID); err != nil {
common.Error(c, 500, err.Error())
return
}
common.Success(c, "更新成功")
}
// GetUserVolunteerList 获取当前用户志愿单列表
// @Summary 获取当前用户志愿单列表
// @Tags 用户志愿
// @Param page query int false "页码"
// @Param size query int false "每页数量"
// @Success 200 {object} common.Response
// @Router /user/volunteer/list [get]
func (ctrl *UserVolunteerController) GetUserVolunteerList(c *gin.Context) {
page := common.GetPage(c)
size := common.GetSize(c)
loginUserID := common.GetLoginUser(c).ID
items, total, err := ctrl.yxVolunteerService.ListByUser(loginUserID, page, size)
if err != nil {
common.Error(c, 500, err.Error())
return
}
common.Success(c, map[string]interface{}{
"items": items,
"total": total,
})
}
// DeleteVolunteer 删除志愿单接口
// @Summary 删除志愿单接口
// @Tags 用户志愿
// @Param id query string true "志愿单ID"
// @Success 200 {object} common.Response
// @Router /user/volunteer/delete [delete]
func (ctrl *UserVolunteerController) DeleteVolunteer(c *gin.Context) {
id := c.Query("id")
if id == "" {
common.Error(c, 400, "参数错误")
return
}
loginUserID := common.GetLoginUser(c).ID
err := ctrl.yxVolunteerService.DeleteVolunteer(id, loginUserID)
if err != nil {
common.Error(c, 500, err.Error())
return
}
common.Success(c, "删除成功")
}
// SwitchVolunteer 切换当前志愿单
// @Summary 切换当前志愿单
// @Tags 用户志愿
// @Param id query string true "志愿单ID"
// @Success 200 {object} common.Response
// @Router /user/volunteer/switch [post]
func (ctrl *UserVolunteerController) SwitchVolunteer(c *gin.Context) {
id := c.Query("id")
if id == "" {
common.Error(c, 400, "参数错误")
return
}
loginUserID := common.GetLoginUser(c).ID
// 1. 先判断是否已是该志愿单 (从 cache 或 db 中查找激活的)
userScoreVO, err := ctrl.userScoreService.GetActiveScoreByUserID(loginUserID)
if err != nil {
common.Error(c, 500, "获取用户成绩信息失败: "+err.Error())
return
}
if userScoreVO.ID != "" {
activeVolunteer, _ := ctrl.yxVolunteerService.FindActiveByScoreId(userScoreVO.ID)
if activeVolunteer != nil && activeVolunteer.ID == id {
common.Success(c, "已是当前志愿单,忽略切换")
return
}
}
// 2. 切换
if err := ctrl.yxVolunteerService.SwitchVolunteer(id, loginUserID, ctrl.userScoreService); err != nil {
common.Error(c, 500, err.Error())
return
}
// 3. Redis 缓存同步 (假设 common 中有清除缓存的方法逻辑,或者由 Service 处理)
// 这里按需求提到:切换志愿单切换后 写入到redis做缓存查询志愿单接口也要从redis读本体数据再清除之前的成绩单redis重新获取写到redis缓存。
// 实际项目中 common.GetLoginUser 已经包含了 ID成绩单和志愿单通常由具体模块处理缓存。
// 如果这里只是标记Service 层已做状态更新,查询接口读取时会感知变化。
// 为了彻底满足“写入/清除 redis 缓存”,通常会有相应的 RedisUtil 调用。
// 假设 service 层或 controller 有 Cache 清理逻辑。
common.Success(c, "切换成功")
}