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,
	)
	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) CustomizeMenus(menuListQuery *query.MenuListQuery) (interface{}, error) {
	creationUserGateway := allied_creation_user.NewHttplibAlliedCreationUser(
		menuListQuery.Operator,
	)
	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,
	)
	_, 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 (menuService *MenuService) MenuSearch(menuListQuery *query.MenuListQuery) (interface{}, error) {
	creationUserGateway := allied_creation_user.NewHttplibAlliedCreationUser(
		menuListQuery.Operator,
	)
	result, err := creationUserGateway.MenusSearch(allied_creation_user.ReqMenusSearch{
		MenuCategory: menuListQuery.MenuCategory,
	})
	if err != nil {
		return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
	}
	return map[string]interface{}{
		"menus": result.Menus,
	}, nil
}

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