package com.manage.controller;

import com.manage.entity.Power_Menu;
import com.manage.service.cache.CacheManager;
import com.manage.service.Power_MenuService;
import com.manage.util.ActionScopeUtils;
import com.manage.util.Constant;
import com.manage.util.Msg;
import com.manage.vo.Power_UserVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * @Author:hjl
 * @Date:Creatid in 1:28 2019/4/17
 * @Description:菜單管理
 */
@Controller
@RequestMapping("menuPower/")
public class Power_MenuController {
    @Autowired
    private Power_MenuService powerMenuService;

    /*@RequestMapping("powerMenuList")
    @ResponseBody
    public Msg list() {
        try {
            List<Power_Menu> list = powerMenuService.queryAllPowerMenu("power");
            CacheManager.addExcCount("noExc");
            return Msg.success().add("list",list);
        }catch (Exception e){
            CacheManager.addExcCount("exc");
            e.printStackTrace();
            return Msg.fail();
        }
    }*/

    @RequestMapping("indexMenu")
    @ResponseBody
    public Msg indexMenu() throws Exception{
        Power_UserVo user = (Power_UserVo) ActionScopeUtils.getSessionAttribute( Constant.CURRENT_USER);
        List<Power_Menu> list = null;
        if(user.getRoleId().equals(0) || user.getRoleId().equals(-100)){
            list = powerMenuService.queryAllPowerMenu("power",user.getRoleId());
        } else{
            list = powerMenuService.selectUserAndRoleMenuListPower(user.getUserId(),"power");
        }
        CacheManager.addExcCount("noExc");
        return Msg.success().add("listPower", list);

/*
        Power_User user = (Power_User) ActionScopeUtils.getSessionAttribute( Constant.CURRENT_USER);
        List<Power_Menu> list = null;
        List<Power_Menu_User> listPower = null;
        if (user.getRoleId().equals(0)) {
            list = powerMenuService.queryAllPowerMenu();
            return Msg.success().add("listRole", list);
        } else if(user.getRoleId().equals(-100)){
            listPower = powerMenuService.queryPoswerMenuByUserId(user.getUserId());
            List<Power_Menu_User> indexList = new ArrayList<>();
            if(null != listPower && !listPower.isEmpty()){
                for (int i = 0; i < listPower.size(); i++) {
                    if(StringUtils.isBlank(listPower.get(i).getMenuName()) || "菜单管理".equals(listPower.get(i).getMenuName())){
                        indexList.add(listPower.get(i));
                    }
                }
            }
            if(null != indexList && !indexList.isEmpty()){
                for (int i = 0; i < indexList.size(); i++) {
                    for (int j = 0; j < listPower.size(); j++) {
                        if(listPower.get(j).getMenuId() == indexList.get(i).getMenuId()){
                            listPower.remove(indexList.get(i));
                        }
                    }
                }
            }
            return Msg.success().add("listPower", listPower);
        } else{
            List<Power_Detailed_Menu> menuList = powerMenuService.queryMenuViewByUserId(user.getUserId());
            List<Power_Detailed_Menu> indexList = new ArrayList<>();
            if(null != menuList && !menuList.isEmpty()){
                for (int i = 0; i < menuList.size(); i++) {
                    if(StringUtils.isBlank(menuList.get(i).getMenuName())){
                        indexList.add(menuList.get(i));
                    }
                }
            }
            if(null != indexList && !indexList.isEmpty()){
                for (int i = 0; i < indexList.size(); i++) {
                    for (int j = 0; j < menuList.size(); j++) {
                        if(menuList.get(j).getMenuId() == indexList.get(i).getMenuId()){
                            menuList.remove(indexList.get(i));
                        }
                    }
                }
            }
            return Msg.success().add("listUser", menuList);
        }
*/
    }

}
