package com.lutao.cloud.admin.controller;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lutao.cloud.admin.api.entity.SysRole;
import com.lutao.cloud.admin.api.vo.RoleVO;
import com.lutao.cloud.admin.service.SysRoleService;
import com.lutao.cloud.common.core.constant.CacheConstants;
import com.lutao.cloud.common.core.util.MessageInfo;
import com.lutao.cloud.common.log.annotation.SysLog;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**

 * @date 2020-02-10
 */
@Api(value = "/role", tags ={"角色管理"})
@RestController
@AllArgsConstructor
@RequestMapping("/role")
public class SysRoleController{

	private final SysRoleService sysRoleService;

	/**
	 * 通过ID查询角色信息
	 * @param id ID
	 * @return 角色信息
	 */
	@ApiOperation(value = "通过ID查询角色信息", notes = "通过ID查询角色信息", httpMethod = "GET")
	@GetMapping("/{id}")
	public MessageInfo<SysRole> getById(@PathVariable Integer id){
		return MessageInfo.ok(sysRoleService.getById(id));
	}

	/**
	 * 添加角色
	 * @param sysRole 角色信息
	 * @return success、false
	 */
	@ApiOperation(value = "添加角色", notes = "添加角色", httpMethod = "POST")
	@SysLog("添加角色")
	@PostMapping
	@PreAuthorize("@pms.hasPermission('sys_role_add')")
	@CacheEvict(value = CacheConstants.ROLE_DETAILS, allEntries = true)
	public MessageInfo<Boolean> save(@Valid @RequestBody SysRole sysRole){
		return MessageInfo.ok(sysRoleService.save(sysRole));
	}

	/**
	 * 修改角色
	 * @param sysRole 角色信息
	 * @return success/false
	 */
	@ApiOperation(value = "修改角色", notes = "修改角色", httpMethod = "PUT")
	@SysLog("修改角色")
	@PutMapping
	@PreAuthorize("@pms.hasPermission('sys_role_edit')")
	@CacheEvict(value = CacheConstants.ROLE_DETAILS, allEntries = true)
	public MessageInfo<Boolean> update(@Valid @RequestBody SysRole sysRole){
		return MessageInfo.ok(sysRoleService.updateById(sysRole));
	}

	/**
	 * 删除角色
	 * @param id
	 * @return
	 */
	@ApiOperation(value = "删除角色", notes = "删除角色", httpMethod = "DELETE")
	@SysLog("删除角色")
	@DeleteMapping("/{id}")
	@PreAuthorize("@pms.hasPermission('sys_role_del')")
	@CacheEvict(value = CacheConstants.ROLE_DETAILS, allEntries = true)
	public MessageInfo<Boolean> removeById(@PathVariable Integer id){
		return MessageInfo.ok(sysRoleService.removeRoleById(id));
	}

	/**
	 * 获取角色列表
	 * @return 角色列表
	 */
	@ApiOperation(value = "获取角色列表", notes = "获取角色列表", httpMethod = "GET")
	@GetMapping("/list")
	public MessageInfo<List<SysRole>> listRoles(){
		return MessageInfo.ok(sysRoleService.list(Wrappers.emptyWrapper()));
	}

//	/**
//	 * 分页查询角色信息
//	 * @param page 分页对象
//	 * @param role 查询条件
//	 * @return 分页对象
//	 */
//	@ApiOperation(value = "分页查询角色信息", notes = "分页查询角色信息", httpMethod = "GET")
//	@GetMapping("/page")
//	public MessageInfo<IPage<SysRole>> getRolePage(Page page, SysRole role){
//		return MessageInfo.ok(sysRoleService.page(page, Wrappers.query(role)));
//	}

	/**
	 * 分页查询角色信息
	 * @param page 分页对象
	 * @param searchKey 查询条件
	 * @return 分页对象
	 */
	@ApiOperation(value = "分页查询角色信息", notes = "分页查询角色信息", httpMethod = "GET")
	@GetMapping("/page")
	public MessageInfo<IPage<SysRole>> getRolePage(Page page,
												   @RequestParam(value = "searchKey",required = false) String searchKey){
		return sysRoleService.getRolePage(page, searchKey);
	}

	/**
	 * 更新角色菜单
	 * @param roleVo 角色对象
	 * @return success、false
	 */
	@ApiOperation(value = "更新角色菜单", notes = "更新角色菜单", httpMethod = "PUT")
	@SysLog("更新角色菜单")
	@PutMapping("/menu")
	@PreAuthorize("@pms.hasPermission('sys_role_perm')")
	public MessageInfo<Boolean> saveRoleMenus(@RequestBody RoleVO roleVo){
		return MessageInfo.ok(sysRoleService.updateRoleMenus(roleVo));
	}

	/**
	 * 通过角色ID 查询角色列表
	 * @param roleIdList 角色ID
	 * @return
	 */
	@ApiOperation(value = "通过角色ID 查询角色列表", notes = "通过角色ID 查询角色列表", httpMethod = "POST")
	@PostMapping("/getRoleList")
	public MessageInfo<List<SysRole>> getRoleList(@RequestBody List<Integer> roleIdList){
		return MessageInfo.ok(sysRoleService.findRolesByRoleIds(roleIdList, CollUtil.join(roleIdList, StrUtil.UNDERLINE)));
	}

}
