package com.tuoheng.system.controller;


import com.tuoheng.common.annotation.Log;
import com.tuoheng.common.enums.LogType;
import com.tuoheng.common.utils.JsonResult;
import com.tuoheng.system.dto.RoleMenuDto;
import com.tuoheng.system.entity.Role;
import com.tuoheng.system.entity.User;
import com.tuoheng.system.query.RoleQuery;
import com.tuoheng.system.service.IRoleService;
import com.tuoheng.system.service.ITenantService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.tuoheng.common.common.BaseController;

/**
 * <p>
 * 系统角色表 前端控制器
 * </p>
 *
 * @author 拓恒
 * @since 2020-10-31
 */
@RestController
@RequestMapping("/role")
public class RoleController extends BaseController {

    @Autowired
    private IRoleService roleService;
    @Autowired
    private ITenantService iTenantService;
    /**
     * 获取角色列表
     *
     * @param roleQuery 查询条件
     * @return
     */
    //@RequiresPermissions("sys:role:index")
    @GetMapping("/index")
    public JsonResult index(RoleQuery roleQuery) {
        return roleService.getList(roleQuery);
    }

    /**
     * 添加角色
     *
     * @param entity 实体对象
     * @return
     */
    @Log(title = "角色管理", logType = LogType.INSERT)
    //@RequiresPermissions("sys:role:add")
    @PostMapping("/add")
    public JsonResult add(@RequestBody Role entity) {
        entity.setCreateUser(iTenantService.getCurrentUserId());
        entity.setTenantId(iTenantService.getCurrentTenantId());
        return roleService.edit(entity);
    }

    /**
     * 编辑角色
     *
     * @param entity 实体对象
     * @return
     */
    @Log(title = "角色管理", logType = LogType.UPDATE)
    //@RequiresPermissions("sys:role:edit")
    @PutMapping("/edit")
    public JsonResult edit(@RequestBody Role entity) {
        entity.setUpdateUser(iTenantService.getCurrentUserId());
        entity.setTenantId(iTenantService.getCurrentTenantId());
        return roleService.edit(entity);
    }

    /**
     * 删除角色
     *
     * @param roleIds 角色ID
     * @return
     */
    @Log(title = "角色管理", logType = LogType.DELETE)
    //@RequiresPermissions("sys:role:delete")
    @DeleteMapping("/delete/{roleIds}")
    public JsonResult delete(@PathVariable("roleIds") Integer[] roleIds) {
        return roleService.deleteByIds(roleIds);
    }

    /**
     * 获取角色列表
     *
     * @return
     */
    @GetMapping("/getRoleList")
    public JsonResult getRoleList() {
        return roleService.getRoleList();
    }

    /**
     * 获取角色菜单列表
     *
     * @param roleId 角色ID
     * @return
     */
    @GetMapping("/getMenuList/{roleId}")
    public JsonResult getMenuList(@PathVariable("roleId") Integer roleId) {
        return roleService.getMenuList(roleId);
    }

    /**
     * 保存角色权限数据
     *
     * @param roleMenuDto 角色菜单Dto
     * @return
     */
    //@RequiresPermissions("sys:role:permission")
    @PostMapping("/savePermission")
    public JsonResult savePermission(@RequestBody RoleMenuDto roleMenuDto) {
        return roleService.savePermission(roleMenuDto);
    }

}
