package controllers

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

type MenuController struct {
	BaseController
}

// CreateMenu
// CreateMenu execute command  create  Menu
func (this *MenuController) CreateMenu() {
	var (
		msg     *protocol.ResponseMessage
		svr     = menu.NewMenuService(nil)
		request *protocolx.CreateMenuRequest
	)
	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.CreateMenu(header, request)
	if err != nil {
		log.Error(err)
	}
	msg = protocol.NewResponseMessageData(data, err)
}

// UpdateMenu
// UpdateMenu execute command  update  Menu
func (this *MenuController) UpdateMenu() {
	var (
		msg     *protocol.ResponseMessage
		svr     = menu.NewMenuService(nil)
		request *protocolx.UpdateMenuRequest
	)
	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.UpdateMenu(header, request)
	if err != nil {
		log.Error(err)
	}
	msg = protocol.NewResponseMessageData(data, err)
}

// GetMenu
// GetMenu execute query  get  Menu
func (this *MenuController) GetMenu() {
	var (
		msg     *protocol.ResponseMessage
		svr     = menu.NewMenuService(nil)
		request *protocolx.GetMenuRequest
	)
	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.GetMenu(header, request)
	if err != nil {
		log.Error(err)
	}
	msg = protocol.NewResponseMessageData(data, err)
}

// DeleteMenu
// DeleteMenu execute command  delete  Menu
func (this *MenuController) DeleteMenu() {
	var (
		msg     *protocol.ResponseMessage
		svr     = menu.NewMenuService(nil)
		request *protocolx.DeleteMenuRequest
	)
	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.DeleteMenu(header, request)
	if err != nil {
		log.Error(err)
	}
	msg = protocol.NewResponseMessageData(data, err)
}

// ListMenu
// ListMenu execute query  list  Menu
func (this *MenuController) ListMenu() {
	var (
		msg     *protocol.ResponseMessage
		svr     = menu.NewMenuService(nil)
		request *protocolx.ListMenuRequest
	)
	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.ListMenu(header, request)
	if err != nil {
		log.Error(err)
	}
	msg = protocol.NewResponseMessageData(data, err)
}