ferry/apis/system/dept.go

176 lines
4.1 KiB
Go
Raw Normal View History

2020-07-13 20:33:20 +08:00
package system
import (
2020-07-14 14:07:44 +08:00
"ferry/models/system"
2020-07-13 20:33:20 +08:00
"ferry/tools"
"ferry/tools/app"
"ferry/tools/app/msg"
"github.com/gin-gonic/gin"
"github.com/gin-gonic/gin/binding"
)
// @Summary 分页部门列表数据
// @Description 分页列表
// @Tags 部门
// @Param name query string false "name"
// @Param id query string false "id"
// @Param position query string false "position"
// @Success 200 {object} app.Response "{"code": 200, "data": [...]}"
// @Router /api/v1/deptList [get]
// @Security
func GetDeptList(c *gin.Context) {
2020-07-16 01:22:01 +08:00
var (
Dept system.Dept
err error
)
2020-07-13 20:33:20 +08:00
Dept.DeptName = c.Request.FormValue("deptName")
Dept.Status = c.Request.FormValue("status")
Dept.DeptId, _ = tools.StringToInt(c.Request.FormValue("deptId"))
2020-07-16 01:22:01 +08:00
2020-07-13 20:33:20 +08:00
result, err := Dept.SetDept(true)
2020-07-16 01:22:01 +08:00
if err != nil {
app.Error(c, -1, err, "")
return
}
2020-07-13 20:33:20 +08:00
app.OK(c, result, "")
}
func GetDeptTree(c *gin.Context) {
2020-07-16 01:22:01 +08:00
var (
Dept system.Dept
err error
)
2020-07-13 20:33:20 +08:00
Dept.DeptName = c.Request.FormValue("deptName")
Dept.Status = c.Request.FormValue("status")
Dept.DeptId, _ = tools.StringToInt(c.Request.FormValue("deptId"))
2020-07-16 01:22:01 +08:00
2020-07-13 20:33:20 +08:00
result, err := Dept.SetDept(false)
2020-07-16 01:22:01 +08:00
if err != nil {
app.Error(c, -1, err, "")
return
}
2020-07-13 20:33:20 +08:00
app.OK(c, result, "")
}
// @Summary 部门列表数据
// @Description 获取JSON
// @Tags 部门
// @Param deptId path string false "deptId"
// @Param position query string false "position"
// @Success 200 {object} app.Response "{"code": 200, "data": [...]}"
// @Router /api/v1/dept/{deptId} [get]
// @Security
func GetDept(c *gin.Context) {
2020-07-16 01:22:01 +08:00
var (
err error
Dept system.Dept
)
2020-07-13 20:33:20 +08:00
Dept.DeptId, _ = tools.StringToInt(c.Param("deptId"))
2020-07-16 01:22:01 +08:00
2020-07-13 20:33:20 +08:00
result, err := Dept.Get()
2020-07-16 01:22:01 +08:00
if err != nil {
app.Error(c, -1, err, "")
return
}
2020-07-13 20:33:20 +08:00
app.OK(c, result, msg.GetSuccess)
}
// @Summary 添加部门
// @Description 获取JSON
// @Tags 部门
// @Accept application/json
// @Product application/json
// @Param data body models.Dept true "data"
// @Success 200 {string} string "{"code": 200, "message": "添加成功"}"
// @Success 200 {string} string "{"code": -1, "message": "添加失败"}"
// @Router /api/v1/dept [post]
// @Security Bearer
func InsertDept(c *gin.Context) {
2020-07-14 14:07:44 +08:00
var data system.Dept
2020-07-13 20:33:20 +08:00
err := c.BindWith(&data, binding.JSON)
2020-07-16 01:22:01 +08:00
if err != nil {
app.Error(c, -1, err, "")
return
}
2020-07-13 20:33:20 +08:00
data.CreateBy = tools.GetUserIdStr(c)
result, err := data.Create()
2020-07-16 01:22:01 +08:00
if err != nil {
app.Error(c, -1, err, "")
return
}
2020-07-13 20:33:20 +08:00
app.OK(c, result, msg.CreatedSuccess)
}
// @Summary 修改部门
// @Description 获取JSON
// @Tags 部门
// @Accept application/json
// @Product application/json
// @Param id path int true "id"
// @Param data body models.Dept true "body"
// @Success 200 {string} string "{"code": 200, "message": "添加成功"}"
// @Success 200 {string} string "{"code": -1, "message": "添加失败"}"
// @Router /api/v1/dept [put]
// @Security Bearer
func UpdateDept(c *gin.Context) {
2020-07-14 14:07:44 +08:00
var data system.Dept
2020-07-13 20:33:20 +08:00
err := c.BindJSON(&data)
2020-07-16 01:22:01 +08:00
if err != nil {
app.Error(c, -1, err, "")
return
}
2020-07-13 20:33:20 +08:00
data.UpdateBy = tools.GetUserIdStr(c)
result, err := data.Update(data.DeptId)
2020-07-16 01:22:01 +08:00
if err != nil {
app.Error(c, -1, err, "")
return
}
2020-07-13 20:33:20 +08:00
app.OK(c, result, msg.UpdatedSuccess)
}
// @Summary 删除部门
// @Description 删除数据
// @Tags 部门
// @Param id path int true "id"
// @Success 200 {string} string "{"code": 200, "message": "删除成功"}"
// @Success 200 {string} string "{"code": -1, "message": "删除失败"}"
// @Router /api/v1/dept/{id} [delete]
func DeleteDept(c *gin.Context) {
2020-07-14 14:07:44 +08:00
var data system.Dept
2020-07-14 14:47:17 +08:00
id, _ := tools.StringToInt(c.Param("id"))
2020-07-16 01:22:01 +08:00
2020-07-14 14:47:17 +08:00
_, err := data.Delete(id)
2020-07-16 01:22:01 +08:00
if err != nil {
app.Error(c, -1, err, "")
return
}
2020-07-13 20:33:20 +08:00
app.OK(c, "", msg.DeletedSuccess)
}
func GetDeptTreeRoleselect(c *gin.Context) {
2020-07-14 14:07:44 +08:00
var Dept system.Dept
var SysRole system.SysRole
2020-07-14 14:47:17 +08:00
id, _ := tools.StringToInt(c.Param("roleId"))
2020-07-16 01:22:01 +08:00
2020-07-13 20:33:20 +08:00
SysRole.RoleId = id
result, err := Dept.SetDeptLable()
2020-07-16 01:22:01 +08:00
if err != nil {
app.Error(c, -1, err, "")
return
}
2020-07-13 20:33:20 +08:00
menuIds := make([]int, 0)
if id != 0 {
menuIds, err = SysRole.GetRoleDeptId()
2020-07-16 01:22:01 +08:00
if err != nil {
app.Error(c, -1, err, "")
return
}
2020-07-13 20:33:20 +08:00
}
app.Custum(c, gin.H{
"code": 200,
"depts": result,
"checkedKeys": menuIds,
})
}