package com.agileboot.system.dept.controller;

import cn.hutool.core.lang.tree.Tree;
import com.agileboot.common.core.core.R;
import com.agileboot.system.dept.dto.AddDeptDTO;
import com.agileboot.system.dept.dto.DeptQuery;
import com.agileboot.system.dept.dto.UpdateDeptDTO;
import com.agileboot.system.dept.service.ISysDeptService;
import com.agileboot.system.dept.vo.DeptVO;
import jakarta.validation.constraints.NotNull;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 部门信息
 */
@RestController
@RequestMapping("/system")
@Validated
@RequiredArgsConstructor
public class SysDeptController {

    private final ISysDeptService sysDeptService;

    /**
     * 获取部门列表
     */
    @GetMapping("/depts")
    public R<List<DeptVO>> list(DeptQuery query) {
        List<DeptVO> deptList = sysDeptService.getDeptList(query);
        return R.ok(deptList);
    }

    /**
     * 根据部门编号获取详细信息
     */
    @GetMapping(value = "/dept/details/{deptId}")
    public R<DeptVO> getInfo(@PathVariable("deptId") Long deptId) {
        DeptVO dept = sysDeptService.getDeptInfo(deptId);
        return R.ok(dept);
    }

    /**
     * 获取部门下拉树列表
     */
    @GetMapping("/depts/dropdown")
    public R<List<Tree<Long>>> dropdownList() {
        List<Tree<Long>> deptTree = sysDeptService.getDeptTree();
        return R.ok(deptTree);
    }

    /**
     * 新增部门
     */
    @PostMapping("/dept")
    public R<Void> add(@RequestBody AddDeptDTO addCommand) {
        sysDeptService.addDept(addCommand);
        return R.ok();
    }

    /**
     * 修改部门
     */
    @PostMapping("/dept/{deptId}")
    public R<Void> edit(@PathVariable("deptId") Long deptId, @RequestBody UpdateDeptDTO updateCommand) {
        updateCommand.setDeptId(deptId);
        sysDeptService.updateDept(updateCommand);
        return R.ok();
    }

    /**
     * 删除部门
     */
    @PostMapping("/dept/del/{deptId}")
    public R<Void> remove(@PathVariable("deptId") @NotNull Long deptId) {
        sysDeptService.removeDept(deptId);
        return R.ok();
    }
}
