package com.muchinfo.mtp3century.controller;


import com.muchinfo.mtp3century.service.ISystemRoleService;
import com.muchinfo.mtp3century.utils.CenturyDecryptUtil;
import com.muchinfo.mtp3common.enumtype.Constants;
import com.muchinfo.mtp3common.enumtype.MessageType;
import com.muchinfo.mtp3common.utils.JsonUtils;
import com.muchinfo.mtp3common.vo.PageResult;
import com.muchinfo.mtp3pojos.vo.SystemRoleVo;
import com.muchinfo.mtp3century.utils.AjaxResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

@Controller
@RequestMapping("/role")
@Api(value = "角色API", description = "管理员角色管理")
public class SystemRoleController {
    private final Logger log = LoggerFactory.getLogger(this.getClass());
    @Resource
    private ISystemRoleService iSystemRoleService;


    @ApiOperation("处理角色菜单信息")
    @RequestMapping(method = RequestMethod.POST, value = "/dealRoleMenus")
    @ResponseBody
    private AjaxResult dealRoleMenus(@RequestBody String systemRoleVo, HttpServletRequest request) {
        try {
            SystemRoleVo currentVo = JsonUtils.toBean(CenturyDecryptUtil.decryptForJson(systemRoleVo, request), SystemRoleVo.class);
            return iSystemRoleService.dealRoleMenus(currentVo, request);
        } catch (Exception e) {
            log.error("处理角色菜单信息失败，失败原因：" + e);
            return AjaxResult.error(Constants.Error_Code_Design, MessageType.MESSAGE_ERROR_CODE_SYSTEM002.getCode(), request.getHeader("Accept-Language"));
        }
    }

    @ApiOperation("删除角色菜单信息")
    @ResponseBody
    @RequestMapping(method = RequestMethod.POST, value = "/deleteRoleMenus")
    private AjaxResult deleteRoleMenus(@RequestBody String systemRoleVo, HttpServletRequest request) {
        try {
            SystemRoleVo currentVo = JsonUtils.toBean(CenturyDecryptUtil.decryptForJson(systemRoleVo, request), SystemRoleVo.class);
            return iSystemRoleService.deleteRoleMenus(currentVo, request);
        } catch (Exception e) {
            log.error("删除角色菜单信息失败，失败原因：" + e);
            return AjaxResult.error(Constants.Error_Code_Design, MessageType.MESSAGE_ERROR_CODE_SYSTEM002.getCode(), request.getHeader("Accept-Language"));
        }
    }

    @ApiOperation("获取角色菜单列表信息")
    @ResponseBody
    @RequestMapping(method = RequestMethod.GET, value = "/query")
    @ApiResponse(code = 200, message = "成功", response = AjaxResult.class)
    private PageResult<SystemRoleVo> query(Integer type, Integer pageNum, Integer pageSize, HttpServletRequest request) {
        return iSystemRoleService.query(type, pageNum, pageSize, request);
    }

    @ApiOperation("获取添加管理员获取角色菜单列表")
    @ResponseBody
    @RequestMapping(method = RequestMethod.GET, value = "/queryByAdd")
    @ApiResponse(code = 200, message = "成功", response = AjaxResult.class)
    private AjaxResult<SystemRoleVo> queryByAdd(HttpServletRequest httpServletRequest) {
        return iSystemRoleService.queryByAdd(httpServletRequest);
    }

    @ApiOperation("获取当前角色详情信息")
    @ResponseBody
    @RequestMapping(method = RequestMethod.GET, value = "/queryDetail")
    @ApiResponse(code = 200, message = "成功", response = AjaxResult.class)
    private AjaxResult<SystemRoleVo> queryDetail(Integer autoId, HttpServletRequest request) {
        return iSystemRoleService.queryDetail(autoId, request);
    }
}
