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.ResponseNodeDto;
import com.app.dto.platform.SearchDeptDto;
import com.app.entity.platform.system.Dept;
import com.app.service.platform.DeptService;
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("PlatformDeptController")
@RequestMapping("/platform/dept")
public class DeptController {

    @Resource
    private DeptService deptService;

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

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

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

    /**
     * 保存/更新[部门]
     *
     */
    @OperationLog("更新 - 部门")
    @ApiOperation(value = "保存/更新[部门]")
    @PostMapping("/save-or-update")
    public ResponseEntity<Result> saveOrUpdate(@Valid @RequestBody Dept dto) {
        deptService.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) {
        deptService.handleIsDel(searchdto.getId());
        return new ResponseEntity(new Result().ok(), HttpStatus.OK);
    }
}
