package cn.ey.biz.module.system.controller.admin.abac;

import cn.ey.biz.common.pojo.PageParam;
import cn.ey.biz.common.pojo.PageResult;
import cn.ey.biz.common.pojo.R;
import cn.ey.biz.framework.datapermission.core.annotation.DataPermission;
import cn.ey.biz.module.system.controller.admin.abac.vo.AbacPageVo;
import cn.ey.biz.module.system.controller.admin.abac.vo.AbacVo;
import cn.ey.biz.module.system.service.abac.AbacService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;

import static cn.ey.biz.common.pojo.R.success;

@Api(tags = "管理后台 - ABAC 基于属性权限控制")
@RestController
@RequestMapping("/system/abac")
@Validated
public class AbacController {

    @Resource
    private AbacService abacService;

    @PostMapping("/create")
    @ApiOperation("创建属性权限")
    @PreAuthorize("@ss.hasPermission('system:abac:create')")
    public R<Long> createAbac(@Valid @RequestBody AbacVo abacVO) {
        abacService.createABAC(abacVO);
        return success(null);
    }

    @PostMapping("/page")
    @ApiOperation("分页查询属性权限")
    @DataPermission
    @PreAuthorize("@ss.hasPermission('system:abac:query')")
    public R<PageResult<AbacVo>> getAbacByPage(@Valid @RequestBody AbacPageVo abacPageVo) {
        return success(abacService.getAbacByPage(abacPageVo));
    }

    @DeleteMapping()
    @ApiOperation("删除属性权限")
    @PreAuthorize("@ss.hasPermission('system:abac:delete')")
    public R<Long> deleteAbac(@Valid @RequestBody AbacVo abacVO) {
        abacService.deleteAbac(abacVO);
        return success(null);
    }
}
