package com.qiaoba.module.system.controller;

import com.qiaoba.api.system.entity.SysDept;
import com.qiaoba.api.system.entity.param.SysDeptParam;
import com.qiaoba.common.base.result.AjaxResult;
import com.qiaoba.common.base.validate.AddGroup;
import com.qiaoba.common.base.validate.EditGroup;
import com.qiaoba.module.system.service.SysDeptService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.Size;
import java.util.List;

/**
 * 部门管理 Web层
 *
 * @author ailanyin
 * @version 1.0
 * @since 2023/5/6 15:06
 */
@Validated
@RestController
@RequestMapping("/system/dept")
@Tag(name = "部门管理")
@RequiredArgsConstructor
public class SysDeptController {

    private final SysDeptService sysDeptService;

    @PreAuthorize("hasAuthority('system:dept:list')")
    @GetMapping("/list")
    @Operation(summary = "获取列表")
    public AjaxResult list(@Validated SysDeptParam param) {
        return AjaxResult.success(sysDeptService.selectList(param));
    }

    @PreAuthorize("hasAuthority('system:dept:query')")
    @GetMapping(value = "/{deptId}")
    @Operation(summary = "获取详情")
    public AjaxResult getInfo(@Size(min = 19, max = 20, message = "ID不合法") @PathVariable String deptId) {
        return AjaxResult.success(sysDeptService.selectById(deptId));
    }

    @PreAuthorize("hasAuthority('system:dept:add')")
    @PostMapping
    @Operation(summary = "新增部门")
    public AjaxResult add(@Validated(AddGroup.class) @RequestBody SysDept dept) {
        return AjaxResult.toAjax(sysDeptService.insert(dept));
    }

    @PreAuthorize("hasAuthority('system:dept:edit')")
    @PutMapping
    @Operation(summary = "修改部门")
    public AjaxResult edit(@Validated(EditGroup.class) @RequestBody SysDept dept) {
        return AjaxResult.toAjax(sysDeptService.updateById(dept));
    }

    @PreAuthorize("hasAuthority('system:dept:remove')")
    @DeleteMapping("/{id}")
    @Operation(summary = "删除部门")
    public AjaxResult remove(@PathVariable String id) {
        return AjaxResult.toAjax(sysDeptService.deleteById(id));
    }

    @PreAuthorize("hasAuthority('system:dept:list')")
    @GetMapping("/list/exclude/{deptId}")
    @Operation(summary = "查询部门列表(排除节点)")
    public AjaxResult excludeChild(@PathVariable(value = "deptId", required = false) String deptId) {
        return AjaxResult.success(sysDeptService.excludeChild(deptId));
    }

    @GetMapping("/dept-tree")
    @Operation(summary = "构建部门树")
    public AjaxResult deptTree(SysDeptParam param) {
        List<SysDept> list = sysDeptService.selectList(param);
        return AjaxResult.success(sysDeptService.buildDeptTree(list));
    }

    @GetMapping(value = "/dept-tree/{roleId}")
    @Operation(summary = "角色部门树")
    public AjaxResult deptTree(@PathVariable("roleId") String roleId, SysDeptParam param) {
        AjaxResult ajax = AjaxResult.success();
        ajax.put("checkedKeys", sysDeptService.selectDeptIdsByRoleId(roleId));
        ajax.put("depts", sysDeptService.buildDeptTree(sysDeptService.selectList(param)));
        return ajax;
    }

}
