package com.xunhong.erp.turbo.rbac.app.executor.query;

import cn.hutool.core.lang.tree.Tree;
import cn.hutool.core.lang.tree.TreeNodeConfig;
import cn.hutool.core.lang.tree.TreeUtil;
import com.xunhong.erp.turbo.api.rbac.dto.qry.MenuTreeQry;
import com.xunhong.erp.turbo.rbac.domain.entity.Menu;
import com.xunhong.erp.turbo.rbac.domain.gateway.MenuGateway;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * @author shenyifei
 */
@Component
@RequiredArgsConstructor
public class MenuTreeQryExe {
	private final MenuGateway menuGateway;

	public List<Tree<Long>> execute(MenuTreeQry menuTreeQry) {
		List<Menu> menuList = menuGateway.tree(menuTreeQry);

		TreeNodeConfig treeNodeConfig = new TreeNodeConfig();
		treeNodeConfig.setWeightKey("sort");
		treeNodeConfig.setIdKey("menuId");
		treeNodeConfig.setParentIdKey("pid");
		treeNodeConfig.setNameKey("name");
		treeNodeConfig.setChildrenKey("routes");
		treeNodeConfig.setDeep(5);

		return TreeUtil.build(menuList, menuTreeQry.getPid(), treeNodeConfig, (appMenu, tree) -> {
			tree.setId(appMenu.getMenuId());
			tree.setParentId(appMenu.getPid());
			tree.setWeight(appMenu.getSort());
			tree.setName(appMenu.getName());
			// 扩展属性 ...
			tree.putExtra("path", appMenu.getPath());
			tree.putExtra("platformId", appMenu.getPlatformId());
			tree.putExtra("hideInMenu", appMenu.getHideInMenu());
			tree.putExtra("type", appMenu.getType());
			tree.putExtra("component", appMenu.getComponent());
		});
	}
}
