evaluation_template_controller.go 3.9 KB
package controllers

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

type TemplateController struct {
	beego.BaseController
}

func (controller *TemplateController) CreateTemplate() {
	ruService := service.NewEvaluationTemplateService()
	in := &command.CreateTemplateCommand{}
	if err := controller.Unmarshal(in); err != nil {
		controller.Response(nil, application.ThrowError(application.ARG_ERROR, err.Error()))
	} else {
		ua := middlewares.GetUser(controller.Ctx)
		in.CompanyId = ua.CompanyId
		in.CreatorId = ua.UserId
		controller.Response(ruService.Create(in))
	}
}

func (controller *TemplateController) UpdateTemplate() {
	ruService := service.NewEvaluationTemplateService()
	in := &command.UpdateTemplateCommand{}
	if err := controller.Unmarshal(in); err != nil {
		controller.Response(nil, application.ThrowError(application.ARG_ERROR, err.Error()))
	} else {
		ua := middlewares.GetUser(controller.Ctx)
		in.CompanyId = ua.CompanyId
		controller.Response(ruService.Update(in))
	}
}

func (controller *TemplateController) GetTemplate() {
	ruService := service.NewEvaluationTemplateService()
	in := &command.GetTemplateCommand{}
	if id, err := controller.GetInt64(":Id"); err != nil {
		controller.Response(nil, application.ThrowError(application.ARG_ERROR, err.Error()))
	} else {
		in.Id = id
		controller.Response(ruService.Get(in))
	}
}

func (controller *TemplateController) RemoveTemplate() {
	ruService := service.NewEvaluationTemplateService()
	in := &command.DeleteTemplateCommand{}
	if err := controller.Unmarshal(in); err != nil {
		controller.Response(nil, application.ThrowError(application.ARG_ERROR, err.Error()))
	} else {
		controller.Response(ruService.Remove(in))
	}
}

func (controller *TemplateController) ListTemplate() {
	ruService := service.NewEvaluationTemplateService()
	in := &command.QueryTemplateCommand{}
	in.State = -1
	if err := controller.Unmarshal(in); err != nil {
		controller.Response(nil, application.ThrowError(application.ARG_ERROR, err.Error()))
	} else {
		if len(in.Name) > 0 {
			in.Name = "%" + in.Name + "%"
		}
		ua := middlewares.GetUser(controller.Ctx)
		in.CompanyId = ua.CompanyId
		controller.Response(ruService.List(in))
	}
}

func (controller *TemplateController) ListEnableTemplate() {
	ruService := service.NewEvaluationTemplateService()
	in := &command.QueryTemplateCommand{}
	in.State = domain.TemplateStateEnable
	in.PageNumber = 1
	in.PageSize = 999999
	if err := controller.Unmarshal(in); err != nil {
		controller.Response(nil, application.ThrowError(application.ARG_ERROR, err.Error()))
	} else {
		ua := middlewares.GetUser(controller.Ctx)
		in.CompanyId = ua.CompanyId
		controller.Response(ruService.List(in))
	}
}

func (controller *TemplateController) StateTemplate() {
	ruService := service.NewEvaluationTemplateService()
	in := &command.StateTemplateCommand{}
	if err := controller.Unmarshal(in); err != nil {
		controller.Response(nil, application.ThrowError(application.ARG_ERROR, err.Error()))
	} else {
		ua := middlewares.GetUser(controller.Ctx)
		in.CompanyId = ua.CompanyId
		controller.Response(ruService.State(in))
	}
}

func (controller *TemplateController) CopyTemplate() {
	ruService := service.NewEvaluationTemplateService()
	in := &command.CopyTemplateCommand{}
	if err := controller.Unmarshal(in); err != nil {
		controller.Response(nil, application.ThrowError(application.ARG_ERROR, err.Error()))
	} else {
		ua := middlewares.GetUser(controller.Ctx)
		in.CompanyId = ua.CompanyId
		in.CreatorId = ua.UserId
		controller.Response(ruService.Copy(in))
	}
}