package com.jiayue.ipp.idp.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jiayue.ipp.common.data.entity.SysMenu;
import com.jiayue.ipp.common.data.entity.SysUser;
import com.jiayue.ipp.common.security.user.impl.SysUserServiceImpl;
import com.jiayue.ipp.idp.mapper.SysMenuMapper;
import com.jiayue.ipp.idp.service.SysMenuService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

/**
 * 菜单业务层
 *
 * @author zy
 * @version 1.0
 * @since 2019/8/7 9:32
 */
@Service
@Slf4j
public class SysMenuServiceImpl extends ServiceImpl<SysMenuMapper, SysMenu> implements SysMenuService {
  @Autowired
  SysMenuMapper sysMenuMapper;
  @Autowired
  SysUserServiceImpl sysUserServiceImpl;


  public List<SysMenu> getAllMenu(){
    List list = new ArrayList();
    try {
      List<SysMenu> allSysMenu = sysMenuMapper.selectList(null);
      /*根据path为空找出找出父菜单*/
      List<SysMenu> fatherSysMenu = allSysMenu.stream().filter(w -> w.getParentId().equals("-1")).collect(Collectors.toList());
      for (SysMenu sysMenu :fatherSysMenu){
        List itemSysMenu = new ArrayList();
        Map map = new HashMap();
//        if(sysMenu.getId() != 37){
          /*根据父path与子itemPath相同找出父的子菜单*/
          for(SysMenu sysMenu1 :allSysMenu){
            if(sysMenu.getId().longValue()==Long.parseLong(sysMenu1.getParentId())){
              itemSysMenu.add(sysMenu1);
            }
          }
//        }
        map.put("id",sysMenu.getId());
        map.put("itemPath",sysMenu.getPath());
        map.put("title",sysMenu.getTitle());
        map.put("children",itemSysMenu);
        map.put("url",sysMenu.getPath());
        list.add(map);
      }
    }catch (Exception e){
      log.error("菜单查询异常：" + e);
    }
    return list;
  }
  public List<SysMenu> getMenuFindByUserName(String username){
    List list = new ArrayList();
    try {
      SysUser user = sysUserServiceImpl.findByUserName(username);
      if(user.getMenu() != null && !user.getMenu().equals("")){
        String[] menuId = user.getMenu().split(",");
        /*根据9000/9001过滤出菜单*/
        List<SysMenu> allMenuList = sysMenuMapper.selectList(null);
        List<SysMenu> sysMenuList = new ArrayList<>();
        for (SysMenu sysMenu:allMenuList){
          for (String str:menuId){
            if(sysMenu.getId().toString().equals(str)){
              sysMenuList.add(sysMenu);
            }
          }
        }

        /*根据path为空找出9000/90001找出父菜单*/
        List<SysMenu> sysFatherMenuList = sysMenuList.stream().filter(w -> w.getParentId().equals("-1")).collect(Collectors.toList());
        sysFatherMenuList.sort(Comparator.comparing(SysMenu::getSort));
        /*根据path不为空找出9000/90001找出子菜单*/
        List<SysMenu> sysChildrenMenuList = sysMenuList.stream().filter(w -> !w.getParentId().equals("-1")).collect(Collectors.toList());
        sysChildrenMenuList.sort(Comparator.comparing(SysMenu::getSort));
        /*根据父path整合父子菜单*/
        for(SysMenu father:sysFatherMenuList){
          List children = new ArrayList();
          for(SysMenu children1:sysChildrenMenuList){
            if(children1.getParentId().equals(father.getId().toString())){
              Map cmap = new HashMap();
              cmap.put("name", children1.getTitle());
              cmap.put("label", children1.getTitle());
              cmap.put("path",children1.getPath());
              cmap.put("hasChildren",false);
              List list2 = new ArrayList();
              cmap.put("children",list2);
              cmap.put("id",children1.getId().toString());
              cmap.put("parentId",children1.getParentId());
              cmap.put("icon","");
              cmap.put("spread",false);
              cmap.put("menuType","0");
              cmap.put("keepAlive","0");
              cmap.put("permission","");
              cmap.put("sort",children1.getSort());
              children.add(cmap);
            }
          }
          Map fmap = new HashMap();
          fmap.put("name", father.getTitle());
          fmap.put("label", father.getTitle());
          fmap.put("path",father.getPath());
          fmap.put("hasChildren",true);
          fmap.put("children",children);
          fmap.put("id",father.getId().toString());
          fmap.put("parentId","-1");
          fmap.put("icon",father.getIcon());
          fmap.put("spread",false);
          fmap.put("menuType","0");
          fmap.put("keepAlive","0");
          fmap.put("permission","");
          fmap.put("sort",father.getSort());
          list.add(fmap);
        }
      }
    }catch (Exception e){
      log.error("根据用户名查询菜单异常：" + e);
    }
    return list;
  }
}
