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.ITaUserAuthButtonService;
import com.huiju.estateagents.entity.SysButtonInMenu;
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: TaUserAuthButtonController
 * @Description: TODO
 * @date 2019/9/23
 */
@RestController
@RequestMapping("/api")
public class TaUserAuthButtonController extends BaseController {

    @Autowired
    public ITaUserAuthButtonService iTaUserAuthButtonService;

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


    /**
     * 获取所有的菜单按钮
     * @param pageNum
     * @param pageSize
     * @return
     */
    @RequestMapping(value="/admin/buttonList",method= RequestMethod.GET)
    public ResponseBean getMenuByRole(@RequestParam("pageNum")Integer pageNum,
                                      @RequestParam("pageSize")Integer pageSize){
        return iTaUserAuthButtonService.getAllButton(pageNum, pageSize);
    }

    /**
     * 用户授权按钮
     * @return
     */
    @RequestMapping(value="/admin/updateAuthButton",method= RequestMethod.PUT)
    public ResponseBean updateUserAuthMenu(HttpServletRequest request,
                                           @RequestBody String sysButton){
        Integer userId = getUserId(request);
        JSONObject Object = JSONObject.parseObject(sysButton);
        JSONArray JSONArray = Object.getJSONArray("sysButton");
        List<SysButtonInMenu> sysButtonInMenuList = JSONArray.toJavaList(SysButtonInMenu.class);
        return iTaUserAuthButtonService.updateUserAuthButton(userId, sysButtonInMenuList);
    }
}
