package com.huimv.admin.controller.primary;

import com.huimv.admin.domain.primary.SysMenuEntity;
import com.huimv.admin.service.primary.IAuthorizeService;
import com.huimv.admin.service.primary.IMenuService;
import com.huimv.admin.utils.Result;
import com.huimv.admin.utils.ResultCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

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

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

    @Autowired
    IAuthorizeService iAuthorizeService;
    @Autowired
    IMenuService iMenuService;

    @RequestMapping("/saveAccountGroup")
    public Result saveAccountGroup(@RequestParam(name = "accountId") String accountId, @RequestParam(name = "groupIds") String groupIds){
        /*
         * 保存账号关联权限组数据
         */
        return iAuthorizeService.saveAccountGroup(accountId,groupIds);
    }

    @RequestMapping("/saveGroupMenu")
    public Result saveGroupMenu(@RequestParam(name = "groupId", required = true) String groupId, @RequestParam(name = "menuIds", required = true) String menuIds){
        /*
         * 保存权限组关联菜单数据
         */
        return iAuthorizeService.saveGroupMenu(groupId,menuIds);
    }

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

    @RequestMapping("/getMenuByGroup")
    public Result getMenuByGroup(@RequestParam(name = "groupId", required = true) String groupId){
        /*
         * 根据权限组获取菜单
         */
        return iAuthorizeService.getMenuByGroup(groupId);
    }

  /*  @RequestMapping("/getMyMenu")
    public Result getMyMenu(@RequestParam(name = "accountName", required = true) String accountName){
        System.out.println("accountName>>"+accountName);
        *//*
         * 获取用户的菜单数据
         *//*

        return iAuthorizeService.getMyMenu(accountName);
    }*/
    @RequestMapping("/getMyMenu")
    public Result getMyMenu(@RequestParam(name = "accountName", required = true) String accountName){
        System.out.println("accountName>>"+accountName);
        /*
         * 获取用户的菜单数据
         */
        List<SysMenuEntity> myMenu = iAuthorizeService.getMyMenuAll(accountName);
        List<Map> list = iMenuService.optimizeListUtil(0,myMenu);

        return new Result(ResultCode.SUCCESS,list);
    }

    @RequestMapping("/getAcountByGroup")
    public Result getAccountByGroup(@RequestParam(name = "groupId", required = true) String groupId){
        /*
         * 根据权限组获取菜单
         */
        return iAuthorizeService.getAccountByGroup(groupId);
    }
}
