/*
 *  Copyright 2019-2020 Zheng Jie
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *  http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
package org.nl.system.controller.dept;

import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.dev33.satoken.annotation.SaMode;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.nl.common.base.TableDataInfo;
import org.nl.common.domain.query.PageQuery;
import org.nl.common.exception.BadRequestException;
import org.nl.common.utils.PageUtil;
import org.nl.common.logging.annotation.Log;
import org.nl.system.service.dept.ISysDeptService;
import org.nl.system.service.dept.dao.SysDept;
import org.nl.system.service.dept.dto.DeptQuery;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.util.CollectionUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Set;

/**
* @author Zheng Jie
* @date 2019-03-25
*/
@RestController
@Api(tags = "系统：部门管理")
@RequestMapping("/api/dept")
public class DeptController {

    @Autowired
    private ISysDeptService deptService;

    @ApiOperation("查询部门")
    @GetMapping
    public ResponseEntity<Object> query(DeptQuery query) throws Exception {
        List<SysDept> list = deptService.list(query.build());
        return new ResponseEntity<>(PageUtil.toPage(list, list.size()),HttpStatus.OK);
    }

    @ApiOperation("查询部门")
    @GetMapping("/vo")
    public ResponseEntity queryvo(DeptQuery query, PageQuery pageQuery) throws Exception {
        Page deptPage = deptService.queryVo(query, pageQuery);
        return new ResponseEntity((TableDataInfo.build(deptPage)),HttpStatus.OK);
    }


    @ApiOperation("查询所有部门树")
    @GetMapping("/allTree")
    public ResponseEntity<Object> allTree(DeptQuery query) {
        return new ResponseEntity<>(deptService.buildTree(query),HttpStatus.OK);
    }

    @ApiOperation("查询部门:根据ID获取同级与上级数据")
    @PostMapping("/superior")
    @SaCheckPermission(value = {"user:list", "dept:list"}, mode = SaMode.AND)
    public ResponseEntity<Object> getSuperior(@RequestBody List<Long> ids) {
        if (CollectionUtils.isEmpty(ids)){
            return  ResponseEntity.noContent().build();
        }
        return new ResponseEntity<>(deptService.getSuperior(ids),HttpStatus.OK);
    }

    @Log("新增部门")
    @ApiOperation("新增部门")
    @PostMapping
//    @SaCheckPermission("dept:add")
    public ResponseEntity<Object> create(@Validated @RequestBody SysDept resources){
        deptService.createDept(resources);
        return new ResponseEntity<>(HttpStatus.CREATED);
    }

    @Log("修改部门")
    @ApiOperation("修改部门")
    @PutMapping
//    @SaCheckPermission("dept:edit")
    public ResponseEntity<Object> update(@Validated @RequestBody SysDept dept){
        if (dept.getPid() != null && dept.getDept_id().equals(dept.getPid())) {
            throw new BadRequestException("上级不能为自己");
        }
        deptService.updateDept(dept);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    @Log("删除部门")
    @ApiOperation("删除部门")
    @DeleteMapping
//    @SaCheckPermission("dept:del")
    public ResponseEntity<Object> delete(@RequestBody Set<String> deptIds){
        if (CollectionUtils.isEmpty(deptIds)){
           return ResponseEntity.noContent().build();
        }
        deptService.delateDept(deptIds);
        return new ResponseEntity<>(HttpStatus.OK);
    }
}
