package controllers import ( "encoding/json" "server/models" "server/services" "strconv" "strings" beego "github.com/beego/beego/v2/server/web" ) // TaskController OA任务管理控制器 type TaskController struct { beego.Controller } // GetTasks 列表查询 // @router /api/oa/tasks [get] func (c *TaskController) GetTasks() { // 获取租户ID(JWT中间件写入) tenantId := 0 if v := c.Ctx.Input.GetData("tenantId"); v != nil { if tid, ok := v.(int); ok { tenantId = tid } } // 允许请求参数覆盖(如有) if reqTenantId, err := c.GetInt("tenant_id"); err == nil && reqTenantId > 0 { tenantId = reqTenantId } if tenantId <= 0 { c.Data["json"] = map[string]interface{}{ "code": 1, "message": "租户ID无效", "data": nil, } c.ServeJSON() return } keyword := c.GetString("keyword") status := c.GetString("status") priority := c.GetString("priority") page, _ := c.GetInt("page", 1) pageSize, _ := c.GetInt("pageSize", 10) items, total, err := services.ListOATasks(tenantId, keyword, status, priority, page, pageSize) if 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": map[string]interface{}{ "list": items, "total": total, }, } c.ServeJSON() } // GetTaskById 详情 // @router /api/oa/tasks/:id [get] func (c *TaskController) GetTaskById() { 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 } t, err := services.GetOATaskById(id) if 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": t, } c.ServeJSON() } // CreateTask 新增 // @router /api/oa/tasks [post] func (c *TaskController) CreateTask() { var t models.Task body := c.Ctx.Input.RequestBody // 先解析 team_employee_ids var extra struct { TeamEmployeeIds []int64 `json:"team_employee_ids"` } _ = json.Unmarshal(body, &extra) // 去除 team_employee_ids 字段后再解析为 Task,避免 array->string 报错 clean := map[string]interface{}{} if err := json.Unmarshal(body, &clean); err != nil { c.Data["json"] = map[string]interface{}{ "code": 1, "message": "参数解析失败: " + err.Error(), "data": nil, } c.ServeJSON() return } delete(clean, "team_employee_ids") buf, _ := json.Marshal(clean) if err := json.Unmarshal(buf, &t); err != nil { c.Data["json"] = map[string]interface{}{ "code": 1, "message": "参数解析失败: " + err.Error(), "data": nil, } c.ServeJSON() return } if len(extra.TeamEmployeeIds) > 0 { ids := make([]string, 0, len(extra.TeamEmployeeIds)) for _, id := range extra.TeamEmployeeIds { ids = append(ids, strconv.FormatInt(id, 10)) } t.TeamEmployeeIds = strings.Join(ids, ",") } // 从上下文取租户和用户名交给服务层处理默认值 tenantId := 0 if v := c.Ctx.Input.GetData("tenantId"); v != nil { if tid, ok := v.(int); ok { tenantId = tid } } username := "" if u, ok := c.Ctx.Input.GetData("username").(string); ok { username = u } if err := services.CreateOATask(&t, tenantId, username); 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": map[string]interface{}{ "id": t.Id, }, } c.ServeJSON() } // UpdateTask 更新 // @router /api/oa/tasks/:id [put] func (c *TaskController) UpdateTask() { 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 := services.GetOATaskById(id); err != nil { c.Data["json"] = map[string]interface{}{ "code": 1, "message": "任务不存在: " + err.Error(), "data": nil, } c.ServeJSON() return } var t models.Task body := c.Ctx.Input.RequestBody // 先解析 team_employee_ids var extra struct { TeamEmployeeIds []int64 `json:"team_employee_ids"` } _ = json.Unmarshal(body, &extra) // 去除 team_employee_ids 字段后再解析为 Task,避免 array->string 报错 clean := map[string]interface{}{} if err := json.Unmarshal(body, &clean); err != nil { c.Data["json"] = map[string]interface{}{ "code": 1, "message": "参数解析失败: " + err.Error(), "data": nil, } c.ServeJSON() return } delete(clean, "team_employee_ids") buf, _ := json.Marshal(clean) if err := json.Unmarshal(buf, &t); err != nil { c.Data["json"] = map[string]interface{}{ "code": 1, "message": "参数解析失败: " + err.Error(), "data": nil, } c.ServeJSON() return } t.Id = id ids := make([]string, 0, len(extra.TeamEmployeeIds)) for _, eid := range extra.TeamEmployeeIds { ids = append(ids, strconv.FormatInt(eid, 10)) } if len(ids) > 0 { t.TeamEmployeeIds = strings.Join(ids, ",") } else { // 明确传空时,清空关联人 t.TeamEmployeeIds = "" } // 操作人交由服务层设置 username := "" if u, ok := c.Ctx.Input.GetData("username").(string); ok { username = u } if err := services.UpdateOATask(&t, username); 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() } // DeleteTask 删除(软删除) // @router /api/oa/tasks/:id [delete] func (c *TaskController) DeleteTask() { 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 } operatorName := "system" operatorId := 0 if username, ok := c.Ctx.Input.GetData("username").(string); ok && username != "" { operatorName = username } if uid, ok := c.Ctx.Input.GetData("userId").(int); ok && uid > 0 { operatorId = uid } if err := services.DeleteOATask(id, operatorName, operatorId); 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() }