package com.huiju.estateagents.center.taUser.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.huiju.estateagents.base.BaseController;
import com.huiju.estateagents.base.ResponseBean;
import com.huiju.estateagents.center.taUser.service.ITaUserAuthMenuService;
import com.huiju.estateagents.entity.SysButtonInMenu;
import com.huiju.estateagents.entity.SysMenu;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @author weicc
 * @Title: TaUserAuthMenuController
 * @Description: TODO
 * @date 2019/9/21
 */
@RestController
@RequestMapping("/api")
public class TaUserAuthMenuController extends BaseController {

    @Autowired
    public ITaUserAuthMenuService iTaUserAuthMenuService;

    /**
     * 根据userId获取菜单列表
     * @param pageNum
     * @param pageSize
     * @return
     */
    @RequestMapping(value="/admin/userAuthMenu",method= RequestMethod.GET)
    public ResponseBean  getMenuByUserId(@RequestParam("pageNum")Integer pageNum,
                                         @RequestParam("pageSize")Integer pageSize,
                                         HttpServletRequest request){
        Integer userId = getUserId(request);
        return iTaUserAuthMenuService.getMenuByUserId(pageNum,pageSize,userId.toString(), getOrgId(request));
    }

    /**
     * 获取所有菜单
     * @param pageNum
     * @param pageSize
     * @return
     */
    @RequestMapping(value="/admin/menuList",method= RequestMethod.GET)
    public ResponseBean getMenuByRole(@RequestParam("pageNum")Integer pageNum,
                                      @RequestParam("pageSize")Integer pageSize, HttpServletRequest request){
        return iTaUserAuthMenuService.getAllMenu(pageNum, pageSize, getOrgId(request), null);
    }

    /**
     * 用户授权菜单
     * @return
     */
    @RequestMapping(value="/admin/updateAuthMenu",method= RequestMethod.POST)
    public ResponseBean updateUserAuthMenu(HttpServletRequest request,
                                           @RequestBody String sysMenu){
        JSONObject Object = JSONObject.parseObject(sysMenu);
        String name = Object.getString("name");
        Integer userId = Object.getInteger("id");
        JSONArray JSONArray = Object.getJSONArray("sysMenu");
        List<SysMenu> sysMenuList = JSONArray.toJavaList(SysMenu.class);

        JSONArray buttonArray = Object.getJSONArray("sysButton");
        List<SysButtonInMenu> sysButtonInMenuList = buttonArray.toJavaList(SysButtonInMenu.class);
        return iTaUserAuthMenuService.updateUserAuthMenu(userId, sysMenuList,name,getOrgId(request),sysButtonInMenuList);
    }
}
