

package com.zcloud.modules.sys.controller;

import com.zcloud.common.annotation.SysLog;
import com.zcloud.common.utils.PageUtils;
import com.zcloud.common.utils.R;
import com.zcloud.modules.sys.entity.SysRoleEntity;
import com.zcloud.modules.sys.service.SysRoleMenuService;
import com.zcloud.modules.sys.service.SysRoleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * 角色管理
 *
 */
@Api(tags = "角色")
@RestController
@RequestMapping("/sys/role")
public class SysRoleController extends AbstractController {
	@Autowired
	private SysRoleService sysRoleService;
	@Autowired
	private SysRoleMenuService sysRoleMenuService;

	/**
	 * 角色列表
	 */
	@ApiOperation("角色-分页查询")
	@PostMapping("/listPage")
	//@RequiresPermission("sys:role:list")
	public R listPage(@RequestBody Map<String, Object> params){
		PageUtils page = sysRoleService.queryPage(params);

		return R.ok().put("page", page);
	}

	@ApiOperation("角色-查询全部")
	@SysLog("角色-查询全部")
	@PostMapping("/listAll")
	public R listAll(@RequestBody Map<String, Object> params) {
		List<SysRoleEntity> list = sysRoleService.list();

		return R.ok().put("roleList", list);
	}

	/**
	 * 角色信息
	 */
	@ApiOperation("角色-根据ID查详情")
	@PostMapping("/info")
	//@RequiresPermission("sys:role:info")
	public R info(@RequestBody SysRoleEntity role){
		role = sysRoleService.getById(role.getRoleId());

		//查询角色对应的菜单
		List<Integer> menuIdList = sysRoleMenuService.queryMenuIdList(role.getRoleId());
		role.setMenuIdList(menuIdList);

		return R.ok().put("role", role);
	}

	/**
	 * 保存角色
	 */
	@ApiOperation("角色-新增数据")
	@SysLog("保存角色")
	@PostMapping("/save")
	//@RequiresPermission("sys:role:save")
	public R save(@RequestBody SysRoleEntity role){
		role.setCreator(getUserId());
		sysRoleService.saveRole(role);

		return R.ok();
	}

	/**
	 * 修改角色
	 */
	@ApiOperation("角色-编辑数据")
	@SysLog("修改角色")
	@PostMapping("/update")
	//@RequiresPermission("sys:role:update")
	public R update(@RequestBody SysRoleEntity role){

		role.setCreator(getUserId());
		sysRoleService.update(role);

		return R.ok();
	}

	/**
	 * 删除角色
	 */
	@ApiOperation("角色-删除数据")
	@SysLog("删除角色")
	@PostMapping("/delete")
	//@RequiresPermission("sys:role:delete")
	public R delete(@RequestBody SysRoleEntity role){
		sysRoleService.deleteBatch(role.getRoleIds());

		return R.ok();
	}
}
