package com.bonus.gs.sub.evaluate.evaluate.service;

import com.bonus.gs.sub.evaluate.evaluate.dao.EvaluateDao;
import com.bonus.gs.sub.evaluate.manager.entity.ResourcesBean;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author xliu
 * @date 2024/11/15 15:23
 */
@Service("EvaluateService")
public class EvaluateServiceImpl implements EvaluateService{


    @Autowired
    private EvaluateDao evaluateDao;

    @Override
    public List<ResourcesBean> getAllList(ResourcesBean bean) {
        return buildTree(evaluateDao.getAllList(bean));
    }

    public static List<ResourcesBean> buildTree(List<ResourcesBean> menusDB) {
        List<ResourcesBean> res = new ArrayList<ResourcesBean>();
        for (ResourcesBean node : menusDB) {
            if ("0".equals(node.getParentId())) {
                List<ResourcesBean> children = getChildren(menusDB, node);
                node.setNodes(children);
                build(menusDB, node, res);
                res.add(node);
            }

        }
        return res;
    }



    private static List<ResourcesBean> getChildren(List<ResourcesBean> menusDB,
                                                   ResourcesBean node) {
        List<ResourcesBean> children = new ArrayList<ResourcesBean>();
        String id = node.getId()+"";
        for (ResourcesBean child : menusDB) {
            if (id.equals(child.getParentId())) {
                children.add(child);
            }
        }
        return children;
    }

    private static void build(List<ResourcesBean> menusDB, ResourcesBean node,
                              List<ResourcesBean> res) {
        List<ResourcesBean> children = getChildren(menusDB, node);
        if (!children.isEmpty()) {
            for (ResourcesBean child : children) {
                List<ResourcesBean> children2 = getChildren(menusDB, child);
                child.setNodes(children2);
                build(menusDB, child, res);
            }
        }
    }

    private static void build(List<ResourcesBean> menusDB, ResourcesBean node,
                              StringBuffer html) {
        List<ResourcesBean> children = getChildren(menusDB, node);
        if (!children.isEmpty()) {
            html.append("\r\n<ul class='submenu'>");
            for (ResourcesBean child : children) {
                boolean childrenHas = false;
                List<ResourcesBean> children2 = getChildren(menusDB, child);
                if (!children2.isEmpty()) {
                    childrenHas = true;
                }
                html.append("\r\n<li id='menu" + child.getId() + "' >");
                html.append("\r\n<a ");
                html.append(" href='#maincontent' style='cursor:pointer;' onclick=\"openMenu(this,'"
                        + child.getType()+ "','menu"+ child.getId()
                        + "','menu"
                        + child.getParentId()
                        + "','"
                        + child.getName()
                        + "','"
                        + (StringUtils.isNotBlank(child.getUrl()) ? child
                        .getUrl().trim() : "noset") + "');\" ");
                if (childrenHas) {
                    html.append(" target='mainFrame' class='dropdown-toggle' ");
                }
                html.append(" >");
                if (!"0".equals(child.getParentId())) {
                    html.append("\r\n<i class='icon-double-angle-right' ></i>");
                }
                html.append("\r\n<span class='menu-text'>");
                if (StringUtils.isNotEmpty(child.getIcon())) {
                    html.append("\r\n<i class= " + child.getIcon()
                            + " ></i>&nbsp;&nbsp;" + child.getName());
                } else {
                    html.append(child.getName());
                }
                html.append("</span>");
                if (childrenHas) {
                    html.append("<b class='arrow icon-angle-down'></b>");
                }
                html.append("</a>");
                build(menusDB, child, html);
                html.append("</li>");
            }
            html.append("\r\n</ul>");
        }
    }










}
