task_controller.go 3.3 KB
package controllers

import (
	"github.com/linmadan/egglib-go/core/application"
	"github.com/linmadan/egglib-go/web/beego"
	"gitlab.fjmaimaimai.com/allied-creation/performance/pkg/application/task/command"
	"gitlab.fjmaimaimai.com/allied-creation/performance/pkg/application/task/service"
	"gitlab.fjmaimaimai.com/allied-creation/performance/pkg/port/beego/middlewares"
)

type TaskController struct {
	beego.BaseController
}

// 获取任务
func (c *TaskController) GetTaskInfo() {
	srv := service.NewTaskService()
	paramReq := &command.GetTaskCommand{}
	err := c.BindJSON(paramReq)
	if err != nil {
		e := application.ThrowError(application.ARG_ERROR, "json 解析错误"+err.Error())
		c.Response(nil, e)
		return
	}
	userReq := middlewares.GetUser(c.Ctx)

	paramReq.CompanyId = int(userReq.CompanyId)
	data, err := srv.GetTaskInfo(paramReq)
	c.Response(data, err)
}

// 更新任务
func (c *TaskController) UpdateTask() {
	srv := service.NewTaskService()
	paramReq := &command.UpdateTaskCommand{}
	err := c.BindJSON(paramReq)
	if err != nil {
		e := application.ThrowError(application.ARG_ERROR, "json 解析错误"+err.Error())
		c.Response(nil, e)
		return
	}

	data, err := srv.UpdateTask(paramReq)
	c.Response(data, err)
}

// 更新任务
func (c *TaskController) CreateTask() {
	srv := service.NewTaskService()
	paramReq := &command.CreateTaskCommand{}
	err := c.BindJSON(paramReq)
	if err != nil {
		e := application.ThrowError(application.ARG_ERROR, "json 解析错误"+err.Error())
		c.Response(nil, e)
		return
	}

	err = srv.CreateTaskTest(paramReq)
	c.Response(nil, err)
}

// 启动任务
func (c *TaskController) RunTask() {
	srv := service.NewTaskService()
	paramReq := &command.RunTaskCommand{}
	err := c.BindJSON(paramReq)
	if err != nil {
		e := application.ThrowError(application.ARG_ERROR, "json 解析错误"+err.Error())
		c.Response(nil, e)
		return
	}
	userReq := middlewares.GetUser(c.Ctx)
	paramReq.CompanyId = int(userReq.CompanyId)
	data, err := srv.RunTask(paramReq)
	c.Response(data, err)
}

// 启动任务
func (c *TaskController) StopTask() {
	srv := service.NewTaskService()
	paramReq := &command.StopTaskCommand{}
	err := c.BindJSON(paramReq)
	if err != nil {
		e := application.ThrowError(application.ARG_ERROR, "json 解析错误"+err.Error())
		c.Response(nil, e)
		return
	}
	userReq := middlewares.GetUser(c.Ctx)
	paramReq.CompanyId = int(userReq.CompanyId)
	data, err := srv.StopTask(paramReq)
	c.Response(data, err)
}

// 任务列表
func (c *TaskController) ListTask() {
	srv := service.NewTaskService()
	paramReq := &command.ListTaskCommand{}
	err := c.BindJSON(paramReq)
	if err != nil {
		e := application.ThrowError(application.ARG_ERROR, "json 解析错误"+err.Error())
		c.Response(nil, e)
		return
	}
	userReq := middlewares.GetUser(c.Ctx)
	paramReq.CompanyId = int(userReq.CompanyId)
	data, err := srv.ListTask(paramReq)
	c.Response(data, err)
}

// CancelAttention 取消关注的任务
func (c *TaskController) CancelAttention() {
	srv := service.NewTaskService()
	paramReq := &command.CancelAttentionCommand{}
	err := c.BindJSON(paramReq)
	if err != nil {
		e := application.ThrowError(application.ARG_ERROR, "json 解析错误"+err.Error())
		c.Response(nil, e)
		return
	}
	userReq := middlewares.GetUser(c.Ctx)
	paramReq.CompanyId = int(userReq.CompanyId)
	err = srv.CancelAttention(paramReq)
	c.Response(nil, err)
}