package service

import (
	"fmt"
	"github.com/linmadan/egglib-go/core/application"
	"github.com/linmadan/egglib-go/utils/tool_funs"
	"gitlab.fjmaimaimai.com/allied-creation/allied-creation-user/pkg/application/factory"
	"gitlab.fjmaimaimai.com/allied-creation/allied-creation-user/pkg/application/menu/command"
	"gitlab.fjmaimaimai.com/allied-creation/allied-creation-user/pkg/application/menu/query"
	"gitlab.fjmaimaimai.com/allied-creation/allied-creation-user/pkg/domain"
	"gitlab.fjmaimaimai.com/allied-creation/allied-creation-user/pkg/infrastructure/utils"
	"strconv"
)

// 菜单服务
type MenuService struct {
}

// 创建菜单服务
func (menuService *MenuService) CreateMenu(createMenuCommand *command.CreateMenuCommand) (interface{}, error) {
	if err := createMenuCommand.ValidateCommand(); err != nil {
		return nil, application.ThrowError(application.ARG_ERROR, err.Error())
	}
	transactionContext, err := factory.CreateTransactionContext(nil)
	if err != nil {
		return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
	}
	if err := transactionContext.StartTransaction(); err != nil {
		return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
	}
	defer func() {
		transactionContext.RollbackTransaction()
	}()
	newMenu := &domain.Menu{
		ParentId:     createMenuCommand.ParentId,
		MenuName:     createMenuCommand.MenuName,
		Code:         createMenuCommand.Code,
		AccessCode:   createMenuCommand.AccessCode,
		MenuType:     createMenuCommand.MenuType,
		Icon:         createMenuCommand.Icon,
		Sort:         createMenuCommand.Sort,
		Remark:       createMenuCommand.Desc,
		IsPublish:    createMenuCommand.IsPublish,
		EnableStatus: domain.MenuStatusDisable,
	}
	var menuRepository domain.MenuRepository
	if value, err := factory.CreateMenuRepository(map[string]interface{}{
		"transactionContext": transactionContext,
	}); err != nil {
		return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
	} else {
		menuRepository = value
	}
	// 1.菜单类型验证
	if !newMenu.ValidMenuType() {
		return nil, application.ThrowError(application.ARG_ERROR, domain.ErrorMenuType.Error())
	}
	// 2.菜单编码验证
	if menu, err := menuRepository.FindOne(map[string]interface{}{"code": createMenuCommand.Code}); err == nil && menu != nil {
		return nil, application.ThrowError(application.ARG_ERROR, fmt.Sprintf("菜单编码:%v已重复,请重新输入", createMenuCommand.Code))
	}
	// 3.更新菜单路径
	if newMenu.ParentId > 0 {
		if pMenu, err := menuRepository.FindOne(map[string]interface{}{"menuId": newMenu.ParentId}); err != nil {
			return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, "父级菜单不存在")
		} else {
			newMenu.ParentPath = pMenu.GetParentPath()
			newMenu.Category = pMenu.GetCategory()
		}
	}
	// 4.保存菜单项
	if menu, err := menuRepository.Save(newMenu); err != nil {
		return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
	} else {
		if err := transactionContext.CommitTransaction(); err != nil {
			return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
		}
		return menu, nil
	}
}

// 返回菜单服务
func (menuService *MenuService) GetMenu(getMenuQuery *query.GetMenuQuery) (interface{}, error) {
	if err := getMenuQuery.ValidateQuery(); err != nil {
		return nil, application.ThrowError(application.ARG_ERROR, err.Error())
	}
	transactionContext, err := factory.CreateTransactionContext(nil)
	if err != nil {
		return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
	}
	if err := transactionContext.StartTransaction(); err != nil {
		return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
	}
	defer func() {
		transactionContext.RollbackTransaction()
	}()
	var menuRepository domain.MenuRepository
	if value, err := factory.CreateMenuRepository(map[string]interface{}{
		"transactionContext": transactionContext,
	}); err != nil {
		return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
	} else {
		menuRepository = value
	}
	menu, err := menuRepository.FindOne(map[string]interface{}{"menuId": getMenuQuery.MenuId})
	if err != nil {
		return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
	}
	if menu.ParentId != 0 {
		if _, parentMenu, err := factory.FastPgMenu(transactionContext, menu.ParentId); err != nil {
			return nil, err
		} else {
			menu.ParentMenuName = parentMenu.MenuName
		}
	}
	if menu == nil {
		return nil, application.ThrowError(application.RES_NO_FIND_ERROR, fmt.Sprintf("%s", string(getMenuQuery.MenuId)))
	} else {
		if err := transactionContext.CommitTransaction(); err != nil {
			return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
		}
		return menu, nil
	}
}

