package com.ruoyi.oa.controller;

import com.ruoyi.common.core.domain.R;
import com.ruoyi.oa.domain.vo.SysUserVo;
import com.ruoyi.oa.service.SysOssAclService;
import lombok.Data;
import org.flowable.spring.security.UserDto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotNull;
import java.util.List;

@RestController
@RequestMapping("/oa/oss/acl")
public class SysOssAclController {

    @Autowired
    private SysOssAclService aclService;

    /** 授权 */
    @PostMapping("/grant")
    public R<?> grant(@RequestBody GrantDto dto) {
        aclService.grant(dto.getOssId(), dto.getUserId(), dto.getGrantBy());
        return R.ok();
    }

    /** 取消共享 */
    @DeleteMapping("/revoke")
    public R<?> revoke(@RequestBody RevokeDto dto) {
        aclService.revoke(dto.getOssId(), dto.getUserId());
        return R.ok();
    }

    /** 判断权限（示例） */
    @GetMapping("/check")
    public R<Boolean> check(@RequestParam Long ossId,
                            @RequestParam Long userId) {
        return R.ok(aclService.hasPerm(userId, ossId));
    }

    @GetMapping("/list")
    public R<List<SysUserVo>> list(@RequestParam Long ossId) {
        return R.ok(aclService.listUsersByOssId(ossId));
    }


    // ====================== DTO ======================
    @Data
    public static class GrantDto {
        @NotNull private Long ossId;
        @NotNull private Long userId;
        @NotNull private Long grantBy;
    }

    @Data
    public static class RevokeDto {
        @NotNull private Long ossId;
        @NotNull private Long userId;
    }
}
