package com.tuoheng.admin.controller;

import com.tuoheng.admin.dto.GetPermissionRoleIdListByApiUrlDto;
import com.tuoheng.admin.entity.OpPermissions;
import com.tuoheng.admin.enums.MarkEnum;
import com.tuoheng.admin.query.RoleClientQuery;
import com.tuoheng.admin.service.permissions.OpPermissionsService;
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("/permission")
public class PermissionController{

    @Autowired
    private OpPermissionsService permissionService;



    /**
     * 根据 RoleId 获取用户权限
     *
     * @return
     */
    @GetMapping("/getRolePermission")
    public JsonResult getMenuAndPermissionByRoleId(RoleClientQuery query) {
        return permissionService.getRolePermissionByRoleId(query);
    }

    /**
     * 根据ApiUrl获取角色集合
     *
     * @return
     */
    @PostMapping("/getRoleIdList")
    public JsonResult getRoleIdListByApiUrl(@RequestBody GetPermissionRoleIdListByApiUrlDto getPermissionRoleIdListByApiUrlDto) {
        return permissionService.getRoleIdListByApiUrl(getPermissionRoleIdListByApiUrlDto.getApiUrl());
    }

    /**
     * 添加权限(按钮)
     *
     * @param opPermissions
     * @return
     */
    @PostMapping("/add")
    public JsonResult add(@RequestBody OpPermissions opPermissions) {
        return permissionService.edit(opPermissions);
    }

    /**
     * 编辑权限(按钮)
     *
     * @param opPermissions
     * @return
     */
    @PutMapping("/edit")
    public JsonResult edit(@RequestBody OpPermissions opPermissions) {
        return permissionService.edit(opPermissions);
    }

    /**
     * 删除权限按钮(更新)
     * @param id
     * @return
     */
    @DeleteMapping("/delete/{id}")
    public JsonResult delete(@PathVariable("id") Integer id) {
        boolean flag = permissionService.update().eq("id",id).set("mark", MarkEnum.NOTVALID.getCode()).update();
        if (flag) {
            return JsonResult.success();
        } else {
            return JsonResult.error();
        }
    }
}
