221 lines
6.2 KiB
Go
221 lines
6.2 KiB
Go
package handler
|
|
|
|
import (
|
|
"strconv"
|
|
|
|
"kra/internal/server/httpx"
|
|
"kra/internal/service"
|
|
"kra/internal/service/dto"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
)
|
|
|
|
type Organization struct{ service *service.OrganizationService }
|
|
|
|
func NewOrganization(service *service.OrganizationService) *Organization {
|
|
return &Organization{service: service}
|
|
}
|
|
|
|
func (h *Organization) ListDepartments(c *gin.Context) {
|
|
var req dto.DepartmentListRequest
|
|
_ = c.ShouldBindJSON(&req)
|
|
items, err := h.service.Departments(c.Request.Context(), req.Name)
|
|
if err != nil {
|
|
httpx.Fail(c, "获取失败")
|
|
return
|
|
}
|
|
httpx.Write(c, httpx.CodeSuccess, items, "获取成功")
|
|
}
|
|
func (h *Organization) CreateDepartment(c *gin.Context) {
|
|
var req dto.DepartmentRequest
|
|
if err := c.ShouldBindJSON(&req); err != nil {
|
|
httpx.Fail(c, err.Error())
|
|
return
|
|
}
|
|
if req.Name == "" {
|
|
httpx.Fail(c, "部门名称不能为空")
|
|
return
|
|
}
|
|
if err := h.service.CreateDepartment(c.Request.Context(), &req); err != nil {
|
|
httpx.Fail(c, "创建失败:"+err.Error())
|
|
return
|
|
}
|
|
httpx.Write(c, httpx.CodeSuccess, nil, "创建成功")
|
|
}
|
|
func (h *Organization) UpdateDepartment(c *gin.Context) {
|
|
var req dto.DepartmentRequest
|
|
if err := c.ShouldBindJSON(&req); err != nil {
|
|
httpx.Fail(c, err.Error())
|
|
return
|
|
}
|
|
if req.ID == 0 {
|
|
httpx.Fail(c, "部门ID不能为空")
|
|
return
|
|
}
|
|
if err := h.service.UpdateDepartment(c.Request.Context(), &req); err != nil {
|
|
httpx.Fail(c, "更新失败:"+err.Error())
|
|
return
|
|
}
|
|
httpx.Write(c, httpx.CodeSuccess, nil, "更新成功")
|
|
}
|
|
func (h *Organization) DeleteDepartment(c *gin.Context) {
|
|
var req dto.DeleteDepartmentRequest
|
|
if c.ShouldBindJSON(&req) != nil {
|
|
httpx.Fail(c, "参数错误")
|
|
return
|
|
}
|
|
if err := h.service.DeleteDepartment(c.Request.Context(), req.ID); err != nil {
|
|
httpx.Fail(c, err.Error())
|
|
return
|
|
}
|
|
httpx.Write(c, httpx.CodeSuccess, nil, "删除成功")
|
|
}
|
|
func (h *Organization) FindDepartment(c *gin.Context) {
|
|
id, _ := strconv.ParseUint(c.Query("id"), 10, 64)
|
|
item, err := h.service.Department(c.Request.Context(), uint(id))
|
|
if err != nil {
|
|
httpx.Fail(c, "获取失败:"+err.Error())
|
|
return
|
|
}
|
|
httpx.Write(c, httpx.CodeSuccess, item, "获取成功")
|
|
}
|
|
func (h *Organization) DepartmentUsers(c *gin.Context) {
|
|
id, _ := strconv.ParseUint(c.Query("deptId"), 10, 64)
|
|
ids, err := h.service.DepartmentUserIDs(c.Request.Context(), uint(id))
|
|
if err != nil {
|
|
httpx.Fail(c, "获取失败:"+err.Error())
|
|
return
|
|
}
|
|
if ids == nil {
|
|
ids = []uint{}
|
|
}
|
|
httpx.Write(c, httpx.CodeSuccess, ids, "获取成功")
|
|
}
|
|
func (h *Organization) SetDepartmentUsers(c *gin.Context) {
|
|
var req dto.SetDepartmentUsersRequest
|
|
if c.ShouldBindJSON(&req) != nil {
|
|
httpx.Fail(c, "参数错误")
|
|
return
|
|
}
|
|
if err := h.service.SetDepartmentUsers(c.Request.Context(), &req); err != nil {
|
|
httpx.Fail(c, "设置失败:"+err.Error())
|
|
return
|
|
}
|
|
httpx.Write(c, httpx.CodeSuccess, gin.H{}, "设置成功")
|
|
}
|
|
func (h *Organization) SetUserDepartments(c *gin.Context) {
|
|
var req dto.SetUserDepartmentsRequest
|
|
if c.ShouldBindJSON(&req) != nil {
|
|
httpx.Fail(c, "参数错误")
|
|
return
|
|
}
|
|
if err := h.service.SetUserDepartments(c.Request.Context(), &req); err != nil {
|
|
httpx.Fail(c, "设置失败")
|
|
return
|
|
}
|
|
httpx.Write(c, httpx.CodeSuccess, gin.H{}, "设置成功")
|
|
}
|
|
|
|
func (h *Organization) ListPositions(c *gin.Context) {
|
|
var req dto.PositionListRequest
|
|
if c.ShouldBindJSON(&req) != nil {
|
|
httpx.Fail(c, "参数错误")
|
|
return
|
|
}
|
|
items, total, err := h.service.Positions(c.Request.Context(), &req)
|
|
if err != nil {
|
|
httpx.Fail(c, "获取失败")
|
|
return
|
|
}
|
|
httpx.Write(c, httpx.CodeSuccess, httpx.PageResult{List: items, Total: total, Page: req.Page, PageSize: req.PageSize}, "获取成功")
|
|
}
|
|
func (h *Organization) CreatePosition(c *gin.Context) {
|
|
var req dto.PositionRequest
|
|
if err := c.ShouldBindJSON(&req); err != nil {
|
|
httpx.Fail(c, err.Error())
|
|
return
|
|
}
|
|
if req.Name == "" {
|
|
httpx.Fail(c, "岗位名称不能为空")
|
|
return
|
|
}
|
|
if err := h.service.CreatePosition(c.Request.Context(), &req); err != nil {
|
|
httpx.Fail(c, "创建失败:"+err.Error())
|
|
return
|
|
}
|
|
httpx.Write(c, httpx.CodeSuccess, nil, "创建成功")
|
|
}
|
|
func (h *Organization) UpdatePosition(c *gin.Context) {
|
|
var req dto.PositionRequest
|
|
if err := c.ShouldBindJSON(&req); err != nil {
|
|
httpx.Fail(c, err.Error())
|
|
return
|
|
}
|
|
if req.ID == 0 {
|
|
httpx.Fail(c, "岗位ID不能为空")
|
|
return
|
|
}
|
|
if err := h.service.UpdatePosition(c.Request.Context(), &req); err != nil {
|
|
httpx.Fail(c, "更新失败:"+err.Error())
|
|
return
|
|
}
|
|
httpx.Write(c, httpx.CodeSuccess, nil, "更新成功")
|
|
}
|
|
func (h *Organization) DeletePosition(c *gin.Context) {
|
|
var req dto.DeletePositionRequest
|
|
if c.ShouldBindJSON(&req) != nil {
|
|
httpx.Fail(c, "参数错误")
|
|
return
|
|
}
|
|
if err := h.service.DeletePosition(c.Request.Context(), req.ID); err != nil {
|
|
httpx.Fail(c, "删除失败:"+err.Error())
|
|
return
|
|
}
|
|
httpx.Write(c, httpx.CodeSuccess, nil, "删除成功")
|
|
}
|
|
func (h *Organization) FindPosition(c *gin.Context) {
|
|
id, _ := strconv.ParseUint(c.Query("id"), 10, 64)
|
|
item, err := h.service.Position(c.Request.Context(), uint(id))
|
|
if err != nil {
|
|
httpx.Fail(c, "获取失败:"+err.Error())
|
|
return
|
|
}
|
|
httpx.Write(c, httpx.CodeSuccess, item, "获取成功")
|
|
}
|
|
func (h *Organization) PositionUsers(c *gin.Context) {
|
|
id, _ := strconv.ParseUint(c.Query("positionId"), 10, 64)
|
|
ids, err := h.service.PositionUserIDs(c.Request.Context(), uint(id))
|
|
if err != nil {
|
|
httpx.Fail(c, "获取失败:"+err.Error())
|
|
return
|
|
}
|
|
if ids == nil {
|
|
ids = []uint{}
|
|
}
|
|
httpx.Write(c, httpx.CodeSuccess, ids, "获取成功")
|
|
}
|
|
func (h *Organization) SetPositionUsers(c *gin.Context) {
|
|
var req dto.SetPositionUsersRequest
|
|
if c.ShouldBindJSON(&req) != nil {
|
|
httpx.Fail(c, "参数错误")
|
|
return
|
|
}
|
|
if err := h.service.SetPositionUsers(c.Request.Context(), &req); err != nil {
|
|
httpx.Fail(c, "设置失败:"+err.Error())
|
|
return
|
|
}
|
|
httpx.Write(c, httpx.CodeSuccess, gin.H{}, "设置成功")
|
|
}
|
|
func (h *Organization) SetUserPositions(c *gin.Context) {
|
|
var req dto.SetUserPositionsRequest
|
|
if c.ShouldBindJSON(&req) != nil {
|
|
httpx.Fail(c, "参数错误")
|
|
return
|
|
}
|
|
if err := h.service.SetUserPositions(c.Request.Context(), &req); err != nil {
|
|
httpx.Fail(c, "设置失败")
|
|
return
|
|
}
|
|
httpx.Write(c, httpx.CodeSuccess, gin.H{}, "设置成功")
|
|
}
|