yinbentan
2024-07-25 1bd58eb901da8ca32d15c121effa7e22cc89e1bd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
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(&params)
    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(&params)
    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(&params)
    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, "删除成功")
}