package com.ym.mec.auth.web.controller;

import com.ym.mec.auth.api.entity.SysRole;
import com.ym.mec.auth.config.constant.SecurityConstants;
import com.ym.mec.auth.service.SysMenuService;
import com.ym.mec.auth.service.SysRoleMenuService;
import com.ym.mec.auth.service.SysRoleService;
import com.ym.mec.common.controller.BaseController;
import com.ym.mec.common.page.QueryInfo;
import io.swagger.annotations.*;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import java.util.Date;

@RestController()
@RequestMapping("role")
@Api(tags = "角色服务")
public class RoleController extends BaseController {

    @Autowired
    private SysRoleService sysRoleService;
    @Autowired
    private SysRoleMenuService sysRoleMenuService;
    @Autowired
    private SysMenuService sysMenuService;

    @ApiOperation("根据权限id查询角色")
    @GetMapping(value = "/{id}")
    public Object getRole(@ApiParam(value = "权限编号", required = true) @PathVariable("id") Integer id) {
        return succeed(sysRoleService.get(id));
    }

    @ApiOperation("删除角色")
    @PostMapping(value = "/del/{id}")
    public Object delRole(@ApiParam(value = "权限编号", required = true) @PathVariable("id") Integer id) {
        return succeed(sysRoleService.delete(id));
    }

    @ApiOperation("修改角色")
    @PostMapping(value = "/update",consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE)
    public Object updateRole(SysRole sysRole) {
        SysRole roleByCode = sysRoleService.findRoleByCode(sysRole.getRoleCode());
        if(roleByCode != null && !roleByCode.getId().equals(sysRole.getId())){
            return failed("权限标识已存在");
        }
        sysRole.setUpdateTime(new Date());
        sysRoleService.update(sysRole);
        return succeed();
    }

    @ApiOperation("新增角色")
    @PostMapping(value = "/add")
    public Object addRole(@RequestBody SysRole sysRole) {
        sysRoleService.addRole(sysRole);
        return succeed();
    }

    @ApiOperation("分页查询角色列表")
    @GetMapping(value = "/queryPage")
    public Object queryPage(QueryInfo queryInfo) {
        return succeed(sysRoleService.queryPage(queryInfo));
    }

    @ApiOperation("角色新增菜单权限(批量)")
    @ApiImplicitParams({ @ApiImplicitParam(name = "roleId", value = "角色编号", required = true, dataType = "Integer"),
            @ApiImplicitParam(name = "menuIds", value = "菜单id,逗号分隔", required = true, dataType = "String") })
    @PostMapping(value = "/addRoleMenu",consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE)
    public Object addRoleMenu(String roleId,String menuIds) {
        if(StringUtils.isEmpty(roleId) || StringUtils.isEmpty(menuIds)){
            return failed(SecurityConstants.PARAM_VERIFY_EXCEPTION);
        }
        sysRoleMenuService.batchInsert(Integer.parseInt(roleId),menuIds);
        return succeed();
    }

    @ApiOperation("角色删除菜单权限(批量)")
    @ApiImplicitParams({ @ApiImplicitParam(name = "roleId", value = "角色编号", required = true, dataType = "Integer"),
            @ApiImplicitParam(name = "menuIds", value = "菜单id,逗号分隔", required = true, dataType = "String") })
    @PostMapping(value = "/delRoleMenu",consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE)
    public Object delRoleMenu(String roleId,String menuIds) {
        if(StringUtils.isEmpty(roleId) || StringUtils.isEmpty(menuIds)){
            return failed(SecurityConstants.PARAM_VERIFY_EXCEPTION);
        }
        sysRoleMenuService.batchdel(Integer.parseInt(roleId),menuIds);
        return succeed();
    }

    @ApiOperation("根据角色编号查询拥有的菜单列表")
    @ApiImplicitParams({ @ApiImplicitParam(name = "roleId", value = "角色编号", required = true, dataType = "Integer")})
    @GetMapping(value = "/getMenus")
    public Object getMenus(Integer roleId) {
        if(roleId == null){
            return failed(SecurityConstants.PARAM_VERIFY_EXCEPTION);
        }
        return succeed(sysMenuService.getMenusByRoleId(roleId));
    }
}
