package com.zsc.bill.modules.system.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zsc.bill.modules.system.dto.AuthorityDto;
import com.zsc.bill.modules.system.entity.Authority;
import com.zsc.bill.modules.system.query.AuthorityQuery;
import com.zsc.bill.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/auth")
public class AuthorityController {

    private AuthorityService service;

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


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

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


}
