package com.zsc.edu.gateway.modules.system.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zsc.edu.gateway.modules.system.dto.AuthorityDto;
import com.zsc.edu.gateway.modules.system.entity.Authority;
import com.zsc.edu.gateway.modules.system.query.AuthorityQuery;
import com.zsc.edu.gateway.modules.system.service.AuthorityService;
import lombok.AllArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

/**
 * 权限Controller
 *
 * @author zhuang
 */
@AllArgsConstructor
@RestController
@RequestMapping("/api/rest/authority")
public class AuthorityController {
    private AuthorityService service;

    /**
     * 返回权限列表 hasAuthority('SYSTEM:AUTHORITY:QUERY')
     *
     * @param query 查询表单
     * @return 权限列表
     */
    @GetMapping
    @PreAuthorize("hasAuthority('SYSTEM:AUTHORITY:QUERY')")
    public Page<Authority> query(AuthorityQuery query, Page<Authority> page) {
        return service.page(page, query.wrapper());
    }


    /**
     * 新建权限 hasAuthority('SYSTEM:AUTHORITY:CREATE')
     *
     * @param dto 表单数据
     * @return Authority 新建的权限
     */
    @PostMapping
    @PreAuthorize("hasAuthority('SYSTEM:AUTHORITY:CREATE')")
    public Authority create(@RequestBody AuthorityDto dto) {
        return service.create(dto);
    }

    /**
     * 更新权限 hasAuthority('SYSTEM:AUTHORITY:UPDATE')
     *
     * @param dto 表单数据
     * @param id  权限ID
     * @return Dept 更新后的权限信息
     */
    @PatchMapping("/{id}")
    @PreAuthorize("hasAuthority('SYSTEM:AUTHORITY:UPDATE')")
    public Boolean update(@RequestBody AuthorityDto dto, @PathVariable("id") Long id) {
        return service.update(dto, id);
    }
    /***
     *  删除权限 hasAuthority('SYSTEM:AUTHORITY:DELETE')
     *  @param id 权限ID
     *  @return Boolean 是否删除成功
     */
    @DeleteMapping("/{id}")
    @PreAuthorize("hasAuthority('SYSTEM:AUTHORITY:DELETE')")
    public Boolean delete(@PathVariable("id") Long id) {
        return service.removeById(id);
    }
    /**
     * 更新权限启用状态
     * */
    @PatchMapping("/toggle/{id}")
    @PreAuthorize("hasAuthority('SYSTEM:AUTHORITY:TOGGLE')")
    public Boolean toggle(@PathVariable("id") Long id) {
        return service.toggle(id);
    }


}
