package com.zjty.tynotes.pas.controller;

import com.zjty.tynotes.misc.config.AutoDocument;
import com.zjty.tynotes.pas.base.advise.Content;
import com.zjty.tynotes.pas.entity.Authority;
import com.zjty.tynotes.pas.entity.Department;
import com.zjty.tynotes.pas.service.IDepartmentService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.ArrayList;
import java.util.List;

import static org.springframework.http.ResponseEntity.ok;

/**
 * @author mcj
 */
@Api(tags = "用户管理权限管理模块",protocols = "http")
@RestController
@RequestMapping("/pas/department")
@AutoDocument
public class DepartController {

    @Autowired
    private IDepartmentService departmentService;


    @ApiOperation(value = "获得所有部门",response = Department.class)
    @GetMapping
    public ResponseEntity departments(){
        return ok(departmentService.findAll());
    }

    @Content(value = "添加部门")
    @ApiOperation(value = "添加部门",response = Department.class)
    @PostMapping
    public ResponseEntity addDepartments(@RequestBody @Valid Department department){

        return ok(departmentService.addDepartments(department));
    }

    @Content(value = "修改部门")
    @ApiOperation(value = "修改部门",response = Department.class)
    @PutMapping
    public ResponseEntity updateDepartments(@RequestBody @Valid Department department){
        boolean b = departmentService.updateDepartment(department);
        if(b){
            return ok("修改部门成功");
        }
        return ok("修改部门失败");
    }

    @ApiOperation(value = "根据id查找部门")
    @GetMapping("/findOne/{id}")
    public ResponseEntity<Department> findDept(@PathVariable("id") String id){
        return ok(departmentService.findOne(id));
    }


    @ApiOperation(value = "查找部门列表")
    @GetMapping("/findList")
    public ResponseEntity findList(){
        return ok(departmentService.findList());
    }

    @Content(value = "删除部门")
    @ApiOperation(value = "删除部门")
    @DeleteMapping("/deleteDepartment/{id}")
    public ResponseEntity deleteDepartment(@PathVariable("id") String id){
        boolean b = departmentService.deleteDepartment(id);
        if(b){
            return ok("删除部门成功");
        }
        return ok("删除部门失败,可能原因是部门下还有部门，或者有角色、用户");
    }



}
