package com.huimv.receive.controller;

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

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

@RestController
@RequestMapping(value = "/admin/auth")
@CrossOrigin
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");
        String isAll = map.get("isAll");
        return iAuthorizeService.saveGroupMenu(groupId,menuIds,isAll);
    }

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

    @PostMapping("/getMenuByGroup")
    public Result getMenuByGroup(@RequestBody Map<String, String> map){
        /*
         * 根据权限组获取菜单
         */
        return iAuthorizeService.getMenuByGroup(map);
    }

    @PostMapping("/getMenuByGroupAndFarm")
    public Result getMenuByGroupAndFarm(@RequestBody Map<String, String> map){
        /*
         * 根据权限组获取菜单
         */
        return iAuthorizeService.getMenuByGroupAndFarm(map);
    }

    @PostMapping("/getMyMenu")
    public Result getMyMenuAndButton(@RequestBody  Map<String,String> map){
        /*
         * 获取用户的菜单数据
         */
        String userId = map.get("userId");
        String farmId = map.get("farmId");
        List<Menu> myMenu = iAuthorizeService.getMyMenuAll(userId,farmId);

        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));
//    }


    @PostMapping("/getMyButton")
    public Result getMyButton(@RequestBody Map<String,Integer> map){
        /*
         * 获取用户的菜单数据
         */
        Integer userId = map.get("userId");
        Integer farmId = map.get("farmId");
        List list = iAuthorizeService.getMyButton(userId,farmId);
        return new Result(ResultCode.SUCCESS,list);
    }


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