package controllers

import (
	"github.com/tiptok/gocomm/pkg/log"
	"gitlab.fjmaimaimai.com/mmm-go/godevp/pkg/application/rbac/command"
	"gitlab.fjmaimaimai.com/mmm-go/godevp/pkg/application/rbac/query"
	"gitlab.fjmaimaimai.com/mmm-go/godevp/pkg/application/rbac/service"
	"gitlab.fjmaimaimai.com/mmm-go/godevp/pkg/protocol"
)

type RbacController struct {
	BaseController
}

// Access
// 权限列表
func (this *RbacController) Access() {
	var (
		msg     *protocol.ResponseMessage
		svr     = service.NewRbacService(nil)
		request *query.AccessRequest
	)
	defer func() {
		this.Resp(msg)
	}()
	header := this.GetRequestHeader(this.Ctx)
	data, err := svr.Access(header, request)
	if err != nil {
		log.Error(err)
	}
	msg = protocol.NewResponseMessageData(data, err)
}

// RoleAccess
// 获取角色权限
func (this *RbacController) RoleAccess() {
	var (
		msg     *protocol.ResponseMessage
		svr     = service.NewRbacService(nil)
		request = &query.RoleAccessRequest{}
	)
	defer func() {
		this.Resp(msg)
	}()
	request.RoleId, _ = this.GetInt64(":roleId")
	header := this.GetRequestHeader(this.Ctx)
	data, err := svr.RoleAccess(header, request)
	if err != nil {
		log.Error(err)
	}
	msg = protocol.NewResponseMessageData(data, err)
}

// SetRoleAccess
// 设置权限
func (this *RbacController) SetRoleAccess() {
	var (
		msg     *protocol.ResponseMessage
		svr     = service.NewRbacService(nil)
		request *command.SetRoleAccessRequest
	)
	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.SetRoleAccess(header, request)
	if err != nil {
		log.Error(err)
	}
	msg = protocol.NewResponseMessageData(data, err)
}