package com.jiayue.ipfcst.console.service;

import com.jiayue.ipfcst.common.data.entity.SysMenu;
import com.jiayue.ipfcst.common.data.repository.SysMenuRepository;
import com.jiayue.ipfcst.common.data.service.BaseService;
import com.jiayue.ipfcst.common.security.user.SysUser;
import com.jiayue.ipfcst.common.security.user.SysUserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 菜单业务层
 *
 * @author zy
 * @version 1.0
 * @since 2019/8/7 9:32
 */
@Service
@Slf4j
public class SysMenuService extends BaseService {

    private final SysMenuRepository sysMenuRepository;
    private final SysUserService sysUserService;
    @Autowired
    public SysMenuService(SysMenuRepository sysMenuRepository, SysUserService sysUserService) {
        this.sysMenuRepository = sysMenuRepository;
      this.sysUserService = sysUserService;
    }

    public List<SysMenu> getAllMenu(){
      List list = new ArrayList();
      try {
        List<SysMenu> allSysMenu = sysMenuRepository.findAll();
        /*根据path为空找出找出父菜单*/
        List<SysMenu> fatherSysMenu = allSysMenu.stream().filter(w -> w.getPath() == null).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.getItemPath().equals(sysMenu1.getPath())){
                itemSysMenu.add(sysMenu1);
              }
            }
          }
          map.put("id",sysMenu.getId());
          map.put("itemPath",sysMenu.getItemPath());
          map.put("title",sysMenu.getTitle());
          map.put("children",itemSysMenu);
          map.put("url",sysMenu.getUrl());
          list.add(map);
        }
      }catch (Exception e){
        log.error("菜单查询异常：" + e);
      }
      return list;
    }
  public List<SysMenu> getMenuFindByUserName(String username,String url){
    List list = new ArrayList();
    try {
      SysUser user = sysUserService.findByUserName(username);
      if(user.getMenu() != null){
        String[] menuId = user.getMenu().split(",");
        /*根据9000/9001过滤出菜单*/
        List<SysMenu> sysMenuList = sysMenuRepository.findAll().stream().filter(w -> w.getUrl().equals(url)).collect(Collectors.toList());
        /*根据path为空找出9000/90001找出父菜单*/
        List<SysMenu> sysFatherMenuList = sysMenuList.stream().filter(w -> w.getPath() == null).collect(Collectors.toList());
        /*根据path不为空找出9000/90001找出子菜单*/
        List<SysMenu> sysChildrenMenuList = sysMenuList.stream().filter(w -> w.getPath() != null).collect(Collectors.toList());
        List<SysMenu> fatherList = new ArrayList();
        List<SysMenu> childrenList = new ArrayList();
        for(String str : menuId){
          /*根据保存的菜单id筛选父菜单*/
          for (SysMenu sysMenu :sysFatherMenuList){
            if(sysMenu.getId().toString().equals(str)){
              fatherList.add(sysMenu);
            }
          }
          /*根据保存的菜单id筛选子菜单*/
          for(SysMenu sysChildrenMenu :sysChildrenMenuList){
            if(sysChildrenMenu.getId().toString().equals(str)){
              childrenList.add(sysChildrenMenu);
            }
          }
        }
        /*根据父path整合父子菜单*/
        for(SysMenu father:fatherList){
          Map map = new HashMap();
          List children = new ArrayList();
          for(SysMenu children1:childrenList){
            if(children1.getPath().equals(father.getItemPath())){
              children.add(children1);
            }
          }
          map.put("id",father.getId());
          map.put("itemPath",father.getItemPath());
          map.put("title",father.getTitle());
          map.put("children",children);
          map.put("url",father.getUrl());
          list.add(map);
        }
      }
    }catch (Exception e){
      log.error("根据用户名查询菜单异常：" + e);
    }
    return list;
  }
}
