package cn.com.countrygarden.bi.main.controller;

import cn.com.countrygarden.bi.main.core.Result;
import cn.com.countrygarden.bi.main.model.AccessToken;
import cn.com.countrygarden.bi.main.model.sys.Menu;
import cn.com.countrygarden.bi.main.service.MenuService;
import cn.com.countrygarden.bi.main.service.RoleService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * package: cn.com.countrygarden.bi.main.controller
 * describe:
 * creat_user: lsl
 * creat_date: 2018/6/8
 * creat_time: 11:03
 **/
@RestController("MenuController")
@RequestMapping("/api/menu")
public class MenuController extends BaseController {
	@Autowired
    private MenuService menuService;
    @Autowired
    private RoleService roleService;
    @GetMapping(value = "/one", produces = "application/json;charset=utf-8")
    public Result queryById(@RequestParam(value = "id", required = true)Integer id,
                            HttpServletRequest request){
        return menuService.queryDataById(id);
    }

    @PostMapping(value = "/add")
    public Result add(Menu menu,
                      HttpServletRequest request){
        return menuService.add(menu,getAccessToken(request));
    }

    @PutMapping(value = "/edit")
    public Result edit(Menu menu, HttpServletRequest request){
        return menuService.edit(menu,getAccessToken(request));
    }

    @DeleteMapping(value="/del")
    public Result del(@RequestParam(value = "id", required = true)Integer id){
        return menuService.del(id);
    }



    @GetMapping(value = "/tree", produces = "application/json;charset=utf-8")
    public Result treeData(@RequestParam(value = "roleCode",required = true)String roleCode,
                           HttpServletRequest request){
        return menuService.queryMenuTreeData(roleCode);
    }
    
    /**
     * 根据人员权限获取菜单
     */
    @GetMapping(value = "/roleTree", produces = "application/json;charset=utf-8")
    public Result getTreeDataByRole(@RequestParam(value = "roleCode",required = true)String roleCode) {
    	AccessToken accessToken = this.getAccessToken();
    	return menuService.queryMenuTreeDataByRole(roleCode,accessToken.getUserCode());
    }
    
    @PutMapping(value = "editRolesMenus", produces = "application/json;charset=utf-8")
    public Result editRolesMenus(
            @RequestParam(value = "rCode",required = true)String roleCode,
            @RequestParam(value = "mCodes",required = true)String menuCodes,
            HttpServletRequest request){
    	AccessToken accessToken = this.getAccessToken();
        roleService.checkIsAdmin(accessToken.getUserCode());    
        return menuService.editRolesMenus(roleCode,menuCodes, getAccessToken(request).getUserCode());
    }
}
