task_controller.go 10.6 KB
package controllers

import (
	"encoding/json"
	"github.com/astaxie/beego"
	"github.com/linmadan/egglib-go/web/beego/utils"
	"gitlab.fjmaimaimai.com/linmadan/mmm-worth/pkg/application/task/command"
	"gitlab.fjmaimaimai.com/linmadan/mmm-worth/pkg/application/task/query"
	"gitlab.fjmaimaimai.com/linmadan/mmm-worth/pkg/application/task/service"
)

type TaskController struct {
	beego.Controller
}

func (controller *TaskController) RejectTaskReceiver() {
	taskService := service.NewTaskService(nil)
	rejectTaskReceiverCommand := &command.RejectTaskReceiverCommand{}
	json.Unmarshal(controller.Ctx.Input.GetData("requestBody").([]byte), rejectTaskReceiverCommand)
	taskId, _ := controller.GetInt64(":taskId")
	rejectTaskReceiverCommand.TaskId = taskId
	data, err := taskService.RejectTaskReceiver(rejectTaskReceiverCommand)
	var response utils.JsonResponse
	if err != nil {
		response = utils.ResponseError(controller.Ctx, err)
	} else {
		response = utils.ResponseData(controller.Ctx, data)
	}
	controller.Data["json"] = response
	controller.ServeJSON()
}

func (controller *TaskController) ConfirmRobTask() {
	taskService := service.NewTaskService(nil)
	confirmRobTaskCommand := &command.ConfirmRobTaskCommand{}
	json.Unmarshal(controller.Ctx.Input.GetData("requestBody").([]byte), confirmRobTaskCommand)
	taskId, _ := controller.GetInt64(":taskId")
	confirmRobTaskCommand.TaskId = taskId
	data, err := taskService.ConfirmRobTask(confirmRobTaskCommand)
	var response utils.JsonResponse
	if err != nil {
		response = utils.ResponseError(controller.Ctx, err)
	} else {
		response = utils.ResponseData(controller.Ctx, data)
	}
	controller.Data["json"] = response
	controller.ServeJSON()
}

func (controller *TaskController) RobTask() {
	taskService := service.NewTaskService(nil)
	robTaskCommand := &command.RobTaskCommand{}
	json.Unmarshal(controller.Ctx.Input.GetData("requestBody").([]byte), robTaskCommand)
	taskId, _ := controller.GetInt64(":taskId")
	robTaskCommand.TaskId = taskId
	data, err := taskService.RobTask(robTaskCommand)
	var response utils.JsonResponse
	if err != nil {
		response = utils.ResponseError(controller.Ctx, err)
	} else {
		response = utils.ResponseData(controller.Ctx, data)
	}
	controller.Data["json"] = response
	controller.ServeJSON()
}

func (controller *TaskController) BidTask() {
	taskService := service.NewTaskService(nil)
	bidTaskCommand := &command.BidTaskCommand{}
	json.Unmarshal(controller.Ctx.Input.GetData("requestBody").([]byte), bidTaskCommand)
	taskId, _ := controller.GetInt64(":taskId")
	bidTaskCommand.TaskId = taskId
	data, err := taskService.BidTask(bidTaskCommand)
	var response utils.JsonResponse
	if err != nil {
		response = utils.ResponseError(controller.Ctx, err)
	} else {
		response = utils.ResponseData(controller.Ctx, data)
	}
	controller.Data["json"] = response
	controller.ServeJSON()
}

func (controller *TaskController) ApplyCompleteTask() {
	taskService := service.NewTaskService(nil)
	applyCompleteTaskCommand := &command.ApplyCompleteTaskCommand{}
	json.Unmarshal(controller.Ctx.Input.GetData("requestBody").([]byte), applyCompleteTaskCommand)
	taskId, _ := controller.GetInt64(":taskId")
	applyCompleteTaskCommand.TaskId = taskId
	data, err := taskService.ApplyCompleteTask(applyCompleteTaskCommand)
	var response utils.JsonResponse
	if err != nil {
		response = utils.ResponseError(controller.Ctx, err)
	} else {
		response = utils.ResponseData(controller.Ctx, data)
	}
	controller.Data["json"] = response
	controller.ServeJSON()
}

