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-wms/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, ¶ms); 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(¶ms); 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-wms/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, ¶ms); 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(¶ms)
|
|
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-wms/v1/organize/department [get]
|
func (slf DepartmentController) List(c *gin.Context) {
|
var params request.GetDepartmentList
|
if err := c.ShouldBindQuery(¶ms); 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-wms/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, "删除成功")
|
}
|