You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

109 lines
2.7 KiB

package admin
import (
"encoding/json"
"github.com/beego/beego/v2/core/logs"
"github.com/beego/beego/v2/core/validation"
"viviman.top/controllers"
"viviman.top/models"
"viviman.top/vo"
)
// DeptController 部门api
type DeptController struct {
controllers.BaseController
}
func (c *DeptController) URLMapping() {
c.Mapping("Post", c.Post)
c.Mapping("GetAll", c.GetAll)
c.Mapping("Put", c.Put)
c.Mapping("Delete", c.Delete)
}
// GetAll @Title获取部门列表
// @Description 获取部门列表
// @Success 200 {object} controllers.Result
// @router / [get]
func (c *DeptController) GetAll() {
name := c.GetString("name")
enabled, _ := c.GetInt8("enabled", -1)
list := models.GetAllDepts(name, enabled)
c.Data["json"] = controllers.SuccessData(vo.ResultList{Content: list, TotalElements: 0})
_ = c.ServeJSON()
}
// Post @Title添加部门
// @Description 添加部门
// @Success 200 {object} controllers.Result
// @router / [post]
func (c *DeptController) Post() {
var model models.SysDept
valid := validation.Validation{}
err := json.Unmarshal(c.Ctx.Input.RequestBody, &model)
if err != nil {
c.Data["json"] = controllers.ErrMsg("转译时,发生异常!")
_ = c.ServeJSON()
return
}
b, _ := valid.Valid(&model)
if !b {
for _, err := range valid.Errors {
c.Data["json"] = controllers.ErrMsg(err.Message)
}
}
_, e := models.AddDept(&model)
if e != nil {
c.Data["json"] = controllers.ErrMsg(e.Error())
}
c.Data["json"] = controllers.SuccessData("操作成功")
_ = c.ServeJSON()
}
// Put @Title修改部门
// @Description 修改部门
// @Success 200 {object} controllers.Result
// @router / [put]
func (c *DeptController) Put() {
var model models.SysDept
valid := validation.Validation{}
err := json.Unmarshal(c.Ctx.Input.RequestBody, &model)
if err != nil {
c.Data["json"] = controllers.ErrMsg("转译时,发生异常!")
_ = c.ServeJSON()
return
}
b, _ := valid.Valid(&model)
if !b {
for _, err := range valid.Errors {
c.Data["json"] = controllers.ErrMsg(err.Message)
}
}
e := models.UpdateByDept(&model)
if e != nil {
c.Data["json"] = controllers.ErrMsg(e.Error())
}
c.Data["json"] = controllers.SuccessData("操作成功")
_ = c.ServeJSON()
}
// Delete @Title 删除部门
// @Description 删除部门
// @Success 200 {object} controllers.Result
// @router / [delete]
func (c *DeptController) Delete() {
var ids []int64
err := json.Unmarshal(c.Ctx.Input.RequestBody, &ids)
if err != nil {
c.Data["json"] = controllers.ErrMsg("转译时,发生异常!")
_ = c.ServeJSON()
return
}
logs.Info("获取对象:{}", ids)
e := models.DelByDept(ids)
if e != nil {
c.Data["json"] = controllers.ErrMsg(e.Error())
}
c.Data["json"] = controllers.SuccessData("操作成功")
_ = c.ServeJSON()
}