package com.tuoheng.admin.controller;

import com.tuoheng.admin.dto.RoleMenuDto;
import com.tuoheng.admin.entity.Role;
import com.tuoheng.admin.query.RoleClientQuery;
import com.tuoheng.admin.query.RoleQuery;
import com.tuoheng.admin.service.role.IRoleService;
import com.tuoheng.common.core.annotation.Log;
import com.tuoheng.common.core.enums.LogType;
import com.tuoheng.common.core.utils.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @Author ChengWang
 * @Date 2022/12/16
 */
@RestController
@RequestMapping("role")
public class RoleController {

    @Autowired
    private IRoleService roleService;

    /**
     * 获取角色列表
     * @param roleQuery
     * @return
     */
    @GetMapping("/index")
    public JsonResult getRolePageList(RoleQuery roleQuery){
        return roleService.getRolePageList(roleQuery);
    }

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

    /**
     * 添加角色
     * @param entity
     * @return
     */
    @PostMapping("/add")
    public JsonResult add(@RequestBody Role entity){
        return roleService.editRole(entity);
    }

    /**
     * 编辑角色
     * @param entity
     * @return
     */
    @PutMapping("/edit")
    public JsonResult edit(@RequestBody Role entity){
        return roleService.editRole(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.deleteByList(roleIds);
    }

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

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

    /**
     * 更新角色状态
     * @param entity
     * @return
     */
    @PutMapping("/status")
    public JsonResult updateStatus(@RequestBody Role entity){
        return roleService.updateStatus(entity);
    }

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

    /**
     * 初始化租户固定角色
     * @param ids
     * @return
     */
    @GetMapping("/test/{ids}")
    public JsonResult test(@PathVariable("ids") Integer[] ids){
        return roleService.roleToTenant(ids);
    }



}