func (controller *TaskController) ReleaseTask() {
	taskService := service.NewTaskService(nil)
	releaseTaskCommand := &command.ReleaseTaskCommand{}
	json.Unmarshal(controller.Ctx.Input.GetData("requestBody").([]byte), releaseTaskCommand)
	taskId, _ := controller.GetInt64(":taskId")
	releaseTaskCommand.TaskId = taskId
	data, err := taskService.ReleaseTask(releaseTaskCommand)
	var response utils.JsonResponse
	if err != nil {
		response = utils.ResponseError(controller.Ctx, err)
	} else {
		response = utils.ResponseData(controller.Ctx, data)
	}
	controller.Data["json"] = response
	controller.ServeJSON()
}

func (controller *TaskController) ChooseSuccessfulBidder() {
	taskService := service.NewTaskService(nil)
	chooseSuccessfulBidderCommand := &command.ChooseSuccessfulBidderCommand{}
	json.Unmarshal(controller.Ctx.Input.GetData("requestBody").([]byte), chooseSuccessfulBidderCommand)
	taskId, _ := controller.GetInt64(":taskId")
	chooseSuccessfulBidderCommand.TaskId = taskId
	data, err := taskService.ChooseSuccessfulBidder(chooseSuccessfulBidderCommand)
	var response utils.JsonResponse
	if err != nil {
		response = utils.ResponseError(controller.Ctx, err)
	} else {
		response = utils.ResponseData(controller.Ctx, data)
	}
	controller.Data["json"] = response
	controller.ServeJSON()
}

func (controller *TaskController) OffTask() {
	taskService := service.NewTaskService(nil)
	offTaskCommand := &command.OffTaskCommand{}
	json.Unmarshal(controller.Ctx.Input.GetData("requestBody").([]byte), offTaskCommand)
	taskId, _ := controller.GetInt64(":taskId")
	offTaskCommand.TaskId = taskId
	data, err := taskService.OffTask(offTaskCommand)
	var response utils.JsonResponse
	if err != nil {
		response = utils.ResponseError(controller.Ctx, err)
	} else {
		response = utils.ResponseData(controller.Ctx, data)
	}
	controller.Data["json"] = response
	controller.ServeJSON()
}

func (controller *TaskController) AcceptanceTask() {
	taskService := service.NewTaskService(nil)
	acceptanceTaskCommand := &command.AcceptanceTaskCommand{}
	json.Unmarshal(controller.Ctx.Input.GetData("requestBody").([]byte), acceptanceTaskCommand)
	taskId, _ := controller.GetInt64(":taskId")
	acceptanceTaskCommand.TaskId = taskId
	data, err := taskService.AcceptanceTask(acceptanceTaskCommand)
	var response utils.JsonResponse
	if err != nil {
		response = utils.ResponseError(controller.Ctx, err)
	} else {
		response = utils.ResponseData(controller.Ctx, data)
	}
	controller.Data["json"] = response
	controller.ServeJSON()
}

func (controller *TaskController) SearchTask() {
	taskService := service.NewTaskService(nil)
	searchTaskCommand := &command.SearchTaskCommand{}
	json.Unmarshal(controller.Ctx.Input.GetData("requestBody").([]byte), searchTaskCommand)
	data, err := taskService.SearchTask(searchTaskCommand)
	var response utils.JsonResponse

	if err != nil {
		response = utils.ResponseError(controller.Ctx, err)
	} else {
		response = utils.ResponseData(controller.Ctx, data)
	}
	controller.Data["json"] = response
	controller.ServeJSON()
}

func (controller *TaskController) CreateTask() {
	taskService := service.NewTaskService(nil)
	createTaskCommand := &command.CreateTaskCommand{}
	json.Unmarshal(controller.Ctx.Input.GetData("requestBody").([]byte), createTaskCommand)
	data, err := taskService.CreateTask(createTaskCommand)
	var response utils.JsonResponse
	if err != nil {
		response = utils.ResponseError(controller.Ctx, err)
	} else {
		response = utils.ResponseData(controller.Ctx, data)
	}
	controller.Data["json"] = response
	controller.ServeJSON()
}

