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.ResponseMenuDto;
import com.app.dto.platform.ResponseNodeDto;
import com.app.dto.platform.SearchMenuDto;
import com.app.service.platform.ResourceService;
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("PlatformResourceController")
@RequestMapping("/platform/resource")
public class ResourceController {

    @Resource
    private ResourceService menuService;

    /**
     * 查询【顶级菜单】列表
     *
     */
    @ApiOperation(value = "查询[顶级菜单]列表")
    @PostMapping("/page")
    public ResponseEntity<Result<PageResult<ResponseMenuDto>>> page(@Valid @RequestBody SearchMenuDto params) {
        return new ResponseEntity(new Result<>().ok(menuService.page(params)), HttpStatus.OK);
    }

    /**
     * 查询【非顶级菜单】列表
     *
     */
    @ApiOperation(value = "查询[非顶级菜单]列表")
    @PostMapping("/list")
    public ResponseEntity<Result<List<ResponseMenuDto>>> list(@Valid @RequestBody SearchMenuDto params) {
        return new ResponseEntity(new Result<>().ok(menuService.list(params)), HttpStatus.OK);
    }

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

    /**
     * 保存/更新[菜单]
     *
     */
    @OperationLog("更新 - 菜单")
    @ApiOperation(value = "保存/更新[菜单]")
    @PostMapping("/save-or-update")
    public ResponseEntity<Result> saveOrUpdate(@Valid @RequestBody com.app.entity.platform.system.Resource dto) {
        menuService.saveOrUpdate(dto);
        return new ResponseEntity(new Result().ok(), HttpStatus.OK);
    }

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

}