// 返回菜单服务列表
func (menuService *MenuService) ListMenu(listMenuQuery *query.ListMenuQuery) (interface{}, error) {
	if err := listMenuQuery.ValidateQuery(); err != nil {
		return nil, application.ThrowError(application.ARG_ERROR, err.Error())
	}
	transactionContext, err := factory.CreateTransactionContext(nil)
	if err != nil {
		return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
	}
	if err := transactionContext.StartTransaction(); err != nil {
		return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
	}
	defer func() {
		transactionContext.RollbackTransaction()
	}()
	var menuRepository domain.MenuRepository
	if value, err := factory.CreateMenuRepository(map[string]interface{}{
		"transactionContext": transactionContext,
	}); err != nil {
		return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
	} else {
		menuRepository = value
	}
	queryOptions := utils.ObjectToMap(listMenuQuery)
	if len(listMenuQuery.MenuCategory) > 0 {
		queryOptions["code"] = ""
		if m, e := menuRepository.FindOne(map[string]interface{}{"code": listMenuQuery.MenuCategory}); e == nil && m != nil {
			queryOptions["category"] = strconv.Itoa(int(m.MenuId))
		}
	}
	if count, menus, err := menuRepository.Find(queryOptions); err != nil {
		return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
	} else {
		if err := transactionContext.CommitTransaction(); err != nil {
			return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
		}
		// 树形返回
		if listMenuQuery.StructType == domain.StructTree {
			var treeNodes = make([]domain.TreeNode, len(menus))
			for i := 0; i < len(menus); i++ {
				treeNodes[i] = menus[i]
			}
			return map[string]interface{}{
				"count": count,
				"menus": domain.NewTrees(treeNodes).Nodes,
			}, nil
		}
		// 列表返回
		return map[string]interface{}{
			"count": count,
			"menus": menus,
		}, nil
	}
}

// 移除菜单服务
func (menuService *MenuService) RemoveMenu(removeMenuCommand *command.RemoveMenuCommand) (interface{}, error) {
	if err := removeMenuCommand.ValidateCommand(); err != nil {
		return nil, application.ThrowError(application.ARG_ERROR, err.Error())
	}
	transactionContext, err := factory.CreateTransactionContext(nil)
	if err != nil {
		return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
	}
	if err := transactionContext.StartTransaction(); err != nil {
		return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
	}
	defer func() {
		transactionContext.RollbackTransaction()
	}()
	var menuRepository domain.MenuRepository
	if value, err := factory.CreateMenuRepository(map[string]interface{}{
		"transactionContext": transactionContext,
	}); err != nil {
		return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
	} else {
		menuRepository = value
	}
	// 1.节点是否存在
	menu, err := menuRepository.FindOne(map[string]interface{}{"menuId": removeMenuCommand.MenuId})
	if err != nil {
		return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
	}
	if menu == nil {
		return nil, application.ThrowError(application.RES_NO_FIND_ERROR, fmt.Sprintf("%s", string(removeMenuCommand.MenuId)))
	}
	// 2. 是否存在子节点
	if m, err := menuRepository.FindOne(map[string]interface{}{"parentId": menu.MenuId}); err == nil && m != nil {
		return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, "下级菜单不为空")
	}
	if menu, err := menuRepository.Remove(menu); err != nil {
		return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
	} else {
		if err := transactionContext.CommitTransaction(); err != nil {
			return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
		}
		return menu, nil
	}
}

