package com.wteam.controller.permission;


import com.wteam.framework.common.enums.ResultUtil;
import com.wteam.framework.common.mybatis.util.PageUtil;
import com.wteam.framework.common.vo.ResultMessage;
import com.wteam.framework.common.vo.SearchVO;
import com.wteam.framework.modules.permission.entity.dos.Department;
import com.wteam.framework.modules.permission.entity.vo.DepartmentVO;
import com.wteam.framework.modules.permission.service.DepartmentService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;


/**
 * 管理端,部门管理接口
 *
 * @author Chopper
 * @since 2020/11/22 12:06
 */
@RestController
@Tag(name = "管理端,部门管理接口")
@RequestMapping("/manager/permission/department")
public class DepartmentManagerController {
    @Autowired
    private DepartmentService departmentService;

    @GetMapping(value = "/{id}")
    @Operation(summary = "查看部门详情")
    public ResultMessage<Department> get(@PathVariable String id) {
        Department department = departmentService.getById(id);
        return ResultUtil.data(department);
    }

    @GetMapping
    @Operation(summary = "获取树状结构")
    public ResultMessage<List<DepartmentVO>> getByPage(Department entity,
                                                       SearchVO searchVo) {
        return ResultUtil.data(departmentService.tree(PageUtil.initWrapper(entity, searchVo)));

    }

    @PostMapping
    @Operation(summary = "新增部门")
    public ResultMessage<Department> save(@Validated Department department) {
        departmentService.save(department);
        return ResultUtil.data(department);
    }

    @PutMapping("/{id}")
    @Operation(summary = "更新部门")
    public ResultMessage<Department> update(@PathVariable String id, @Validated Department department) {
        departmentService.updateById(department);
        return ResultUtil.data(department);
    }

    @DeleteMapping(value = "/{ids}")
    @Operation(summary = "删除部门")
    public ResultMessage<Object> delAllByIds(@PathVariable List<String> ids) {
        departmentService.deleteByIds(ids);
        return ResultUtil.success();
    }
}
