package controllers import ( "github.com/gin-gonic/gin" "github.com/spf13/cast" "gorm.io/gorm" "silkserver/controllers/request" "silkserver/extend/code" "silkserver/extend/util" "silkserver/middleware" "silkserver/models" ) type MentorController struct { } // CreateMentorInfo // @Tags 带徒管理 // @Summary 创建带徒信息 // @Produce application/json // @Param object body request.AddMentorRequest true "参数" // @Param Authorization header string true "token" // @Success 200 {object} util.Response "成功" // @Router /api-jl/v1/mentor/createMentorInfo [post] func (slf MentorController) CreateMentorInfo(c *gin.Context) { var params request.AddMentorRequest err := c.BindJSON(¶ms) if err != nil { util.ResponseFormat(c, code.RequestParamError, "参数解析失败,数据类型错误") return } if params.WorkerId == "" || params.Month == "" || params.Days == 0 { util.ResponseFormat(c, code.RequestParamError, "参数缺失") return } //查询是否重复 _, err = models.NewMentorSearch().SetWorkerId(params.WorkerId).SetMonth(params.Month).First() if err != gorm.ErrRecordNotFound { util.ResponseFormat(c, code.RequestParamError, "请勿重复添加") return } record := &models.Mentor{ WorkerId: params.WorkerId, Month: params.Month, Days: params.Days, } info := middleware.GetUserInfo(c) if info != nil { record.Creator = info.NickName } err = models.NewMentorSearch().Create(record) if err != nil { util.ResponseFormat(c, code.RequestParamError, "创建失败, 请重试") return } util.ResponseFormat(c, code.Success, "创建成功") } // UpdateMentorInfo // @Tags 带徒管理 // @Summary 更新带徒信息 // @Produce application/json // @Param object body request.UpdateMentorRequest true "参数" // @Param Authorization header string true "token" // @Success 200 {object} util.Response "成功" // @Router /api-jl/v1/mentor/updateMentorInfo [post] func (slf MentorController) UpdateMentorInfo(c *gin.Context) { var params request.UpdateMentorRequest err := c.BindJSON(¶ms) if err != nil { util.ResponseFormat(c, code.RequestParamError, "参数解析失败,数据类型错误") return } if params.ID == 0 || params.WorkerId == "" || params.Month == "" || params.Days == 0 { util.ResponseFormat(c, code.RequestParamError, "参数缺失") return } record := &models.Mentor{ WorkerId: params.WorkerId, Month: params.Month, Days: params.Days, } info := middleware.GetUserInfo(c) if info != nil { record.Creator = info.NickName } err = models.NewMentorSearch().SetId(params.ID).Update(record) if err != nil { util.ResponseFormat(c, code.RequestParamError, "更新失败") return } util.ResponseFormat(c, code.Success, "更新成功") } // GetMentorList // // @Tags 带徒管理 // @Summary 获取带徒信息列表 // @Produce application/json // @Param object body request.GetMentorList true "参数" // @Param Authorization header string true "token" // @Success 200 {object} util.ResponseList{data=[]models.Mentor} "成功" // @Router /api-jl/v1/mentor/getMentorList [post] func (slf MentorController) GetMentorList(c *gin.Context) { var params request.GetMentorList err := c.BindJSON(¶ms) if err != nil { util.ResponseFormat(c, code.RequestParamError, "参数解析失败,数据类型错误") return } mentors, total, err := models.NewMentorSearch().SetPage(params.Page, params.PageSize).SetKeyword(params.Keyword).SetPreload(true).SetOrder("id desc").Find() if err != nil { util.ResponseFormat(c, code.RequestParamError, "查询失败") return } util.ResponseFormatList(c, code.Success, mentors, total) } // DeleteMentorInfo // // @Tags 带徒管理 // @Summary 删除带徒信息 // @Produce application/json // @Param id path string true "id" // @Param Authorization header string true "token" // @Success 200 {object} util.Response "成功" // @Router /api-jl/v1/mentor/deleteMentorInfo/{id} [delete] func (slf MentorController) DeleteMentorInfo(c *gin.Context) { id := c.Param("id") if id == "" { util.ResponseFormat(c, code.RequestParamError, "无效的id") return } idInt := cast.ToUint(id) if idInt == 0 { util.ResponseFormat(c, code.RequestParamError, "无效的id") return } err := models.NewMentorSearch().SetId(idInt).Delete() if err != nil { util.ResponseFormat(c, code.RequestParamError, "删除失败") return } util.ResponseFormat(c, code.Success, "删除成功") }