package service

import (
	"github.com/linmadan/egglib-go/core/application"
	"gitlab.fjmaimaimai.com/allied-creation/allied-creation-gateway/pkg/application/web/custommenu/command"
	"gitlab.fjmaimaimai.com/allied-creation/allied-creation-gateway/pkg/application/web/custommenu/query"
	"gitlab.fjmaimaimai.com/allied-creation/allied-creation-gateway/pkg/infrastructure/service_gateway/allied_creation_user"
)

// 用户菜单维护
type MenuService struct {
}

// 设置收藏菜单
func (menuService *MenuService) MenuFavorite(menuFavoriteCommand *command.MenuFavoriteCommand) (interface{}, error) {

	return nil, nil
}

// 返回菜单列表
func (menuService *MenuService) MenuList(menuListQuery *query.MenuListQuery) (interface{}, error) {
	creationUserGateway := allied_creation_user.NewHttplibAlliedCreationUser(
		menuListQuery.Operator.CompanyId,
		menuListQuery.Operator.OrgId,
		menuListQuery.Operator.UserId)
	result, err := creationUserGateway.CompanyGetCustomizeMenus(allied_creation_user.ReqCompanyGetCustomizeMenus{
		CompanyId: menuListQuery.Operator.CompanyId,
	})
	if err != nil {
		return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
	}
	return result, nil
}

// 更新菜单
func (menuService *MenuService) MenuUpdate(menuUpdateCommand *command.MenuUpdateCommand) (interface{}, error) {
	creationUserGateway := allied_creation_user.NewHttplibAlliedCreationUser(
		menuUpdateCommand.Operator.CompanyId,
		menuUpdateCommand.Operator.OrgId,
		menuUpdateCommand.Operator.UserId)
	_, err := creationUserGateway.CompanySetCustomizeMenus(allied_creation_user.ReqCompanySetCustomizeMenus{
		CompanyId: menuUpdateCommand.CompanyId,
		MenuAlias: menuUpdateCommand.MenuName,
		MenuId:    int(menuUpdateCommand.MenuId),
		Sort:      menuUpdateCommand.Sort,
	})
	if err != nil {
		return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
	}
	return menuUpdateCommand, nil
}

func NewMenuService(options map[string]interface{}) *MenuService {
	newMenuService := &MenuService{}
	return newMenuService
}