package cn.fateverse.admin.controller;

import cn.fateverse.admin.dto.DeptDto;
import cn.fateverse.admin.facade.DeptFacade;
import cn.fateverse.admin.vo.DeptVo;
import cn.fateverse.common.core.entity.OptionTree;
import cn.fateverse.common.core.result.Result;
import cn.fateverse.common.log.annotation.Log;
import cn.fateverse.common.log.enums.BusinessType;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author Clay
 * @date 2022/11/2
 */
@Api(tags = "部门接口")
@RestController
@RequestMapping("/dept")
@RequiredArgsConstructor
public class DeptController {

    private final DeptFacade deptFacade;


    @ApiOperation("获取列表信息")
    @GetMapping
    @PreAuthorize("@ss.hasPermission('admin:dept:list')")
    public Result<List<DeptVo>> list(
            @ApiParam(name="deptName",value="部门名称") String deptName,
            @ApiParam(name="state",value="状态(1: 正常 0 : 停用)") Integer state){
        List<DeptVo> deptVoList = deptFacade.searchTree(deptName, state);
        return Result.ok(deptVoList);
    }

    @ApiOperation("获取部门详情")
    @GetMapping("/{deptId}")
    @PreAuthorize("@ss.hasPermission('admin:dept:info')")
    public Result<DeptVo> info(
            @ApiParam(name="deptId",value="部门id")
            @PathVariable Long deptId){
        DeptVo deptVo = deptFacade.searchById(deptId);
        return Result.ok(deptVo);
    }

    @ApiOperation("获取树形接口的option")
    @GetMapping("/option")
    public Result<List<OptionTree>> option(){
        List<OptionTree> optionTreeList = deptFacade.searchTreeOption();
        return Result.ok(optionTreeList);
    }

    @ApiOperation("获取修改时的部门列表")
    @GetMapping("/option/exclude/{deptId}")
    public Result<List<OptionTree>> exclude(@PathVariable Long deptId){
        List<OptionTree> deptVoList = deptFacade.searchExcludeTree(deptId);
        return Result.ok(deptVoList);
    }

    @ApiOperation("新增部门")
    @PostMapping
    @PreAuthorize("@ss.hasPermission('admin:dept:add')")
    @Log(title = "新增部门",businessType = BusinessType.INSERT)
    public Result<Void> add(@RequestBody @Validated DeptDto deptDto){
        deptFacade.save(deptDto);
        return Result.ok();
    }

    @ApiOperation("修改部门")
    @PutMapping
    @PreAuthorize("@ss.hasPermission('admin:dept:edit')")
    @Log(title = "修改部门",businessType = BusinessType.UPDATE)
    public Result<Void> edit(@RequestBody @Validated DeptDto deptDto){
        deptFacade.edit(deptDto);
        return Result.ok();
    }


    @ApiOperation("删除部门")
    @DeleteMapping("/{deptId}")
    @PreAuthorize("@ss.hasPermission('admin:dept:del')")
    @Log(title = "删除部门",businessType = BusinessType.DELETE)
    public Result<Void> delete(@PathVariable Long deptId){
        deptFacade.remove(deptId);
        return Result.ok();
    }





}
