package org.nl.system.controller.role;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.alibaba.fastjson.JSONObject;
import io.swagger.annotations.ApiOperation;
import org.nl.common.base.TableDataInfo;
import org.nl.common.domain.query.PageQuery;
import org.nl.common.logging.annotation.Log;
import org.nl.system.service.role.ISysRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.Map;
import java.util.Set;

/**
 * <p>
 * 角色表 前端控制器
 * </p>
 *
 * @author generator
 * @since 2022-12-15
 */
@RestController
@RequestMapping("/api/sysRole")
public class SysRoleController {

    @Autowired
    private ISysRoleService roleService;

    @ApiOperation("分页查询角色")
    @GetMapping
    @SaCheckPermission("roles:list")
    public ResponseEntity<Object> pageQuery(@RequestParam Map param, PageQuery page) {
        return new ResponseEntity<>(TableDataInfo.build(roleService.query(param, page)), HttpStatus.OK);
    }

    @ApiOperation("查询所有角色")
    @GetMapping("/all")
    public ResponseEntity<Object> queryAll() {
        return new ResponseEntity<>(roleService.list(), HttpStatus.OK);
    }

    @Log("新增角色")
    @ApiOperation("新增角色")
    @PostMapping
    @SaCheckPermission("roles:add")
    public ResponseEntity<Object> create(@RequestBody JSONObject param) {
        roleService.create(param);
        return new ResponseEntity<>(HttpStatus.CREATED);
    }

    @Log("修改角色")
    @ApiOperation("修改角色")
    @PutMapping
    @SaCheckPermission("roles:edit")
    public ResponseEntity<Object> update(@RequestBody JSONObject param) {
        roleService.update(param);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    @Log("删除角色")
    @ApiOperation("删除角色")
    @DeleteMapping
    @SaCheckPermission("roles:del")
    public ResponseEntity<Object> delete(@RequestBody Set<String> ids) {
        roleService.deleteBatchByIds(ids);
        return new ResponseEntity<>(HttpStatus.OK);
    }

    @Log("修改角色菜单")
    @ApiOperation("修改角色菜单")
    @PutMapping(value = "/menu")
    @SaCheckPermission("roles:edit")
    public ResponseEntity<Object> updateMenu(@RequestBody JSONObject form) {
        roleService.updateMenu(form);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }
}

