package com.app.controller.platform;

import com.app.annotation.platform.OperationLog;
import com.app.dto.PageResult;
import com.app.dto.Result;
import com.app.dto.SearchDto;
import com.app.dto.platform.*;
import com.app.service.platform.RoleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;

/**
 * 功能描述：
 *
 * @Auther: 梁展鹏
 * @Date: 2018/7/24 11:38
 */
@Api(tags = "角色管理")
@Slf4j
@RestController("PlatformRoleController")
@RequestMapping("/platform/role")
public class RoleController {

    @Resource
    private RoleService roleService;

    /**
     * 查询【角色】列表
     */
    @ApiOperation(value = "查询[角色]列表")
    @PostMapping("/page")
    public ResponseEntity<Result<PageResult<ResponseRoleDto>>> page(@Valid @RequestBody SearchRoleDto params) {
        return new ResponseEntity(new Result<>().ok(roleService.page(params)), HttpStatus.OK);
    }

    /**
     * 查询[菜单]节点
     */
    @ApiOperation(value = "查询[菜单]节点")
    @PostMapping("/menu-nodes")
    public ResponseEntity<List<ResponseNodeDto>> getMenuNodes() {
        return new ResponseEntity(new Result<>().ok(roleService.getMenuNodes()), HttpStatus.OK);
    }

    /**
     * 保存/更新[角色]
     */
    @OperationLog("更新 - 角色")
    @ApiOperation(value = "保存/更新[角色]")
    @PostMapping("/save-or-update")
    public ResponseEntity<Result> saveOrUpdate(@Valid @RequestBody RoleDto dto) {
        roleService.saveOrUpdate(dto);
        return new ResponseEntity(new Result().ok(), HttpStatus.OK);
    }

    /**
     * 保存/更新[菜单]权限
     */
    @OperationLog("更新 - 菜单权限")
    @ApiOperation(value = "保存/更新[菜单]权限")
    @PostMapping("/save-menu")
    public ResponseEntity<Result> saveMenu(@Valid @RequestBody RoleMenuDto dto) {
        roleService.saveMenu(dto);
        return new ResponseEntity(new Result().ok(), HttpStatus.OK);
    }

    @OperationLog("替换删除状态 - 角色")
    @ApiOperation(value = "替换[角色]删除状态")
    @PostMapping("/handle-is-del")
    public ResponseEntity<Result> handleIsDel(@Valid @RequestBody SearchDto searchdto) {
        roleService.handleIsDel(searchdto.getId());
        return new ResponseEntity(new Result().ok(), HttpStatus.OK);
    }

    /**
     * 查询【角色】节点树
     *
     */
    @ApiOperation(value = "查询【角色】节点树")
    @PostMapping("/nodes")
    public ResponseEntity<Result<List<ResponseNodeDto>>> nodes() {
        return new ResponseEntity(new Result<>().ok(roleService.getAllNodes()), HttpStatus.OK);
    }
}
