package handler import ( "strconv" "kra/internal/server/httpx" "kra/internal/service" "kra/internal/service/dto" "github.com/gin-gonic/gin" ) type Organization struct { departments *service.DepartmentService positions *service.PositionService } func NewOrganization(departments *service.DepartmentService, positions *service.PositionService) *Organization { return &Organization{departments: departments, positions: positions} } func (h *Organization) ListDepartments(c *gin.Context) { var req dto.DepartmentListRequest _ = c.ShouldBindJSON(&req) items, err := h.departments.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.departments.Create(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.departments.Update(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.departments.Delete(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.departments.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.departments.UserIDs(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.departments.SetUsers(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.departments.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.positions.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.positions.Create(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.positions.Update(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.positions.Delete(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.positions.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.positions.UserIDs(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.positions.SetUsers(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.positions.SetUserPositions(c.Request.Context(), &req); err != nil { httpx.Fail(c, "设置失败") return } httpx.Write(c, httpx.CodeSuccess, gin.H{}, "设置成功") }