liujiandao
2024-01-08 dcf9d1324c4ecc23bc0b120ee9af9f0f74cde034
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
package controllers
 
import (
    "errors"
    "fmt"
    "github.com/gin-gonic/gin"
    "gorm.io/gorm"
    "wms/extend/code"
    "wms/extend/util"
    "wms/models"
    "wms/pkg/convertx"
    "wms/pkg/structx"
    "wms/request"
)
 
type DepartmentController struct{}
 
// Add
// @Tags      部门信息
// @Summary   添加部门信息
// @Produce   application/json
// @Param     object  body  request.AddDepartment true  "部门信息信息"
// @Success   200 {object} util.Response "成功"
// @Router    /api-s/v1/organize/department [post]
func (slf DepartmentController) Add(c *gin.Context) {
    var reqParams request.AddDepartment
    var params models.Department
    if err := c.BindJSON(&reqParams); err != nil {
        util.ResponseFormat(c, code.RequestParamError, "参数解析失败,数据类型错误")
        return
    }
    if err := structx.AssignTo(reqParams, &params); err != nil {
        util.ResponseFormat(c, code.RequestParamError, "数据转换错误")
        return
    }
 
    if err := slf.ParamsCheck(params); err != nil {
        util.ResponseFormat(c, code.RequestParamError, err.Error())
        return
    }
    if err := models.NewDepartmentSearch().Create(&params); err != nil {
        util.ResponseFormat(c, code.SaveFail, "插入失败")
        return
    }
 
    util.ResponseFormat(c, code.Success, "添加成功")
}
 
// Update
// @Tags      部门信息
// @Summary   编辑部门信息
// @Produce   application/json
// @Param     object  body request.UpdateDepartment true  "部门信息信息"
// @Success   200 {object} util.Response "成功"
// @Router    /api-s/v1/organize/department/{id} [put]
func (slf DepartmentController) Update(c *gin.Context) {
    idStr := c.Param("id")
    if idStr == "0" || idStr == "" {
        util.ResponseFormat(c, code.RequestParamError, "空的记录id")
        return
    }
    var (
        reqParams request.UpdateDepartment
        params    models.Department
    )
    reqParams.ID = uint(convertx.Atoi(idStr))
    if err := c.BindJSON(&reqParams); err != nil {
        util.ResponseFormat(c, code.RequestParamError, fmt.Sprintf("参数解析失败: %v"+err.Error()))
        return
    }
    if err := structx.AssignTo(reqParams, &params); err != nil {
        util.ResponseFormat(c, code.RequestParamError, fmt.Sprintf("数据转换错误: %v", err.Error()))
        return
    }
    if err := slf.ParamsCheck(params); err != nil {
        util.ResponseFormat(c, code.RequestParamError, err.Error())
        return
    }
 
    _, err := models.NewDepartmentSearch().SetID(params.ID).First()
    if err != nil {
        util.ResponseFormat(c, code.RequestParamError, "ID不存在")
        return
    }
 
    if params.ParentId != 0 {
        //判断有没有环(修改上级部门时错选了自己的下级部门)
        parentId := params.ParentId
        for {
            if parentId == params.ID {
                util.ResponseFormat(c, code.RequestParamError, "不能把自己或自己的下级部门当做上级部门")
                return
            }
            if parentId == 0 {
                break
            }
            parent, err := models.NewDepartmentSearch().SetID(parentId).First()
            if err == gorm.ErrRecordNotFound {
                break
            } else if err != nil {
                util.ResponseFormat(c, code.RequestParamError, "数据查询错误")
                return
            }
            parentId = parent.ParentId
        }
    }
 
    err = models.NewDepartmentSearch().SetID(params.ID).Save(&params)
 
    if err != nil {
        util.ResponseFormat(c, code.RequestParamError, "修改失败")
        return
    }
 
    util.ResponseFormat(c, code.UpdateSuccess, "更新成功")
}
 
func (slf DepartmentController) ParamsCheck(params models.Department) (err error) {
    if params.ParentId != 0 {
        _, err := models.NewDepartmentSearch().SetID(params.ParentId).First()
        if err != nil {
            return errors.New("上级部门不存在")
        }
    }
    dep, err := models.NewDepartmentSearch().SetNumber(params.Number).First()
    if err != gorm.ErrRecordNotFound && dep != nil && dep.ID != params.ID {
        return errors.New("部门编号重复")
    }
 
    _, err = models.NewDepartmentSearch().SetName(params.Name).First()
    if err != gorm.ErrRecordNotFound && dep != nil && dep.ID != params.ID {
        return errors.New("部门名称重复")
    }
 
    return nil
}
 
// List
// @Tags      部门信息
// @Summary   查询部门信息列表
// @Produce   application/json
// @Param     object  query    request.GetDepartmentList true  "查询参数"
// @Success   200   {object}  util.ResponseList{data=[]models.Department}  "成功"
// @Router    /api-s/v1/organize/department [get]
func (slf DepartmentController) List(c *gin.Context) {
    var params request.GetDepartmentList
    if err := c.ShouldBindQuery(&params); err != nil {
        util.ResponseFormat(c, code.RequestParamError, err.Error())
        return
    }
 
    treeMap := make(map[uint][]*models.Department)
    allMenus, err := models.NewDepartmentSearch().FindNotTotal()
    for _, v := range allMenus {
        treeMap[v.ParentId] = append(treeMap[v.ParentId], v)
    }
 
    menus := treeMap[0] //parentId == 0 代表1级部门(没有上级部门)
    for i := 0; i < len(menus); i++ {
        _ = getMenuChildrenList(menus[i], treeMap)
    }
 
    if err != nil {
        util.ResponseFormat(c, code.RequestParamError, "查找失败")
        return
    }
 
    util.ResponseFormatList(c, code.Success, menus, len(menus))
}
 
func getMenuChildrenList(menu *models.Department, treeMap map[uint][]*models.Department) (err error) {
    menu.Children = treeMap[menu.ID]
    for i := 0; i < len(menu.Children); i++ {
        err = getMenuChildrenList(menu.Children[i], treeMap)
    }
    return err
}
 
// Delete
// @Tags      部门信息
// @Summary   编辑部门信息
// @Produce   application/json
// @Param     object  body request.UpdateDepartment true  "部门信息信息"
// @Success   200 {object} util.Response "成功"
// @Router    /api-s/v1/organize/department/{id} [delete]
func (slf DepartmentController) Delete(c *gin.Context) {
    idStr := c.Param("id")
    if idStr == "0" || idStr == "" {
        util.ResponseFormat(c, code.RequestParamError, "空的记录id")
        return
    }
 
    id := convertx.StringToUInt(idStr)
    _, err := models.NewDepartmentSearch().SetParentId(id).First()
    if err != gorm.ErrRecordNotFound {
        util.ResponseFormat(c, code.RequestParamError, "请先删除下级部门")
        return
    }
 
    err = models.NewDepartmentSearch().SetID(id).Delete()
    if err != nil {
        util.ResponseFormat(c, code.RequestParamError, "删除失败")
        return
    }
    util.ResponseFormat(c, code.UpdateSuccess, "删除成功")
}