package com.ema.admin.modules.system.controller;

import com.mybatisflex.core.paginate.Page;
import com.ema.admin.modules.system.entity.SysDepartment;
import com.ema.admin.modules.system.entity.SysUser;
import com.ema.admin.modules.system.service.SysDepartmentService;
import com.ema.admin.modules.system.vo.SysDepartmentQueryVo;
import com.ema.admin.modules.system.vo.SysDepartmentVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 *  部门信息控制层。
 *
 * @author LIJIAN
 * @since 2026-05-12
 */
@RestController
@Api(tags = "部门信息接口")
@RequestMapping("/sysDepartment")
public class SysDepartmentController {

    @Autowired
    private SysDepartmentService sysDepartmentService;

    /**
     * 添加。
     *
     * @param sysDepartment 
     * @return {@code true} 添加成功，{@code false} 添加失败
     */
    @PostMapping("save")
    @ApiOperation("保存")
    public boolean save(@RequestBody @ApiParam("部门信息") SysDepartment sysDepartment) {
        return sysDepartmentService.save(sysDepartment);
    }

    /**
     * 根据主键删除。
     *
     * @param id 主键
     * @return {@code true} 删除成功，{@code false} 删除失败
     */
    @DeleteMapping("remove/{id}")
    @ApiOperation("根据主键")
    public boolean remove(@PathVariable @ApiParam("主键") String id) {
        return sysDepartmentService.removeById(id);
    }

    /**
     * 根据主键更新。
     *
     * @param sysDepartment 
     * @return {@code true} 更新成功，{@code false} 更新失败
     */
    @PutMapping("update")
    @ApiOperation("根据主键更新")
    public boolean update(@RequestBody @ApiParam("主键") SysDepartment sysDepartment) {
        return sysDepartmentService.updateById(sysDepartment);
    }

    /**
     * 查询所有。
     *
     * @return 所有数据
     */
    @GetMapping("list")
    @ApiOperation("查询所有")
    public List<SysDepartment> list() {
        return sysDepartmentService.list();
    }

    /**
     * 根据主键获取详细信息。
     *
     * @param id 主键
     * @return 详情
     */
    @GetMapping("getInfo/{id}")
    @ApiOperation("根据主键获取")
    public SysDepartment getInfo(@PathVariable @ApiParam("主键") String id) {
        return sysDepartmentService.getById(id);
    }

    /**
     * 分页查询（带条件）。
     *
     * @param queryVo 查询条件
     * @return 分页对象
     */
    @PostMapping("page")
    @ApiOperation("分页查询（带条件）")
    public Page<SysDepartment> page(@RequestBody @ApiParam("查询条件") SysDepartmentQueryVo queryVo) {
        return sysDepartmentService.page(queryVo);
    }

    /**
     * 查询部门树。
     *
     * @return 部门树
     */
    @GetMapping("tree")
    @ApiOperation("部门树")
    public List<SysDepartmentVo> departmentTree() {
        List<SysDepartmentVo> departmentTree = sysDepartmentService.tree();
        return departmentTree;
    }

    //根据部门查询用户列表
    @GetMapping("userList/{departmentId}")
    @ApiOperation("根据部门查询用户列表")
    public List<SysUser> userList(@PathVariable @ApiParam("部门ID") String departmentId) {
        List<SysUser> userList = sysDepartmentService.userList(departmentId);
        return userList;
    }

}
