mirror of
https://gitlab.52pay.top/go/easygoadmin.git
synced 2025-09-26 22:58:01 +08:00
170 lines
3.2 KiB
Go
170 lines
3.2 KiB
Go
/**
|
|
* 部门管理-控制器
|
|
* @author
|
|
* @since 2021/9/13
|
|
* @File : dept
|
|
*/
|
|
package controller
|
|
|
|
import (
|
|
"easygoadmin/app/dto"
|
|
"easygoadmin/app/model"
|
|
"easygoadmin/app/service"
|
|
"easygoadmin/utils"
|
|
"easygoadmin/utils/common"
|
|
"easygoadmin/utils/gconv"
|
|
"easygoadmin/utils/response"
|
|
"github.com/gin-gonic/gin"
|
|
"net/http"
|
|
)
|
|
|
|
var Dept = new(deptCtl)
|
|
|
|
type deptCtl struct{}
|
|
|
|
func (c *deptCtl) Index(ctx *gin.Context) {
|
|
// 渲染模板
|
|
response.BuildTpl(ctx, "dept_index.html").WriteTpl()
|
|
}
|
|
|
|
func (c *deptCtl) List(ctx *gin.Context) {
|
|
//// 参数绑定
|
|
//var req *dto.DeptPageReq
|
|
//if err := ctx.ShouldBind(&req); err != nil {
|
|
// ctx.JSON(http.StatusOK, common.JsonResult{
|
|
// Code: -1,
|
|
// Msg: err.Error(),
|
|
// })
|
|
// return
|
|
//}
|
|
|
|
// 调用查询列表方法
|
|
list, err := service.Dept.GetList(nil)
|
|
if err != nil {
|
|
ctx.JSON(http.StatusOK, common.JsonResult{
|
|
Code: -1,
|
|
Msg: err.Error(),
|
|
})
|
|
return
|
|
}
|
|
|
|
// 返回结果
|
|
ctx.JSON(http.StatusOK, common.JsonResult{
|
|
Code: 0,
|
|
Msg: "操作成功",
|
|
Data: list,
|
|
})
|
|
}
|
|
|
|
func (c *deptCtl) Edit(ctx *gin.Context) {
|
|
// 查询记录
|
|
id := gconv.Int(ctx.Query("id"))
|
|
if id > 0 {
|
|
info := &model.Dept{Id: id}
|
|
isOk, err := info.Get()
|
|
if !isOk || err != nil {
|
|
ctx.JSON(http.StatusOK, common.JsonResult{
|
|
Code: -1,
|
|
Msg: err.Error(),
|
|
})
|
|
}
|
|
// 渲染模板
|
|
response.BuildTpl(ctx, "dept_edit.html").WriteTpl(gin.H{
|
|
"info": info,
|
|
"typeList": common.DEPT_TYPE_LIST,
|
|
})
|
|
} else {
|
|
// 渲染模板
|
|
response.BuildTpl(ctx, "dept_edit.html").WriteTpl(gin.H{
|
|
"typeList": common.DEPT_TYPE_LIST,
|
|
})
|
|
}
|
|
}
|
|
|
|
func (c *deptCtl) Add(ctx *gin.Context) {
|
|
// 参数绑定
|
|
var req *dto.DeptAddReq
|
|
if err := ctx.ShouldBind(&req); err != nil {
|
|
ctx.JSON(http.StatusOK, common.JsonResult{
|
|
Code: -1,
|
|
Msg: err.Error(),
|
|
})
|
|
return
|
|
}
|
|
|
|
// 调用添加方法
|
|
rows, err := service.Dept.Add(req, utils.Uid(ctx))
|
|
if err != nil || rows == 0 {
|
|
ctx.JSON(http.StatusOK, common.JsonResult{
|
|
Code: -1,
|
|
Msg: err.Error(),
|
|
})
|
|
return
|
|
}
|
|
|
|
// 返回结果
|
|
ctx.JSON(http.StatusOK, common.JsonResult{
|
|
Code: 0,
|
|
Msg: "添加成功",
|
|
})
|
|
}
|
|
|
|
func (c *deptCtl) Update(ctx *gin.Context) {
|
|
// 参数绑定
|
|
var req *dto.DeptUpdateReq
|
|
if err := ctx.ShouldBind(&req); err != nil {
|
|
ctx.JSON(http.StatusOK, common.JsonResult{
|
|
Code: -1,
|
|
Msg: err.Error(),
|
|
})
|
|
return
|
|
}
|
|
|
|
// 调用更新方法
|
|
rows, err := service.Dept.Update(req, utils.Uid(ctx))
|
|
if err != nil || rows == 0 {
|
|
ctx.JSON(http.StatusOK, common.JsonResult{
|
|
Code: -1,
|
|
Msg: err.Error(),
|
|
})
|
|
}
|
|
|
|
// 返回结果
|
|
ctx.JSON(http.StatusOK, common.JsonResult{
|
|
Code: 0,
|
|
Msg: "更新成功",
|
|
})
|
|
}
|
|
|
|
func (c *deptCtl) Delete(ctx *gin.Context) {
|
|
// 记录ID
|
|
ids := ctx.Param("ids")
|
|
// 调用删除方法
|
|
rows, err := service.Dept.Delete(ids)
|
|
if err != nil || rows == 0 {
|
|
ctx.JSON(http.StatusOK, common.JsonResult{
|
|
Code: -1,
|
|
Msg: err.Error(),
|
|
})
|
|
return
|
|
}
|
|
|
|
// 返回结果
|
|
ctx.JSON(http.StatusOK, common.JsonResult{
|
|
Code: 0,
|
|
Msg: "删除成功",
|
|
})
|
|
}
|
|
|
|
func (c *deptCtl) GetDeptList(ctx *gin.Context) {
|
|
// 查询部门列表
|
|
list := make([]model.Dept, 0)
|
|
utils.XormDb.Where("mark=1").Asc("sort").Find(&list)
|
|
// 返回结果
|
|
ctx.JSON(http.StatusOK, common.JsonResult{
|
|
Code: 0,
|
|
Msg: "查询成功",
|
|
Data: list,
|
|
})
|
|
}
|