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) {
	creationUserGateway := allied_creation_user.NewHttplibAlliedCreationUser(
		menuFavoriteCommand.Operator.CompanyId,
		menuFavoriteCommand.Operator.OrgId,
		menuFavoriteCommand.Operator.UserId)
	result, err := creationUserGateway.CompanyGetCustomizeMenus(allied_creation_user.ReqCompanyGetCustomizeMenus{
		CompanyId: menuFavoriteCommand.Operator.CompanyId,
	})
	if err != nil {
		return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
	}
	return result, 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,
		MenuCategory: menuListQuery.MenuCategory,
	})
	if err != nil {
		return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
	}
	return map[string]interface{}{
		"menus": result.Menus,
	}, 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.Operator.CompanyId,
		MenuAlias: menuUpdateCommand.MenuAlias,
		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
}