

package com.nantian.sc.cloud.admin.controller;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.nantian.sc.cloud.admin.api.dto.MenuTree;
import com.nantian.sc.cloud.admin.api.entity.SysRole;
import com.nantian.sc.cloud.admin.api.entity.SysRoleMenu;
import com.nantian.sc.cloud.admin.api.entity.SysUserRole;
import com.nantian.sc.cloud.admin.api.vo.MenuVO;
import com.nantian.sc.cloud.admin.service.SysRoleMenuService;
import com.nantian.sc.cloud.admin.service.SysRoleService;
import com.nantian.sc.cloud.admin.service.SysUserRoleService;
import com.nantian.sc.cloud.common.core.util.R;
import com.nantian.sc.cloud.common.log.annotation.SysLog;
import io.swagger.annotations.Api;
import lombok.AllArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

/**
 *
 * @date 2017/11/5
 */
@RestController
@AllArgsConstructor
@RequestMapping("/role")
@Api(value = "role", tags = "角色管理模块")
public class SysRoleController {
	private final SysRoleService sysRoleService;
	private final SysRoleMenuService sysRoleMenuService;
	private final SysUserRoleService sysUserRoleService;

	/**
	 * 通过ID查询角色信息
	 *
	 * @param id ID
	 * @return 角色信息
	 */
	@GetMapping("/{id}")
	public R getById(@PathVariable Integer id) {
		//return R.ok(sysRoleService.getById(id));
		SysRole sysRole = sysRoleService.getById(id);
		List<SysRoleMenu> menus = sysRoleMenuService.list(Wrappers.<SysRoleMenu>query()
				.lambda().eq(SysRoleMenu::getRoleId, id));
		/*List<String> menuIds = menus.stream().map(sysRoleMenu -> {
			return sysRoleMenu.getMenuId().toString();
		}).collect(Collectors.toList());*/
		List<Integer> menuIds = menus.stream()
				.sorted(Comparator.comparingInt(SysRoleMenu::getMenuId))
				.map(SysRoleMenu::getMenuId)
				.collect(Collectors.toList());
		sysRole.setMenuIdArr(menuIds);
		return R.ok(sysRole);
	}

	/**
	 * 添加角色
	 *
	 * @param sysRole 角色信息
	 * @return success、false
	 */
	@SysLog("添加角色")
	@PostMapping
	@PreAuthorize("@pms.hasPermission('sys_role_add')")
	public R save(@Valid @RequestBody SysRole sysRole) {
		//return R.ok(sysRoleService.save(sysRole));
		sysRoleService.save(sysRole);
		return R.ok(sysRoleMenuService.saveRoleMenus(sysRole.getRoleCode(), sysRole.getRoleId(), sysRole.getMenuIds()));
	}

	/**
	 * 修改角色
	 *
	 * @param sysRole 角色信息
	 * @return success/false
	 */
	@SysLog("修改角色")
	@PutMapping
	@PreAuthorize("@pms.hasPermission('sys_role_edit')")
	public R update(@Valid @RequestBody SysRole sysRole) {
		//return R.ok(sysRoleService.updateById(sysRole));
		sysRoleService.updateById(sysRole);
		return R.ok(sysRoleMenuService.saveRoleMenus(sysRole.getRoleCode(), sysRole.getRoleId(), sysRole.getMenuIds()));
	}

	/**
	 * 删除角色
	 *
	 * @param id
	 * @return
	 */
	@SysLog("删除角色")
	@DeleteMapping("/{id}")
	@PreAuthorize("@pms.hasPermission('sys_role_del')")
	public R removeById(@PathVariable Integer id) {
		List<SysUserRole> sysUserRoleList = sysUserRoleService.list(Wrappers.<SysUserRole>query()
				.lambda().eq(SysUserRole::getRoleId, id));
		if(sysUserRoleList.size()>0){
			return R.failed("该角色已有用户使用，不能删除！");
		}
		return R.ok(sysRoleService.removeRoleById(id));
	}

	/**
	 * 获取角色列表
	 *
	 * @return 角色列表
	 */
	@GetMapping("/list")
	public R listRoles() {
		return R.ok(sysRoleService.list(Wrappers.emptyWrapper()));
	}

	/**
	 * 分页查询角色信息
	 *
	 * @param page 分页对象
	 * @return 分页对象
	 */
	@GetMapping("/page")
	public R getRolePage(Page page) {
		return R.ok(sysRoleService.page(page, Wrappers.emptyWrapper()));
	}

	/**
	 * 更新角色菜单
	 *
	 * @param roleId  角色ID
	 * @param menuIds 菜单ID拼成的字符串，每个id之间根据逗号分隔
	 * @return success、false
	 */
	@SysLog("更新角色菜单")
	@PutMapping("/menu")
	@PreAuthorize("@pms.hasPermission('sys_role_perm')")
	public R saveRoleMenus(Integer roleId, @RequestParam(value = "menuIds", required = false) String menuIds) {
		SysRole sysRole = sysRoleService.getById(roleId);
		return R.ok(sysRoleMenuService.saveRoleMenus(sysRole.getRoleCode(), roleId, menuIds));
	}

	/**
	 * 通过角色ID 查询角色列表
	 *
	 * @param roleIdList 角色ID
	 * @return
	 */
	@PostMapping("/getRoleList")
	public R getRoleList(@RequestBody List<String> roleIdList) {
		return R.ok(sysRoleService.listByIds(roleIdList));
	}
}
