package com.keao.edu.auth.web.controller;

import com.keao.edu.auth.api.entity.SysRole;
import com.keao.edu.auth.api.util.SecurityConstants;
import com.keao.edu.auth.service.SysMenuService;
import com.keao.edu.auth.service.SysRoleMenuService;
import com.keao.edu.auth.service.SysRoleService;
import com.keao.edu.auth.service.SysUserRoleService;
import com.keao.edu.common.controller.BaseController;
import com.keao.edu.common.page.QueryInfo;
import com.keao.edu.common.tenant.TenantContextHolder;
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.security.access.prepost.PreAuthorize;
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;

	@Autowired
	private SysUserRoleService sysUserRoleService;

	@GetMapping(value = "/queryRoleCodeListByUserId")
	public Object queryRoleCodeListByUserId(Integer userId) {
		return sysUserRoleService.queryRoleCodeListByUserId(userId);
	}

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

	@ApiOperation("删除角色")
	@PostMapping(value = "/del")
    @PreAuthorize("@pcs.hasPermissions('role/del')")
	public Object delRole(Integer id) {
		sysRoleService.delRole(id);
		return succeed();
	}

	@ApiOperation("修改角色")
	@PostMapping(value = "/update")
    @PreAuthorize("@pcs.hasPermissions('role/update')")
	public Object updateRole(@RequestBody SysRole sysRole) {
		sysRole.setUpdateTime(new Date());
		sysRole.setTenantId(TenantContextHolder.getTenantId());
		sysRoleService.updateRole(sysRole);
		return succeed();
	}

	@ApiOperation("新增角色")
	@PostMapping(value = "/add")
    @PreAuthorize("@pcs.hasPermissions('role/add')")
	public Object addRole(@RequestBody SysRole sysRole) {
		sysRole.setTenantId(TenantContextHolder.getTenantId());
		sysRoleService.addRole(sysRole);
		return succeed();
	}

	@ApiOperation("分页查询角色列表")
	@GetMapping(value = "/queryPage")
    @PreAuthorize("@pcs.hasPermissions('role/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)
    @PreAuthorize("@pcs.hasPermissions('role/addRoleMenu')")
	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)
    @PreAuthorize("@pcs.hasPermissions('role/delRoleMenu')")
	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")
    @PreAuthorize("@pcs.hasPermissions('role/getMenus')")
	public Object getMenus(Integer roleId) {
		if (roleId == null) {
			return failed(SecurityConstants.PARAM_VERIFY_EXCEPTION);
		}
		return succeed(sysMenuService.getMenusByRoleId(roleId));
	}
}