// 更新菜单服务
func (menuService *MenuService) UpdateMenu(updateMenuCommand *command.UpdateMenuCommand) (interface{}, error) {
	if err := updateMenuCommand.ValidateCommand(); err != nil {
		return nil, application.ThrowError(application.ARG_ERROR, err.Error())
	}
	transactionContext, err := factory.CreateTransactionContext(nil)
	if err != nil {
		return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
	}
	if err := transactionContext.StartTransaction(); err != nil {
		return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
	}
	defer func() {
		transactionContext.RollbackTransaction()
	}()
	var menuRepository domain.MenuRepository
	if value, err := factory.CreateMenuRepository(map[string]interface{}{
		"transactionContext": transactionContext,
	}); err != nil {
		return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
	} else {
		menuRepository = value
	}
	menu, err := menuRepository.FindOne(map[string]interface{}{"menuId": updateMenuCommand.MenuId})
	if err != nil {
		return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
	}
	if menu == nil {
		return nil, application.ThrowError(application.RES_NO_FIND_ERROR, fmt.Sprintf("%s", string(updateMenuCommand.MenuId)))
	}
	// 1.验证code是否有重复的情况
	if menu.Code != updateMenuCommand.Code {
		if m, e := menuRepository.FindOne(map[string]interface{}{"code": updateMenuCommand.Code}); e == nil && m != nil && m.MenuId != menu.MenuId {
			return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, fmt.Sprintf("菜单编码:%v已重复,请重新输入", updateMenuCommand.Code))
		}
	}
	// 2.验证父级节点是否有发生更新
	if updateMenuCommand.ParentId != menu.ParentId && (updateMenuCommand.ParentId != 0 && menu.ParentId != 0) {
		var oldParentFullPath, newParentFullPath = menu.GetFullPath(), menu.GetFullPath()
		var oldParentPath = menu.ParentPath
		if pMenu, err := menuRepository.FindOne(map[string]interface{}{"menuId": updateMenuCommand.ParentId}); err != nil {
			return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
		} else {
			newParentFullPath = pMenu.GetParentPath()
			menu.ParentPath = newParentFullPath
		}
		// 父级目录不一致时,联动更新其他的子节点的父级目录
		if oldParentFullPath != newParentFullPath {
			if _, menus, err := menuRepository.Find(map[string]interface{}{"parentPath": oldParentFullPath}); err != nil {
				return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
			} else {
				for i := range menus {
					if menus[i].ParentPath == oldParentPath {
						continue
					}
					menus[i].UpdateParentPath(oldParentPath, newParentFullPath)
					if _, err := menuRepository.Save(menus[i]); err != nil {
						return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
					}
				}
			}
		}
	}
	if err := menu.Update(tool_funs.SimpleStructToMap(updateMenuCommand)); err != nil {
		return nil, application.ThrowError(application.BUSINESS_ERROR, err.Error())
	}
	if menu, err := menuRepository.Save(menu); err != nil {
		return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
	} else {
		if err := transactionContext.CommitTransaction(); err != nil {
			return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
		}
		return menu, nil
	}
}

// 批量删除菜单
func (menuService *MenuService) BatchDeleteMenu(batchDeleteMenu *command.BatchDeleteMenuCommand) (interface{}, error) {
	if err := batchDeleteMenu.ValidateCommand(); err != nil {
		return nil, application.ThrowError(application.ARG_ERROR, err.Error())
	}
	transactionContext, err := factory.CreateTransactionContext(nil)
	if err != nil {
		return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
	}
	if err := transactionContext.StartTransaction(); err != nil {
		return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
	}
	defer func() {
		transactionContext.RollbackTransaction()
	}()
	menuRepository, _, err := factory.FastPgMenu(transactionContext, 0)
	if err != nil {
		return nil, err
	}

	for i := 0; i < len(batchDeleteMenu.MenuIds); i++ {
		if menu, err := menuRepository.FindOne(map[string]interface{}{"menuId": batchDeleteMenu.MenuIds[i]}); err != nil {
			return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
		} else {
			if _, err := menuRepository.Remove(menu); err != nil {
				return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
			}
		}
	}

	if err := transactionContext.CommitTransaction(); err != nil {
		return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
	}
	return struct{}{}, nil
}

// 批量删除菜单
func (menuService *MenuService) BatchEnableMenu(batchEnableMenu *command.BatchEnableMenuCommand) (interface{}, error) {
	if err := batchEnableMenu.ValidateCommand(); err != nil {
		return nil, application.ThrowError(application.ARG_ERROR, err.Error())
	}
	transactionContext, err := factory.CreateTransactionContext(nil)
	if err != nil {
		return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
	}
	if err := transactionContext.StartTransaction(); err != nil {
		return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
	}
	defer func() {
		transactionContext.RollbackTransaction()
	}()

	menuRepository, _, err := factory.FastPgMenu(transactionContext, 0)
	if err != nil {
		return nil, err
	}
	for i := 0; i < len(batchEnableMenu.MenuIds); i++ {
		if menu, err := menuRepository.FindOne(map[string]interface{}{"menuId": batchEnableMenu.MenuIds[i]}); err != nil {
			return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
		} else {
			if err := menu.SetPublic(batchEnableMenu.Status); err != nil {
				return nil, application.ThrowError(application.BUSINESS_ERROR, err.Error())
			}
			if _, err := menuRepository.Save(menu); err != nil {
				return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
			}
		}
	}

	if err := transactionContext.CommitTransaction(); err != nil {
		return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
	}
	return struct{}{}, nil
}

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