package service

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

// 菜单服务
type MenuService struct {
}

// 创建菜单服务
func (menuService *MenuService) CreateMenu(createMenuCommand *command.CreateMenuCommand) (interface{}, error) {
	creationUserGateway := allied_creation_user.NewHttplibAlliedCreationUser(
		createMenuCommand.Operator)
	result, err := creationUserGateway.MenusCreate(allied_creation_user.ReqMenus{
		ParentId:   createMenuCommand.ParentId,
		MenuName:   createMenuCommand.MenuName,
		Code:       createMenuCommand.Code,
		AccessCode: createMenuCommand.AccessCode,
		MenuType:   createMenuCommand.MenuType,
		Icon:       createMenuCommand.Icon,
		Sort:       createMenuCommand.Sort,
		Desc:       createMenuCommand.Desc,
		IsPublish:  createMenuCommand.IsPublish,
	})
	if err != nil {
		return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
	}
	return result, nil
}

// 返回菜单服务
func (menuService *MenuService) GetMenu(getMenuQuery *query.GetMenuQuery) (interface{}, error) {
	creationUserGateway := allied_creation_user.NewHttplibAlliedCreationUser(
		getMenuQuery.Operator)
	result, err := creationUserGateway.MenusGet(allied_creation_user.ReqMenusGet{
		MenuId: getMenuQuery.MenuId,
	})
	if err != nil {
		return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
	}
	return result, nil
}

// 返回菜单服务列表
func (menuService *MenuService) ListMenu(listMenuQuery *query.ListMenuQuery) (interface{}, error) {
	creationUserGateway := allied_creation_user.NewHttplibAlliedCreationUser(
		listMenuQuery.Operator)
	result, err := creationUserGateway.MenusSearch(allied_creation_user.ReqMenusSearch{
		MenuCategory: listMenuQuery.MenuCategory,
		ParentId:     listMenuQuery.ParentId,
		MenuName:     listMenuQuery.MenuName,
	})
	if err != nil {
		return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
	}
	return result, nil
}

// 移除菜单服务
func (menuService *MenuService) RemoveMenu(removeMenuCommand *command.RemoveMenuCommand) (interface{}, error) {

	return struct{}{}, nil
}

// 更新菜单服务
func (menuService *MenuService) UpdateMenu(updateMenuCommand *command.UpdateMenuCommand) (interface{}, error) {
	creationUserGateway := allied_creation_user.NewHttplibAlliedCreationUser(
		updateMenuCommand.Operator)
	result, err := creationUserGateway.MenusUpdate(allied_creation_user.ReqMenusUpdate{
		MenuId:     updateMenuCommand.MenuId,
		ParentId:   updateMenuCommand.ParentId,
		MenuName:   updateMenuCommand.MenuName,
		Code:       updateMenuCommand.Code,
		AccessCode: updateMenuCommand.AccessCode,
		MenuType:   updateMenuCommand.MenuType,
		Icon:       updateMenuCommand.Icon,
		Sort:       updateMenuCommand.Sort,
		Desc:       updateMenuCommand.Desc,
		IsPublish:  updateMenuCommand.IsPublish,
	})
	if err != nil {
		return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
	}
	return result, nil
}

// 批量删除菜单
func (menuService *MenuService) BatchDeleteMenu(batchDeleteMenu *command.BatchDeleteMenuCommand) (interface{}, error) {
	creationUserGateway := allied_creation_user.NewHttplibAlliedCreationUser(
		batchDeleteMenu.Operator)
	result, err := creationUserGateway.MenusBatchDelete(allied_creation_user.ReqMenusBatchDelete{
		MenuIds: batchDeleteMenu.MenuIds,
	})
	if err != nil {
		return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
	}
	return result, nil
}

// 批量启用菜单
func (menuService *MenuService) BatchEnableMenu(batchEnableMenu *command.BatchEnableMenuCommand) (interface{}, error) {
	creationUserGateway := allied_creation_user.NewHttplibAlliedCreationUser(
		batchEnableMenu.Operator)
	result, err := creationUserGateway.MenusBatchEnable(allied_creation_user.ReqMenusBatchEnable{
		MenuIds: batchEnableMenu.MenuIds,
		Status:  batchEnableMenu.Status,
	})
	if err != nil {
		return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
	}
	return result, nil
}

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