package com.huimv.manage.sys.controller;

import com.huimv.manage.sys.entity.Menu;
import com.huimv.manage.sys.service.IAuthorizeService;
import com.huimv.manage.sys.service.IMenuService;
import com.huimv.manage.common.utils.Result;
import com.huimv.manage.common.utils.ResultCode;;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@CrossOrigin
@RestController
@RequestMapping(value = "/auth")
public class AuthorizeController {

    @Autowired
    IAuthorizeService iAuthorizeService;
    @Autowired
    IMenuService iMenuService;

    @PostMapping("/saveAccountGroup")
    public Result saveAccountGroup(@RequestBody Map<String,String> map){
        /*
         * 保存账号关联权限组数据
         */
        String accountId = map.get("accountId");
        String groupIds = map.get("groupIds");
        return iAuthorizeService.saveAccountGroup(accountId,groupIds);
    }

    @PostMapping("/saveGroupMenu")
    public Result saveGroupMenu(@RequestBody Map<String,String> map){
        /*
         * 保存权限组关联菜单数据
         */
        String groupId = map.get("groupId");
        String menuIds = map.get("menuIds");
        return iAuthorizeService.saveGroupMenu(groupId,menuIds);
    }

    @RequestMapping("/getGroupByAccount")
    public Result getGroupByAccount(@RequestParam(name = "accountId", required = true) String accountId){
        /*
         * 根据账号获取权限组
         */
        return iAuthorizeService.getGroupByAccount (accountId);
    }

    @GetMapping("/getMenuByGroup")
    public Result getMenuByGroup(String groupId){
        /*
         * 根据权限组获取菜单
         */
        return iAuthorizeService.getMenuByGroup(groupId);
    }

    @RequestMapping("/getMyMenu")
    public Result getMyMenuAndButton(@RequestParam(name = "userId", required = true) String userId){
        /*
         * 获取用户的菜单数据
         */
        List<Menu> myMenu = iAuthorizeService.getMyMenuAll(userId);
        List<Map> list = iMenuService.optimizeListUtil(0,myMenu);
        return new Result(ResultCode.SUCCESS,list);
    }
//    //获取菜单-多级
//    @RequestMapping("/getMyMenuMultilevel")
//    public Result getMyMenuMultilevel(@RequestParam(name = "userId", required = true) String userId){
//        return new Result(ResultCode.SUCCESS,iAuthorizeService.getMyMenuMultilevel(userId));
//    }


    @GetMapping("/getMyButton")
    public Result getMyButton(@RequestParam(name = "userId", required = true) Integer userId){
        /*
         * 获取用户的菜单数据
         */
      List list = iAuthorizeService.getMyButton(userId);
        return new Result(ResultCode.SUCCESS,list);
    }


    @GetMapping("/getAcountByGroup")
    public Result getAccountByGroup(String groupId){
        /*
         * 根据权限组获取菜单
         */
        return iAuthorizeService.getAccountByGroup(groupId);
    }
}
