320 lines
7.3 KiB
Go
320 lines
7.3 KiB
Go
package controllers
|
|
|
|
import (
|
|
"encoding/json"
|
|
"server/models"
|
|
|
|
beego "github.com/beego/beego/v2/server/web"
|
|
)
|
|
|
|
// PositionController 职位控制器
|
|
type PositionController struct {
|
|
beego.Controller
|
|
}
|
|
|
|
// GetTenantPositions 获取租户下的所有职位
|
|
// @router /positions/tenant/:tenantId [get]
|
|
func (c *PositionController) GetTenantPositions() {
|
|
tenantId, err := c.GetInt(":tenantId")
|
|
if err != nil || tenantId <= 0 {
|
|
c.Data["json"] = map[string]interface{}{
|
|
"code": 1,
|
|
"message": "租户ID无效",
|
|
"data": nil,
|
|
}
|
|
c.ServeJSON()
|
|
return
|
|
}
|
|
|
|
positions, err := models.GetTenantPositions(tenantId)
|
|
if err != nil {
|
|
c.Data["json"] = map[string]interface{}{
|
|
"code": 1,
|
|
"message": "获取职位列表失败: " + err.Error(),
|
|
"data": nil,
|
|
}
|
|
c.ServeJSON()
|
|
return
|
|
}
|
|
|
|
// 格式化返回数据
|
|
posList := make([]map[string]interface{}, 0)
|
|
for _, pos := range positions {
|
|
posList = append(posList, map[string]interface{}{
|
|
"id": pos.Id,
|
|
"tenant_id": pos.TenantId,
|
|
"name": pos.Name,
|
|
"code": pos.Code,
|
|
"department_id": pos.DepartmentId,
|
|
"level": pos.Level,
|
|
"description": pos.Description,
|
|
"sort_order": pos.SortOrder,
|
|
"status": pos.Status,
|
|
})
|
|
}
|
|
|
|
c.Data["json"] = map[string]interface{}{
|
|
"code": 0,
|
|
"message": "获取职位列表成功",
|
|
"data": posList,
|
|
}
|
|
c.ServeJSON()
|
|
}
|
|
|
|
// GetPositionsByDepartment 根据部门ID获取职位列表
|
|
// @router /positions/department/:departmentId [get]
|
|
func (c *PositionController) GetPositionsByDepartment() {
|
|
departmentId, err := c.GetInt(":departmentId")
|
|
if err != nil || departmentId <= 0 {
|
|
c.Data["json"] = map[string]interface{}{
|
|
"code": 1,
|
|
"message": "部门ID无效",
|
|
"data": nil,
|
|
}
|
|
c.ServeJSON()
|
|
return
|
|
}
|
|
|
|
positions, err := models.GetPositionsByDepartment(departmentId)
|
|
if err != nil {
|
|
c.Data["json"] = map[string]interface{}{
|
|
"code": 1,
|
|
"message": "获取职位列表失败: " + err.Error(),
|
|
"data": nil,
|
|
}
|
|
c.ServeJSON()
|
|
return
|
|
}
|
|
|
|
// 格式化返回数据
|
|
posList := make([]map[string]interface{}, 0)
|
|
for _, pos := range positions {
|
|
posList = append(posList, map[string]interface{}{
|
|
"id": pos.Id,
|
|
"tenant_id": pos.TenantId,
|
|
"name": pos.Name,
|
|
"code": pos.Code,
|
|
"department_id": pos.DepartmentId,
|
|
"level": pos.Level,
|
|
"description": pos.Description,
|
|
"sort_order": pos.SortOrder,
|
|
"status": pos.Status,
|
|
})
|
|
}
|
|
|
|
c.Data["json"] = map[string]interface{}{
|
|
"code": 0,
|
|
"message": "获取职位列表成功",
|
|
"data": posList,
|
|
}
|
|
c.ServeJSON()
|
|
}
|
|
|
|
// GetPositionInfo 获取职位详情
|
|
// @router /positions/:id [get]
|
|
func (c *PositionController) GetPositionInfo() {
|
|
id, err := c.GetInt(":id")
|
|
if err != nil || id <= 0 {
|
|
c.Data["json"] = map[string]interface{}{
|
|
"code": 1,
|
|
"message": "职位ID无效",
|
|
"data": nil,
|
|
}
|
|
c.ServeJSON()
|
|
return
|
|
}
|
|
|
|
position, err := models.GetPositionById(id)
|
|
if err != nil {
|
|
c.Data["json"] = map[string]interface{}{
|
|
"code": 1,
|
|
"message": "职位不存在",
|
|
"data": nil,
|
|
}
|
|
c.ServeJSON()
|
|
return
|
|
}
|
|
|
|
c.Data["json"] = map[string]interface{}{
|
|
"code": 0,
|
|
"message": "获取职位信息成功",
|
|
"data": map[string]interface{}{
|
|
"id": position.Id,
|
|
"tenant_id": position.TenantId,
|
|
"name": position.Name,
|
|
"code": position.Code,
|
|
"department_id": position.DepartmentId,
|
|
"level": position.Level,
|
|
"description": position.Description,
|
|
"sort_order": position.SortOrder,
|
|
"status": position.Status,
|
|
},
|
|
}
|
|
c.ServeJSON()
|
|
}
|
|
|
|
// AddPosition 添加职位
|
|
// @router /positions [post]
|
|
func (c *PositionController) AddPosition() {
|
|
var posData struct {
|
|
TenantId int `json:"tenant_id"`
|
|
Name string `json:"name"`
|
|
Code string `json:"code"`
|
|
DepartmentId int `json:"department_id"`
|
|
Level int `json:"level"`
|
|
Description string `json:"description"`
|
|
SortOrder int `json:"sort_order"`
|
|
Status int8 `json:"status"`
|
|
}
|
|
|
|
err := json.Unmarshal(c.Ctx.Input.RequestBody, &posData)
|
|
if err != nil {
|
|
c.Data["json"] = map[string]interface{}{
|
|
"code": 1,
|
|
"message": "请求参数错误: " + err.Error(),
|
|
"data": nil,
|
|
}
|
|
c.ServeJSON()
|
|
return
|
|
}
|
|
|
|
position := &models.Position{
|
|
TenantId: posData.TenantId,
|
|
Name: posData.Name,
|
|
Code: posData.Code,
|
|
DepartmentId: posData.DepartmentId,
|
|
Level: posData.Level,
|
|
Description: posData.Description,
|
|
SortOrder: posData.SortOrder,
|
|
Status: posData.Status,
|
|
}
|
|
|
|
if position.TenantId == 0 {
|
|
position.TenantId = 1
|
|
}
|
|
|
|
id, err := models.AddPosition(position)
|
|
if err != nil {
|
|
c.Data["json"] = map[string]interface{}{
|
|
"code": 1,
|
|
"message": "添加职位失败: " + err.Error(),
|
|
"data": nil,
|
|
}
|
|
c.ServeJSON()
|
|
return
|
|
}
|
|
|
|
position.Id = int(id)
|
|
c.Data["json"] = map[string]interface{}{
|
|
"code": 0,
|
|
"message": "添加职位成功",
|
|
"data": position,
|
|
}
|
|
c.ServeJSON()
|
|
}
|
|
|
|
// UpdatePosition 更新职位信息
|
|
// @router /positions/:id [put]
|
|
func (c *PositionController) UpdatePosition() {
|
|
id, err := c.GetInt(":id")
|
|
if err != nil || id <= 0 {
|
|
c.Data["json"] = map[string]interface{}{
|
|
"code": 1,
|
|
"message": "职位ID无效",
|
|
"data": nil,
|
|
}
|
|
c.ServeJSON()
|
|
return
|
|
}
|
|
|
|
var updateData struct {
|
|
Name string `json:"name"`
|
|
Code string `json:"code"`
|
|
DepartmentId int `json:"department_id"`
|
|
Level int `json:"level"`
|
|
Description string `json:"description"`
|
|
SortOrder int `json:"sort_order"`
|
|
Status int8 `json:"status"`
|
|
}
|
|
|
|
err = json.Unmarshal(c.Ctx.Input.RequestBody, &updateData)
|
|
if err != nil {
|
|
c.Data["json"] = map[string]interface{}{
|
|
"code": 1,
|
|
"message": "请求参数错误: " + err.Error(),
|
|
"data": nil,
|
|
}
|
|
c.ServeJSON()
|
|
return
|
|
}
|
|
|
|
position, err := models.GetPositionById(id)
|
|
if err != nil {
|
|
c.Data["json"] = map[string]interface{}{
|
|
"code": 1,
|
|
"message": "职位不存在",
|
|
"data": nil,
|
|
}
|
|
c.ServeJSON()
|
|
return
|
|
}
|
|
|
|
position.Name = updateData.Name
|
|
position.Code = updateData.Code
|
|
position.DepartmentId = updateData.DepartmentId
|
|
position.Level = updateData.Level
|
|
position.Description = updateData.Description
|
|
position.SortOrder = updateData.SortOrder
|
|
position.Status = updateData.Status
|
|
|
|
if err := models.UpdatePosition(position); err != nil {
|
|
c.Data["json"] = map[string]interface{}{
|
|
"code": 1,
|
|
"message": "更新职位信息失败: " + err.Error(),
|
|
"data": nil,
|
|
}
|
|
c.ServeJSON()
|
|
return
|
|
}
|
|
|
|
c.Data["json"] = map[string]interface{}{
|
|
"code": 0,
|
|
"message": "更新职位信息成功",
|
|
"data": position,
|
|
}
|
|
c.ServeJSON()
|
|
}
|
|
|
|
// DeletePosition 删除职位
|
|
// @router /positions/:id [delete]
|
|
func (c *PositionController) DeletePosition() {
|
|
id, err := c.GetInt(":id")
|
|
if err != nil || id <= 0 {
|
|
c.Data["json"] = map[string]interface{}{
|
|
"code": 1,
|
|
"message": "职位ID无效",
|
|
"data": nil,
|
|
}
|
|
c.ServeJSON()
|
|
return
|
|
}
|
|
|
|
if err := models.DeletePosition(id); err != nil {
|
|
c.Data["json"] = map[string]interface{}{
|
|
"code": 1,
|
|
"message": "删除职位失败: " + err.Error(),
|
|
"data": nil,
|
|
}
|
|
c.ServeJSON()
|
|
return
|
|
}
|
|
|
|
c.Data["json"] = map[string]interface{}{
|
|
"code": 0,
|
|
"message": "删除职位成功",
|
|
"data": nil,
|
|
}
|
|
c.ServeJSON()
|
|
}
|
|
|