func (controller *TaskController) UpdateTask() {
	taskService := service.NewTaskService(nil)
	updateTaskCommand := &command.UpdateTaskCommand{}
	json.Unmarshal(controller.Ctx.Input.GetData("requestBody").([]byte), updateTaskCommand)
	taskId, _ := controller.GetInt64(":taskId")
	updateTaskCommand.TaskId = taskId
	data, err := taskService.UpdateTask(updateTaskCommand)
	var response utils.JsonResponse
	if err != nil {
		response = utils.ResponseError(controller.Ctx, err)
	} else {
		response = utils.ResponseData(controller.Ctx, data)
	}
	controller.Data["json"] = response
	controller.ServeJSON()
}

// 更新过期任务状态
func (controller *TaskController) UpdateTaskStatus() {
	taskService := service.NewTaskService(nil)
	updateTaskCommand := &command.UpdateTaskCommand{}
	json.Unmarshal(controller.Ctx.Input.GetData("requestBody").([]byte), updateTaskCommand)
	data, err := taskService.UpdateTaskStatus()
	var response utils.JsonResponse
	if err != nil {
		response = utils.ResponseError(controller.Ctx, err)
	} else {
		response = utils.ResponseData(controller.Ctx, data)
	}
	controller.Data["json"] = response
	controller.ServeJSON()
}

func (controller *TaskController) GetTask() {
	taskService := service.NewTaskService(nil)
	getTaskQuery := &query.GetTaskQuery{}
	taskId, _ := controller.GetInt64(":taskId")
	getTaskQuery.TaskId = taskId
	data, err := taskService.GetTask(getTaskQuery)
	var response utils.JsonResponse
	if err != nil {
		response = utils.ResponseError(controller.Ctx, err)
	} else {
		response = utils.ResponseData(controller.Ctx, data)
	}
	controller.Data["json"] = response
	controller.ServeJSON()
}

func (controller *TaskController) RemoveTask() {
	taskService := service.NewTaskService(nil)
	removeTaskCommand := &command.RemoveTaskCommand{}
	taskId, _ := controller.GetInt64(":taskId")
	removeTaskCommand.TaskId = taskId
	data, err := taskService.RemoveTask(removeTaskCommand)
	var response utils.JsonResponse
	if err != nil {
		response = utils.ResponseError(controller.Ctx, err)
	} else {
		response = utils.ResponseData(controller.Ctx, data)
	}
	controller.Data["json"] = response
	controller.ServeJSON()
}

func (controller *TaskController) ListTask() {
	taskService := service.NewTaskService(nil)
	listTaskQuery := &query.ListTaskQuery{}
	companyId, _ := controller.GetInt64("companyId")
	listTaskQuery.CompanyId = companyId
	sponsor, _ := controller.GetInt64("sponsor")
	listTaskQuery.Sponsor = sponsor
	taskContentMatch := controller.GetString("taskContentMatch")
	listTaskQuery.TaskContentMatch = taskContentMatch
	taskType, _ := controller.GetInt("taskType")
	listTaskQuery.TaskType = taskType
	taskStatus, _ := controller.GetInt("taskStatus")
	listTaskQuery.TaskStatus = taskStatus
	taskNature := controller.GetString("taskNature")
	listTaskQuery.TaskNature = taskNature
	isRewardTake, _ := controller.GetBool("isRewardTake")
	listTaskQuery.IsRewardTake = isRewardTake
	bidTimeMatch, _ := controller.GetInt("bidTimeMatch")
	listTaskQuery.BidTimeMatch = bidTimeMatch
	receiver, _ := controller.GetInt64("receiver")
	listTaskQuery.Receiver = receiver
	participator, _ := controller.GetInt64("participator")
	listTaskQuery.Participator = participator
	isFilterCloseStatus, _ := controller.GetBool("isFilterCloseStatus")
	listTaskQuery.IsFilterCloseStatus = isFilterCloseStatus
	offset, _ := controller.GetInt("offset")
	listTaskQuery.Offset = offset
	limit, _ := controller.GetInt("limit")
	listTaskQuery.Limit = limit
	data, err := taskService.ListTask(listTaskQuery)
	var response utils.JsonResponse
	if err != nil {
		response = utils.ResponseError(controller.Ctx, err)
	} else {
		response = utils.ResponseData(controller.Ctx, data)
	}
	controller.Data["json"] = response
	controller.ServeJSON()
}