package controllers

import (
	"github.com/tiptok/gocomm/pkg/log"
	"gitlab.fjmaimaimai.com/mmm-go/godevp/pkg/application/role"
	"gitlab.fjmaimaimai.com/mmm-go/godevp/pkg/protocol"
	protocolx "gitlab.fjmaimaimai.com/mmm-go/godevp/pkg/protocol/role"
)

type RoleController struct {
	BaseController
}

// CreateRole
// CreateRole execute command  create  Role
func (this *RoleController) CreateRole() {
	var (
		msg     *protocol.ResponseMessage
		svr     = role.NewRoleService(nil)
		request *protocolx.CreateRoleRequest
	)
	defer func() {
		this.Resp(msg)
	}()
	if err := this.JsonUnmarshal(&request); err != nil {
		msg = protocol.NewResponseMessage(2, "")
		return
	}
	header := this.GetRequestHeader(this.Ctx)
	data, err := svr.CreateRole(header, request)
	if err != nil {
		log.Error(err)
	}
	msg = protocol.NewResponseMessageData(data, err)
}

// UpdateRole
// UpdateRole execute command  update  Role
func (this *RoleController) UpdateRole() {
	var (
		msg     *protocol.ResponseMessage
		svr     = role.NewRoleService(nil)
		request *protocolx.UpdateRoleRequest
	)
	defer func() {
		this.Resp(msg)
	}()
	if err := this.JsonUnmarshal(&request); err != nil {
		msg = protocol.NewResponseMessage(2, "")
		return
	}
	request.Id, _ = this.GetInt64(":roleId")
	header := this.GetRequestHeader(this.Ctx)
	data, err := svr.UpdateRole(header, request)
	if err != nil {
		log.Error(err)
	}
	msg = protocol.NewResponseMessageData(data, err)
}

// GetRole
// GetRole execute query  get  Role
func (this *RoleController) GetRole() {
	var (
		msg     *protocol.ResponseMessage
		svr     = role.NewRoleService(nil)
		request = &protocolx.GetRoleRequest{}
	)
	defer func() {
		this.Resp(msg)
	}()
	if err := this.JsonUnmarshal(&request); err != nil {
		msg = protocol.NewResponseMessage(2, "")
		return
	}
	request.Id, _ = this.GetInt64(":roleId")
	header := this.GetRequestHeader(this.Ctx)
	data, err := svr.GetRole(header, request)
	if err != nil {
		log.Error(err)
	}
	msg = protocol.NewResponseMessageData(data, err)
}

// DeleteRole
// DeleteRole execute command  delete  Role
func (this *RoleController) DeleteRole() {
	var (
		msg     *protocol.ResponseMessage
		svr     = role.NewRoleService(nil)
		request = &protocolx.DeleteRoleRequest{}
	)
	defer func() {
		this.Resp(msg)
	}()
	request.Id, _ = this.GetInt64(":roleId")
	header := this.GetRequestHeader(this.Ctx)
	data, err := svr.DeleteRole(header, request)
	if err != nil {
		log.Error(err)
	}
	msg = protocol.NewResponseMessageData(data, err)
}

// ListRole
// ListRole execute query  list  Role
func (this *RoleController) ListRole() {
	var (
		msg     *protocol.ResponseMessage
		svr     = role.NewRoleService(nil)
		request *protocolx.ListRoleRequest
	)
	defer func() {
		this.Resp(msg)
	}()
	if err := this.JsonUnmarshal(&request); err != nil {
		msg = protocol.NewResponseMessage(2, "")
		return
	}
	header := this.GetRequestHeader(this.Ctx)
	data, err := svr.ListRole(header, request)
	if err != nil {
		log.Error(err)
	}
	msg = protocol.NewResponseMessageData(data, err)
}