238 lines
6.5 KiB
Go
238 lines
6.5 KiB
Go
package server
|
|
|
|
import (
|
|
"strconv"
|
|
|
|
"kra/internal/biz"
|
|
"kra/internal/service"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
)
|
|
|
|
func registerDepartmentRoutes(group *gin.RouterGroup, svc *service.AccessService) {
|
|
router := group.Group("/department")
|
|
router.POST("/getDepartmentList", func(c *gin.Context) {
|
|
var req struct {
|
|
Name string `json:"name"`
|
|
}
|
|
_ = c.ShouldBindJSON(&req)
|
|
items, err := svc.Departments(c.Request.Context(), req.Name)
|
|
if err != nil {
|
|
fail(c, "获取失败")
|
|
return
|
|
}
|
|
writeResult(c, codeSuccess, items, "获取成功")
|
|
})
|
|
type departmentRequest struct {
|
|
ID uint `json:"ID"`
|
|
Name string `json:"name"`
|
|
ParentID uint `json:"parentId"`
|
|
Sort int `json:"sort"`
|
|
LeaderID uint `json:"leaderId"`
|
|
Status bool `json:"status"`
|
|
}
|
|
router.POST("/createDepartment", func(c *gin.Context) {
|
|
var req departmentRequest
|
|
if c.ShouldBindJSON(&req) != nil {
|
|
fail(c, "参数错误")
|
|
return
|
|
}
|
|
if err := svc.CreateDepartment(c.Request.Context(), &biz.Department{Name: req.Name, ParentID: req.ParentID, Sort: req.Sort, LeaderID: req.LeaderID, Status: req.Status}); err != nil {
|
|
fail(c, "创建失败")
|
|
return
|
|
}
|
|
ok(c)
|
|
})
|
|
router.PUT("/updateDepartment", func(c *gin.Context) {
|
|
var req departmentRequest
|
|
if c.ShouldBindJSON(&req) != nil {
|
|
fail(c, "参数错误")
|
|
return
|
|
}
|
|
if err := svc.UpdateDepartment(c.Request.Context(), &biz.Department{ID: req.ID, Name: req.Name, ParentID: req.ParentID, Sort: req.Sort, LeaderID: req.LeaderID, Status: req.Status}); err != nil {
|
|
fail(c, "更新失败")
|
|
return
|
|
}
|
|
ok(c)
|
|
})
|
|
router.DELETE("/deleteDepartment", func(c *gin.Context) {
|
|
var req struct {
|
|
ID uint `json:"ID"`
|
|
}
|
|
if c.ShouldBindJSON(&req) != nil {
|
|
fail(c, "参数错误")
|
|
return
|
|
}
|
|
if err := svc.DeleteDepartment(c.Request.Context(), req.ID); err != nil {
|
|
fail(c, err.Error())
|
|
return
|
|
}
|
|
ok(c)
|
|
})
|
|
router.GET("/findDepartment", func(c *gin.Context) {
|
|
id, _ := strconv.ParseUint(c.Query("id"), 10, 64)
|
|
item, err := svc.Department(c.Request.Context(), uint(id))
|
|
if err != nil {
|
|
fail(c, "获取失败")
|
|
return
|
|
}
|
|
writeResult(c, codeSuccess, item, "获取成功")
|
|
})
|
|
router.GET("/getDepartmentUsers", func(c *gin.Context) {
|
|
id, _ := strconv.ParseUint(c.Query("deptId"), 10, 64)
|
|
ids, err := svc.DepartmentUserIDs(c.Request.Context(), uint(id))
|
|
if err != nil {
|
|
fail(c, "获取失败")
|
|
return
|
|
}
|
|
writeResult(c, codeSuccess, ids, "获取成功")
|
|
})
|
|
router.POST("/setDepartmentUsers", func(c *gin.Context) {
|
|
var req struct {
|
|
DepartmentID uint `json:"deptId"`
|
|
UserIDs []uint `json:"userIds"`
|
|
}
|
|
if c.ShouldBindJSON(&req) != nil {
|
|
fail(c, "参数错误")
|
|
return
|
|
}
|
|
if err := svc.SetDepartmentUsers(c.Request.Context(), req.DepartmentID, req.UserIDs); err != nil {
|
|
fail(c, "设置失败")
|
|
return
|
|
}
|
|
ok(c)
|
|
})
|
|
group.POST("/user/setUserDepartments", func(c *gin.Context) {
|
|
var req struct {
|
|
ID uint `json:"ID"`
|
|
DepartmentIDs []uint `json:"deptIds"`
|
|
Primary uint `json:"primaryDeptId"`
|
|
}
|
|
if c.ShouldBindJSON(&req) != nil {
|
|
fail(c, "参数错误")
|
|
return
|
|
}
|
|
if err := svc.SetUserDepartments(c.Request.Context(), req.ID, req.DepartmentIDs, req.Primary); err != nil {
|
|
fail(c, "设置失败")
|
|
return
|
|
}
|
|
ok(c)
|
|
})
|
|
}
|
|
|
|
func registerPositionRoutes(group *gin.RouterGroup, svc *service.AccessService) {
|
|
router := group.Group("/position")
|
|
router.POST("/getPositionList", func(c *gin.Context) {
|
|
var req struct {
|
|
Page int `json:"page"`
|
|
PageSize int `json:"pageSize"`
|
|
Name string `json:"name"`
|
|
Code string `json:"code"`
|
|
}
|
|
if c.ShouldBindJSON(&req) != nil {
|
|
fail(c, "参数错误")
|
|
return
|
|
}
|
|
items, total, err := svc.Positions(c.Request.Context(), req.Page, req.PageSize, &biz.Position{Name: req.Name, Code: req.Code})
|
|
if err != nil {
|
|
fail(c, "获取失败")
|
|
return
|
|
}
|
|
writeResult(c, codeSuccess, pageResult{List: items, Total: total, Page: req.Page, PageSize: req.PageSize}, "获取成功")
|
|
})
|
|
type positionRequest struct {
|
|
ID uint `json:"ID"`
|
|
Name string `json:"name"`
|
|
Code string `json:"code"`
|
|
Sort int `json:"sort"`
|
|
Status bool `json:"status"`
|
|
Remark string `json:"remark"`
|
|
}
|
|
router.POST("/createPosition", func(c *gin.Context) {
|
|
var req positionRequest
|
|
if c.ShouldBindJSON(&req) != nil {
|
|
fail(c, "参数错误")
|
|
return
|
|
}
|
|
if err := svc.CreatePosition(c.Request.Context(), &biz.Position{Name: req.Name, Code: req.Code, Sort: req.Sort, Status: req.Status, Remark: req.Remark}); err != nil {
|
|
fail(c, "创建失败")
|
|
return
|
|
}
|
|
ok(c)
|
|
})
|
|
router.PUT("/updatePosition", func(c *gin.Context) {
|
|
var req positionRequest
|
|
if c.ShouldBindJSON(&req) != nil {
|
|
fail(c, "参数错误")
|
|
return
|
|
}
|
|
if err := svc.UpdatePosition(c.Request.Context(), &biz.Position{ID: req.ID, Name: req.Name, Code: req.Code, Sort: req.Sort, Status: req.Status, Remark: req.Remark}); err != nil {
|
|
fail(c, "更新失败")
|
|
return
|
|
}
|
|
ok(c)
|
|
})
|
|
router.DELETE("/deletePosition", func(c *gin.Context) {
|
|
var req struct {
|
|
ID uint `json:"ID"`
|
|
}
|
|
if c.ShouldBindJSON(&req) != nil {
|
|
fail(c, "参数错误")
|
|
return
|
|
}
|
|
if err := svc.DeletePosition(c.Request.Context(), req.ID); err != nil {
|
|
fail(c, "删除失败")
|
|
return
|
|
}
|
|
ok(c)
|
|
})
|
|
router.GET("/findPosition", func(c *gin.Context) {
|
|
id, _ := strconv.ParseUint(c.Query("id"), 10, 64)
|
|
item, err := svc.Position(c.Request.Context(), uint(id))
|
|
if err != nil {
|
|
fail(c, "获取失败")
|
|
return
|
|
}
|
|
writeResult(c, codeSuccess, item, "获取成功")
|
|
})
|
|
router.GET("/getPositionUsers", func(c *gin.Context) {
|
|
id, _ := strconv.ParseUint(c.Query("positionId"), 10, 64)
|
|
ids, err := svc.PositionUserIDs(c.Request.Context(), uint(id))
|
|
if err != nil {
|
|
fail(c, "获取失败")
|
|
return
|
|
}
|
|
writeResult(c, codeSuccess, ids, "获取成功")
|
|
})
|
|
router.POST("/setPositionUsers", func(c *gin.Context) {
|
|
var req struct {
|
|
PositionID uint `json:"positionId"`
|
|
UserIDs []uint `json:"userIds"`
|
|
}
|
|
if c.ShouldBindJSON(&req) != nil {
|
|
fail(c, "参数错误")
|
|
return
|
|
}
|
|
if err := svc.SetPositionUsers(c.Request.Context(), req.PositionID, req.UserIDs); err != nil {
|
|
fail(c, "设置失败")
|
|
return
|
|
}
|
|
ok(c)
|
|
})
|
|
group.POST("/user/setUserPositions", func(c *gin.Context) {
|
|
var req struct {
|
|
ID uint `json:"ID"`
|
|
PositionIDs []uint `json:"positionIds"`
|
|
}
|
|
if c.ShouldBindJSON(&req) != nil {
|
|
fail(c, "参数错误")
|
|
return
|
|
}
|
|
if err := svc.SetUserPositions(c.Request.Context(), req.ID, req.PositionIDs); err != nil {
|
|
fail(c, "设置失败")
|
|
return
|
|
}
|
|
ok(c)
|
|
})
|
|
}
|